matrix_sdk_base/room/
call.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 ruma::OwnedUserId;
16
17use super::Room;
18
19impl Room {
20    /// Is there a non expired membership with application `m.call` and scope
21    /// `m.room` in this room.
22    pub fn has_active_room_call(&self) -> bool {
23        self.inner.read().has_active_room_call()
24    }
25
26    /// Returns a `Vec` of `OwnedUserId`'s that participate in the room call.
27    ///
28    /// MatrixRTC memberships with application `m.call` and scope `m.room` are
29    /// considered. A user can occur twice if they join with two devices.
30    /// Convert to a set depending if the different users are required or the
31    /// amount of sessions.
32    ///
33    /// The vector is ordered by oldest membership user to newest.
34    pub fn active_room_call_participants(&self) -> Vec<OwnedUserId> {
35        self.inner.read().active_room_call_participants()
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use std::{ops::Sub, sync::Arc, time::Duration};
42
43    use assign::assign;
44    use matrix_sdk_test::{ALICE, BOB, CAROL};
45    use ruma::{
46        DeviceId, EventId, MilliSecondsSinceUnixEpoch, OwnedUserId, UserId, device_id, event_id,
47        events::{
48            AnySyncStateEvent, StateUnsigned, SyncStateEvent,
49            call::member::{
50                ActiveFocus, ActiveLivekitFocus, Application, CallApplicationContent,
51                CallMemberEventContent, CallMemberStateKey, Focus, LegacyMembershipData,
52                LegacyMembershipDataInit, LivekitFocus, OriginalSyncCallMemberEvent,
53            },
54        },
55        room_id,
56        time::SystemTime,
57        user_id,
58    };
59    use similar_asserts::assert_eq;
60
61    use super::super::{Room, RoomState};
62    use crate::store::MemoryStore;
63
64    fn make_room_test_helper(room_type: RoomState) -> (Arc<MemoryStore>, Room) {
65        let store = Arc::new(MemoryStore::new());
66        let user_id = user_id!("@me:example.org");
67        let room_id = room_id!("!test:localhost");
68        let (sender, _receiver) = tokio::sync::broadcast::channel(1);
69
70        (store.clone(), Room::new(user_id, store, room_id, room_type, sender))
71    }
72
73    fn timestamp(minutes_ago: u32) -> MilliSecondsSinceUnixEpoch {
74        MilliSecondsSinceUnixEpoch::from_system_time(
75            SystemTime::now().sub(Duration::from_secs((60 * minutes_ago).into())),
76        )
77        .expect("date out of range")
78    }
79
80    fn legacy_membership_for_my_call(
81        device_id: &DeviceId,
82        membership_id: &str,
83        minutes_ago: u32,
84    ) -> LegacyMembershipData {
85        let (application, foci) = foci_and_application();
86        assign!(
87            LegacyMembershipData::from(LegacyMembershipDataInit {
88                application,
89                device_id: device_id.to_owned(),
90                expires: Duration::from_millis(3_600_000),
91                foci_active: foci,
92                membership_id: membership_id.to_owned(),
93            }),
94            { created_ts: Some(timestamp(minutes_ago)) }
95        )
96    }
97
98    fn legacy_member_state_event(
99        memberships: Vec<LegacyMembershipData>,
100        ev_id: &EventId,
101        user_id: &UserId,
102    ) -> AnySyncStateEvent {
103        let content = CallMemberEventContent::new_legacy(memberships);
104
105        AnySyncStateEvent::CallMember(SyncStateEvent::Original(OriginalSyncCallMemberEvent {
106            content,
107            event_id: ev_id.to_owned(),
108            sender: user_id.to_owned(),
109            // we can simply use now here since this will be dropped when using a MinimalStateEvent
110            // in the roomInfo
111            origin_server_ts: timestamp(0),
112            state_key: CallMemberStateKey::new(user_id.to_owned(), None, false),
113            unsigned: StateUnsigned::new(),
114        }))
115    }
116
117    struct InitData<'a> {
118        device_id: &'a DeviceId,
119        minutes_ago: u32,
120    }
121
122    fn session_member_state_event(
123        ev_id: &EventId,
124        user_id: &UserId,
125        init_data: Option<InitData<'_>>,
126    ) -> AnySyncStateEvent {
127        let application = Application::Call(CallApplicationContent::new(
128            "my_call_id_1".to_owned(),
129            ruma::events::call::member::CallScope::Room,
130        ));
131        let foci_preferred = vec![Focus::Livekit(LivekitFocus::new(
132            "my_call_foci_alias".to_owned(),
133            "https://lk.org".to_owned(),
134        ))];
135        let focus_active = ActiveFocus::Livekit(ActiveLivekitFocus::new());
136
137        let (content, state_key) = match init_data {
138            Some(InitData { device_id, minutes_ago }) => {
139                let member_id = format!("{device_id}_m.call");
140                (
141                    CallMemberEventContent::new(
142                        application,
143                        device_id.to_owned(),
144                        focus_active,
145                        foci_preferred,
146                        Some(timestamp(minutes_ago)),
147                        None,
148                    ),
149                    CallMemberStateKey::new(user_id.to_owned(), Some(member_id), false),
150                )
151            }
152
153            None => (
154                CallMemberEventContent::new_empty(None),
155                CallMemberStateKey::new(user_id.to_owned(), None, false),
156            ),
157        };
158
159        AnySyncStateEvent::CallMember(SyncStateEvent::Original(OriginalSyncCallMemberEvent {
160            content,
161            event_id: ev_id.to_owned(),
162            sender: user_id.to_owned(),
163            // we can simply use now here since this will be dropped when using a MinimalStateEvent
164            // in the roomInfo
165            origin_server_ts: timestamp(0),
166            state_key,
167            unsigned: StateUnsigned::new(),
168        }))
169    }
170
171    fn foci_and_application() -> (Application, Vec<Focus>) {
172        (
173            Application::Call(CallApplicationContent::new(
174                "my_call_id_1".to_owned(),
175                ruma::events::call::member::CallScope::Room,
176            )),
177            vec![Focus::Livekit(LivekitFocus::new(
178                "my_call_foci_alias".to_owned(),
179                "https://lk.org".to_owned(),
180            ))],
181        )
182    }
183
184    fn receive_state_events(room: &Room, events: Vec<&AnySyncStateEvent>) {
185        room.inner.update_if(|info| {
186            let mut res = false;
187            for ev in events {
188                res |= info.handle_state_event(ev);
189            }
190            res
191        });
192    }
193
194    /// `user_a`: empty memberships
195    /// `user_b`: one membership
196    /// `user_c`: two memberships (two devices)
197    fn legacy_create_call_with_member_events_for_user(a: &UserId, b: &UserId, c: &UserId) -> Room {
198        let (_, room) = make_room_test_helper(RoomState::Joined);
199
200        let a_empty = legacy_member_state_event(Vec::new(), event_id!("$1234"), a);
201
202        // make b 10min old
203        let m_init_b = legacy_membership_for_my_call(device_id!("DEVICE_0"), "0", 1);
204        let b_one = legacy_member_state_event(vec![m_init_b], event_id!("$12345"), b);
205
206        // c1 1min old
207        let m_init_c1 = legacy_membership_for_my_call(device_id!("DEVICE_0"), "0", 10);
208        // c2 20min old
209        let m_init_c2 = legacy_membership_for_my_call(device_id!("DEVICE_1"), "0", 20);
210        let c_two = legacy_member_state_event(vec![m_init_c1, m_init_c2], event_id!("$123456"), c);
211
212        // Intentionally use a non time sorted receive order.
213        receive_state_events(&room, vec![&c_two, &a_empty, &b_one]);
214
215        room
216    }
217
218    /// `user_a`: empty memberships
219    /// `user_b`: one membership
220    /// `user_c`: two memberships (two devices)
221    fn session_create_call_with_member_events_for_user(a: &UserId, b: &UserId, c: &UserId) -> Room {
222        let (_, room) = make_room_test_helper(RoomState::Joined);
223
224        let a_empty = session_member_state_event(event_id!("$1234"), a, None);
225
226        // make b 10min old
227        let b_one = session_member_state_event(
228            event_id!("$12345"),
229            b,
230            Some(InitData { device_id: "DEVICE_0".into(), minutes_ago: 1 }),
231        );
232
233        let m_c1 = session_member_state_event(
234            event_id!("$123456_0"),
235            c,
236            Some(InitData { device_id: "DEVICE_0".into(), minutes_ago: 10 }),
237        );
238        let m_c2 = session_member_state_event(
239            event_id!("$123456_1"),
240            c,
241            Some(InitData { device_id: "DEVICE_1".into(), minutes_ago: 20 }),
242        );
243        // Intentionally use a non time sorted receive order1
244        receive_state_events(&room, vec![&m_c1, &m_c2, &a_empty, &b_one]);
245
246        room
247    }
248
249    #[test]
250    fn test_show_correct_active_call_state() {
251        let room_legacy = legacy_create_call_with_member_events_for_user(&ALICE, &BOB, &CAROL);
252
253        // This check also tests the ordering.
254        // We want older events to be in the front.
255        // user_b (Bob) is 1min old, c1 (CAROL) 10min old, c2 (CAROL) 20min old
256        assert_eq!(
257            vec![CAROL.to_owned(), CAROL.to_owned(), BOB.to_owned()],
258            room_legacy.active_room_call_participants()
259        );
260        assert!(room_legacy.has_active_room_call());
261
262        let room_session = session_create_call_with_member_events_for_user(&ALICE, &BOB, &CAROL);
263        assert_eq!(
264            vec![CAROL.to_owned(), CAROL.to_owned(), BOB.to_owned()],
265            room_session.active_room_call_participants()
266        );
267        assert!(room_session.has_active_room_call());
268    }
269
270    #[test]
271    fn test_active_call_is_false_when_everyone_left() {
272        let room = legacy_create_call_with_member_events_for_user(&ALICE, &BOB, &CAROL);
273
274        let b_empty_membership = legacy_member_state_event(Vec::new(), event_id!("$1234_1"), &BOB);
275        let c_empty_membership =
276            legacy_member_state_event(Vec::new(), event_id!("$12345_1"), &CAROL);
277
278        receive_state_events(&room, vec![&b_empty_membership, &c_empty_membership]);
279
280        // We have no active call anymore after emptying the memberships
281        assert_eq!(Vec::<OwnedUserId>::new(), room.active_room_call_participants());
282        assert!(!room.has_active_room_call());
283    }
284}