| 1 | use futures_util::{StreamExt, stream}; |
| 2 | use matrix_sdk::{Client, Room, RoomState}; |
| 3 | |
| 4 | use crate::{RoomMembership, RoomNotificationMode, RoomSummary}; |
| 5 | |
| 6 | pub(crate) async fn room_summaries(client: &Client) -> Vec<RoomSummary> { |
| 7 | let mut summaries = stream::iter(client.rooms()) |
| 8 | .map(|room| async move { room_summary(&room).await }) |
| 9 | .buffer_unordered(16) |
| 10 | .collect::<Vec<_>>() |
| 11 | .await; |
| 12 | summaries.sort_by(|left, right| { |
| 13 | right |
| 14 | .latest_event_timestamp_ms |
| 15 | .cmp(&left.latest_event_timestamp_ms) |
| 16 | .then_with(|| left.name.to_lowercase().cmp(&right.name.to_lowercase())) |
| 17 | .then_with(|| left.room_id.cmp(&right.room_id)) |
| 18 | }); |
| 19 | summaries |
| 20 | } |
| 21 | |
| 22 | async fn room_summary(room: &Room) -> RoomSummary { |
| 23 | let name = room |
| 24 | .display_name() |
| 25 | .await |
| 26 | .map(|name| name.to_string()) |
| 27 | .unwrap_or_else(|_| room.room_id().to_string()); |
| 28 | let custom_notification_mode = room.user_defined_notification_mode().await; |
| 29 | let effective_notification_mode = match custom_notification_mode { |
| 30 | Some(mode) => Some(mode), |
| 31 | None => room.notification_mode().await, |
| 32 | }; |
| 33 | RoomSummary { |
| 34 | room_id: room.room_id().to_owned(), |
| 35 | name, |
| 36 | membership: match room.state() { |
| 37 | RoomState::Joined => RoomMembership::Joined, |
| 38 | RoomState::Invited => RoomMembership::Invited, |
| 39 | RoomState::Left => RoomMembership::Left, |
| 40 | RoomState::Knocked => RoomMembership::Knocked, |
| 41 | RoomState::Banned => RoomMembership::Banned, |
| 42 | }, |
| 43 | unread_messages: room.num_unread_messages(), |
| 44 | unread_mentions: room.num_unread_mentions(), |
| 45 | encrypted: room.encryption_state().is_encrypted(), |
| 46 | space: room.is_space(), |
| 47 | direct: room.is_direct().await.unwrap_or(false), |
| 48 | favourite: room.is_favourite(), |
| 49 | low_priority: room.is_low_priority(), |
| 50 | avatar: room.avatar_url(), |
| 51 | latest_event_timestamp_ms: room |
| 52 | .latest_event_timestamp() |
| 53 | .map(|value| value.get().into()), |
| 54 | notification_mode: effective_notification_mode.map(notification_mode), |
| 55 | notification_mode_custom: custom_notification_mode.is_some(), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub(crate) const fn notification_mode( |
| 60 | mode: matrix_sdk::notification_settings::RoomNotificationMode, |
| 61 | ) -> RoomNotificationMode { |
| 62 | match mode { |
| 63 | matrix_sdk::notification_settings::RoomNotificationMode::AllMessages => { |
| 64 | RoomNotificationMode::AllMessages |
| 65 | } |
| 66 | matrix_sdk::notification_settings::RoomNotificationMode::MentionsAndKeywordsOnly => { |
| 67 | RoomNotificationMode::MentionsAndKeywordsOnly |
| 68 | } |
| 69 | matrix_sdk::notification_settings::RoomNotificationMode::Mute => RoomNotificationMode::Mute, |
| 70 | } |
| 71 | } |