atagen

macltok

public
Star0
Watch0
411bc1c78b65ac2d29c0a44b166993a7e76f1663
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};
31ce2453c82fatagen2026-09-24 13:07 UTC2
use matrix_sdk::{
31ce2453c82fatagen2026-09-24 13:07 UTC3
    Client, Room, RoomState,
31ce2453c82fatagen2026-09-24 13:07 UTC4
    deserialized_responses::SyncOrStrippedState,
31ce2453c82fatagen2026-09-24 13:07 UTC5
    latest_events::LatestEventValue,
31ce2453c82fatagen2026-09-24 13:07 UTC6
    ruma::events::{
31ce2453c82fatagen2026-09-24 13:07 UTC7
        AnyMessageLikeEventContent, AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncStateEvent,
5a52012c9b1datagen2026-09-24 23:45 UTC8
        room::message::{Relation, RoomMessageEventContent},
31ce2453c82fatagen2026-09-24 13:07 UTC9
        space::child::SpaceChildEventContent,
31ce2453c82fatagen2026-09-24 13:07 UTC10
    },
31ce2453c82fatagen2026-09-24 13:07 UTC11
};
31ce2453c82fatagen2026-09-24 13:07 UTC12
use matrix_sdk_ui::spaces::{SpaceRoom, SpaceService};
31ce2453c82fatagen2026-09-24 13:07 UTC13
use ruma::OwnedRoomId;
96fdf90aacd6atagen2026-09-08 08:24 UTC14
5a52012c9b1datagen2026-09-24 23:45 UTC15
use super::message_kind;
31ce2453c82fatagen2026-09-24 13:07 UTC16
use crate::{
5d2974de5826atagen2026-10-08 11:29 UTC17
    LatestEvent, MediaId, MessageKind, RoomHero, RoomMembership, RoomNotificationMode, RoomSummary,
5a52012c9b1datagen2026-09-24 23:45 UTC18
    SendState, SpaceChild,
31ce2453c82fatagen2026-09-24 13:07 UTC19
};
96fdf90aacd6atagen2026-09-08 08:24 UTC20
31ce2453c82fatagen2026-09-24 13:07 UTC21
pub(crate) async fn room_summaries(client: &Client, spaces: &SpaceService) -> Vec<RoomSummary> {
96fdf90aacd6atagen2026-09-08 08:24 UTC22
    let mut summaries = stream::iter(client.rooms())
31ce2453c82fatagen2026-09-24 13:07 UTC23
        .map(|room| async move { room_summary(&room, spaces).await })
96fdf90aacd6atagen2026-09-08 08:24 UTC24
        .buffer_unordered(16)
96fdf90aacd6atagen2026-09-08 08:24 UTC25
        .collect::<Vec<_>>()
96fdf90aacd6atagen2026-09-08 08:24 UTC26
        .await;
96fdf90aacd6atagen2026-09-08 08:24 UTC27
    summaries.sort_by(|left, right| {
96fdf90aacd6atagen2026-09-08 08:24 UTC28
        right
96fdf90aacd6atagen2026-09-08 08:24 UTC29
            .latest_event_timestamp_ms
96fdf90aacd6atagen2026-09-08 08:24 UTC30
            .cmp(&left.latest_event_timestamp_ms)
96fdf90aacd6atagen2026-09-08 08:24 UTC31
            .then_with(|| left.name.to_lowercase().cmp(&right.name.to_lowercase()))
96fdf90aacd6atagen2026-09-08 08:24 UTC32
            .then_with(|| left.room_id.cmp(&right.room_id))
96fdf90aacd6atagen2026-09-08 08:24 UTC33
    });
96fdf90aacd6atagen2026-09-08 08:24 UTC34
    summaries
96fdf90aacd6atagen2026-09-08 08:24 UTC35
}
96fdf90aacd6atagen2026-09-08 08:24 UTC36
31ce2453c82fatagen2026-09-24 13:07 UTC37
async fn room_summary(room: &Room, spaces: &SpaceService) -> RoomSummary {
96fdf90aacd6atagen2026-09-08 08:24 UTC38
    let name = room
96fdf90aacd6atagen2026-09-08 08:24 UTC39
        .display_name()
96fdf90aacd6atagen2026-09-08 08:24 UTC40
        .await
96fdf90aacd6atagen2026-09-08 08:24 UTC41
        .map(|name| name.to_string())
96fdf90aacd6atagen2026-09-08 08:24 UTC42
        .unwrap_or_else(|_| room.room_id().to_string());
96fdf90aacd6atagen2026-09-08 08:24 UTC43
    let custom_notification_mode = room.user_defined_notification_mode().await;
96fdf90aacd6atagen2026-09-08 08:24 UTC44
    let effective_notification_mode = match custom_notification_mode {
96fdf90aacd6atagen2026-09-08 08:24 UTC45
        Some(mode) => Some(mode),
96fdf90aacd6atagen2026-09-08 08:24 UTC46
        None => room.notification_mode().await,
96fdf90aacd6atagen2026-09-08 08:24 UTC47
    };
31ce2453c82fatagen2026-09-24 13:07 UTC48
    let inviter = match room.state() {
31ce2453c82fatagen2026-09-24 13:07 UTC49
        RoomState::Invited => room
31ce2453c82fatagen2026-09-24 13:07 UTC50
            .invite_details()
31ce2453c82fatagen2026-09-24 13:07 UTC51
            .await
31ce2453c82fatagen2026-09-24 13:07 UTC52
            .ok()
31ce2453c82fatagen2026-09-24 13:07 UTC53
            .map(|invite| invite.inviter_id),
31ce2453c82fatagen2026-09-24 13:07 UTC54
        _ => None,
31ce2453c82fatagen2026-09-24 13:07 UTC55
    };
31ce2453c82fatagen2026-09-24 13:07 UTC56
    let children = if room.is_space() {
31ce2453c82fatagen2026-09-24 13:07 UTC57
        space_children(room).await
31ce2453c82fatagen2026-09-24 13:07 UTC58
    } else {
31ce2453c82fatagen2026-09-24 13:07 UTC59
        Vec::new()
31ce2453c82fatagen2026-09-24 13:07 UTC60
    };
96fdf90aacd6atagen2026-09-08 08:24 UTC61
    RoomSummary {
96fdf90aacd6atagen2026-09-08 08:24 UTC62
        room_id: room.room_id().to_owned(),
96fdf90aacd6atagen2026-09-08 08:24 UTC63
        name,
31ce2453c82fatagen2026-09-24 13:07 UTC64
        membership: membership(room.state()),
96fdf90aacd6atagen2026-09-08 08:24 UTC65
        unread_messages: room.num_unread_messages(),
96fdf90aacd6atagen2026-09-08 08:24 UTC66
        unread_mentions: room.num_unread_mentions(),
5a52012c9b1datagen2026-09-24 23:45 UTC67
        unread_notifications: room.num_unread_notifications(),
96fdf90aacd6atagen2026-09-08 08:24 UTC68
        encrypted: room.encryption_state().is_encrypted(),
96fdf90aacd6atagen2026-09-08 08:24 UTC69
        space: room.is_space(),
96fdf90aacd6atagen2026-09-08 08:24 UTC70
        direct: room.is_direct().await.unwrap_or(false),
96fdf90aacd6atagen2026-09-08 08:24 UTC71
        favourite: room.is_favourite(),
96fdf90aacd6atagen2026-09-08 08:24 UTC72
        low_priority: room.is_low_priority(),
5d2974de5826atagen2026-10-08 11:29 UTC73
        avatar: room.avatar_url().as_deref().map(MediaId::plain),
96fdf90aacd6atagen2026-09-08 08:24 UTC74
        latest_event_timestamp_ms: room
96fdf90aacd6atagen2026-09-08 08:24 UTC75
            .latest_event_timestamp()
96fdf90aacd6atagen2026-09-08 08:24 UTC76
            .map(|value| value.get().into()),
31ce2453c82fatagen2026-09-24 13:07 UTC77
        latest_event: latest_event(room),
96fdf90aacd6atagen2026-09-08 08:24 UTC78
        notification_mode: effective_notification_mode.map(notification_mode),
96fdf90aacd6atagen2026-09-08 08:24 UTC79
        notification_mode_custom: custom_notification_mode.is_some(),
31ce2453c82fatagen2026-09-24 13:07 UTC80
        topic: room.topic(),
31ce2453c82fatagen2026-09-24 13:07 UTC81
        canonical_alias: room.canonical_alias(),
31ce2453c82fatagen2026-09-24 13:07 UTC82
        joined_members: room.joined_members_count(),
31ce2453c82fatagen2026-09-24 13:07 UTC83
        invited_members: room.invited_members_count(),
31ce2453c82fatagen2026-09-24 13:07 UTC84
        heroes: room
31ce2453c82fatagen2026-09-24 13:07 UTC85
            .heroes()
31ce2453c82fatagen2026-09-24 13:07 UTC86
            .into_iter()
31ce2453c82fatagen2026-09-24 13:07 UTC87
            .map(|hero| RoomHero {
31ce2453c82fatagen2026-09-24 13:07 UTC88
                user_id: hero.user_id,
31ce2453c82fatagen2026-09-24 13:07 UTC89
                display_name: hero.display_name,
5d2974de5826atagen2026-10-08 11:29 UTC90
                avatar: hero.avatar_url.as_deref().map(MediaId::plain),
31ce2453c82fatagen2026-09-24 13:07 UTC91
            })
31ce2453c82fatagen2026-09-24 13:07 UTC92
            .collect(),
31ce2453c82fatagen2026-09-24 13:07 UTC93
        successor: room.successor_room().map(|successor| successor.room_id),
31ce2453c82fatagen2026-09-24 13:07 UTC94
        inviter,
31ce2453c82fatagen2026-09-24 13:07 UTC95
        parent_spaces: spaces
31ce2453c82fatagen2026-09-24 13:07 UTC96
            .joined_parents_of_child(room.room_id())
31ce2453c82fatagen2026-09-24 13:07 UTC97
            .await
31ce2453c82fatagen2026-09-24 13:07 UTC98
            .into_iter()
31ce2453c82fatagen2026-09-24 13:07 UTC99
            .map(|parent| parent.room_id)
31ce2453c82fatagen2026-09-24 13:07 UTC100
            .collect(),
31ce2453c82fatagen2026-09-24 13:07 UTC101
        children,
31ce2453c82fatagen2026-09-24 13:07 UTC102
    }
31ce2453c82fatagen2026-09-24 13:07 UTC103
}
31ce2453c82fatagen2026-09-24 13:07 UTC104
31ce2453c82fatagen2026-09-24 13:07 UTC105
const fn membership(state: RoomState) -> RoomMembership {
31ce2453c82fatagen2026-09-24 13:07 UTC106
    match state {
31ce2453c82fatagen2026-09-24 13:07 UTC107
        RoomState::Joined => RoomMembership::Joined,
31ce2453c82fatagen2026-09-24 13:07 UTC108
        RoomState::Invited => RoomMembership::Invited,
31ce2453c82fatagen2026-09-24 13:07 UTC109
        RoomState::Left => RoomMembership::Left,
31ce2453c82fatagen2026-09-24 13:07 UTC110
        RoomState::Knocked => RoomMembership::Knocked,
31ce2453c82fatagen2026-09-24 13:07 UTC111
        RoomState::Banned => RoomMembership::Banned,
31ce2453c82fatagen2026-09-24 13:07 UTC112
    }
31ce2453c82fatagen2026-09-24 13:07 UTC113
}
31ce2453c82fatagen2026-09-24 13:07 UTC114
31ce2453c82fatagen2026-09-24 13:07 UTC115
/// what the space itself lists, straight from local state. redacted children don't count
31ce2453c82fatagen2026-09-24 13:07 UTC116
async fn space_children(room: &Room) -> Vec<OwnedRoomId> {
31ce2453c82fatagen2026-09-24 13:07 UTC117
    let Ok(events) = room
31ce2453c82fatagen2026-09-24 13:07 UTC118
        .get_state_events_static::<SpaceChildEventContent>()
31ce2453c82fatagen2026-09-24 13:07 UTC119
        .await
31ce2453c82fatagen2026-09-24 13:07 UTC120
    else {
31ce2453c82fatagen2026-09-24 13:07 UTC121
        return Vec::new();
31ce2453c82fatagen2026-09-24 13:07 UTC122
    };
31ce2453c82fatagen2026-09-24 13:07 UTC123
    events
31ce2453c82fatagen2026-09-24 13:07 UTC124
        .into_iter()
31ce2453c82fatagen2026-09-24 13:07 UTC125
        .filter_map(|event| match event.deserialize().ok()? {
31ce2453c82fatagen2026-09-24 13:07 UTC126
            SyncOrStrippedState::Sync(SyncStateEvent::Original(event)) => Some(event.state_key),
31ce2453c82fatagen2026-09-24 13:07 UTC127
            SyncOrStrippedState::Stripped(event) => Some(event.state_key),
31ce2453c82fatagen2026-09-24 13:07 UTC128
            SyncOrStrippedState::Sync(SyncStateEvent::Redacted(_)) => None,
31ce2453c82fatagen2026-09-24 13:07 UTC129
        })
31ce2453c82fatagen2026-09-24 13:07 UTC130
        .collect()
31ce2453c82fatagen2026-09-24 13:07 UTC131
}
31ce2453c82fatagen2026-09-24 13:07 UTC132
31ce2453c82fatagen2026-09-24 13:07 UTC133
fn latest_event(room: &Room) -> Option<LatestEvent> {
5a52012c9b1datagen2026-09-24 23:45 UTC134
    let own = || Some(room.own_user_id().to_owned());
5a52012c9b1datagen2026-09-24 23:45 UTC135
    let local = |local: &matrix_sdk::latest_events::LocalLatestEventValue| {
5a52012c9b1datagen2026-09-24 23:45 UTC136
        let preview = match local.content.deserialize() {
5a52012c9b1datagen2026-09-24 23:45 UTC137
            Ok(AnyMessageLikeEventContent::RoomMessage(content)) => preview(&content),
5a52012c9b1datagen2026-09-24 23:45 UTC138
            _ => (None, None),
5a52012c9b1datagen2026-09-24 23:45 UTC139
        };
5a52012c9b1datagen2026-09-24 23:45 UTC140
        (preview, Some(local.timestamp))
5a52012c9b1datagen2026-09-24 23:45 UTC141
    };
5a52012c9b1datagen2026-09-24 23:45 UTC142
    let (sender, (kind, body), timestamp, send_state) = match room.latest_event() {
31ce2453c82fatagen2026-09-24 13:07 UTC143
        LatestEventValue::None => return None,
31ce2453c82fatagen2026-09-24 13:07 UTC144
        LatestEventValue::Remote(event) => {
5a52012c9b1datagen2026-09-24 23:45 UTC145
            let preview = match event.raw().deserialize() {
31ce2453c82fatagen2026-09-24 13:07 UTC146
                Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
31ce2453c82fatagen2026-09-24 13:07 UTC147
                    message,
31ce2453c82fatagen2026-09-24 13:07 UTC148
                ))) => message
31ce2453c82fatagen2026-09-24 13:07 UTC149
                    .as_original()
5a52012c9b1datagen2026-09-24 23:45 UTC150
                    .map_or((None, None), |message| preview(&message.content)),
5a52012c9b1datagen2026-09-24 23:45 UTC151
                _ => (None, None),
31ce2453c82fatagen2026-09-24 13:07 UTC152
            };
