| @@ -68,6 +68,31 @@ fn bounded_member_profiles( |
| 68 | 68 | profiles |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | fn resolved_room_avatar_mxc( |
| 72 | explicit_room_avatar: Option<String>, |
| 73 | is_direct: bool, |
| 74 | own_user_id: &str, |
| 75 | profiles: &[MatrixMemberProfile], |
| 76 | ) -> Option<String> { |
| 77 | if explicit_room_avatar.is_some() || !is_direct { |
| 78 | return explicit_room_avatar; |
| 79 | } |
| 80 | |
| 81 | let mut joined_peers = profiles.iter().filter(|profile| { |
| 82 | profile.membership == MembershipState::Join.as_str() |
| 83 | && profile.user_id != own_user_id |
| 84 | }); |
| 85 | let peer = joined_peers.next()?; |
| 86 | |
| 87 | // An m.direct room is not necessarily a two-person conversation. Avoid |
| 88 | // assigning one arbitrary member's face to a multi-user direct room. |
| 89 | if joined_peers.next().is_some() { |
| 90 | return None; |
| 91 | } |
| 92 | |
| 93 | peer.avatar_mxc.clone() |
| 94 | } |
| 95 | |
| 71 | 96 | #[derive(Clone, Debug)] |
| 72 | 97 | pub struct WeechatRoomMember { |
| 73 | 98 | inner: RoomMember, |
| @@ -162,6 +187,19 @@ impl Members { |
| 162 | 187 | .map(|entry| entry.value().clone()) |
| 163 | 188 | .collect(); |
| 164 | 189 | let profiles = bounded_member_profiles(profiles); |
| 190 | let room = active_room(&self.room); |
| 191 | let room_avatar = resolved_room_avatar_mxc( |
| 192 | room.avatar_url().map(|uri| uri.as_str().to_owned()), |
| 193 | buffer |
| 194 | .get_localvar("type") |
| 195 | .is_some_and(|kind| kind == "private"), |
| 196 | room.own_user_id().as_str(), |
| 197 | &profiles, |
| 198 | ); |
| 199 | buffer.set_localvar( |
| 200 | "matrix_avatar_mxc", |
| 201 | room_avatar.as_deref().unwrap_or_default(), |
| 202 | ); |
| 165 | 203 | let profiles: Vec<_> = profiles |
| 166 | 204 | .into_iter() |
| 167 | 205 | .map(|profile| { |
| @@ -543,6 +581,57 @@ mod tests { |
| 543 | 581 | assert_eq!(profiles.first().unwrap().display_name, "Member 0000"); |
| 544 | 582 | assert_eq!(profiles.last().unwrap().display_name, "Member 0511"); |
| 545 | 583 | } |
| 584 | |
| 585 | #[test] |
| 586 | fn explicit_room_avatar_wins_for_rooms_and_direct_chats() { |
| 587 | let profiles = vec![profile(1, "Peer")]; |
| 588 | let explicit = Some("mxc://example.org/room-avatar".to_owned()); |
| 589 | |
| 590 | assert_eq!( |
| 591 | resolved_room_avatar_mxc( |
| 592 | explicit.clone(), |
| 593 | false, |
| 594 | "@me:example.org", |
| 595 | &profiles, |
| 596 | ), |
| 597 | explicit, |
| 598 | ); |
| 599 | } |
| 600 | |
| 601 | #[test] |
| 602 | fn one_to_one_direct_chat_uses_the_other_members_avatar() { |
| 603 | let mut own = profile(0, "Me"); |
| 604 | own.user_id = "@me:example.org".to_owned(); |
| 605 | own.avatar_mxc = Some("mxc://example.org/me".to_owned()); |
| 606 | let peer = profile(1, "Peer"); |
| 607 | |
| 608 | assert_eq!( |
| 609 | resolved_room_avatar_mxc( |
| 610 | None, |
| 611 | true, |
| 612 | "@me:example.org", |
| 613 | &[own, peer], |
| 614 | ) |
| 615 | .as_deref(), |
| 616 | Some("mxc://example.org/avatar1"), |
| 617 | ); |
| 618 | } |
| 619 | |
| 620 | #[test] |
| 621 | fn multi_user_direct_room_does_not_choose_an_arbitrary_avatar() { |
| 622 | let mut own = profile(0, "Me"); |
| 623 | own.user_id = "@me:example.org".to_owned(); |
| 624 | |
| 625 | assert_eq!( |
| 626 | resolved_room_avatar_mxc( |
| 627 | None, |
| 628 | true, |
| 629 | "@me:example.org", |
| 630 | &[own, profile(1, "One"), profile(2, "Two")], |
| 631 | ), |
| 632 | None, |
| 633 | ); |
| 634 | } |
| 546 | 635 | } |
| 547 | 636 | |
| 548 | 637 | impl WeechatRoomMember { |