Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

room names: better make use of the hero names for locally computing a room display name #3461

Merged
merged 11 commits into from
May 27, 2024
Merged
8 changes: 2 additions & 6 deletions crates/matrix-sdk-base/src/rooms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ impl BaseRoomInfo {
&self,
joined_member_count: u64,
invited_member_count: u64,
heroes: Vec<RoomMember>,
heroes: Vec<&str>,
) -> DisplayName {
calculate_room_name(
joined_member_count,
invited_member_count,
heroes.iter().map(|mem| mem.name()).collect::<Vec<&str>>(),
)
calculate_room_name(joined_member_count, invited_member_count, heroes)
}

/// Get the room version of this room.
Expand Down
83 changes: 42 additions & 41 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::{

use bitflags::bitflags;
use eyeball::{SharedObservable, Subscriber};
use futures_util::stream::{self, StreamExt};
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
use matrix_sdk_common::ring_buffer::RingBuffer;
#[cfg(feature = "experimental-sliding-sync")]
Expand Down Expand Up @@ -628,46 +627,44 @@ impl Room {

let own_user_id = self.own_user_id().as_str();

let (heroes, guessed_num_members): (Vec<RoomMember>, _) =
if summary.heroes_user_ids.is_empty() {
let mut members = self.members(RoomMemberships::ACTIVE).await?;

// Make the ordering deterministic.
members.sort_unstable_by(|lhs, rhs| lhs.name().cmp(rhs.name()));

// We can make a good prediction of the total number of members here. This might
// be incorrect if the database info is outdated.
let guessed_num_members = Some(members.len());

(
members
.into_iter()
.take(NUM_HEROES)
.filter(|u| u.user_id() != own_user_id)
.collect(),
guessed_num_members,
)
} else {
let mut heroes = summary.heroes_user_ids;

// Make the ordering deterministic.
heroes.sort_unstable();

let members: Vec<_> = stream::iter(heroes.iter())
.filter_map(|user_id| async move {
// Filter out the current user.
if user_id == own_user_id {
return None;
}
self.get_member(&user_id).await.transpose()
})
.collect()
.await;

let members: StoreResult<Vec<_>> = members.into_iter().collect();
let (heroes, guessed_num_members): (Vec<String>, _) = if !summary.heroes_names.is_empty() {
// Straight-forward path: pass through the heroes names, don't give a guess of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Straight-forward
+ Straightforward

bnjbvr marked this conversation as resolved.
Show resolved Hide resolved
// the number of members.
(summary.heroes_names, None)
} else if !summary.heroes_user_ids.is_empty() {
// Use the heroes, if available.
let heroes = summary.heroes_user_ids;

let mut names = Vec::with_capacity(heroes.len());
for user_id in heroes {
if user_id == own_user_id {
continue;
}
if let Some(member) = self.get_member(&user_id).await? {
names.push(member.name().to_owned());
}
}

(members?, None)
};
(names, None)
} else {
let mut members = self.members(RoomMemberships::ACTIVE).await?;

// Make the ordering deterministic.
members.sort_unstable_by(|lhs, rhs| lhs.name().cmp(rhs.name()));

// We can make a good prediction of the total number of members here. This might
// be incorrect if the database info is outdated.
let guessed_num_members = Some(members.len());

(
members
.into_iter()
.take(NUM_HEROES)
.filter_map(|u| (u.user_id() != own_user_id).then(|| u.name().to_owned()))
.collect(),
guessed_num_members,
)
};

let (num_joined, num_invited) = match self.state() {
RoomState::Invited => {
Expand Down Expand Up @@ -698,7 +695,11 @@ impl Room {
"Calculating name for a room",
);

Ok(self.inner.read().base_info.calculate_room_name(num_joined, num_invited, heroes))
Ok(self.inner.read().base_info.calculate_room_name(
num_joined,
num_invited,
heroes.iter().map(|hero| hero.as_str()).collect(),
))
}

/// Subscribe to the inner `RoomInfo`.
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::RoomMemberships;
use crate::{
error::Result,
read_receipts::{compute_unread_counts, PreviousEventsProvider},
rooms::{RoomState},
rooms::RoomState,
store::{ambiguity_map::AmbiguityCache, StateChanges, Store},
sync::{JoinedRoomUpdate, LeftRoomUpdate, Notification, RoomUpdates, SyncResponse},
Room, RoomInfo,
Expand Down