5a52012c9b1datagen2026-09-24 23:45 UTC153
            (event.sender(), preview, event.timestamp(), SendState::Sent)
31ce2453c82fatagen2026-09-24 13:07 UTC154
        }
31ce2453c82fatagen2026-09-24 13:07 UTC155
        LatestEventValue::RemoteInvite {
31ce2453c82fatagen2026-09-24 13:07 UTC156
            timestamp, inviter, ..
5a52012c9b1datagen2026-09-24 23:45 UTC157
        } => (inviter, (None, None), Some(timestamp), SendState::Sent),
5a52012c9b1datagen2026-09-24 23:45 UTC158
        LatestEventValue::LocalIsSending(value) => {
5a52012c9b1datagen2026-09-24 23:45 UTC159
            let (preview, timestamp) = local(&value);
5a52012c9b1datagen2026-09-24 23:45 UTC160
            (own(), preview, timestamp, SendState::Sending)
5a52012c9b1datagen2026-09-24 23:45 UTC161
        }
31ce2453c82fatagen2026-09-24 13:07 UTC162
        LatestEventValue::LocalHasBeenSent { value, .. } => {
5a52012c9b1datagen2026-09-24 23:45 UTC163
            let (preview, timestamp) = local(&value);
5a52012c9b1datagen2026-09-24 23:45 UTC164
            (own(), preview, timestamp, SendState::Sent)
31ce2453c82fatagen2026-09-24 13:07 UTC165
        }
