Skip to content

Commit ca466b5

Browse files
authored
Attempt to clarify how event_match works (matrix-org#3690)
Fixes matrix-org#3082, matrix-org#2637, matrix-org#3075.
1 parent c420fc4 commit ca466b5

File tree

3 files changed

+104
-22
lines changed

3 files changed

+104
-22
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify the behaviour of `event_match` in push rule conditions.

content/client-server-api/modules/push.md

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,33 +211,123 @@ other keys as their parameters, e.g.
211211

212212
`override` and `underride` rules MAY have a list of 'conditions'. All
213213
conditions must hold true for an event in order for the rule to match. A
214-
rule with no conditions always matches. The following conditions are
215-
defined:
214+
rule with no conditions always matches.
215+
216+
Unrecognised conditions MUST NOT match any events, effectively making
217+
the push rule disabled.
218+
219+
`room`, `sender` and `content` rules do not have conditions in the same
220+
way, but instead have predefined conditions. In the cases of `room` and
221+
`sender` rules, the `rule_id` of the rule determines its behaviour.
222+
223+
The following conditions are defined:
224+
225+
**`event_match`**
216226

217-
`event_match`
218227
This is a glob pattern match on a field of the event. Parameters:
219228

220-
- `key`: The dot-separated field of the event to match, e.g.
221-
`content.body`
222-
- `pattern`: The glob-style pattern to match against. Patterns with no
223-
special glob characters should be treated as having asterisks
224-
prepended and appended when testing the condition.
229+
- `key`: The dot-separated path of the property of the event to match, e.g.
230+
`content.body`.
231+
232+
- `pattern`: The glob-style pattern to match against.
233+
234+
The match is performed case-insensitively, and must match the entire value of
235+
the event field given by `key` (though see below regarding `content.body`). The
236+
exact meaning of "case insensitive" is defined by the implementation of the
237+
homeserver.
238+
239+
Within `pattern`:
240+
241+
* The character `*` matches zero or more characters.
242+
* `?` matches exactly one character.
243+
244+
If the property specified by `key` is completely absent from the event, or does
245+
not have a string value, then the condition will not match, even if `pattern`
246+
is `*`.
247+
248+
{{% boxes/note %}}
249+
For example, if `key` is `content.topic`, and `pattern` is `lunc?*`, then
250+
the following event will match:
251+
252+
```json
253+
{
254+
"content": {
255+
"topic": "Lunch plans",
256+
},
257+
"event_id": "$143273582443PhrSn:example.org",
258+
"room_id": "!636q39766251:example.com",
259+
"sender": "@example:example.org",
260+
"state_key": "",
261+
"type": "m.room.topic"
262+
}
263+
```
264+
265+
Other `topic` values which will match are:
266+
267+
* `"LUNCH"` (case-insensitive; `*` may match zero characters)
268+
269+
The following `membership` values will NOT match:
270+
* `" lunch"` (note leading space)
271+
* `"lunc"` (`?` must match a character)
272+
* `null` (not a string)
273+
{{% /boxes/note %}}
274+
275+
As a special case, if `key` is `content.body`, then `pattern` must instead
276+
match any substring of the value of the property which starts and ends at a
277+
word boundary. A word boundary is defined as the start or end of the value, or
278+
any character not in the sets `[A-Z]`, `[a-z]`, `[0-9]` or `_`.
279+
280+
{{% boxes/note %}}
281+
For example, if `key` is `content.body` and `pattern` is `ex*ple`, the
282+
following event will match:
283+
284+
```json
285+
{
286+
"content": {
287+
"body": "An example event.",
288+
},
289+
"event_id": "$143273976499sgjks:example.org",
290+
"room_id": "!636q39766251:example.com",
291+
"sender": "@example:example.org",
292+
"type": "m.room.message"
293+
}
294+
```
295+
296+
Other `body` values which will match are:
297+
298+
* `"exple"` (the pattern can match at the start and end of the body.)
299+
* `"An exciting triple-whammy"` (the pattern can span multiple words, and `-`
300+
acts as a word separator.)
301+
{{% /boxes/note %}}
302+
303+
304+
{{% boxes/warning %}}
305+
Note that there is no implicit condition for `state_key`. In other words, push
306+
rules which should match only state events must include an explicit condition
307+
for `state_key`.
308+
309+
For an example of this, see the default rule
310+
[`.m.rule.tombstone`](#mruletombstone) below.
311+
{{% /boxes/warning %}}
312+
313+
**`contains_display_name`**
225314

226-
`contains_display_name`
227315
This matches unencrypted messages where `content.body` contains the
228316
owner's display name in that room. This is a separate rule because
229317
display names may change and as such it would be hard to maintain a rule
230318
that matched the user's display name. This condition has no parameters.
231319

232-
`room_member_count`
320+
**`room_member_count`**
321+
233322
This matches the current number of members in the room. Parameters:
234323

235324
- `is`: A decimal integer optionally prefixed by one of, `==`, `<`,
236325
`>`, `>=` or `<=`. A prefix of `<` matches rooms where the member
237326
count is strictly less than the given number and so forth. If no
238327
prefix is present, this parameter defaults to `==`.
239328

240-
`sender_notification_permission`
329+
**`sender_notification_permission`**
330+
241331
This takes into account the current power levels in the room, ensuring
242332
the sender of the event has high enough power to trigger the
243333
notification.
@@ -251,13 +341,6 @@ Parameters:
251341
to look up the power level required to send a notification type from
252342
the `notifications` object in the power level event content.
253343

254-
Unrecognised conditions MUST NOT match any events, effectively making
255-
the push rule disabled.
256-
257-
`room`, `sender` and `content` rules do not have conditions in the same
258-
way, but instead have predefined conditions. In the cases of `room` and
259-
`sender` rules, the `rule_id` of the rule determines its behaviour.
260-
261344
##### Predefined Rules
262345

263346
Homeservers can specify "server-default rules" which operate at a lower
@@ -403,7 +486,7 @@ Definition:
403486
}
404487
```
405488

406-
**`.m.rule.tombstone`**
489+
**<a name="mruletombstone"></a>`.m.rule.tombstone`**
407490

408491
Matches any state event whose type is `m.room.tombstone`. This is
409492
intended to notify users of a room when it is upgraded, similar to what

data/api/client-server/definitions/push_condition.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ properties:
3535
type: string
3636
description: |-
3737
Required for `event_match` conditions. The glob-style pattern to
38-
match against. Patterns with no special glob characters should be
39-
treated as having asterisks prepended and appended when testing the
40-
condition.
38+
match against.
4139
is:
4240
type: string
4341
description: |-

0 commit comments

Comments
 (0)