From 82bacca9a194197b04fd2871a4724968fb5bc33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 26 May 2023 14:59:23 +0200 Subject: [PATCH] room: Also show other user avatar if they are alone in room --- src/session/model/room/mod.rs | 49 ++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/session/model/room/mod.rs b/src/session/model/room/mod.rs index ac44663f..1eb3f6a8 100644 --- a/src/session/model/room/mod.rs +++ b/src/session/model/room/mod.rs @@ -1636,36 +1636,43 @@ impl Room { let avatar_url = matrix_room.avatar_url(); let avatar_data = self.avatar_data(); - if avatar_url.is_none() && matrix_room.active_members_count() == 2 { - // Fallback to other user's avatar if this is a 1-to-1 room. + let members_count = matrix_room.active_members_count(); + if avatar_url.is_none() && members_count > 0 && members_count <= 2 { + // Check if this is a 1-to-1 room to see if we can use a fallback. // First, make sure the members are loaded. self.load_members().await; let own_user_id = self.session().user().unwrap().user_id(); let members = self.members(); + let mut has_own_member = false; + let mut other_member = None; - if members.n_items() >= 1 { - // Try to get the member from the list. - for member in members.iter::() { - match member { - Ok(member) => { - if member.user_id() != own_user_id - && matches!( - member.membership(), - Membership::Join | Membership::Invite - ) - { - avatar_data.set_image(member.avatar_data().image()); - return; - } - } - Err(error) => { - debug!("Error iterating through room members: {error}"); - break; - } + // Get the other member from the list. + for member in members.iter::() { + let Ok(member) = member else { + break; + }; + + if matches!(member.membership(), Membership::Join | Membership::Invite) { + if member.user_id() == own_user_id { + has_own_member = true; + } else { + other_member = Some(member); } } + + if has_own_member && other_member.is_some() { + break; + } + } + + // Fallback to other user's avatar if this is a 1-to-1 room. + if members_count == 1 || (members_count == 2 && has_own_member) { + if let Some(other_member) = other_member { + avatar_data.set_image(other_member.avatar_data().image()); + return; + } } }