ruma_events/call/
notify.rs
1use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::member::Application;
9use crate::Mentions;
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)]
15pub struct CallNotifyEventContent {
16 pub call_id: String,
18
19 pub application: ApplicationType,
21
22 pub notify_type: NotifyType,
24
25 #[serde(rename = "m.mentions")]
29 pub mentions: Mentions,
30}
31
32impl CallNotifyEventContent {
33 pub fn new(
35 call_id: String,
36 application: ApplicationType,
37 notify_type: NotifyType,
38 mentions: Mentions,
39 ) -> Self {
40 Self { call_id, application, notify_type, mentions }
41 }
42}
43
44#[derive(Clone, Debug, Deserialize, Serialize)]
46#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
47pub enum NotifyType {
48 #[serde(rename = "ring")]
50 Ring,
51
52 #[serde(rename = "notify")]
54 Notify,
55}
56
57#[derive(Clone, Debug, Deserialize, Serialize)]
64#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
65pub enum ApplicationType {
66 #[serde(rename = "m.call")]
68 Call,
69}
70
71impl From<Application> for ApplicationType {
72 fn from(val: Application) -> Self {
73 match val {
74 Application::Call(_) => ApplicationType::Call,
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
82
83 use crate::{
84 call::notify::{ApplicationType, CallNotifyEventContent, NotifyType},
85 Mentions,
86 };
87
88 #[test]
89 fn notify_event_serialization() {
90 use ruma_common::owned_user_id;
91
92 let content_user_mention = CallNotifyEventContent::new(
93 "abcdef".into(),
94 ApplicationType::Call,
95 NotifyType::Ring,
96 Mentions::with_user_ids(vec![
97 owned_user_id!("@user:example.com"),
98 owned_user_id!("@user2:example.com"),
99 ]),
100 );
101
102 let content_room_mention = CallNotifyEventContent::new(
103 "abcdef".into(),
104 ApplicationType::Call,
105 NotifyType::Ring,
106 Mentions::with_room_mention(),
107 );
108
109 assert_eq!(
110 to_json_value(&content_user_mention).unwrap(),
111 json!({
112 "call_id": "abcdef",
113 "application": "m.call",
114 "m.mentions": {
115 "user_ids": ["@user2:example.com","@user:example.com"],
116 },
117 "notify_type": "ring",
118 })
119 );
120 assert_eq!(
121 to_json_value(&content_room_mention).unwrap(),
122 json!({
123 "call_id": "abcdef",
124 "application": "m.call",
125 "m.mentions": { "room": true },
126 "notify_type": "ring",
127 })
128 );
129 }
130
131 #[test]
132 fn notify_event_deserialization() {
133 use std::collections::BTreeSet;
134
135 use assert_matches2::assert_matches;
136 use ruma_common::owned_user_id;
137
138 use crate::{AnyMessageLikeEvent, MessageLikeEvent};
139
140 let json_data = json!({
141 "content": {
142 "call_id": "abcdef",
143 "application": "m.call",
144 "m.mentions": {
145 "room": false,
146 "user_ids": ["@user:example.com", "@user2:example.com"],
147 },
148 "notify_type": "ring",
149 },
150 "event_id": "$event:notareal.hs",
151 "origin_server_ts": 134_829_848,
152 "room_id": "!roomid:notareal.hs",
153 "sender": "@user:notareal.hs",
154 "type": "m.call.notify",
155 });
156
157 let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
158 assert_matches!(
159 event,
160 AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
161 );
162 let content = message_event.content;
163 assert_eq!(content.call_id, "abcdef");
164 assert!(!content.mentions.room);
165 assert_eq!(
166 content.mentions.user_ids,
167 BTreeSet::from([
168 owned_user_id!("@user:example.com"),
169 owned_user_id!("@user2:example.com")
170 ])
171 );
172 }
173}