You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

584 lines
19 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2017 New Vector Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """
  17. Push rules is the system used to determine which events trigger a push (and a
  18. bump in notification counts).
  19. This consists of a list of "push rules" for each user, where a push rule is a
  20. pair of "conditions" and "actions". When a user receives an event Synapse
  21. iterates over the list of push rules until it finds one where all the conditions
  22. match the event, at which point "actions" describe the outcome (e.g. notify,
  23. highlight, etc).
  24. Push rules are split up into 5 different "kinds" (aka "priority classes"), which
  25. are run in order:
  26. 1. Override — highest priority rules, e.g. always ignore notices
  27. 2. Content — content specific rules, e.g. @ notifications
  28. 3. Room — per room rules, e.g. enable/disable notifications for all messages
  29. in a room
  30. 4. Sender — per sender rules, e.g. never notify for messages from a given
  31. user
  32. 5. Underride — the lowest priority "default" rules, e.g. notify for every
  33. message.
  34. The set of "base rules" are the list of rules that every user has by default. A
  35. user can modify their copy of the push rules in one of three ways:
  36. 1. Adding a new push rule of a certain kind
  37. 2. Changing the actions of a base rule
  38. 3. Enabling/disabling a base rule.
  39. The base rules are split into whether they come before or after a particular
  40. kind, so the order of push rule evaluation would be: base rules for before
  41. "override" kind, user defined "override" rules, base rules after "override"
  42. kind, etc, etc.
  43. """
  44. import itertools
  45. import logging
  46. from typing import Dict, Iterator, List, Mapping, Sequence, Tuple, Union
  47. import attr
  48. from synapse.config.experimental import ExperimentalConfig
  49. from synapse.push.rulekinds import PRIORITY_CLASS_MAP
  50. logger = logging.getLogger(__name__)
  51. @attr.s(auto_attribs=True, slots=True, frozen=True)
  52. class PushRule:
  53. """A push rule
  54. Attributes:
  55. rule_id: a unique ID for this rule
  56. priority_class: what "kind" of push rule this is (see
  57. `PRIORITY_CLASS_MAP` for mapping between int and kind)
  58. conditions: the sequence of conditions that all need to match
  59. actions: the actions to apply if all conditions are met
  60. default: is this a base rule?
  61. default_enabled: is this enabled by default?
  62. """
  63. rule_id: str
  64. priority_class: int
  65. conditions: Sequence[Mapping[str, str]]
  66. actions: Sequence[Union[str, Mapping]]
  67. default: bool = False
  68. default_enabled: bool = True
  69. @attr.s(auto_attribs=True, slots=True, frozen=True, weakref_slot=False)
  70. class PushRules:
  71. """A collection of push rules for an account.
  72. Can be iterated over, producing push rules in priority order.
  73. """
  74. # A mapping from rule ID to push rule that overrides a base rule. These will
  75. # be returned instead of the base rule.
  76. overriden_base_rules: Dict[str, PushRule] = attr.Factory(dict)
  77. # The following stores the custom push rules at each priority class.
  78. #
  79. # We keep these separate (rather than combining into one big list) to avoid
  80. # copying the base rules around all the time.
  81. override: List[PushRule] = attr.Factory(list)
  82. content: List[PushRule] = attr.Factory(list)
  83. room: List[PushRule] = attr.Factory(list)
  84. sender: List[PushRule] = attr.Factory(list)
  85. underride: List[PushRule] = attr.Factory(list)
  86. def __iter__(self) -> Iterator[PushRule]:
  87. # When iterating over the push rules we need to return the base rules
  88. # interspersed at the correct spots.
  89. for rule in itertools.chain(
  90. BASE_PREPEND_OVERRIDE_RULES,
  91. self.override,
  92. BASE_APPEND_OVERRIDE_RULES,
  93. self.content,
  94. BASE_APPEND_CONTENT_RULES,
  95. self.room,
  96. self.sender,
  97. self.underride,
  98. BASE_APPEND_UNDERRIDE_RULES,
  99. ):
  100. # Check if a base rule has been overriden by a custom rule. If so
  101. # return that instead.
  102. override_rule = self.overriden_base_rules.get(rule.rule_id)
  103. if override_rule:
  104. yield override_rule
  105. else:
  106. yield rule
  107. def __len__(self) -> int:
  108. # The length is mostly used by caches to get a sense of "size" / amount
  109. # of memory this object is using, so we only count the number of custom
  110. # rules.
  111. return (
  112. len(self.overriden_base_rules)
  113. + len(self.override)
  114. + len(self.content)
  115. + len(self.room)
  116. + len(self.sender)
  117. + len(self.underride)
  118. )
  119. @attr.s(auto_attribs=True, slots=True, frozen=True, weakref_slot=False)
  120. class FilteredPushRules:
  121. """A wrapper around `PushRules` that filters out disabled experimental push
  122. rules, and includes the "enabled" state for each rule when iterated over.
  123. """
  124. push_rules: PushRules
  125. enabled_map: Dict[str, bool]
  126. experimental_config: ExperimentalConfig
  127. def __iter__(self) -> Iterator[Tuple[PushRule, bool]]:
  128. for rule in self.push_rules:
  129. if not _is_experimental_rule_enabled(
  130. rule.rule_id, self.experimental_config
  131. ):
  132. continue
  133. enabled = self.enabled_map.get(rule.rule_id, rule.default_enabled)
  134. yield rule, enabled
  135. def __len__(self) -> int:
  136. return len(self.push_rules)
  137. DEFAULT_EMPTY_PUSH_RULES = PushRules()
  138. def compile_push_rules(rawrules: List[PushRule]) -> PushRules:
  139. """Given a set of custom push rules return a `PushRules` instance (which
  140. includes the base rules).
  141. """
  142. if not rawrules:
  143. # Fast path to avoid allocating empty lists when there are no custom
  144. # rules for the user.
  145. return DEFAULT_EMPTY_PUSH_RULES
  146. rules = PushRules()
  147. for rule in rawrules:
  148. # We need to decide which bucket each custom push rule goes into.
  149. # If it has the same ID as a base rule then it overrides that...
  150. overriden_base_rule = BASE_RULES_BY_ID.get(rule.rule_id)
  151. if overriden_base_rule:
  152. rules.overriden_base_rules[rule.rule_id] = attr.evolve(
  153. overriden_base_rule, actions=rule.actions
  154. )
  155. continue
  156. # ... otherwise it gets added to the appropriate priority class bucket
  157. collection: List[PushRule]
  158. if rule.priority_class == 5:
  159. collection = rules.override
  160. elif rule.priority_class == 4:
  161. collection = rules.content
  162. elif rule.priority_class == 3:
  163. collection = rules.room
  164. elif rule.priority_class == 2:
  165. collection = rules.sender
  166. elif rule.priority_class == 1:
  167. collection = rules.underride
  168. elif rule.priority_class <= 0:
  169. logger.info(
  170. "Got rule with priority class less than zero, but doesn't override a base rule: %s",
  171. rule,
  172. )
  173. continue
  174. else:
  175. # We log and continue here so as not to break event sending
  176. logger.error("Unknown priority class: %", rule.priority_class)
  177. continue
  178. collection.append(rule)
  179. return rules
  180. def _is_experimental_rule_enabled(
  181. rule_id: str, experimental_config: ExperimentalConfig
  182. ) -> bool:
  183. """Used by `FilteredPushRules` to filter out experimental rules when they
  184. have not been enabled.
  185. """
  186. if (
  187. rule_id == "global/override/.org.matrix.msc3786.rule.room.server_acl"
  188. and not experimental_config.msc3786_enabled
  189. ):
  190. return False
  191. if (
  192. rule_id == "global/underride/.org.matrix.msc3772.thread_reply"
  193. and not experimental_config.msc3772_enabled
  194. ):
  195. return False
  196. return True
  197. BASE_APPEND_CONTENT_RULES = [
  198. PushRule(
  199. default=True,
  200. priority_class=PRIORITY_CLASS_MAP["content"],
  201. rule_id="global/content/.m.rule.contains_user_name",
  202. conditions=[
  203. {
  204. "kind": "event_match",
  205. "key": "content.body",
  206. # Match the localpart of the requester's MXID.
  207. "pattern_type": "user_localpart",
  208. }
  209. ],
  210. actions=[
  211. "notify",
  212. {"set_tweak": "sound", "value": "default"},
  213. {"set_tweak": "highlight"},
  214. ],
  215. )
  216. ]
  217. BASE_PREPEND_OVERRIDE_RULES = [
  218. PushRule(
  219. default=True,
  220. priority_class=PRIORITY_CLASS_MAP["override"],
  221. rule_id="global/override/.m.rule.master",
  222. default_enabled=False,
  223. conditions=[],
  224. actions=["dont_notify"],
  225. )
  226. ]
  227. BASE_APPEND_OVERRIDE_RULES = [
  228. PushRule(
  229. default=True,
  230. priority_class=PRIORITY_CLASS_MAP["override"],
  231. rule_id="global/override/.m.rule.suppress_notices",
  232. conditions=[
  233. {
  234. "kind": "event_match",
  235. "key": "content.msgtype",
  236. "pattern": "m.notice",
  237. "_cache_key": "_suppress_notices",
  238. }
  239. ],
  240. actions=["dont_notify"],
  241. ),
  242. # NB. .m.rule.invite_for_me must be higher prio than .m.rule.member_event
  243. # otherwise invites will be matched by .m.rule.member_event
  244. PushRule(
  245. default=True,
  246. priority_class=PRIORITY_CLASS_MAP["override"],
  247. rule_id="global/override/.m.rule.invite_for_me",
  248. conditions=[
  249. {
  250. "kind": "event_match",
  251. "key": "type",
  252. "pattern": "m.room.member",
  253. "_cache_key": "_member",
  254. },
  255. {
  256. "kind": "event_match",
  257. "key": "content.membership",
  258. "pattern": "invite",
  259. "_cache_key": "_invite_member",
  260. },
  261. # Match the requester's MXID.
  262. {"kind": "event_match", "key": "state_key", "pattern_type": "user_id"},
  263. ],
  264. actions=[
  265. "notify",
  266. {"set_tweak": "sound", "value": "default"},
  267. {"set_tweak": "highlight", "value": False},
  268. ],
  269. ),
  270. # Will we sometimes want to know about people joining and leaving?
  271. # Perhaps: if so, this could be expanded upon. Seems the most usual case
  272. # is that we don't though. We add this override rule so that even if
  273. # the room rule is set to notify, we don't get notifications about
  274. # join/leave/avatar/displayname events.
  275. # See also: https://matrix.org/jira/browse/SYN-607
  276. PushRule(
  277. default=True,
  278. priority_class=PRIORITY_CLASS_MAP["override"],
  279. rule_id="global/override/.m.rule.member_event",
  280. conditions=[
  281. {
  282. "kind": "event_match",
  283. "key": "type",
  284. "pattern": "m.room.member",
  285. "_cache_key": "_member",
  286. }
  287. ],
  288. actions=["dont_notify"],
  289. ),
  290. # This was changed from underride to override so it's closer in priority
  291. # to the content rules where the user name highlight rule lives. This
  292. # way a room rule is lower priority than both but a custom override rule
  293. # is higher priority than both.
  294. PushRule(
  295. default=True,
  296. priority_class=PRIORITY_CLASS_MAP["override"],
  297. rule_id="global/override/.m.rule.contains_display_name",
  298. conditions=[{"kind": "contains_display_name"}],
  299. actions=[
  300. "notify",
  301. {"set_tweak": "sound", "value": "default"},
  302. {"set_tweak": "highlight"},
  303. ],
  304. ),
  305. PushRule(
  306. default=True,
  307. priority_class=PRIORITY_CLASS_MAP["override"],
  308. rule_id="global/override/.m.rule.roomnotif",
  309. conditions=[
  310. {
  311. "kind": "event_match",
  312. "key": "content.body",
  313. "pattern": "@room",
  314. "_cache_key": "_roomnotif_content",
  315. },
  316. {
  317. "kind": "sender_notification_permission",
  318. "key": "room",
  319. "_cache_key": "_roomnotif_pl",
  320. },
  321. ],
  322. actions=["notify", {"set_tweak": "highlight", "value": True}],
  323. ),
  324. PushRule(
  325. default=True,
  326. priority_class=PRIORITY_CLASS_MAP["override"],
  327. rule_id="global/override/.m.rule.tombstone",
  328. conditions=[
  329. {
  330. "kind": "event_match",
  331. "key": "type",
  332. "pattern": "m.room.tombstone",
  333. "_cache_key": "_tombstone",
  334. },
  335. {
  336. "kind": "event_match",
  337. "key": "state_key",
  338. "pattern": "",
  339. "_cache_key": "_tombstone_statekey",
  340. },
  341. ],
  342. actions=["notify", {"set_tweak": "highlight", "value": True}],
  343. ),
  344. PushRule(
  345. default=True,
  346. priority_class=PRIORITY_CLASS_MAP["override"],
  347. rule_id="global/override/.m.rule.reaction",
  348. conditions=[
  349. {
  350. "kind": "event_match",
  351. "key": "type",
  352. "pattern": "m.reaction",
  353. "_cache_key": "_reaction",
  354. }
  355. ],
  356. actions=["dont_notify"],
  357. ),
  358. # XXX: This is an experimental rule that is only enabled if msc3786_enabled
  359. # is enabled, if it is not the rule gets filtered out in _load_rules() in
  360. # PushRulesWorkerStore
  361. PushRule(
  362. default=True,
  363. priority_class=PRIORITY_CLASS_MAP["override"],
  364. rule_id="global/override/.org.matrix.msc3786.rule.room.server_acl",
  365. conditions=[
  366. {
  367. "kind": "event_match",
  368. "key": "type",
  369. "pattern": "m.room.server_acl",
  370. "_cache_key": "_room_server_acl",
  371. },
  372. {
  373. "kind": "event_match",
  374. "key": "state_key",
  375. "pattern": "",
  376. "_cache_key": "_room_server_acl_state_key",
  377. },
  378. ],
  379. actions=[],
  380. ),
  381. ]
  382. BASE_APPEND_UNDERRIDE_RULES = [
  383. PushRule(
  384. default=True,
  385. priority_class=PRIORITY_CLASS_MAP["underride"],
  386. rule_id="global/underride/.m.rule.call",
  387. conditions=[
  388. {
  389. "kind": "event_match",
  390. "key": "type",
  391. "pattern": "m.call.invite",
  392. "_cache_key": "_call",
  393. }
  394. ],
  395. actions=[
  396. "notify",
  397. {"set_tweak": "sound", "value": "ring"},
  398. {"set_tweak": "highlight", "value": False},
  399. ],
  400. ),
  401. # XXX: once m.direct is standardised everywhere, we should use it to detect
  402. # a DM from the user's perspective rather than this heuristic.
  403. PushRule(
  404. default=True,
  405. priority_class=PRIORITY_CLASS_MAP["underride"],
  406. rule_id="global/underride/.m.rule.room_one_to_one",
  407. conditions=[
  408. {"kind": "room_member_count", "is": "2", "_cache_key": "member_count"},
  409. {
  410. "kind": "event_match",
  411. "key": "type",
  412. "pattern": "m.room.message",
  413. "_cache_key": "_message",
  414. },
  415. ],
  416. actions=[
  417. "notify",
  418. {"set_tweak": "sound", "value": "default"},
  419. {"set_tweak": "highlight", "value": False},
  420. ],
  421. ),
  422. # XXX: this is going to fire for events which aren't m.room.messages
  423. # but are encrypted (e.g. m.call.*)...
  424. PushRule(
  425. default=True,
  426. priority_class=PRIORITY_CLASS_MAP["underride"],
  427. rule_id="global/underride/.m.rule.encrypted_room_one_to_one",
  428. conditions=[
  429. {"kind": "room_member_count", "is": "2", "_cache_key": "member_count"},
  430. {
  431. "kind": "event_match",
  432. "key": "type",
  433. "pattern": "m.room.encrypted",
  434. "_cache_key": "_encrypted",
  435. },
  436. ],
  437. actions=[
  438. "notify",
  439. {"set_tweak": "sound", "value": "default"},
  440. {"set_tweak": "highlight", "value": False},
  441. ],
  442. ),
  443. PushRule(
  444. default=True,
  445. priority_class=PRIORITY_CLASS_MAP["underride"],
  446. rule_id="global/underride/.org.matrix.msc3772.thread_reply",
  447. conditions=[
  448. {
  449. "kind": "org.matrix.msc3772.relation_match",
  450. "rel_type": "m.thread",
  451. # Match the requester's MXID.
  452. "sender_type": "user_id",
  453. }
  454. ],
  455. actions=["notify", {"set_tweak": "highlight", "value": False}],
  456. ),
  457. PushRule(
  458. default=True,
  459. priority_class=PRIORITY_CLASS_MAP["underride"],
  460. rule_id="global/underride/.m.rule.message",
  461. conditions=[
  462. {
  463. "kind": "event_match",
  464. "key": "type",
  465. "pattern": "m.room.message",
  466. "_cache_key": "_message",
  467. }
  468. ],
  469. actions=["notify", {"set_tweak": "highlight", "value": False}],
  470. ),
  471. # XXX: this is going to fire for events which aren't m.room.messages
  472. # but are encrypted (e.g. m.call.*)...
  473. PushRule(
  474. default=True,
  475. priority_class=PRIORITY_CLASS_MAP["underride"],
  476. rule_id="global/underride/.m.rule.encrypted",
  477. conditions=[
  478. {
  479. "kind": "event_match",
  480. "key": "type",
  481. "pattern": "m.room.encrypted",
  482. "_cache_key": "_encrypted",
  483. }
  484. ],
  485. actions=["notify", {"set_tweak": "highlight", "value": False}],
  486. ),
  487. PushRule(
  488. default=True,
  489. priority_class=PRIORITY_CLASS_MAP["underride"],
  490. rule_id="global/underride/.im.vector.jitsi",
  491. conditions=[
  492. {
  493. "kind": "event_match",
  494. "key": "type",
  495. "pattern": "im.vector.modular.widgets",
  496. "_cache_key": "_type_modular_widgets",
  497. },
  498. {
  499. "kind": "event_match",
  500. "key": "content.type",
  501. "pattern": "jitsi",
  502. "_cache_key": "_content_type_jitsi",
  503. },
  504. {
  505. "kind": "event_match",
  506. "key": "state_key",
  507. "pattern": "*",
  508. "_cache_key": "_is_state_event",
  509. },
  510. ],
  511. actions=["notify", {"set_tweak": "highlight", "value": False}],
  512. ),
  513. ]
  514. BASE_RULE_IDS = set()
  515. BASE_RULES_BY_ID: Dict[str, PushRule] = {}
  516. for r in BASE_APPEND_CONTENT_RULES:
  517. BASE_RULE_IDS.add(r.rule_id)
  518. BASE_RULES_BY_ID[r.rule_id] = r
  519. for r in BASE_PREPEND_OVERRIDE_RULES:
  520. BASE_RULE_IDS.add(r.rule_id)
  521. BASE_RULES_BY_ID[r.rule_id] = r
  522. for r in BASE_APPEND_OVERRIDE_RULES:
  523. BASE_RULE_IDS.add(r.rule_id)
  524. BASE_RULES_BY_ID[r.rule_id] = r
  525. for r in BASE_APPEND_UNDERRIDE_RULES:
  526. BASE_RULE_IDS.add(r.rule_id)
  527. BASE_RULES_BY_ID[r.rule_id] = r