5a52012c9b1datagen2026-09-24 23:45 UTC166
        // the sdk won't say whether it'll retry, so assume the user has to
5a52012c9b1datagen2026-09-24 23:45 UTC167
        LatestEventValue::LocalCannotBeSent(value) => {
5a52012c9b1datagen2026-09-24 23:45 UTC168
            let (preview, timestamp) = local(&value);
5a52012c9b1datagen2026-09-24 23:45 UTC169
            (
5a52012c9b1datagen2026-09-24 23:45 UTC170
                own(),
5a52012c9b1datagen2026-09-24 23:45 UTC171
                preview,
5a52012c9b1datagen2026-09-24 23:45 UTC172
                timestamp,
5a52012c9b1datagen2026-09-24 23:45 UTC173
                SendState::Failed { will_retry: false },
5a52012c9b1datagen2026-09-24 23:45 UTC174
            )
31ce2453c82fatagen2026-09-24 13:07 UTC175
        }
31ce2453c82fatagen2026-09-24 13:07 UTC176
    };
31ce2453c82fatagen2026-09-24 13:07 UTC177
    Some(LatestEvent {
31ce2453c82fatagen2026-09-24 13:07 UTC178
        sender,
5a52012c9b1datagen2026-09-24 23:45 UTC179
        kind,
31ce2453c82fatagen2026-09-24 13:07 UTC180
        body,
31ce2453c82fatagen2026-09-24 13:07 UTC181
        timestamp_ms: timestamp.map(|timestamp| timestamp.get().into()),
31ce2453c82fatagen2026-09-24 13:07 UTC182
        send_state,
31ce2453c82fatagen2026-09-24 13:07 UTC183
    })
