Skip to content

Commit d109691

Browse files
committed
Add CallNotify event as described by MSC4075
See: [MSC4075]( matrix-org/matrix-spec-proposals#4075) Signed-off-by: Timo K <toger5@hotmail.de>
1 parent 9728f97 commit d109691

File tree

6 files changed

+166
-2
lines changed

6 files changed

+166
-2
lines changed

crates/ruma-events/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ unstable-msc3927 = ["unstable-msc3551"]
4040
unstable-msc3954 = ["unstable-msc1767"]
4141
unstable-msc3955 = ["unstable-msc1767"]
4242
unstable-msc3956 = ["unstable-msc1767"]
43+
unstable-msc4075 = []
4344
unstable-pdu = []
4445

4546
# Allow some mandatory fields to be missing, defaulting them to an empty string

crates/ruma-events/src/call.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub mod invite;
99
#[cfg(feature = "unstable-msc3401")]
1010
pub mod member;
1111
pub mod negotiate;
12+
#[cfg(feature = "unstable-msc4075")]
13+
pub mod notify;
1214
pub mod reject;
1315
pub mod select_answer;
1416

crates/ruma-events/src/call/notify.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Types for matrixRTC call notify event ([MSC4075]).
2+
//!
3+
//! This implements the event type defined in MSC4075.
4+
//!
5+
//! [MSC3401]: https://github.com/matrix-org/matrix-spec-proposals/pull/4075
6+
7+
use ruma_macros::EventContent;
8+
use serde::{Deserialize, Serialize};
9+
10+
use super::member::Application;
11+
use crate::Mentions;
12+
13+
/// The content of an `m.call.notify` event.
14+
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15+
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
16+
#[ruma_event(type = "m.call.notify", kind = MessageLike)]
17+
pub struct CallNotifyEventContent {
18+
/// A unique identifier for the call.
19+
pub call_id: String,
20+
/// The application this notify event applies to.
21+
pub application: ApplicationType,
22+
/// How this notify event should notify the receiver.
23+
pub notify_type: NotifyType,
24+
/// The users that are notified by this event (See [MSC3952](https://github.com/matrix-org/matrix-spec-proposals/pull/3952)(Intentional Mentions)).
25+
#[serde(rename = "m.mentions")]
26+
pub mentions: Mentions,
27+
}
28+
29+
impl CallNotifyEventContent {
30+
/// Creates a new `CallNotifyEventContent` with the given configuration.
31+
pub fn new(
32+
call_id: String,
33+
application: ApplicationType,
34+
notify_type: NotifyType,
35+
mentions: Mentions,
36+
) -> Self {
37+
Self { call_id, application, notify_type, mentions }
38+
}
39+
}
40+
41+
/// How this notify event should notify the receiver.
42+
#[derive(Clone, Debug, Deserialize, Serialize)]
43+
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
44+
pub enum NotifyType {
45+
/// The receiving client should ring with an audible sound.
46+
#[serde(rename = "ring")]
47+
Ring,
48+
/// The receiving client should display a visual notification.
49+
#[serde(rename = "notify")]
50+
Notify,
51+
}
52+
53+
/// The type of matrix RTC application.
54+
///
55+
/// This is different to [`Application`] because application contains all the information from the
56+
/// call.member event.
57+
///
58+
/// An `Application` can be converted into an `ApplicationType`:
59+
/// ```
60+
/// let a: Application = myApp;
61+
/// let b: ApplicationType = a.into();
62+
/// ```
63+
#[derive(Clone, Debug, Deserialize, Serialize)]
64+
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
65+
pub enum ApplicationType {
66+
/// A VoIP call.
67+
#[serde(rename = "m.call")]
68+
Call,
69+
}
70+
71+
impl From<Application> for ApplicationType {
72+
fn from(val: Application) -> Self {
73+
match val {
74+
Application::Call(_) => ApplicationType::Call,
75+
}
76+
}
77+
}

crates/ruma-events/src/enums.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ event_enum! {
9393
#[cfg(feature = "unstable-msc3245")]
9494
#[ruma_enum(alias = "m.voice")]
9595
"org.matrix.msc3245.voice.v2" => super::voice,
96+
#[cfg(feature = "unstable-msc4075")]
97+
#[ruma_enum(alias = "m.call.notify")]
98+
"org.matrix.msc4075.call.notify" => super::call::notify,
9699
}
97100

98101
/// Any state event.
@@ -352,6 +355,8 @@ impl AnyMessageLikeEventContent {
352355
}
353356
#[cfg(feature = "unstable-msc3381")]
354357
Self::PollStart(_) | Self::UnstablePollStart(_) => None,
358+
#[cfg(feature = "unstable-msc4075")]
359+
Self::CallNotify(_) => None,
355360
Self::CallNegotiate(_)
356361
| Self::CallReject(_)
357362
| Self::CallSelectAnswer(_)

crates/ruma-events/tests/it/call.rs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use std::collections::BTreeSet;
2+
13
use assert_matches2::assert_matches;
24
#[cfg(feature = "unstable-msc2747")]
35
use assign::assign;
46
use js_int::uint;
5-
use ruma_common::{room_id, serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, VoipVersionId};
7+
use ruma_common::{room_id, serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, UserId, VoipVersionId};
8+
#[cfg(feature = "unstable-msc4075")]
9+
use ruma_events::call::notify::{ApplicationType, CallNotifyEventContent, NotifyType};
610
#[cfg(feature = "unstable-msc2747")]
711
use ruma_events::call::CallCapabilities;
812
use ruma_events::{
@@ -16,7 +20,7 @@ use ruma_events::{
1620
select_answer::CallSelectAnswerEventContent,
1721
SessionDescription,
1822
},
19-
AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent,
23+
AnyMessageLikeEvent, AnySyncMessageLikeEvent, Mentions, MessageLikeEvent,
2024
};
2125
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
2226

@@ -598,3 +602,76 @@ fn select_v1_answer_event_deserialization() {
598602
assert_eq!(content.selected_party_id, "6336");
599603
assert_eq!(content.version, VoipVersionId::V1);
600604
}
605+
#[cfg(feature = "unstable-msc4075")]
606+
#[test]
607+
fn notify_event_serialization() {
608+
let content_user_mention = CallNotifyEventContent::new(
609+
"abcdef".into(),
610+
ApplicationType::Call,
611+
NotifyType::Ring,
612+
Mentions::with_user_ids(vec![
613+
<&UserId>::try_from("@user:example.com").unwrap().into(),
614+
<&UserId>::try_from("@user2:example.com").unwrap().into(),
615+
]),
616+
);
617+
618+
let content_room_mention = CallNotifyEventContent::new(
619+
"abcdef".into(),
620+
ApplicationType::Call,
621+
NotifyType::Ring,
622+
Mentions::with_room_mention(),
623+
);
624+
625+
assert_eq!(
626+
to_json_value(&content_user_mention).unwrap(),
627+
json!({
628+
"call_id": "abcdef",
629+
"application": "m.call",
630+
"m.mentions": {"user_ids": ["@user2:example.com","@user:example.com"]},
631+
"notify_type": "ring",
632+
})
633+
);
634+
assert_eq!(
635+
to_json_value(&content_room_mention).unwrap(),
636+
json!({
637+
"call_id": "abcdef",
638+
"application": "m.call",
639+
"m.mentions": {"room": true},
640+
"notify_type": "ring",
641+
})
642+
);
643+
}
644+
645+
#[cfg(feature = "unstable-msc4075")]
646+
#[test]
647+
fn notify_event_deserialization() {
648+
let json_data = json!({
649+
"content": {
650+
"call_id": "abcdef",
651+
"application": "m.call",
652+
"m.mentions": {"room": false, "user_ids": ["@user:example.com", "@user2:example.com"]},
653+
"notify_type": "ring",
654+
},
655+
"event_id": "$event:notareal.hs",
656+
"origin_server_ts": 134_829_848,
657+
"room_id": "!roomid:notareal.hs",
658+
"sender": "@user:notareal.hs",
659+
"type": "m.call.notify",
660+
});
661+
662+
let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
663+
assert_matches!(
664+
event,
665+
AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
666+
);
667+
let content = message_event.content;
668+
assert_eq!(content.call_id, "abcdef");
669+
assert!(!content.mentions.room);
670+
assert_eq!(
671+
content.mentions.user_ids,
672+
BTreeSet::from([
673+
<&UserId>::try_from("@user:example.com").unwrap().to_owned(),
674+
<&UserId>::try_from("@user2:example.com").unwrap().to_owned(),
675+
])
676+
);
677+
}

crates/ruma/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ unstable-msc3955 = ["ruma-events?/unstable-msc3955"]
214214
unstable-msc3956 = ["ruma-events?/unstable-msc3956"]
215215
unstable-msc3958 = ["ruma-common/unstable-msc3958"]
216216
unstable-msc3983 = ["ruma-client-api?/unstable-msc3983"]
217+
unstable-msc4075 = ["ruma-events?/unstable-msc4075"]
217218
unstable-pdu = ["ruma-events?/unstable-pdu"]
218219
unstable-unspecified = [
219220
"ruma-common/unstable-unspecified",
@@ -259,6 +260,7 @@ __ci = [
259260
"unstable-msc3956",
260261
"unstable-msc3958",
261262
"unstable-msc3983",
263+
"unstable-msc4075",
262264
]
263265

264266
[dependencies]

0 commit comments

Comments
 (0)