ruma_events/call/
notify.rs1use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::member::Application;
9use crate::{Mentions, rtc};
10
11#[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 pub call_id: String,
19
20 pub application: ApplicationType,
22
23 pub notify_type: rtc::notification::NotificationType,
25
26 #[serde(rename = "m.mentions")]
30 pub mentions: Mentions,
31}
32impl CallNotifyEventContent {
33 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#[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 #[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 ruma_common::canonical_json::assert_to_canonical_json_eq;
70 use serde_json::{from_value as from_json_value, json};
71 use strass::assert_let;
72
73 use crate::{
74 Mentions,
75 call::notify::{ApplicationType, CallNotifyEventContent},
76 rtc,
77 };
78
79 #[test]
80 fn notify_event_serialization() {
81 use ruma_common::owned_user_id;
82
83 let content_user_mention = CallNotifyEventContent::new(
84 "abcdef".into(),
85 ApplicationType::Call,
86 rtc::notification::NotificationType::Ring,
87 Mentions::with_user_ids(vec![
88 owned_user_id!("@user:example.com"),
89 owned_user_id!("@user2:example.com"),
90 ]),
91 );
92
93 let content_room_mention = CallNotifyEventContent::new(
94 "abcdef".into(),
95 ApplicationType::Call,
96 rtc::notification::NotificationType::Ring,
97 Mentions::with_room_mention(),
98 );
99
100 assert_to_canonical_json_eq!(
101 content_user_mention,
102 json!({
103 "call_id": "abcdef",
104 "application": "m.call",
105 "m.mentions": {
106 "user_ids": ["@user2:example.com","@user:example.com"],
107 },
108 "notify_type": "ring",
109 })
110 );
111 assert_to_canonical_json_eq!(
112 content_room_mention,
113 json!({
114 "call_id": "abcdef",
115 "application": "m.call",
116 "m.mentions": { "room": true },
117 "notify_type": "ring",
118 })
119 );
120 }
121
122 #[test]
123 fn notify_event_deserialization() {
124 use std::collections::BTreeSet;
125
126 use ruma_common::owned_user_id;
127
128 use crate::{AnyMessageLikeEvent, MessageLikeEvent};
129
130 let json_data = json!({
131 "content": {
132 "call_id": "abcdef",
133 "application": "m.call",
134 "m.mentions": {
135 "room": false,
136 "user_ids": ["@user:example.com", "@user2:example.com"],
137 },
138 "notify_type": "ring",
139 },
140 "event_id": "$event:notareal.hs",
141 "origin_server_ts": 134_829_848,
142 "room_id": "!roomid:notareal.hs",
143 "sender": "@user:notareal.hs",
144 "type": "m.call.notify",
145 });
146
147 let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
148 assert_let!(
149 AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event)) = 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}