Skip to content

Commit

Permalink
fix not showing displaynames of already synced rooms
Browse files Browse the repository at this point in the history
Fixes #149

The display_names only got populated from the
OriginalSyncRoomMemberEvent. When opening an already synced room,
display_names is not populated.

ClientWorker::get_room extends the returned FetchedRoom to include
RoomMembers from Room::members_no_sync. This allows RoomState::new to
populate the RoomInfo's display_names.

Thanks to <benjamin@computer.surgery> for finding the source of the bug
in that issue. It was mentioned that it might be problematic to iterate
over all members of large rooms. The largest room I had available to
test this was only about 200 members, but it seemed fine.
  • Loading branch information
benjajaja committed Oct 27, 2023
1 parent 8943909 commit 2f51e96
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ pub mod tests {
use super::*;
use crate::config::user_style_from_color;
use crate::tests::*;
use matrix_sdk::ruma::user_id;
use modalkit::tui::style::Color;

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ impl Window<IambInfo> for IambWindow {
fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
match id {
IambId::Room(room_id) => {
let (room, name, tags) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, tags, store);
let (room, name, tags, members) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, tags, members, store);

return Ok(room.into());
},
Expand Down Expand Up @@ -707,8 +707,8 @@ impl Window<IambInfo> for IambWindow {
let room_id = worker.join_room(name.clone())?;
names.insert(name, room_id.clone());

let (room, name, tags) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, tags, store);
let (room, name, tags, members) = store.application.worker.get_room(room_id)?;
let room = RoomState::new(room, name, tags, members, store);

Ok(room.into())
}
Expand Down
27 changes: 26 additions & 1 deletion src/windows/room/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! # Windows for Matrix rooms and spaces

use matrix_sdk::{
room::{Invited, Room as MatrixRoom},
room::{Invited, Room as MatrixRoom, RoomMember},
ruma::{
events::{
room::{name::RoomNameEventContent, topic::RoomTopicEventContent},
Expand Down Expand Up @@ -107,13 +108,37 @@ impl RoomState {
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
members: Vec<RoomMember>,
store: &mut ProgramStore,
) -> Self {
let room_id = room.room_id().to_owned();
let info = store.application.get_room_info(room_id);
info.name = name.to_string().into();
info.tags = tags;

for member in &members {
let ambiguous_name =
member.display_name().unwrap_or_else(|| member.user_id().localpart());
let ambiguos = members
.iter()
.filter(|other| {
other
.display_name()
.map(|other| other == ambiguous_name)
.unwrap_or_default()
})
.count() >
1;
let user_id = member.user_id();
if ambiguos {
info.display_names.remove(user_id);
} else if let Some(display_name) = member.display_name() {
info.display_names.insert(user_id.to_owned(), display_name.to_owned());
} else {
info.display_names.remove(user_id);
}
}

if room.is_space() {
SpaceState::new(room).into()
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/windows/room/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a> StatefulWidget for Space<'a> {
let mut items = members
.into_iter()
.filter_map(|id| {
let (room, _, tags) =
let (room, _, tags, _) =
self.store.application.worker.get_room(id.clone()).ok()?;
let room_info = std::sync::Arc::new((room, tags));

Expand Down
5 changes: 3 additions & 2 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) {
return (reply, response);
}

pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>, Vec<RoomMember>);

pub enum WorkerTask {
Init(AsyncProgramStore, ClientReply<()>),
Expand Down Expand Up @@ -1146,8 +1146,9 @@ impl ClientWorker {
if let Some(room) = self.client.get_room(&room_id) {
let name = room.display_name().await.map_err(IambError::from)?;
let tags = room.tags().await.map_err(IambError::from)?;
let members = room.members_no_sync().await.map_err(IambError::from)?;

Ok((room, name, tags))
Ok((room, name, tags, members))
} else {
Err(IambError::UnknownRoom(room_id).into())
}
Expand Down

0 comments on commit 2f51e96

Please sign in to comment.