Skip to content

Commit 7f03ca2

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 7f03ca2

File tree

6 files changed

+168
-0
lines changed

6 files changed

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

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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
#[cfg(feature = "unstable-msc4075")]
2+
use std::collections::BTreeSet;
3+
14
use assert_matches2::assert_matches;
25
#[cfg(feature = "unstable-msc2747")]
36
use assign::assign;
47
use js_int::uint;
8+
#[cfg(feature = "unstable-msc4075")]
9+
use ruma_common::UserId;
510
use ruma_common::{room_id, serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, VoipVersionId};
611
#[cfg(feature = "unstable-msc2747")]
712
use ruma_events::call::CallCapabilities;
13+
#[cfg(feature = "unstable-msc4075")]
14+
use ruma_events::{
15+
call::notify::{ApplicationType, CallNotifyEventContent, NotifyType},
16+
Mentions,
17+
};
818
use ruma_events::{
919
call::{
1020
answer::CallAnswerEventContent,
@@ -598,3 +608,76 @@ fn select_v1_answer_event_deserialization() {
598608
assert_eq!(content.selected_party_id, "6336");
599609
assert_eq!(content.version, VoipVersionId::V1);
600610
}
611+
#[cfg(feature = "unstable-msc4075")]
612+
#[test]
613+
fn notify_event_serialization() {
614+
let content_user_mention = CallNotifyEventContent::new(
615+
"abcdef".into(),
616+
ApplicationType::Call,
617+
NotifyType::Ring,
618+
Mentions::with_user_ids(vec![
619+
<&UserId>::try_from("@user:example.com").unwrap().into(),
620+
<&UserId>::try_from("@user2:example.com").unwrap().into(),
621+
]),
622+
);
623+
624+
let content_room_mention = CallNotifyEventContent::new(
625+
"abcdef".into(),
626+
ApplicationType::Call,
627+
NotifyType::Ring,
628+
Mentions::with_room_mention(),
629+
);
630+
631+
assert_eq!(
632+
to_json_value(&content_user_mention).unwrap(),
633+
json!({
634+
"call_id": "abcdef",
635+
"application": "m.call",
636+
"m.mentions": {"user_ids": ["@user2:example.com","@user:example.com"]},
637+
"notify_type": "ring",
638+
})
639+
);
640+
assert_eq!(
641+
to_json_value(&content_room_mention).unwrap(),
642+
json!({
643+
"call_id": "abcdef",
644+
"application": "m.call",
645+
"m.mentions": {"room": true},
646+
"notify_type": "ring",
647+
})
648+
);
649+
}
650+
651+
#[cfg(feature = "unstable-msc4075")]
652+
#[test]
653+
fn notify_event_deserialization() {
654+
let json_data = json!({
655+
"content": {
656+
"call_id": "abcdef",
657+
"application": "m.call",
658+
"m.mentions": {"room": false, "user_ids": ["@user:example.com", "@user2:example.com"]},
659+
"notify_type": "ring",
660+
},
661+
"event_id": "$event:notareal.hs",
662+
"origin_server_ts": 134_829_848,
663+
"room_id": "!roomid:notareal.hs",
664+
"sender": "@user:notareal.hs",
665+
"type": "m.call.notify",
666+
});
667+
668+
let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
669+
assert_matches!(
670+
event,
671+
AnyMessageLikeEvent::CallNotify(MessageLikeEvent::Original(message_event))
672+
);
673+
let content = message_event.content;
674+
assert_eq!(content.call_id, "abcdef");
675+
assert!(!content.mentions.room);
676+
assert_eq!(
677+
content.mentions.user_ids,
678+
BTreeSet::from([
679+
<&UserId>::try_from("@user:example.com").unwrap().to_owned(),
680+
<&UserId>::try_from("@user2:example.com").unwrap().to_owned(),
681+
])
682+
);
683+
}

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)