31ce2453c82fatagen2026-09-24 13:07 UTC184
}
31ce2453c82fatagen2026-09-24 13:07 UTC185
5a52012c9b1datagen2026-09-24 23:45 UTC186
/// the latest event of an edited message is the edit, whose own body is the `* fallback`
5a52012c9b1datagen2026-09-24 23:45 UTC187
fn preview(content: &RoomMessageEventContent) -> (Option<MessageKind>, Option<String>) {
5a52012c9b1datagen2026-09-24 23:45 UTC188
    let msgtype = match &content.relates_to {
5a52012c9b1datagen2026-09-24 23:45 UTC189
        Some(Relation::Replacement(replacement)) => &replacement.new_content.msgtype,
5a52012c9b1datagen2026-09-24 23:45 UTC190
        _ => &content.msgtype,
31ce2453c82fatagen2026-09-24 13:07 UTC191
    };
5a52012c9b1datagen2026-09-24 23:45 UTC192
    (Some(message_kind(msgtype)), Some(msgtype.body().to_owned()))
31ce2453c82fatagen2026-09-24 13:07 UTC193
}
31ce2453c82fatagen2026-09-24 13:07 UTC194
31ce2453c82fatagen2026-09-24 13:07 UTC195
pub(crate) fn space_child(room: SpaceRoom) -> SpaceChild {
31ce2453c82fatagen2026-09-24 13:07 UTC196
    SpaceChild {
31ce2453c82fatagen2026-09-24 13:07 UTC197
        name: room.display_name,
31ce2453c82fatagen2026-09-24 13:07 UTC198
        room_id: room.room_id,
31ce2453c82fatagen2026-09-24 13:07 UTC199
        topic: room.topic,
5d2974de5826atagen2026-10-08 11:29 UTC200
        avatar: room.avatar_url.as_deref().map(MediaId::plain),
31ce2453c82fatagen2026-09-24 13:07 UTC201
        canonical_alias: room.canonical_alias,
31ce2453c82fatagen2026-09-24 13:07 UTC202
        joined_members: room.num_joined_members,
31ce2453c82fatagen2026-09-24 13:07 UTC203
        space: room.room_type == Some(ruma::room::RoomType::Space),
31ce2453c82fatagen2026-09-24 13:07 UTC204
        children_count: room.children_count,
31ce2453c82fatagen2026-09-24 13:07 UTC205
        membership: room.state.map(membership),
31ce2453c82fatagen2026-09-24 13:07 UTC206
        via: room.via,
31ce2453c82fatagen2026-09-24 13:07 UTC207
        suggested: room.suggested,
31ce2453c82fatagen2026-09-24 13:07 UTC208
        world_readable: room.world_readable,
31ce2453c82fatagen2026-09-24 13:07 UTC209
        guest_can_join: room.guest_can_join,
96fdf90aacd6atagen2026-09-08 08:24 UTC210
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC211
}
96fdf90aacd6atagen2026-09-08 08:24 UTC212
96fdf90aacd6atagen2026-09-08 08:24 UTC213
pub(crate) const fn notification_mode(
96fdf90aacd6atagen2026-09-08 08:24 UTC214
    mode: matrix_sdk::notification_settings::RoomNotificationMode,
96fdf90aacd6atagen2026-09-08 08:24 UTC215
) -> RoomNotificationMode {
96fdf90aacd6atagen2026-09-08 08:24 UTC216
    match mode {
96fdf90aacd6atagen2026-09-08 08:24 UTC217
        matrix_sdk::notification_settings::RoomNotificationMode::AllMessages => {
96fdf90aacd6atagen2026-09-08 08:24 UTC218
            RoomNotificationMode::AllMessages
96fdf90aacd6atagen2026-09-08 08:24 UTC219
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC220
        matrix_sdk::notification_settings::RoomNotificationMode::MentionsAndKeywordsOnly => {
96fdf90aacd6atagen2026-09-08 08:24 UTC221
            RoomNotificationMode::MentionsAndKeywordsOnly
96fdf90aacd6atagen2026-09-08 08:24 UTC222
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC223
        matrix_sdk::notification_settings::RoomNotificationMode::Mute => RoomNotificationMode::Mute,
96fdf90aacd6atagen2026-09-08 08:24 UTC224
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC225
}