ruma_events/push_rules.rs
1//! Types for the [`m.push_rules`] event.
2//!
3//! [`m.push_rules`]: https://spec.matrix.org/latest/client-server-api/#mpush_rules
4
5use ruma_common::push::Ruleset;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9/// The content of an `m.push_rules` event.
10///
11/// Describes all push rules for a user.
12#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14#[ruma_event(type = "m.push_rules", kind = GlobalAccountData)]
15pub struct PushRulesEventContent {
16 /// The global ruleset.
17 pub global: Ruleset,
18}
19
20impl PushRulesEventContent {
21 /// Creates a new `PushRulesEventContent` with the given global ruleset.
22 ///
23 /// You can also construct a `PushRulesEventContent` from a global ruleset using `From` /
24 /// `Into`.
25 pub fn new(global: Ruleset) -> Self {
26 Self { global }
27 }
28}
29
30impl From<Ruleset> for PushRulesEventContent {
31 fn from(global: Ruleset) -> Self {
32 Self::new(global)
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use serde_json::{from_value as from_json_value, json};
39
40 use super::PushRulesEvent;
41
42 #[test]
43 fn sanity_check() {
44 // This is a full example of a push rules event from the specification.
45 let json_data = json!({
46 "content": {
47 "global": {
48 "content": [
49 {
50 "actions": [
51 "notify",
52 {
53 "set_tweak": "sound",
54 "value": "default"
55 },
56 {
57 "set_tweak": "highlight"
58 }
59 ],
60 "default": true,
61 "enabled": true,
62 "pattern": "alice",
63 "rule_id": ".m.rule.contains_user_name"
64 }
65 ],
66 "override": [
67 {
68 "actions": [],
69 "conditions": [],
70 "default": true,
71 "enabled": false,
72 "rule_id": ".m.rule.master"
73 },
74 {
75 "actions": [],
76 "conditions": [
77 {
78 "key": "content.msgtype",
79 "kind": "event_match",
80 "pattern": "m.notice"
81 }
82 ],
83 "default": true,
84 "enabled": true,
85 "rule_id": ".m.rule.suppress_notices"
86 }
87 ],
88 "room": [],
89 "sender": [],
90 "underride": [
91 {
92 "actions": [
93 "notify",
94 {
95 "set_tweak": "sound",
96 "value": "ring"
97 },
98 {
99 "set_tweak": "highlight",
100 "value": false
101 }
102 ],
103 "conditions": [
104 {
105 "key": "type",
106 "kind": "event_match",
107 "pattern": "m.call.invite"
108 }
109 ],
110 "default": true,
111 "enabled": true,
112 "rule_id": ".m.rule.call"
113 },
114 {
115 "actions": [
116 "notify",
117 {
118 "set_tweak": "sound",
119 "value": "default"
120 },
121 {
122 "set_tweak": "highlight"
123 }
124 ],
125 "conditions": [
126 {
127 "kind": "contains_display_name"
128 }
129 ],
130 "default": true,
131 "enabled": true,
132 "rule_id": ".m.rule.contains_display_name"
133 },
134 {
135 "actions": [
136 "notify",
137 {
138 "set_tweak": "sound",
139 "value": "default"
140 },
141 {
142 "set_tweak": "highlight",
143 "value": false
144 }
145 ],
146 "conditions": [
147 {
148 "is": "2",
149 "kind": "room_member_count"
150 }
151 ],
152 "default": true,
153 "enabled": true,
154 "rule_id": ".m.rule.room_one_to_one"
155 },
156 {
157 "actions": [
158 "notify",
159 {
160 "set_tweak": "sound",
161 "value": "default"
162 },
163 {
164 "set_tweak": "highlight",
165 "value": false
166 }
167 ],
168 "conditions": [
169 {
170 "key": "type",
171 "kind": "event_match",
172 "pattern": "m.room.member"
173 },
174 {
175 "key": "content.membership",
176 "kind": "event_match",
177 "pattern": "invite"
178 },
179 {
180 "key": "state_key",
181 "kind": "event_match",
182 "pattern": "@alice:example.com"
183 }
184 ],
185 "default": true,
186 "enabled": true,
187 "rule_id": ".m.rule.invite_for_me"
188 },
189 {
190 "actions": [
191 "notify",
192 {
193 "set_tweak": "highlight",
194 "value": false
195 }
196 ],
197 "conditions": [
198 {
199 "key": "type",
200 "kind": "event_match",
201 "pattern": "m.room.member"
202 }
203 ],
204 "default": true,
205 "enabled": true,
206 "rule_id": ".m.rule.member_event"
207 },
208 {
209 "actions": [
210 "notify",
211 {
212 "set_tweak": "highlight",
213 "value": false
214 }
215 ],
216 "conditions": [
217 {
218 "key": "type",
219 "kind": "event_match",
220 "pattern": "m.room.message"
221 }
222 ],
223 "default": true,
224 "enabled": true,
225 "rule_id": ".m.rule.message"
226 }
227 ]
228 }
229 },
230 "type": "m.push_rules"
231 });
232
233 from_json_value::<PushRulesEvent>(json_data).unwrap();
234 }
235}