Skip to main content

ruma_events/room/
presence_sharing.rs

1//! Types for the `m.room.presence_sharing` state event.
2//!
3//! This event uses the unstable prefix defined in [MSC4495].
4//!
5//! [MSC4495]: https://github.com/matrix-org/matrix-spec-proposals/pull/4495
6
7use ruma_macros::{EventContent, StringEnum};
8use serde::{Deserialize, Serialize};
9
10use crate::{EmptyStateKey, PrivOwnedStr};
11
12/// A room's presence sharing hint.
13///
14/// The room's presence sharing hint indicates either:
15/// 1. Sharing presence within the room is prohibited ([Self::Forbid]), or
16/// 2. Sharing presence within the room should be recommended by clients ([Self::Suggest])
17///
18/// This type is defined in [MSC4495].
19///
20/// [MSC4495]: https://github.com/matrix-org/matrix-spec-proposals/pull/4495
21#[derive(Clone, StringEnum)]
22#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
23#[ruma_enum(rename_all = "snake_case")]
24pub enum PresenceSharingHint {
25    /// Sharing presence within the room is prohibited.
26    Forbid,
27
28    /// Sharing presence within the room should be recommended by clients.
29    Suggest,
30
31    #[doc(hidden)]
32    _Custom(PrivOwnedStr),
33}
34
35/// The content of an `m.room.presence_sharing` event.
36///
37/// The room's presence sharing hint indicates either:
38/// 1. Sharing presence within the room is prohibited (`forbid`), or
39/// 2. Sharing presence within the room should be recommended by clients (`suggest`)
40///
41/// This event uses the unstable prefix defined in [MSC4495].
42///
43/// [MSC4495]: https://github.com/matrix-org/matrix-spec-proposals/pull/4495
44#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
45#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
46#[ruma_event(type = "org.continuwuity.presence_v2.msc4495.room.presence_sharing", kind = State, state_key_type = EmptyStateKey)]
47pub struct RoomPresenceSharingEventContent {
48    /// The room's presence sharing hint.
49    pub presence_sharing: PresenceSharingHint,
50}
51
52impl RoomPresenceSharingEventContent {
53    /// Creates a new `RoomPresenceSharingEventContent` with the given hint.
54    pub fn new(hint: PresenceSharingHint) -> Self {
55        Self { presence_sharing: hint }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use ruma_common::canonical_json::assert_to_canonical_json_eq;
62    use serde_json::{from_value as from_json_value, json};
63
64    use super::{PresenceSharingHint, RoomPresenceSharingEventContent};
65    use crate::OriginalStateEvent;
66
67    #[test]
68    fn serialization() {
69        let content =
70            RoomPresenceSharingEventContent { presence_sharing: PresenceSharingHint::Forbid };
71
72        assert_to_canonical_json_eq!(
73            content,
74            json!({
75                "presence_sharing": "forbid",
76            }),
77        );
78    }
79
80    #[test]
81    fn deserialization() {
82        let json_data = json!({
83            "content": {
84                "presence_sharing": "suggest"
85            },
86            "event_id": "$h29iv0s8:example.com",
87            "origin_server_ts": 1,
88            "room_id": "!n8f893n9:example.com",
89            "sender": "@carl:example.com",
90            "state_key": "",
91            "type": "org.continuwuity.presence_v2.msc4495.room.presence_sharing"
92        });
93
94        assert_eq!(
95            from_json_value::<OriginalStateEvent<RoomPresenceSharingEventContent>>(json_data)
96                .unwrap()
97                .content
98                .presence_sharing,
99            PresenceSharingHint::Suggest
100        );
101    }
102}