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::{
18    api::client::sync::sync_events::v5 as http,
19    events::{receipt::ReceiptEventContent, AnySyncEphemeralRoomEvent, SyncEphemeralRoomEvent},
20    serde::Raw,
21    OwnedRoomId, RoomId,
22};
23
24use super::super::super::{
25    account_data::for_room as account_data_for_room, ephemeral_events::dispatch_receipt, Context,
26};
27use crate::{
28    store::BaseStateStore,
29    sync::{JoinedRoomUpdate, RoomUpdates},
30    RoomState,
31};
32
33/// Dispatch the ephemeral events in the `extensions.typing` part of the
34/// response.
35pub fn dispatch_typing_ephemeral_events(
36    typing: &http::response::Typing,
37    joined_room_updates: &mut BTreeMap<OwnedRoomId, JoinedRoomUpdate>,
38) {
39    for (room_id, raw) in &typing.rooms {
40        joined_room_updates
41            .entry(room_id.to_owned())
42            .or_default()
43            .ephemeral
44            .push(raw.clone().cast());
45    }
46}
47
48/// Dispatch the ephemeral event in the `extensions.receipts` part of the
49/// response for a particular room.
50pub fn dispatch_receipt_ephemeral_event_for_room(
51    context: &mut Context,
52    room_id: &RoomId,
53    receipt: &Raw<SyncEphemeralRoomEvent<ReceiptEventContent>>,
54    joined_room_update: &mut JoinedRoomUpdate,
55) {
56    let receipt: Raw<AnySyncEphemeralRoomEvent> = receipt.cast_ref().clone();
57
58    dispatch_receipt(context, &receipt, room_id);
59    joined_room_update.ephemeral.push(receipt);
60}
61
62pub async fn room_account_data(
63    context: &mut Context,
64    account_data: &http::response::AccountData,
65    room_updates: &mut RoomUpdates,
66    state_store: &BaseStateStore,
67) {
68    for (room_id, raw) in &account_data.rooms {
69        account_data_for_room(context, room_id, raw, state_store).await;
70
71        if let Some(room) = state_store.room(room_id) {
72            match room.state() {
73                RoomState::Joined => room_updates
74                    .joined
75                    .entry(room_id.to_owned())
76                    .or_default()
77                    .account_data
78                    .append(&mut raw.to_vec()),
79                RoomState::Left | RoomState::Banned => room_updates
80                    .left
81                    .entry(room_id.to_owned())
82                    .or_default()
83                    .account_data
84                    .append(&mut raw.to_vec()),
85                RoomState::Invited | RoomState::Knocked => {}
86            }
87        }
88    }
89}