Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 42d261c

Browse files
authored
Port the push rule classes to Rust. (#13768)
1 parent c802ef1 commit 42d261c

File tree

14 files changed

+930
-615
lines changed

14 files changed

+930
-615
lines changed

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
group_imports = "StdExternalCrate"

changelog.d/13768.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port push rules to using Rust.

rust/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ crate-type = ["cdylib"]
1818
name = "synapse.synapse_rust"
1919

2020
[dependencies]
21-
pyo3 = { version = "0.16.5", features = ["extension-module", "macros", "abi3", "abi3-py37"] }
21+
anyhow = "1.0.63"
22+
lazy_static = "1.4.0"
23+
log = "0.4.17"
24+
pyo3 = { version = "0.17.1", features = ["extension-module", "macros", "anyhow", "abi3", "abi3-py37"] }
25+
pyo3-log = "0.7.0"
26+
pythonize = "0.17.0"
27+
regex = "1.6.0"
28+
serde = { version = "1.0.144", features = ["derive"] }
29+
serde_json = "1.0.85"
2230

2331
[build-dependencies]
2432
blake2 = "0.10.4"

rust/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use pyo3::prelude::*;
22

3+
pub mod push;
4+
35
/// Returns the hash of all the rust source files at the time it was compiled.
46
///
57
/// Used by python to detect if the rust library is outdated.
@@ -17,8 +19,13 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
1719

1820
/// The entry point for defining the Python module.
1921
#[pymodule]
20-
fn synapse_rust(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
22+
fn synapse_rust(py: Python<'_>, m: &PyModule) -> PyResult<()> {
23+
pyo3_log::init();
24+
2125
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
2226
m.add_function(wrap_pyfunction!(get_rust_file_digest, m)?)?;
27+
28+
push::register_module(py, m)?;
29+
2330
Ok(())
2431
}

rust/src/push/base_rules.rs

Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
// Copyright 2022 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! Contains the definitions of the "base" push rules.
16+
17+
use std::borrow::Cow;
18+
use std::collections::HashMap;
19+
20+
use lazy_static::lazy_static;
21+
use serde_json::Value;
22+
23+
use super::KnownCondition;
24+
use crate::push::Action;
25+
use crate::push::Condition;
26+
use crate::push::EventMatchCondition;
27+
use crate::push::PushRule;
28+
use crate::push::SetTweak;
29+
use crate::push::TweakValue;
30+
31+
const HIGHLIGHT_ACTION: Action = Action::SetTweak(SetTweak {
32+
set_tweak: Cow::Borrowed("highlight"),
33+
value: None,
34+
other_keys: Value::Null,
35+
});
36+
37+
const HIGHLIGHT_FALSE_ACTION: Action = Action::SetTweak(SetTweak {
38+
set_tweak: Cow::Borrowed("highlight"),
39+
value: Some(TweakValue::Other(Value::Bool(false))),
40+
other_keys: Value::Null,
41+
});
42+
43+
const SOUND_ACTION: Action = Action::SetTweak(SetTweak {
44+
set_tweak: Cow::Borrowed("sound"),
45+
value: Some(TweakValue::String(Cow::Borrowed("default"))),
46+
other_keys: Value::Null,
47+
});
48+
49+
const RING_ACTION: Action = Action::SetTweak(SetTweak {
50+
set_tweak: Cow::Borrowed("sound"),
51+
value: Some(TweakValue::String(Cow::Borrowed("ring"))),
52+
other_keys: Value::Null,
53+
});
54+
55+
pub const BASE_PREPEND_OVERRIDE_RULES: &[PushRule] = &[PushRule {
56+
rule_id: Cow::Borrowed("global/override/.m.rule.master"),
57+
priority_class: 5,
58+
conditions: Cow::Borrowed(&[]),
59+
actions: Cow::Borrowed(&[Action::DontNotify]),
60+
default: true,
61+
default_enabled: false,
62+
}];
63+
64+
pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
65+
PushRule {
66+
rule_id: Cow::Borrowed("global/override/.m.rule.suppress_notices"),
67+
priority_class: 5,
68+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
69+
EventMatchCondition {
70+
key: Cow::Borrowed("content.msgtype"),
71+
pattern: Some(Cow::Borrowed("m.notice")),
72+
pattern_type: None,
73+
},
74+
))]),
75+
actions: Cow::Borrowed(&[Action::DontNotify]),
76+
default: true,
77+
default_enabled: true,
78+
},
79+
PushRule {
80+
rule_id: Cow::Borrowed("global/override/.m.rule.invite_for_me"),
81+
priority_class: 5,
82+
conditions: Cow::Borrowed(&[
83+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
84+
key: Cow::Borrowed("type"),
85+
pattern: Some(Cow::Borrowed("m.room.member")),
86+
pattern_type: None,
87+
})),
88+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
89+
key: Cow::Borrowed("content.membership"),
90+
pattern: Some(Cow::Borrowed("invite")),
91+
pattern_type: None,
92+
})),
93+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
94+
key: Cow::Borrowed("state_key"),
95+
pattern: None,
96+
pattern_type: Some(Cow::Borrowed("user_id")),
97+
})),
98+
]),
99+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION, SOUND_ACTION]),
100+
default: true,
101+
default_enabled: true,
102+
},
103+
PushRule {
104+
rule_id: Cow::Borrowed("global/override/.m.rule.member_event"),
105+
priority_class: 5,
106+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
107+
EventMatchCondition {
108+
key: Cow::Borrowed("type"),
109+
pattern: Some(Cow::Borrowed("m.room.member")),
110+
pattern_type: None,
111+
},
112+
))]),
113+
actions: Cow::Borrowed(&[Action::DontNotify]),
114+
default: true,
115+
default_enabled: true,
116+
},
117+
PushRule {
118+
rule_id: Cow::Borrowed("global/override/.m.rule.contains_display_name"),
119+
priority_class: 5,
120+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::ContainsDisplayName)]),
121+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
122+
default: true,
123+
default_enabled: true,
124+
},
125+
PushRule {
126+
rule_id: Cow::Borrowed("global/override/.m.rule.roomnotif"),
127+
priority_class: 5,
128+
conditions: Cow::Borrowed(&[
129+
Condition::Known(KnownCondition::SenderNotificationPermission {
130+
key: Cow::Borrowed("room"),
131+
}),
132+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
133+
key: Cow::Borrowed("content.body"),
134+
pattern: Some(Cow::Borrowed("@room")),
135+
pattern_type: None,
136+
})),
137+
]),
138+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION]),
139+
default: true,
140+
default_enabled: true,
141+
},
142+
PushRule {
143+
rule_id: Cow::Borrowed("global/override/.m.rule.tombstone"),
144+
priority_class: 5,
145+
conditions: Cow::Borrowed(&[
146+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
147+
key: Cow::Borrowed("type"),
148+
pattern: Some(Cow::Borrowed("m.room.tombstone")),
149+
pattern_type: None,
150+
})),
151+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
152+
key: Cow::Borrowed("state_key"),
153+
pattern: Some(Cow::Borrowed("")),
154+
pattern_type: None,
155+
})),
156+
]),
157+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION]),
158+
default: true,
159+
default_enabled: true,
160+
},
161+
PushRule {
162+
rule_id: Cow::Borrowed("global/override/.m.rule.reaction"),
163+
priority_class: 5,
164+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
165+
EventMatchCondition {
166+
key: Cow::Borrowed("type"),
167+
pattern: Some(Cow::Borrowed("m.reaction")),
168+
pattern_type: None,
169+
},
170+
))]),
171+
actions: Cow::Borrowed(&[Action::DontNotify]),
172+
default: true,
173+
default_enabled: true,
174+
},
175+
PushRule {
176+
rule_id: Cow::Borrowed("global/override/.org.matrix.msc3786.rule.room.server_acl"),
177+
priority_class: 5,
178+
conditions: Cow::Borrowed(&[
179+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
180+
key: Cow::Borrowed("type"),
181+
pattern: Some(Cow::Borrowed("m.room.server_acl")),
182+
pattern_type: None,
183+
})),
184+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
185+
key: Cow::Borrowed("state_key"),
186+
pattern: Some(Cow::Borrowed("")),
187+
pattern_type: None,
188+
})),
189+
]),
190+
actions: Cow::Borrowed(&[]),
191+
default: true,
192+
default_enabled: true,
193+
},
194+
];
195+
196+
pub const BASE_APPEND_CONTENT_RULES: &[PushRule] = &[PushRule {
197+
rule_id: Cow::Borrowed("global/content/.m.rule.contains_user_name"),
198+
priority_class: 4,
199+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
200+
EventMatchCondition {
201+
key: Cow::Borrowed("content.body"),
202+
pattern: None,
203+
pattern_type: Some(Cow::Borrowed("user_localpart")),
204+
},
205+
))]),
206+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
207+
default: true,
208+
default_enabled: true,
209+
}];
210+
211+
pub const BASE_APPEND_UNDERRIDE_RULES: &[PushRule] = &[
212+
PushRule {
213+
rule_id: Cow::Borrowed("global/underride/.m.rule.call"),
214+
priority_class: 1,
215+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
216+
EventMatchCondition {
217+
key: Cow::Borrowed("type"),
218+
pattern: Some(Cow::Borrowed("m.call.invite")),
219+
pattern_type: None,
220+
},
221+
))]),
222+
actions: Cow::Borrowed(&[Action::Notify, RING_ACTION, HIGHLIGHT_FALSE_ACTION]),
223+
default: true,
224+
default_enabled: true,
225+
},
226+
PushRule {
227+
rule_id: Cow::Borrowed("global/underride/.m.rule.room_one_to_one"),
228+
priority_class: 1,
229+
conditions: Cow::Borrowed(&[
230+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
231+
key: Cow::Borrowed("type"),
232+
pattern: Some(Cow::Borrowed("m.room.message")),
233+
pattern_type: None,
234+
})),
235+
Condition::Known(KnownCondition::RoomMemberCount {
236+
is: Some(Cow::Borrowed("2")),
237+
}),
238+
]),
239+
actions: Cow::Borrowed(&[Action::Notify, SOUND_ACTION, HIGHLIGHT_FALSE_ACTION]),
240+
default: true,
241+
default_enabled: true,
242+
},
243+
PushRule {
244+
rule_id: Cow::Borrowed("global/underride/.m.rule.encrypted_room_one_to_one"),
245+
priority_class: 1,
246+
conditions: Cow::Borrowed(&[
247+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
248+
key: Cow::Borrowed("type"),
249+
pattern: Some(Cow::Borrowed("m.room.encrypted")),
250+
pattern_type: None,
251+
})),
252+
Condition::Known(KnownCondition::RoomMemberCount {
253+
is: Some(Cow::Borrowed("2")),
254+
}),
255+
]),
256+
actions: Cow::Borrowed(&[Action::Notify, SOUND_ACTION, HIGHLIGHT_FALSE_ACTION]),
257+
default: true,
258+
default_enabled: true,
259+
},
260+
PushRule {
261+
rule_id: Cow::Borrowed("global/underride/.org.matrix.msc3772.thread_reply"),
262+
priority_class: 1,
263+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::RelationMatch {
264+
rel_type: Cow::Borrowed("m.thread"),
265+
sender: None,
266+
sender_type: Some(Cow::Borrowed("user_id")),
267+
})]),
268+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
269+
default: true,
270+
default_enabled: true,
271+
},
272+
PushRule {
273+
rule_id: Cow::Borrowed("global/underride/.m.rule.message"),
274+
priority_class: 1,
275+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
276+
EventMatchCondition {
277+
key: Cow::Borrowed("type"),
278+
pattern: Some(Cow::Borrowed("m.room.message")),
279+
pattern_type: None,
280+
},
281+
))]),
282+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
283+
default: true,
284+
default_enabled: true,
285+
},
286+
PushRule {
287+
rule_id: Cow::Borrowed("global/underride/.m.rule.encrypted"),
288+
priority_class: 1,
289+
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::EventMatch(
290+
EventMatchCondition {
291+
key: Cow::Borrowed("type"),
292+
pattern: Some(Cow::Borrowed("m.room.encrypted")),
293+
pattern_type: None,
294+
},
295+
))]),
296+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
297+
default: true,
298+
default_enabled: true,
299+
},
300+
PushRule {
301+
rule_id: Cow::Borrowed("global/underride/.im.vector.jitsi"),
302+
priority_class: 1,
303+
conditions: Cow::Borrowed(&[
304+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
305+
key: Cow::Borrowed("type"),
306+
pattern: Some(Cow::Borrowed("im.vector.modular.widgets")),
307+
pattern_type: None,
308+
})),
309+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
310+
key: Cow::Borrowed("content.type"),
311+
pattern: Some(Cow::Borrowed("jitsi")),
312+
pattern_type: None,
313+
})),
314+
Condition::Known(KnownCondition::EventMatch(EventMatchCondition {
315+
key: Cow::Borrowed("state_key"),
316+
pattern: Some(Cow::Borrowed("*")),
317+
pattern_type: None,
318+
})),
319+
]),
320+
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_FALSE_ACTION]),
321+
default: true,
322+
default_enabled: true,
323+
},
324+
];
325+
326+
lazy_static! {
327+
pub static ref BASE_RULES_BY_ID: HashMap<&'static str, &'static PushRule> =
328+
BASE_PREPEND_OVERRIDE_RULES
329+
.iter()
330+
.chain(BASE_APPEND_OVERRIDE_RULES.iter())
331+
.chain(BASE_APPEND_CONTENT_RULES.iter())
332+
.chain(BASE_APPEND_UNDERRIDE_RULES.iter())
333+
.map(|rule| { (&*rule.rule_id, rule) })
334+
.collect();
335+
}

0 commit comments

Comments
 (0)