ruma_events/rtc/
decline.rs

1//! Type for the MatrixRTC decline event ([MSC4310]).
2//!
3//! Unstable: `org.matrix.msc4310.rtc.decline`
4//!
5//! This event is sent as a reference relation to an `m.rtc.notification` event.
6//!
7//! [MSC4310]: https://github.com/matrix-org/matrix-spec-proposals/pull/4310
8
9use ruma_events::relation::Reference;
10use ruma_macros::EventContent;
11use serde::{Deserialize, Serialize};
12
13/// The content of an `m.rtc.decline` event.
14#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "org.matrix.msc4310.rtc.decline", alias = "m.rtc.decline", kind = MessageLike)]
17pub struct RtcDeclineEventContent {
18    /// The reference to the original call notification message event.
19    ///
20    /// This must be an `m.reference` to the `m.rtc.notification` / `m.call.notify` event.
21    #[serde(rename = "m.relates_to")]
22    pub relates_to: Reference,
23}
24
25impl RtcDeclineEventContent {
26    /// Creates a new `RtcDeclineEventContent` targeting the given notification event id.
27    pub fn new<E: Into<ruma_common::OwnedEventId>>(notification_event_id: E) -> Self {
28        Self { relates_to: Reference::new(notification_event_id.into()) }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use assert_matches2::assert_matches;
35    use js_int::uint;
36    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_event_id};
37    use serde_json::{from_value as from_json_value, json};
38
39    use super::RtcDeclineEventContent;
40    use crate::{AnyMessageLikeEvent, MessageLikeEvent};
41
42    #[test]
43    fn decline_event_serialization() {
44        let content = RtcDeclineEventContent::new(owned_event_id!("$abc:example.org"));
45
46        assert_to_canonical_json_eq!(
47            content,
48            json!({
49                "m.relates_to": {
50                    "rel_type": "m.reference",
51                    "event_id": "$abc:example.org"
52                },
53            })
54        );
55    }
56
57    #[test]
58    fn decline_event_deserialization() {
59        let json_data = json!({
60            "content": {
61                "m.relates_to": {
62                    "rel_type": "m.reference",
63                    "event_id": "$abc:example.org"
64                },
65            },
66            "event_id": "$event:notareal.hs",
67            "origin_server_ts": 134_829_848,
68            "room_id": "!roomid:notareal.hs",
69            "sender": "@user:notareal.hs",
70            "type": "m.rtc.decline"
71        });
72
73        let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
74        assert_matches!(
75            event,
76            AnyMessageLikeEvent::RtcDecline(MessageLikeEvent::Original(decline_event))
77        );
78        assert_eq!(decline_event.sender, "@user:notareal.hs");
79        assert_eq!(decline_event.origin_server_ts.get(), uint!(134_829_848));
80        assert_eq!(decline_event.room_id, "!roomid:notareal.hs");
81        assert_eq!(decline_event.content.relates_to.event_id, "$abc:example.org");
82    }
83}