ruma_events/call/
notify.rs

1//! Type for the MatrixRTC notify event ([MSC4075]).
2//!
3//! [MSC4075]: https://github.com/matrix-org/matrix-spec-proposals/pull/4075
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::member::Application;
9use crate::{Mentions, rtc};
10
11/// The content of an `m.call.notify` event.
12#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14#[ruma_event(type = "m.call.notify", kind = MessageLike)]
15#[deprecated = "Use the m.rtc.notification event instead."]
16pub struct CallNotifyEventContent {
17    /// A unique identifier for the call.
18    pub call_id: String,
19
20    /// The application this notify event applies to.
21    pub application: ApplicationType,
22
23    /// How this notify event should notify the receiver.
24    pub notify_type: rtc::notification::NotificationType,
25
26    /// The users that are notified by this event (See [MSC3952] (Intentional Mentions)).
27    ///
28    /// [MSC3952]: https://github.com/matrix-org/matrix-spec-proposals/pull/3952
29    #[serde(rename = "m.mentions")]
30    pub mentions: Mentions,
31}
32impl CallNotifyEventContent {
33    /// Creates a new `CallNotifyEventContent` with the given configuration.
34    pub fn new(
35        call_id: String,
36        application: ApplicationType,
37        notify_type: rtc::notification::NotificationType,
38        mentions: Mentions,
39    ) -> Self {
40        Self { call_id, application, notify_type, mentions }
41    }
42}
43
44/// The type of matrix RTC application.
45///
46/// This is different to [`Application`] because application contains all the information from the
47/// `m.call.member` event.
48///
49/// An `Application` can be converted into an `ApplicationType` using `.into()`.
50#[derive(Clone, Debug, Deserialize, Serialize)]
51#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
52#[deprecated = "Part of the deprecated CallNotifyEventContent."]
53pub enum ApplicationType {
54    /// A VoIP call.
55    #[serde(rename = "m.call")]
56    Call,
57}
58
59impl From<Application> for ApplicationType {
60    fn from(val: Application) -> Self {
61        match val {
62            Application::Call(_) => ApplicationType::Call,
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
70
71    use crate::{
72        Mentions,
73        call::notify::{ApplicationType, CallNotifyEventContent},
74        rtc,
75    };
76
77    #[test]
78    fn notify_event_serialization() {
79        use ruma_common::owned_user_id;
80
81        let content_user_mention = CallNotifyEventContent::new(
82            "abcdef".into(),
83            ApplicationType::Call,
84            rtc::notification::NotificationType::Ring,
85            Mentions::with_user_ids(vec![
86                owned_user_id!("@user:example.com"),
87                owned_user_id!("@user2:example.com"),
88            ]),
89        );
90
91        let content_room_mention = CallNotifyEventContent::new(
92            "abcdef".into(),
93            ApplicationType::Call,
94            rtc::notification::NotificationType::Ring,
95            Mentions::with_room_mention(),
96        );
97
98        assert_eq!(
99            to_json_value(&content_user_mention).unwrap(),
100            json!({
101                "call_id": "abcdef",
102                "application": "m.call",
103                "m.mentions": {
104                    "user_ids": ["@user2:example.com","@user:example.com"],
105                },
106                "notify_type": "ring",
107            })
108        );
109        assert_eq!(
110            to_json_value(&content_room_mention).unwrap(),
111            json!({
112                "call_id": "abcdef",
113                "application": "m.call",
114                "m.mentions": { "room": true },
115                "notify_type": "ring",
116            })
117        );
118    }
119
120    #[test]
121    fn notify_event_deserialization() {
122        use std::collections::BTreeSet;
123
124        use assert_matches2::assert_matches;
125        use ruma_common::owned_user_id;
126
127        use crate::{AnyMessageLikeEvent, MessageLikeEvent};
128
129        let json_data = json!({
130            "content": {
131                "call_id": "abcdef",
132                "application": "m.call",
133                "m.mentions": {
134                    "room": false,
135                    "user_ids": ["@user:example.com", "@user2:example.com"],
136                },
137                "notify_type": "ring",
138            },
139            "event_id": "$event:notareal.hs",
140            "origin_server_ts": 134_829_848,
141            "room_id": "!roomid:notareal.hs",
142            "sender": "@user:notareal.hs",
143            "type": "m.call.notify",
144        });
145
146        let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
147        assert_matches!(
148            event,
149            AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
150        );
151        let content = message_event.content;
152        assert_eq!(content.call_id, "abcdef");
153        assert!(!content.mentions.room);
154        assert_eq!(
155            content.mentions.user_ids,
156            BTreeSet::from([
157                owned_user_id!("@user:example.com"),
158                owned_user_id!("@user2:example.com")
159            ])
160        );
161    }
162}