1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::collections::BTreeMap;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use crate::OwnedUserId;
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.ignored_user_list", kind = GlobalAccountData)]
pub struct IgnoredUserListEventContent {
pub ignored_users: BTreeMap<OwnedUserId, IgnoredUser>,
}
impl IgnoredUserListEventContent {
pub fn new(ignored_users: BTreeMap<OwnedUserId, IgnoredUser>) -> Self {
Self { ignored_users }
}
pub fn users(ignored_users: impl IntoIterator<Item = OwnedUserId>) -> Self {
Self::new(ignored_users.into_iter().map(|id| (id, IgnoredUser {})).collect())
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct IgnoredUser {}
impl IgnoredUser {
pub fn new() -> Self {
Self::default()
}
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::IgnoredUserListEventContent;
use crate::{events::AnyGlobalAccountDataEvent, user_id};
#[test]
fn serialization() {
let ignored_user_list =
IgnoredUserListEventContent::users(vec![user_id!("@carl:example.com").to_owned()]);
let json = json!({
"ignored_users": {
"@carl:example.com": {}
},
});
assert_eq!(to_json_value(ignored_user_list).unwrap(), json);
}
#[test]
fn deserialization() {
let json = json!({
"content": {
"ignored_users": {
"@carl:example.com": {}
}
},
"type": "m.ignored_user_list"
});
let ev = assert_matches!(
from_json_value::<AnyGlobalAccountDataEvent>(json),
Ok(AnyGlobalAccountDataEvent::IgnoredUserList(ev)) => ev
);
assert_eq!(
ev.content.ignored_users.keys().collect::<Vec<_>>(),
vec![user_id!("@carl:example.com")]
);
}
}