Skip to content

Commit 986b14c

Browse files
committed
Add CallNotify event as described by MSC4075
See: [MSC4075]( matrix-org/matrix-spec-proposals#4075) Signed-off-by: Timo K <[email protected]>
1 parent 9728f97 commit 986b14c

File tree

6 files changed

+163
-2
lines changed

6 files changed

+163
-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

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: 4 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.
@@ -361,6 +364,7 @@ impl AnyMessageLikeEventContent {
361364
| Self::CallCandidates(_)
362365
| Self::RoomRedaction(_)
363366
| Self::Sticker(_)
367+
| Self::CallNotify(_)
364368
| Self::_Custom { .. } => None,
365369
}
366370
}

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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};
68
#[cfg(feature = "unstable-msc2747")]
79
use ruma_events::call::CallCapabilities;
810
use ruma_events::{
@@ -12,11 +14,12 @@ use ruma_events::{
1214
hangup::{CallHangupEventContent, Reason},
1315
invite::CallInviteEventContent,
1416
negotiate::CallNegotiateEventContent,
17+
notify::{ApplicationType, CallNotifyEventContent, NotifyType},
1518
reject::CallRejectEventContent,
1619
select_answer::CallSelectAnswerEventContent,
1720
SessionDescription,
1821
},
19-
AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent,
22+
AnyMessageLikeEvent, AnySyncMessageLikeEvent, Mentions, MessageLikeEvent,
2023
};
2124
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
2225

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

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)