ruma_events/call/
notify.rsuse ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::member::Application;
use crate::Mentions;
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.call.notify", kind = MessageLike)]
pub struct CallNotifyEventContent {
pub call_id: String,
pub application: ApplicationType,
pub notify_type: NotifyType,
#[serde(rename = "m.mentions")]
pub mentions: Mentions,
}
impl CallNotifyEventContent {
pub fn new(
call_id: String,
application: ApplicationType,
notify_type: NotifyType,
mentions: Mentions,
) -> Self {
Self { call_id, application, notify_type, mentions }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum NotifyType {
#[serde(rename = "ring")]
Ring,
#[serde(rename = "notify")]
Notify,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum ApplicationType {
#[serde(rename = "m.call")]
Call,
}
impl From<Application> for ApplicationType {
fn from(val: Application) -> Self {
match val {
Application::Call(_) => ApplicationType::Call,
}
}
}
#[cfg(test)]
mod tests {
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use crate::{
call::notify::{ApplicationType, CallNotifyEventContent, NotifyType},
Mentions,
};
#[test]
fn notify_event_serialization() {
use ruma_common::owned_user_id;
let content_user_mention = CallNotifyEventContent::new(
"abcdef".into(),
ApplicationType::Call,
NotifyType::Ring,
Mentions::with_user_ids(vec![
owned_user_id!("@user:example.com"),
owned_user_id!("@user2:example.com"),
]),
);
let content_room_mention = CallNotifyEventContent::new(
"abcdef".into(),
ApplicationType::Call,
NotifyType::Ring,
Mentions::with_room_mention(),
);
assert_eq!(
to_json_value(&content_user_mention).unwrap(),
json!({
"call_id": "abcdef",
"application": "m.call",
"m.mentions": {
"user_ids": ["@user2:example.com","@user:example.com"],
},
"notify_type": "ring",
})
);
assert_eq!(
to_json_value(&content_room_mention).unwrap(),
json!({
"call_id": "abcdef",
"application": "m.call",
"m.mentions": { "room": true },
"notify_type": "ring",
})
);
}
#[test]
fn notify_event_deserialization() {
use std::collections::BTreeSet;
use assert_matches2::assert_matches;
use ruma_common::owned_user_id;
use crate::{AnyMessageLikeEvent, MessageLikeEvent};
let json_data = json!({
"content": {
"call_id": "abcdef",
"application": "m.call",
"m.mentions": {
"room": false,
"user_ids": ["@user:example.com", "@user2:example.com"],
},
"notify_type": "ring",
},
"event_id": "$event:notareal.hs",
"origin_server_ts": 134_829_848,
"room_id": "!roomid:notareal.hs",
"sender": "@user:notareal.hs",
"type": "m.call.notify",
});
let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
assert_matches!(
event,
AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
);
let content = message_event.content;
assert_eq!(content.call_id, "abcdef");
assert!(!content.mentions.room);
assert_eq!(
content.mentions.user_ids,
BTreeSet::from([
owned_user_id!("@user:example.com"),
owned_user_id!("@user2:example.com")
])
);
}
}