atagen

macltok

public
Star0
Watch0
fa9114fcf75f7bdf1a7cec3b3367d604cf7de472
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: src/normalise/room.rs

96fdf90aacd6atagen2026-09-08 08:24 UTC1
use futures_util::{StreamExt, stream};
96fdf90aacd6atagen2026-09-08 08:24 UTC2
use matrix_sdk::{Client, Room, RoomState};
96fdf90aacd6atagen2026-09-08 08:24 UTC3
96fdf90aacd6atagen2026-09-08 08:24 UTC4
use crate::{RoomMembership, RoomNotificationMode, RoomSummary};
96fdf90aacd6atagen2026-09-08 08:24 UTC5
96fdf90aacd6atagen2026-09-08 08:24 UTC6
pub(crate) async fn room_summaries(client: &Client) -> Vec<RoomSummary> {
96fdf90aacd6atagen2026-09-08 08:24 UTC7
    let mut summaries = stream::iter(client.rooms())
96fdf90aacd6atagen2026-09-08 08:24 UTC8
        .map(|room| async move { room_summary(&room).await })
96fdf90aacd6atagen2026-09-08 08:24 UTC9
        .buffer_unordered(16)
96fdf90aacd6atagen2026-09-08 08:24 UTC10
        .collect::<Vec<_>>()
96fdf90aacd6atagen2026-09-08 08:24 UTC11
        .await;
96fdf90aacd6atagen2026-09-08 08:24 UTC12
    summaries.sort_by(|left, right| {
96fdf90aacd6atagen2026-09-08 08:24 UTC13
        right
96fdf90aacd6atagen2026-09-08 08:24 UTC14
            .latest_event_timestamp_ms
96fdf90aacd6atagen2026-09-08 08:24 UTC15
            .cmp(&left.latest_event_timestamp_ms)
96fdf90aacd6atagen2026-09-08 08:24 UTC16
            .then_with(|| left.name.to_lowercase().cmp(&right.name.to_lowercase()))
96fdf90aacd6atagen2026-09-08 08:24 UTC17
            .then_with(|| left.room_id.cmp(&right.room_id))
96fdf90aacd6atagen2026-09-08 08:24 UTC18
    });
96fdf90aacd6atagen2026-09-08 08:24 UTC19
    summaries
96fdf90aacd6atagen2026-09-08 08:24 UTC20
}
96fdf90aacd6atagen2026-09-08 08:24 UTC21
96fdf90aacd6atagen2026-09-08 08:24 UTC22
async fn room_summary(room: &Room) -> RoomSummary {
96fdf90aacd6atagen2026-09-08 08:24 UTC23
    let name = room
96fdf90aacd6atagen2026-09-08 08:24 UTC24
        .display_name()
96fdf90aacd6atagen2026-09-08 08:24 UTC25
        .await
96fdf90aacd6atagen2026-09-08 08:24 UTC26
        .map(|name| name.to_string())
96fdf90aacd6atagen2026-09-08 08:24 UTC27
        .unwrap_or_else(|_| room.room_id().to_string());
96fdf90aacd6atagen2026-09-08 08:24 UTC28
    let custom_notification_mode = room.user_defined_notification_mode().await;
96fdf90aacd6atagen2026-09-08 08:24 UTC29
    let effective_notification_mode = match custom_notification_mode {
96fdf90aacd6atagen2026-09-08 08:24 UTC30
        Some(mode) => Some(mode),
96fdf90aacd6atagen2026-09-08 08:24 UTC31
        None => room.notification_mode().await,
96fdf90aacd6atagen2026-09-08 08:24 UTC32
    };
96fdf90aacd6atagen2026-09-08 08:24 UTC33
    RoomSummary {
96fdf90aacd6atagen2026-09-08 08:24 UTC34
        room_id: room.room_id().to_owned(),
96fdf90aacd6atagen2026-09-08 08:24 UTC35
        name,
96fdf90aacd6atagen2026-09-08 08:24 UTC36
        membership: match room.state() {
96fdf90aacd6atagen2026-09-08 08:24 UTC37
            RoomState::Joined => RoomMembership::Joined,
96fdf90aacd6atagen2026-09-08 08:24 UTC38
            RoomState::Invited => RoomMembership::Invited,
96fdf90aacd6atagen2026-09-08 08:24 UTC39
            RoomState::Left => RoomMembership::Left,
96fdf90aacd6atagen2026-09-08 08:24 UTC40
            RoomState::Knocked => RoomMembership::Knocked,
96fdf90aacd6atagen2026-09-08 08:24 UTC41
            RoomState::Banned => RoomMembership::Banned,
96fdf90aacd6atagen2026-09-08 08:24 UTC42
        },
96fdf90aacd6atagen2026-09-08 08:24 UTC43
        unread_messages: room.num_unread_messages(),
96fdf90aacd6atagen2026-09-08 08:24 UTC44
        unread_mentions: room.num_unread_mentions(),
96fdf90aacd6atagen2026-09-08 08:24 UTC45
        encrypted: room.encryption_state().is_encrypted(),
96fdf90aacd6atagen2026-09-08 08:24 UTC46
        space: room.is_space(),
96fdf90aacd6atagen2026-09-08 08:24 UTC47
        direct: room.is_direct().await.unwrap_or(false),
96fdf90aacd6atagen2026-09-08 08:24 UTC48
        favourite: room.is_favourite(),
96fdf90aacd6atagen2026-09-08 08:24 UTC49
        low_priority: room.is_low_priority(),
96fdf90aacd6atagen2026-09-08 08:24 UTC50
        avatar: room.avatar_url(),
96fdf90aacd6atagen2026-09-08 08:24 UTC51
        latest_event_timestamp_ms: room
96fdf90aacd6atagen2026-09-08 08:24 UTC52
            .latest_event_timestamp()
96fdf90aacd6atagen2026-09-08 08:24 UTC53
            .map(|value| value.get().into()),
96fdf90aacd6atagen2026-09-08 08:24 UTC54
        notification_mode: effective_notification_mode.map(notification_mode),
96fdf90aacd6atagen2026-09-08 08:24 UTC55
        notification_mode_custom: custom_notification_mode.is_some(),
96fdf90aacd6atagen2026-09-08 08:24 UTC56
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC57
}
96fdf90aacd6atagen2026-09-08 08:24 UTC58
96fdf90aacd6atagen2026-09-08 08:24 UTC59
pub(crate) const fn notification_mode(
96fdf90aacd6atagen2026-09-08 08:24 UTC60
    mode: matrix_sdk::notification_settings::RoomNotificationMode,
96fdf90aacd6atagen2026-09-08 08:24 UTC61
) -> RoomNotificationMode {
96fdf90aacd6atagen2026-09-08 08:24 UTC62
    match mode {
96fdf90aacd6atagen2026-09-08 08:24 UTC63
        matrix_sdk::notification_settings::RoomNotificationMode::AllMessages => {
96fdf90aacd6atagen2026-09-08 08:24 UTC64
            RoomNotificationMode::AllMessages
96fdf90aacd6atagen2026-09-08 08:24 UTC65
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC66
        matrix_sdk::notification_settings::RoomNotificationMode::MentionsAndKeywordsOnly => {
96fdf90aacd6atagen2026-09-08 08:24 UTC67
            RoomNotificationMode::MentionsAndKeywordsOnly
96fdf90aacd6atagen2026-09-08 08:24 UTC68
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC69
        matrix_sdk::notification_settings::RoomNotificationMode::Mute => RoomNotificationMode::Mute,
96fdf90aacd6atagen2026-09-08 08:24 UTC70
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC71
}