matrix_sdk_base/response_processors/room/msc4186/
extensions.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::BTreeMap;
16
17use ruma::{api::client::sync::sync_events::v5 as http, OwnedRoomId};
18
19use super::super::super::{
20    account_data::for_room as account_data_for_room, ephemeral_events::dispatch_receipt, Context,
21};
22use crate::{
23    store::BaseStateStore,
24    sync::{JoinedRoomUpdate, RoomUpdates},
25    RoomState,
26};
27
28pub fn dispatch_ephemeral_events(
29    context: &mut Context,
30    receipts: &http::response::Receipts,
31    typing: &http::response::Typing,
32    joined_room_updates: &mut BTreeMap<OwnedRoomId, JoinedRoomUpdate>,
33) {
34    for (room_id, raw) in &receipts.rooms {
35        dispatch_receipt(context, raw.cast_ref(), room_id);
36
37        joined_room_updates
38            .entry(room_id.to_owned())
39            .or_default()
40            .ephemeral
41            .push(raw.clone().cast());
42    }
43
44    for (room_id, raw) in &typing.rooms {
45        joined_room_updates
46            .entry(room_id.to_owned())
47            .or_default()
48            .ephemeral
49            .push(raw.clone().cast());
50    }
51}
52
53pub async fn room_account_data(
54    context: &mut Context,
55    account_data: &http::response::AccountData,
56    room_updates: &mut RoomUpdates,
57    state_store: &BaseStateStore,
58) {
59    for (room_id, raw) in &account_data.rooms {
60        account_data_for_room(context, room_id, raw, state_store).await;
61
62        if let Some(room) = state_store.room(room_id) {
63            match room.state() {
64                RoomState::Joined => room_updates
65                    .joined
66                    .entry(room_id.to_owned())
67                    .or_default()
68                    .account_data
69                    .append(&mut raw.to_vec()),
70                RoomState::Left | RoomState::Banned => room_updates
71                    .left
72                    .entry(room_id.to_owned())
73                    .or_default()
74                    .account_data
75                    .append(&mut raw.to_vec()),
76                RoomState::Invited | RoomState::Knocked => {}
77            }
78        }
79    }
80}