Skip to main content

ruma_events/presence/
prompted.rs

1//! Types for the `m.presence.prompted` account data key.
2//!
3//! This uses the unstable prefix defined in [MSC4495].
4//!
5//! [MSC4495]: https://github.com/matrix-org/matrix-spec-proposals/pull/4495
6
7use ruma_common::{OwnedRoomId, OwnedUserId};
8use ruma_macros::EventContent;
9use serde::{Deserialize, Serialize};
10
11/// The content of the `m.presence.prompted` account data key.
12///
13/// This uses the unstable prefix defined in [MSC4495].
14///
15/// [MSC4495]: https://github.com/matrix-org/matrix-spec-proposals/pull/4495
16#[derive(Clone, Default, Debug, Deserialize, Serialize, EventContent)]
17#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
18#[ruma_event(type = "org.continuwuity.presence_v2.msc4495.presence.prompted", kind = GlobalAccountData)]
19pub struct PresencePromptedEventContent {
20    /// The list of users that have previously had the presence sharing prompt displayed.
21    #[serde(default, skip_serializing_if = "Vec::is_empty")]
22    pub users: Vec<OwnedUserId>,
23
24    /// The list of rooms that have previously had the presence sharing prompt displayed.
25    #[serde(default, skip_serializing_if = "Vec::is_empty")]
26    pub rooms: Vec<OwnedRoomId>,
27}
28
29impl PresencePromptedEventContent {
30    /// Creates a new `PresencePromptedEventContent` with the given users and rooms.
31    pub fn new(users: Vec<OwnedUserId>, rooms: Vec<OwnedRoomId>) -> Self {
32        Self { users, rooms }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_room_id, owned_user_id};
39    use serde_json::{from_value as from_json_value, json};
40
41    use super::PresencePromptedEventContent;
42
43    #[test]
44    fn serialization() {
45        let content = PresencePromptedEventContent {
46            users: vec![
47                owned_user_id!("@alice:example.com"),
48                owned_user_id!("@mallory:example.com"),
49            ],
50            rooms: vec![owned_room_id!("!family-group-chat")],
51        };
52
53        assert_to_canonical_json_eq!(
54            content,
55            json!({
56                "users": [
57                    "@alice:example.com",
58                    "@mallory:example.com",
59                ],
60                "rooms": [
61                    "!family-group-chat",
62                ],
63            }),
64        );
65    }
66
67    #[test]
68    fn deserialization() {
69        let json_data = json!({
70            "users": [
71                "@alice:example.com",
72                "@mallory:example.com",
73            ],
74            "rooms": [
75                "!family-group-chat",
76            ],
77        });
78
79        let content = from_json_value::<PresencePromptedEventContent>(json_data).unwrap();
80
81        assert_eq!(content.users[0], "@alice:example.com");
82        assert_eq!(content.users[1], "@mallory:example.com");
83        assert_eq!(content.rooms[0], "!family-group-chat");
84    }
85}