ruma_events/
ignored_user_list.rs

1//! Types for the [`m.ignored_user_list`] event.
2//!
3//! [`m.ignored_user_list`]: https://spec.matrix.org/latest/client-server-api/#mignored_user_list
4
5use std::collections::BTreeMap;
6
7use ruma_common::OwnedUserId;
8use ruma_macros::EventContent;
9use serde::{Deserialize, Serialize};
10
11/// The content of an `m.ignored_user_list` event.
12///
13/// A list of users to ignore.
14#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(type = "m.ignored_user_list", kind = GlobalAccountData)]
17pub struct IgnoredUserListEventContent {
18    /// A map of users to ignore.
19    ///
20    /// As [`IgnoredUser`] is currently empty, only the user IDs are useful and
21    /// can be accessed with the `.keys()` and `into_keys()` iterators.
22    pub ignored_users: BTreeMap<OwnedUserId, IgnoredUser>,
23}
24
25impl IgnoredUserListEventContent {
26    /// Creates a new `IgnoredUserListEventContent` from the given map of ignored user.
27    pub fn new(ignored_users: BTreeMap<OwnedUserId, IgnoredUser>) -> Self {
28        Self { ignored_users }
29    }
30
31    /// Creates a new `IgnoredUserListEventContent` from the given list of users.
32    pub fn users(ignored_users: impl IntoIterator<Item = OwnedUserId>) -> Self {
33        Self::new(ignored_users.into_iter().map(|id| (id, IgnoredUser {})).collect())
34    }
35}
36
37/// Details about an ignored user.
38///
39/// This is currently empty.
40#[derive(Clone, Debug, Default, Deserialize, Serialize)]
41#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
42pub struct IgnoredUser {}
43
44impl IgnoredUser {
45    /// Creates an empty `IgnoredUser`.
46    pub fn new() -> Self {
47        Self::default()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use assert_matches2::assert_matches;
54    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_user_id, user_id};
55    use serde_json::{from_value as from_json_value, json};
56
57    use super::IgnoredUserListEventContent;
58    use crate::AnyGlobalAccountDataEvent;
59
60    #[test]
61    fn serialization() {
62        let ignored_user_list =
63            IgnoredUserListEventContent::users(vec![owned_user_id!("@carl:example.com")]);
64
65        assert_to_canonical_json_eq!(
66            ignored_user_list,
67            json!({
68                "ignored_users": {
69                    "@carl:example.com": {}
70                },
71            }),
72        );
73    }
74
75    #[test]
76    fn deserialization() {
77        let json = json!({
78            "content": {
79                "ignored_users": {
80                    "@carl:example.com": {}
81                }
82            },
83            "type": "m.ignored_user_list"
84        });
85
86        assert_matches!(
87            from_json_value::<AnyGlobalAccountDataEvent>(json),
88            Ok(AnyGlobalAccountDataEvent::IgnoredUserList(ev))
89        );
90        assert_eq!(
91            ev.content.ignored_users.keys().collect::<Vec<_>>(),
92            vec![user_id!("@carl:example.com")]
93        );
94    }
95}