Skip to main content

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    OwnedRoomId, RoomId,
19    api::client::sync::sync_events::v5 as http,
20    events::{AnySyncEphemeralRoomEvent, SyncEphemeralRoomEvent, receipt::ReceiptEventContent},
21    serde::Raw,
22};
23
24use super::super::super::{
25    Context, account_data::for_room as account_data_for_room, ephemeral_events::dispatch_receipt,
26};
27use crate::{
28    RoomState,
29    store::BaseStateStore,
30    sync::{JoinedRoomUpdate, RoomUpdates},
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) {
55    let receipt: Raw<AnySyncEphemeralRoomEvent> = receipt.cast_ref().clone();
56
57    dispatch_receipt(context, &receipt, room_id);
58}
59
60pub fn room_account_data(
61    context: &mut Context,
62    account_data: &http::response::AccountData,
63    room_updates: &mut RoomUpdates,
64    state_store: &BaseStateStore,
65) {
66    for (room_id, raw) in &account_data.rooms {
67        account_data_for_room(context, room_id, raw, state_store);
68
69        if let Some(room) = state_store.room(room_id) {
70            match room.state() {
71                RoomState::Joined => room_updates
72                    .joined
73                    .entry(room_id.to_owned())
74                    .or_default()
75                    .account_data
76                    .append(&mut raw.to_vec()),
77                RoomState::Left | RoomState::Banned => room_updates
78                    .left
79                    .entry(room_id.to_owned())
80                    .or_default()
81                    .account_data
82                    .append(&mut raw.to_vec()),
83                RoomState::Invited | RoomState::Knocked => {}
84            }
85        }
86    }
87}