baserules.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2017 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.push.rulekinds import PRIORITY_CLASS_MAP, PRIORITY_CLASS_INVERSE_MAP
  16. import copy
  17. def list_with_base_rules(rawrules):
  18. """Combine the list of rules set by the user with the default push rules
  19. Args:
  20. rawrules(list): The rules the user has modified or set.
  21. Returns:
  22. A new list with the rules set by the user combined with the defaults.
  23. """
  24. ruleslist = []
  25. # Grab the base rules that the user has modified.
  26. # The modified base rules have a priority_class of -1.
  27. modified_base_rules = {
  28. r['rule_id']: r for r in rawrules if r['priority_class'] < 0
  29. }
  30. # Remove the modified base rules from the list, They'll be added back
  31. # in the default postions in the list.
  32. rawrules = [r for r in rawrules if r['priority_class'] >= 0]
  33. # shove the server default rules for each kind onto the end of each
  34. current_prio_class = list(PRIORITY_CLASS_INVERSE_MAP)[-1]
  35. ruleslist.extend(make_base_prepend_rules(
  36. PRIORITY_CLASS_INVERSE_MAP[current_prio_class], modified_base_rules
  37. ))
  38. for r in rawrules:
  39. if r['priority_class'] < current_prio_class:
  40. while r['priority_class'] < current_prio_class:
  41. ruleslist.extend(make_base_append_rules(
  42. PRIORITY_CLASS_INVERSE_MAP[current_prio_class],
  43. modified_base_rules,
  44. ))
  45. current_prio_class -= 1
  46. if current_prio_class > 0:
  47. ruleslist.extend(make_base_prepend_rules(
  48. PRIORITY_CLASS_INVERSE_MAP[current_prio_class],
  49. modified_base_rules,
  50. ))
  51. ruleslist.append(r)
  52. while current_prio_class > 0:
  53. ruleslist.extend(make_base_append_rules(
  54. PRIORITY_CLASS_INVERSE_MAP[current_prio_class],
  55. modified_base_rules,
  56. ))
  57. current_prio_class -= 1
  58. if current_prio_class > 0:
  59. ruleslist.extend(make_base_prepend_rules(
  60. PRIORITY_CLASS_INVERSE_MAP[current_prio_class],
  61. modified_base_rules,
  62. ))
  63. return ruleslist
  64. def make_base_append_rules(kind, modified_base_rules):
  65. rules = []
  66. if kind == 'override':
  67. rules = BASE_APPEND_OVERRIDE_RULES
  68. elif kind == 'underride':
  69. rules = BASE_APPEND_UNDERRIDE_RULES
  70. elif kind == 'content':
  71. rules = BASE_APPEND_CONTENT_RULES
  72. # Copy the rules before modifying them
  73. rules = copy.deepcopy(rules)
  74. for r in rules:
  75. # Only modify the actions, keep the conditions the same.
  76. modified = modified_base_rules.get(r['rule_id'])
  77. if modified:
  78. r['actions'] = modified['actions']
  79. return rules
  80. def make_base_prepend_rules(kind, modified_base_rules):
  81. rules = []
  82. if kind == 'override':
  83. rules = BASE_PREPEND_OVERRIDE_RULES
  84. # Copy the rules before modifying them
  85. rules = copy.deepcopy(rules)
  86. for r in rules:
  87. # Only modify the actions, keep the conditions the same.
  88. modified = modified_base_rules.get(r['rule_id'])
  89. if modified:
  90. r['actions'] = modified['actions']
  91. return rules
  92. BASE_APPEND_CONTENT_RULES = [
  93. {
  94. 'rule_id': 'global/content/.m.rule.contains_user_name',
  95. 'conditions': [
  96. {
  97. 'kind': 'event_match',
  98. 'key': 'content.body',
  99. 'pattern_type': 'user_localpart'
  100. }
  101. ],
  102. 'actions': [
  103. 'notify',
  104. {
  105. 'set_tweak': 'sound',
  106. 'value': 'default',
  107. }, {
  108. 'set_tweak': 'highlight'
  109. }
  110. ]
  111. },
  112. ]
  113. BASE_PREPEND_OVERRIDE_RULES = [
  114. {
  115. 'rule_id': 'global/override/.m.rule.master',
  116. 'enabled': False,
  117. 'conditions': [],
  118. 'actions': [
  119. "dont_notify"
  120. ]
  121. }
  122. ]
  123. BASE_APPEND_OVERRIDE_RULES = [
  124. {
  125. 'rule_id': 'global/override/.m.rule.suppress_notices',
  126. 'conditions': [
  127. {
  128. 'kind': 'event_match',
  129. 'key': 'content.msgtype',
  130. 'pattern': 'm.notice',
  131. '_id': '_suppress_notices',
  132. }
  133. ],
  134. 'actions': [
  135. 'dont_notify',
  136. ]
  137. },
  138. # NB. .m.rule.invite_for_me must be higher prio than .m.rule.member_event
  139. # otherwise invites will be matched by .m.rule.member_event
  140. {
  141. 'rule_id': 'global/override/.m.rule.invite_for_me',
  142. 'conditions': [
  143. {
  144. 'kind': 'event_match',
  145. 'key': 'type',
  146. 'pattern': 'm.room.member',
  147. '_id': '_member',
  148. },
  149. {
  150. 'kind': 'event_match',
  151. 'key': 'content.membership',
  152. 'pattern': 'invite',
  153. '_id': '_invite_member',
  154. },
  155. {
  156. 'kind': 'event_match',
  157. 'key': 'state_key',
  158. 'pattern_type': 'user_id'
  159. },
  160. ],
  161. 'actions': [
  162. 'notify',
  163. {
  164. 'set_tweak': 'sound',
  165. 'value': 'default'
  166. }, {
  167. 'set_tweak': 'highlight',
  168. 'value': False
  169. }
  170. ]
  171. },
  172. # Will we sometimes want to know about people joining and leaving?
  173. # Perhaps: if so, this could be expanded upon. Seems the most usual case
  174. # is that we don't though. We add this override rule so that even if
  175. # the room rule is set to notify, we don't get notifications about
  176. # join/leave/avatar/displayname events.
  177. # See also: https://matrix.org/jira/browse/SYN-607
  178. {
  179. 'rule_id': 'global/override/.m.rule.member_event',
  180. 'conditions': [
  181. {
  182. 'kind': 'event_match',
  183. 'key': 'type',
  184. 'pattern': 'm.room.member',
  185. '_id': '_member',
  186. }
  187. ],
  188. 'actions': [
  189. 'dont_notify'
  190. ]
  191. },
  192. # This was changed from underride to override so it's closer in priority
  193. # to the content rules where the user name highlight rule lives. This
  194. # way a room rule is lower priority than both but a custom override rule
  195. # is higher priority than both.
  196. {
  197. 'rule_id': 'global/override/.m.rule.contains_display_name',
  198. 'conditions': [
  199. {
  200. 'kind': 'contains_display_name'
  201. }
  202. ],
  203. 'actions': [
  204. 'notify',
  205. {
  206. 'set_tweak': 'sound',
  207. 'value': 'default'
  208. }, {
  209. 'set_tweak': 'highlight'
  210. }
  211. ]
  212. },
  213. {
  214. 'rule_id': 'global/override/.m.rule.roomnotif',
  215. 'conditions': [
  216. {
  217. 'kind': 'event_match',
  218. 'key': 'content.body',
  219. 'pattern': '@room',
  220. '_id': '_roomnotif_content',
  221. },
  222. {
  223. 'kind': 'sender_notification_permission',
  224. 'key': 'room',
  225. '_id': '_roomnotif_pl',
  226. },
  227. ],
  228. 'actions': [
  229. 'notify', {
  230. 'set_tweak': 'highlight',
  231. 'value': True,
  232. }
  233. ]
  234. }
  235. ]
  236. BASE_APPEND_UNDERRIDE_RULES = [
  237. {
  238. 'rule_id': 'global/underride/.m.rule.call',
  239. 'conditions': [
  240. {
  241. 'kind': 'event_match',
  242. 'key': 'type',
  243. 'pattern': 'm.call.invite',
  244. '_id': '_call',
  245. }
  246. ],
  247. 'actions': [
  248. 'notify',
  249. {
  250. 'set_tweak': 'sound',
  251. 'value': 'ring'
  252. }, {
  253. 'set_tweak': 'highlight',
  254. 'value': False
  255. }
  256. ]
  257. },
  258. # XXX: once m.direct is standardised everywhere, we should use it to detect
  259. # a DM from the user's perspective rather than this heuristic.
  260. {
  261. 'rule_id': 'global/underride/.m.rule.room_one_to_one',
  262. 'conditions': [
  263. {
  264. 'kind': 'room_member_count',
  265. 'is': '2',
  266. '_id': 'member_count',
  267. },
  268. {
  269. 'kind': 'event_match',
  270. 'key': 'type',
  271. 'pattern': 'm.room.message',
  272. '_id': '_message',
  273. }
  274. ],
  275. 'actions': [
  276. 'notify',
  277. {
  278. 'set_tweak': 'sound',
  279. 'value': 'default'
  280. }, {
  281. 'set_tweak': 'highlight',
  282. 'value': False
  283. }
  284. ]
  285. },
  286. # XXX: this is going to fire for events which aren't m.room.messages
  287. # but are encrypted (e.g. m.call.*)...
  288. {
  289. 'rule_id': 'global/underride/.m.rule.encrypted_room_one_to_one',
  290. 'conditions': [
  291. {
  292. 'kind': 'room_member_count',
  293. 'is': '2',
  294. '_id': 'member_count',
  295. },
  296. {
  297. 'kind': 'event_match',
  298. 'key': 'type',
  299. 'pattern': 'm.room.encrypted',
  300. '_id': '_encrypted',
  301. }
  302. ],
  303. 'actions': [
  304. 'notify',
  305. {
  306. 'set_tweak': 'sound',
  307. 'value': 'default'
  308. }, {
  309. 'set_tweak': 'highlight',
  310. 'value': False
  311. }
  312. ]
  313. },
  314. {
  315. 'rule_id': 'global/underride/.m.rule.message',
  316. 'conditions': [
  317. {
  318. 'kind': 'event_match',
  319. 'key': 'type',
  320. 'pattern': 'm.room.message',
  321. '_id': '_message',
  322. }
  323. ],
  324. 'actions': [
  325. 'notify', {
  326. 'set_tweak': 'highlight',
  327. 'value': False
  328. }
  329. ]
  330. },
  331. # XXX: this is going to fire for events which aren't m.room.messages
  332. # but are encrypted (e.g. m.call.*)...
  333. {
  334. 'rule_id': 'global/underride/.m.rule.encrypted',
  335. 'conditions': [
  336. {
  337. 'kind': 'event_match',
  338. 'key': 'type',
  339. 'pattern': 'm.room.encrypted',
  340. '_id': '_encrypted',
  341. }
  342. ],
  343. 'actions': [
  344. 'notify', {
  345. 'set_tweak': 'highlight',
  346. 'value': False
  347. }
  348. ]
  349. }
  350. ]
  351. BASE_RULE_IDS = set()
  352. for r in BASE_APPEND_CONTENT_RULES:
  353. r['priority_class'] = PRIORITY_CLASS_MAP['content']
  354. r['default'] = True
  355. BASE_RULE_IDS.add(r['rule_id'])
  356. for r in BASE_PREPEND_OVERRIDE_RULES:
  357. r['priority_class'] = PRIORITY_CLASS_MAP['override']
  358. r['default'] = True
  359. BASE_RULE_IDS.add(r['rule_id'])
  360. for r in BASE_APPEND_OVERRIDE_RULES:
  361. r['priority_class'] = PRIORITY_CLASS_MAP['override']
  362. r['default'] = True
  363. BASE_RULE_IDS.add(r['rule_id'])
  364. for r in BASE_APPEND_UNDERRIDE_RULES:
  365. r['priority_class'] = PRIORITY_CLASS_MAP['underride']
  366. r['default'] = True
  367. BASE_RULE_IDS.add(r['rule_id'])