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

Commit fd95240

Browse files
committed
temp temp temp
This won't work since it will failed if mentions: {}
1 parent 58f5f78 commit fd95240

File tree

6 files changed

+22
-100
lines changed

6 files changed

+22
-100
lines changed

rust/benches/evaluator.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ fn bench_match_exact(b: &mut Bencher) {
4343

4444
let eval = PushRuleEvaluator::py_new(
4545
flattened_keys,
46-
false,
47-
BTreeSet::new(),
4846
10,
4947
Some(0),
5048
Default::default(),
@@ -54,6 +52,7 @@ fn bench_match_exact(b: &mut Bencher) {
5452
false,
5553
false,
5654
false,
55+
false,
5756
)
5857
.unwrap();
5958

@@ -92,8 +91,6 @@ fn bench_match_word(b: &mut Bencher) {
9291

9392
let eval = PushRuleEvaluator::py_new(
9493
flattened_keys,
95-
false,
96-
BTreeSet::new(),
9794
10,
9895
Some(0),
9996
Default::default(),
@@ -103,6 +100,7 @@ fn bench_match_word(b: &mut Bencher) {
103100
false,
104101
false,
105102
false,
103+
false,
106104
)
107105
.unwrap();
108106

@@ -141,8 +139,6 @@ fn bench_match_word_miss(b: &mut Bencher) {
141139

142140
let eval = PushRuleEvaluator::py_new(
143141
flattened_keys,
144-
false,
145-
BTreeSet::new(),
146142
10,
147143
Some(0),
148144
Default::default(),
@@ -152,6 +148,7 @@ fn bench_match_word_miss(b: &mut Bencher) {
152148
false,
153149
false,
154150
false,
151+
false,
155152
)
156153
.unwrap();
157154

@@ -190,8 +187,6 @@ fn bench_eval_message(b: &mut Bencher) {
190187

191188
let eval = PushRuleEvaluator::py_new(
192189
flattened_keys,
193-
false,
194-
BTreeSet::new(),
195190
10,
196191
Some(0),
197192
Default::default(),
@@ -201,6 +196,7 @@ fn bench_eval_message(b: &mut Bencher) {
201196
false,
202197
false,
203198
false,
199+
false,
204200
)
205201
.unwrap();
206202

rust/src/push/evaluator.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::borrow::Cow;
16-
use std::collections::{BTreeMap, BTreeSet};
16+
use std::collections::BTreeMap;
1717

1818
use crate::push::JsonValue;
1919
use anyhow::{Context, Error};
@@ -70,11 +70,6 @@ pub struct PushRuleEvaluator {
7070
/// The "content.body", if any.
7171
body: String,
7272

73-
/// True if the event has a mentions property and MSC3952 support is enabled.
74-
has_mentions: bool,
75-
/// The user mentions that were part of the message.
76-
user_mentions: BTreeSet<String>,
77-
7873
/// The number of users in the room.
7974
room_member_count: u64,
8075

@@ -104,6 +99,9 @@ pub struct PushRuleEvaluator {
10499

105100
/// If MSC3966 (exact_event_property_contains push rule condition) is enabled.
106101
msc3966_exact_event_property_contains: bool,
102+
103+
/// True if the event has a mentions property and MSC3952 support is enabled.
104+
msc3952_intentional_mentions_enabled: bool,
107105
}
108106

109107
#[pymethods]
@@ -113,8 +111,6 @@ impl PushRuleEvaluator {
113111
#[new]
114112
pub fn py_new(
115113
flattened_keys: BTreeMap<String, JsonValue>,
116-
has_mentions: bool,
117-
user_mentions: BTreeSet<String>,
118114
room_member_count: u64,
119115
sender_power_level: Option<i64>,
120116
notification_power_levels: BTreeMap<String, i64>,
@@ -124,6 +120,7 @@ impl PushRuleEvaluator {
124120
msc3931_enabled: bool,
125121
msc3758_exact_event_match: bool,
126122
msc3966_exact_event_property_contains: bool,
123+
msc3952_intentional_mentions_enabled: bool,
127124
) -> Result<Self, Error> {
128125
let body = match flattened_keys.get("content.body") {
129126
Some(JsonValue::Value(SimpleJsonValue::Str(s))) => s.clone(),
@@ -133,8 +130,6 @@ impl PushRuleEvaluator {
133130
Ok(PushRuleEvaluator {
134131
flattened_keys,
135132
body,
136-
has_mentions,
137-
user_mentions,
138133
room_member_count,
139134
notification_power_levels,
140135
sender_power_level,
@@ -144,6 +139,7 @@ impl PushRuleEvaluator {
144139
msc3931_enabled,
145140
msc3758_exact_event_match,
146141
msc3966_exact_event_property_contains,
142+
msc3952_intentional_mentions_enabled,
147143
})
148144
}
149145

@@ -170,9 +166,11 @@ impl PushRuleEvaluator {
170166

171167
// For backwards-compatibility the legacy mention rules are disabled
172168
// if the event contains the 'm.mentions' property (and if the
173-
// experimental feature is enabled, both of these are represented
174-
// by the has_mentions flag).
175-
if self.has_mentions
169+
// experimental feature is enabled).
170+
if self.msc3952_intentional_mentions_enabled
171+
&& self
172+
.flattened_keys
173+
.contains_key("contents.org.matrix.msc3952.mentions")
176174
&& (rule_id == "global/override/.m.rule.contains_display_name"
177175
|| rule_id == "global/content/.m.rule.contains_user_name"
178176
|| rule_id == "global/override/.m.rule.roomnotif")
@@ -269,13 +267,6 @@ impl PushRuleEvaluator {
269267
KnownCondition::ExactEventPropertyContains(exact_event_match) => {
270268
self.match_exact_event_property_contains(exact_event_match, user_id)?
271269
}
272-
KnownCondition::IsUserMention => {
273-
if let Some(uid) = user_id {
274-
self.user_mentions.contains(uid)
275-
} else {
276-
false
277-
}
278-
}
279270
KnownCondition::ContainsDisplayName => {
280271
if let Some(dn) = display_name {
281272
if !dn.is_empty() {
@@ -546,8 +537,6 @@ fn push_rule_evaluator() {
546537
);
547538
let evaluator = PushRuleEvaluator::py_new(
548539
flattened_keys,
549-
false,
550-
BTreeSet::new(),
551540
10,
552541
Some(0),
553542
BTreeMap::new(),
@@ -557,6 +546,7 @@ fn push_rule_evaluator() {
557546
true,
558547
true,
559548
true,
549+
true,
560550
)
561551
.unwrap();
562552

@@ -578,8 +568,6 @@ fn test_requires_room_version_supports_condition() {
578568
let flags = vec![RoomVersionFeatures::ExtensibleEvents.as_str().to_string()];
579569
let evaluator = PushRuleEvaluator::py_new(
580570
flattened_keys,
581-
false,
582-
BTreeSet::new(),
583571
10,
584572
Some(0),
585573
BTreeMap::new(),
@@ -589,6 +577,7 @@ fn test_requires_room_version_supports_condition() {
589577
true,
590578
true,
591579
true,
580+
true,
592581
)
593582
.unwrap();
594583

rust/src/push/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,6 @@ pub enum KnownCondition {
334334
RelatedEventMatch(RelatedEventMatchCondition),
335335
#[serde(rename = "org.matrix.msc3966.exact_event_property_contains")]
336336
ExactEventPropertyContains(ExactEventMatchCondition),
337-
#[serde(rename = "org.matrix.msc3952.is_user_mention")]
338-
IsUserMention,
339337
ContainsDisplayName,
340338
RoomMemberCount {
341339
#[serde(skip_serializing_if = "Option::is_none")]
@@ -657,17 +655,6 @@ fn test_deserialize_unstable_msc3758_condition() {
657655
));
658656
}
659657

660-
#[test]
661-
fn test_deserialize_unstable_msc3952_user_condition() {
662-
let json = r#"{"kind":"org.matrix.msc3952.is_user_mention"}"#;
663-
664-
let condition: Condition = serde_json::from_str(json).unwrap();
665-
assert!(matches!(
666-
condition,
667-
Condition::Known(KnownCondition::IsUserMention)
668-
));
669-
}
670-
671658
#[test]
672659
fn test_deserialize_custom_condition() {
673660
let json = r#"{"kind":"custom_tag"}"#;

stubs/synapse/synapse_rust/push.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Collection, Dict, Mapping, Optional, Sequence, Set, Tuple, Union
15+
from typing import Any, Collection, Dict, Mapping, Optional, Sequence, Tuple, Union
1616

1717
from synapse.types import JsonDict, JsonValue
1818

@@ -57,8 +57,6 @@ class PushRuleEvaluator:
5757
def __init__(
5858
self,
5959
flattened_keys: Mapping[str, JsonValue],
60-
has_mentions: bool,
61-
user_mentions: Set[str],
6260
room_member_count: int,
6361
sender_power_level: Optional[int],
6462
notification_power_levels: Mapping[str, int],
@@ -68,6 +66,7 @@ class PushRuleEvaluator:
6866
msc3931_enabled: bool,
6967
msc3758_exact_event_match: bool,
7068
msc3966_exact_event_property_contains: bool,
69+
msc3952_intentional_mentions_enabled: bool,
7170
): ...
7271
def run(
7372
self,

synapse/push/bulk_push_rule_evaluator.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,13 @@
2323
Mapping,
2424
Optional,
2525
Sequence,
26-
Set,
2726
Tuple,
2827
Union,
2928
)
3029

3130
from prometheus_client import Counter
3231

33-
from synapse.api.constants import (
34-
MAIN_TIMELINE,
35-
EventContentFields,
36-
EventTypes,
37-
Membership,
38-
RelationTypes,
39-
)
32+
from synapse.api.constants import MAIN_TIMELINE, EventTypes, Membership, RelationTypes
4033
from synapse.api.room_versions import PushRuleRoomFlag
4134
from synapse.event_auth import auth_types_for_event, get_user_power_level
4235
from synapse.events import EventBase, relation_from_event
@@ -396,27 +389,11 @@ async def _action_for_event_by_user(
396389
except (TypeError, ValueError):
397390
del notification_levels[key]
398391

399-
# Pull out any user and room mentions.
400-
mentions = event.content.get(EventContentFields.MSC3952_MENTIONS)
401-
has_mentions = self._intentional_mentions_enabled and isinstance(mentions, dict)
402-
user_mentions: Set[str] = set()
403-
if has_mentions:
404-
# mypy seems to have lost the type even though it must be a dict here.
405-
assert isinstance(mentions, dict)
406-
# Remove out any non-string items and convert to a set.
407-
user_mentions_raw = mentions.get("user_ids")
408-
if isinstance(user_mentions_raw, list):
409-
user_mentions = set(
410-
filter(lambda item: isinstance(item, str), user_mentions_raw)
411-
)
412-
413392
evaluator = PushRuleEvaluator(
414393
_flatten_dict(
415394
event,
416395
msc3783_escape_event_match_key=self.hs.config.experimental.msc3783_escape_event_match_key,
417396
),
418-
has_mentions,
419-
user_mentions,
420397
room_member_count,
421398
sender_power_level,
422399
notification_levels,
@@ -426,6 +403,7 @@ async def _action_for_event_by_user(
426403
self.hs.config.experimental.msc1767_enabled, # MSC3931 flag
427404
self.hs.config.experimental.msc3758_exact_event_match,
428405
self.hs.config.experimental.msc3966_exact_event_property_contains,
406+
self.hs.config.experimental.msc3952_intentional_mentions,
429407
)
430408

431409
users = rules_by_user.keys()

tests/push/test_push_rule_evaluator.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ def _get_evaluator(
167167
power_levels: Dict[str, Union[int, Dict[str, int]]] = {}
168168
return PushRuleEvaluator(
169169
_flatten_dict(event),
170-
has_mentions,
171-
user_mentions or set(),
172170
room_member_count,
173171
sender_power_level,
174172
cast(Dict[str, int], power_levels.get("notifications", {})),
@@ -178,6 +176,7 @@ def _get_evaluator(
178176
msc3931_enabled=True,
179177
msc3758_exact_event_match=True,
180178
msc3966_exact_event_property_contains=True,
179+
msc3952_intention_mentions=True,
181180
)
182181

183182
def test_display_name(self) -> None:
@@ -204,32 +203,6 @@ def test_display_name(self) -> None:
204203
# A display name with spaces should work fine.
205204
self.assertTrue(evaluator.matches(condition, "@user:test", "foo bar"))
206205

207-
def test_user_mentions(self) -> None:
208-
"""Check for user mentions."""
209-
condition = {"kind": "org.matrix.msc3952.is_user_mention"}
210-
211-
# No mentions shouldn't match.
212-
evaluator = self._get_evaluator({}, has_mentions=True)
213-
self.assertFalse(evaluator.matches(condition, "@user:test", None))
214-
215-
# An empty set shouldn't match
216-
evaluator = self._get_evaluator({}, has_mentions=True, user_mentions=set())
217-
self.assertFalse(evaluator.matches(condition, "@user:test", None))
218-
219-
# The Matrix ID appearing anywhere in the mentions list should match
220-
evaluator = self._get_evaluator(
221-
{}, has_mentions=True, user_mentions={"@user:test"}
222-
)
223-
self.assertTrue(evaluator.matches(condition, "@user:test", None))
224-
225-
evaluator = self._get_evaluator(
226-
{}, has_mentions=True, user_mentions={"@another:test", "@user:test"}
227-
)
228-
self.assertTrue(evaluator.matches(condition, "@user:test", None))
229-
230-
# Note that invalid data is tested at tests.push.test_bulk_push_rule_evaluator.TestBulkPushRuleEvaluator.test_mentions
231-
# since the BulkPushRuleEvaluator is what handles data sanitisation.
232-
233206
def _assert_matches(
234207
self, condition: JsonDict, content: JsonMapping, msg: Optional[str] = None
235208
) -> None:

0 commit comments

Comments
 (0)