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::{rtc, Mentions};
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        call::notify::{ApplicationType, CallNotifyEventContent},
73        rtc, Mentions,
74    };
75
76    #[test]
77    fn notify_event_serialization() {
78        use ruma_common::owned_user_id;
79
80        let content_user_mention = CallNotifyEventContent::new(
81            "abcdef".into(),
82            ApplicationType::Call,
83            rtc::notification::NotificationType::Ring,
84            Mentions::with_user_ids(vec![
85                owned_user_id!("@user:example.com"),
86                owned_user_id!("@user2:example.com"),
87            ]),
88        );
89
90        let content_room_mention = CallNotifyEventContent::new(
91            "abcdef".into(),
92            ApplicationType::Call,
93            rtc::notification::NotificationType::Ring,
94            Mentions::with_room_mention(),
95        );
96
97        assert_eq!(
98            to_json_value(&content_user_mention).unwrap(),
99            json!({
100                "call_id": "abcdef",
101                "application": "m.call",
102                "m.mentions": {
103                    "user_ids": ["@user2:example.com","@user:example.com"],
104                },
105                "notify_type": "ring",
106            })
107        );
108        assert_eq!(
109            to_json_value(&content_room_mention).unwrap(),
110            json!({
111                "call_id": "abcdef",
112                "application": "m.call",
113                "m.mentions": { "room": true },
114                "notify_type": "ring",
115            })
116        );
117    }
118
119    #[test]
120    fn notify_event_deserialization() {
121        use std::collections::BTreeSet;
122
123        use assert_matches2::assert_matches;
124        use ruma_common::owned_user_id;
125
126        use crate::{AnyMessageLikeEvent, MessageLikeEvent};
127
128        let json_data = json!({
129            "content": {
130                "call_id": "abcdef",
131                "application": "m.call",
132                "m.mentions": {
133                    "room": false,
134                    "user_ids": ["@user:example.com", "@user2:example.com"],
135                },
136                "notify_type": "ring",
137            },
138            "event_id": "$event:notareal.hs",
139            "origin_server_ts": 134_829_848,
140            "room_id": "!roomid:notareal.hs",
141            "sender": "@user:notareal.hs",
142            "type": "m.call.notify",
143        });
144
145        let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
146        assert_matches!(
147            event,
148            AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
149        );
150        let content = message_event.content;
151        assert_eq!(content.call_id, "abcdef");
152        assert!(!content.mentions.room);
153        assert_eq!(
154            content.mentions.user_ids,
155            BTreeSet::from([
156                owned_user_id!("@user:example.com"),
157                owned_user_id!("@user2:example.com")
158            ])
159        );
160    }
161}