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/MSC4310
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::owned_event_id;
37    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
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        let value = to_json_value(&content).unwrap();
47        assert_eq!(
48            value,
49            json!({
50                "m.relates_to": {
51                    "rel_type": "m.reference",
52                    "event_id": "$abc:example.org"
53                },
54            })
55        );
56    }
57
58    #[test]
59    fn decline_event_deserialization() {
60        let json_data = json!({
61            "content": {
62                "m.relates_to": {
63                    "rel_type": "m.reference",
64                    "event_id": "$abc:example.org"
65                },
66            },
67            "event_id": "$event:notareal.hs",
68            "origin_server_ts": 134_829_848,
69            "room_id": "!roomid:notareal.hs",
70            "sender": "@user:notareal.hs",
71            "type": "m.rtc.decline"
72        });
73
74        let event = from_json_value::<AnyMessageLikeEvent>(json_data).unwrap();
75        assert_matches!(
76            event,
77            AnyMessageLikeEvent::RtcDecline(MessageLikeEvent::Original(decline_event))
78        );
79        assert_eq!(decline_event.sender, "@user:notareal.hs");
80        assert_eq!(decline_event.origin_server_ts.get(), uint!(134_829_848));
81        assert_eq!(decline_event.room_id, "!roomid:notareal.hs");
82        assert_eq!(decline_event.content.relates_to.event_id, "$abc:example.org");
83    }
84}