ruma_events/room/
presence_sharing.rs1use ruma_macros::{EventContent, StringEnum};
8use serde::{Deserialize, Serialize};
9
10use crate::{EmptyStateKey, PrivOwnedStr};
11
12#[derive(Clone, StringEnum)]
22#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
23#[ruma_enum(rename_all = "snake_case")]
24pub enum PresenceSharingHint {
25 Forbid,
27
28 Suggest,
30
31 #[doc(hidden)]
32 _Custom(PrivOwnedStr),
33}
34
35#[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 pub presence_sharing: PresenceSharingHint,
50}
51
52impl RoomPresenceSharingEventContent {
53 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}