push_rule.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  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. import abc
  17. import logging
  18. from canonicaljson import json
  19. from twisted.internet import defer
  20. from synapse.push.baserules import list_with_base_rules
  21. from synapse.storage.appservice import ApplicationServiceWorkerStore
  22. from synapse.storage.pusher import PusherWorkerStore
  23. from synapse.storage.receipts import ReceiptsWorkerStore
  24. from synapse.storage.roommember import RoomMemberWorkerStore
  25. from synapse.util.caches.descriptors import cachedInlineCallbacks, cachedList
  26. from synapse.util.caches.stream_change_cache import StreamChangeCache
  27. from ._base import SQLBaseStore
  28. logger = logging.getLogger(__name__)
  29. def _load_rules(rawrules, enabled_map):
  30. ruleslist = []
  31. for rawrule in rawrules:
  32. rule = dict(rawrule)
  33. rule["conditions"] = json.loads(rawrule["conditions"])
  34. rule["actions"] = json.loads(rawrule["actions"])
  35. ruleslist.append(rule)
  36. # We're going to be mutating this a lot, so do a deep copy
  37. rules = list(list_with_base_rules(ruleslist))
  38. for i, rule in enumerate(rules):
  39. rule_id = rule["rule_id"]
  40. if rule_id in enabled_map:
  41. if rule.get("enabled", True) != bool(enabled_map[rule_id]):
  42. # Rules are cached across users.
  43. rule = dict(rule)
  44. rule["enabled"] = bool(enabled_map[rule_id])
  45. rules[i] = rule
  46. return rules
  47. class PushRulesWorkerStore(
  48. ApplicationServiceWorkerStore,
  49. ReceiptsWorkerStore,
  50. PusherWorkerStore,
  51. RoomMemberWorkerStore,
  52. SQLBaseStore,
  53. ):
  54. """This is an abstract base class where subclasses must implement
  55. `get_max_push_rules_stream_id` which can be called in the initializer.
  56. """
  57. # This ABCMeta metaclass ensures that we cannot be instantiated without
  58. # the abstract methods being implemented.
  59. __metaclass__ = abc.ABCMeta
  60. def __init__(self, db_conn, hs):
  61. super(PushRulesWorkerStore, self).__init__(db_conn, hs)
  62. push_rules_prefill, push_rules_id = self._get_cache_dict(
  63. db_conn,
  64. "push_rules_stream",
  65. entity_column="user_id",
  66. stream_column="stream_id",
  67. max_value=self.get_max_push_rules_stream_id(),
  68. )
  69. self.push_rules_stream_cache = StreamChangeCache(
  70. "PushRulesStreamChangeCache",
  71. push_rules_id,
  72. prefilled_cache=push_rules_prefill,
  73. )
  74. @abc.abstractmethod
  75. def get_max_push_rules_stream_id(self):
  76. """Get the position of the push rules stream.
  77. Returns:
  78. int
  79. """
  80. raise NotImplementedError()
  81. @cachedInlineCallbacks(max_entries=5000)
  82. def get_push_rules_for_user(self, user_id):
  83. rows = yield self._simple_select_list(
  84. table="push_rules",
  85. keyvalues={"user_name": user_id},
  86. retcols=(
  87. "user_name",
  88. "rule_id",
  89. "priority_class",
  90. "priority",
  91. "conditions",
  92. "actions",
  93. ),
  94. desc="get_push_rules_enabled_for_user",
  95. )
  96. rows.sort(key=lambda row: (-int(row["priority_class"]), -int(row["priority"])))
  97. enabled_map = yield self.get_push_rules_enabled_for_user(user_id)
  98. rules = _load_rules(rows, enabled_map)
  99. return rules
  100. @cachedInlineCallbacks(max_entries=5000)
  101. def get_push_rules_enabled_for_user(self, user_id):
  102. results = yield self._simple_select_list(
  103. table="push_rules_enable",
  104. keyvalues={"user_name": user_id},
  105. retcols=("user_name", "rule_id", "enabled"),
  106. desc="get_push_rules_enabled_for_user",
  107. )
  108. return {r["rule_id"]: False if r["enabled"] == 0 else True for r in results}
  109. def have_push_rules_changed_for_user(self, user_id, last_id):
  110. if not self.push_rules_stream_cache.has_entity_changed(user_id, last_id):
  111. return defer.succeed(False)
  112. else:
  113. def have_push_rules_changed_txn(txn):
  114. sql = (
  115. "SELECT COUNT(stream_id) FROM push_rules_stream"
  116. " WHERE user_id = ? AND ? < stream_id"
  117. )
  118. txn.execute(sql, (user_id, last_id))
  119. count, = txn.fetchone()
  120. return bool(count)
  121. return self.runInteraction(
  122. "have_push_rules_changed", have_push_rules_changed_txn
  123. )
  124. @cachedList(
  125. cached_method_name="get_push_rules_for_user",
  126. list_name="user_ids",
  127. num_args=1,
  128. inlineCallbacks=True,
  129. )
  130. def bulk_get_push_rules(self, user_ids):
  131. if not user_ids:
  132. return {}
  133. results = {user_id: [] for user_id in user_ids}
  134. rows = yield self._simple_select_many_batch(
  135. table="push_rules",
  136. column="user_name",
  137. iterable=user_ids,
  138. retcols=("*",),
  139. desc="bulk_get_push_rules",
  140. )
  141. rows.sort(key=lambda row: (-int(row["priority_class"]), -int(row["priority"])))
  142. for row in rows:
  143. results.setdefault(row["user_name"], []).append(row)
  144. enabled_map_by_user = yield self.bulk_get_push_rules_enabled(user_ids)
  145. for user_id, rules in results.items():
  146. results[user_id] = _load_rules(rules, enabled_map_by_user.get(user_id, {}))
  147. return results
  148. @defer.inlineCallbacks
  149. def move_push_rule_from_room_to_room(self, new_room_id, user_id, rule):
  150. """Move a single push rule from one room to another for a specific user.
  151. Args:
  152. new_room_id (str): ID of the new room.
  153. user_id (str): ID of user the push rule belongs to.
  154. rule (Dict): A push rule.
  155. """
  156. # Create new rule id
  157. rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1])
  158. new_rule_id = rule_id_scope + "/" + new_room_id
  159. # Change room id in each condition
  160. for condition in rule.get("conditions", []):
  161. if condition.get("key") == "room_id":
  162. condition["pattern"] = new_room_id
  163. # Add the rule for the new room
  164. yield self.add_push_rule(
  165. user_id=user_id,
  166. rule_id=new_rule_id,
  167. priority_class=rule["priority_class"],
  168. conditions=rule["conditions"],
  169. actions=rule["actions"],
  170. )
  171. # Delete push rule for the old room
  172. yield self.delete_push_rule(user_id, rule["rule_id"])
  173. @defer.inlineCallbacks
  174. def move_push_rules_from_room_to_room_for_user(
  175. self, old_room_id, new_room_id, user_id
  176. ):
  177. """Move all of the push rules from one room to another for a specific
  178. user.
  179. Args:
  180. old_room_id (str): ID of the old room.
  181. new_room_id (str): ID of the new room.
  182. user_id (str): ID of user to copy push rules for.
  183. """
  184. # Retrieve push rules for this user
  185. user_push_rules = yield self.get_push_rules_for_user(user_id)
  186. # Get rules relating to the old room, move them to the new room, then
  187. # delete them from the old room
  188. for rule in user_push_rules:
  189. conditions = rule.get("conditions", [])
  190. if any(
  191. (c.get("key") == "room_id" and c.get("pattern") == old_room_id)
  192. for c in conditions
  193. ):
  194. self.move_push_rule_from_room_to_room(new_room_id, user_id, rule)
  195. @defer.inlineCallbacks
  196. def bulk_get_push_rules_for_room(self, event, context):
  197. state_group = context.state_group
  198. if not state_group:
  199. # If state_group is None it means it has yet to be assigned a
  200. # state group, i.e. we need to make sure that calls with a state_group
  201. # of None don't hit previous cached calls with a None state_group.
  202. # To do this we set the state_group to a new object as object() != object()
  203. state_group = object()
  204. current_state_ids = yield context.get_current_state_ids(self)
  205. result = yield self._bulk_get_push_rules_for_room(
  206. event.room_id, state_group, current_state_ids, event=event
  207. )
  208. return result
  209. @cachedInlineCallbacks(num_args=2, cache_context=True)
  210. def _bulk_get_push_rules_for_room(
  211. self, room_id, state_group, current_state_ids, cache_context, event=None
  212. ):
  213. # We don't use `state_group`, its there so that we can cache based
  214. # on it. However, its important that its never None, since two current_state's
  215. # with a state_group of None are likely to be different.
  216. # See bulk_get_push_rules_for_room for how we work around this.
  217. assert state_group is not None
  218. # We also will want to generate notifs for other people in the room so
  219. # their unread countss are correct in the event stream, but to avoid
  220. # generating them for bot / AS users etc, we only do so for people who've
  221. # sent a read receipt into the room.
  222. users_in_room = yield self._get_joined_users_from_context(
  223. room_id,
  224. state_group,
  225. current_state_ids,
  226. on_invalidate=cache_context.invalidate,
  227. event=event,
  228. )
  229. # We ignore app service users for now. This is so that we don't fill
  230. # up the `get_if_users_have_pushers` cache with AS entries that we
  231. # know don't have pushers, nor even read receipts.
  232. local_users_in_room = set(
  233. u
  234. for u in users_in_room
  235. if self.hs.is_mine_id(u)
  236. and not self.get_if_app_services_interested_in_user(u)
  237. )
  238. # users in the room who have pushers need to get push rules run because
  239. # that's how their pushers work
  240. if_users_with_pushers = yield self.get_if_users_have_pushers(
  241. local_users_in_room, on_invalidate=cache_context.invalidate
  242. )
  243. user_ids = set(
  244. uid for uid, have_pusher in if_users_with_pushers.items() if have_pusher
  245. )
  246. users_with_receipts = yield self.get_users_with_read_receipts_in_room(
  247. room_id, on_invalidate=cache_context.invalidate
  248. )
  249. # any users with pushers must be ours: they have pushers
  250. for uid in users_with_receipts:
  251. if uid in local_users_in_room:
  252. user_ids.add(uid)
  253. rules_by_user = yield self.bulk_get_push_rules(
  254. user_ids, on_invalidate=cache_context.invalidate
  255. )
  256. rules_by_user = {k: v for k, v in rules_by_user.items() if v is not None}
  257. return rules_by_user
  258. @cachedList(
  259. cached_method_name="get_push_rules_enabled_for_user",
  260. list_name="user_ids",
  261. num_args=1,
  262. inlineCallbacks=True,
  263. )
  264. def bulk_get_push_rules_enabled(self, user_ids):
  265. if not user_ids:
  266. return {}
  267. results = {user_id: {} for user_id in user_ids}
  268. rows = yield self._simple_select_many_batch(
  269. table="push_rules_enable",
  270. column="user_name",
  271. iterable=user_ids,
  272. retcols=("user_name", "rule_id", "enabled"),
  273. desc="bulk_get_push_rules_enabled",
  274. )
  275. for row in rows:
  276. enabled = bool(row["enabled"])
  277. results.setdefault(row["user_name"], {})[row["rule_id"]] = enabled
  278. return results
  279. class PushRuleStore(PushRulesWorkerStore):
  280. @defer.inlineCallbacks
  281. def add_push_rule(
  282. self,
  283. user_id,
  284. rule_id,
  285. priority_class,
  286. conditions,
  287. actions,
  288. before=None,
  289. after=None,
  290. ):
  291. conditions_json = json.dumps(conditions)
  292. actions_json = json.dumps(actions)
  293. with self._push_rules_stream_id_gen.get_next() as ids:
  294. stream_id, event_stream_ordering = ids
  295. if before or after:
  296. yield self.runInteraction(
  297. "_add_push_rule_relative_txn",
  298. self._add_push_rule_relative_txn,
  299. stream_id,
  300. event_stream_ordering,
  301. user_id,
  302. rule_id,
  303. priority_class,
  304. conditions_json,
  305. actions_json,
  306. before,
  307. after,
  308. )
  309. else:
  310. yield self.runInteraction(
  311. "_add_push_rule_highest_priority_txn",
  312. self._add_push_rule_highest_priority_txn,
  313. stream_id,
  314. event_stream_ordering,
  315. user_id,
  316. rule_id,
  317. priority_class,
  318. conditions_json,
  319. actions_json,
  320. )
  321. def _add_push_rule_relative_txn(
  322. self,
  323. txn,
  324. stream_id,
  325. event_stream_ordering,
  326. user_id,
  327. rule_id,
  328. priority_class,
  329. conditions_json,
  330. actions_json,
  331. before,
  332. after,
  333. ):
  334. # Lock the table since otherwise we'll have annoying races between the
  335. # SELECT here and the UPSERT below.
  336. self.database_engine.lock_table(txn, "push_rules")
  337. relative_to_rule = before or after
  338. res = self._simple_select_one_txn(
  339. txn,
  340. table="push_rules",
  341. keyvalues={"user_name": user_id, "rule_id": relative_to_rule},
  342. retcols=["priority_class", "priority"],
  343. allow_none=True,
  344. )
  345. if not res:
  346. raise RuleNotFoundException(
  347. "before/after rule not found: %s" % (relative_to_rule,)
  348. )
  349. base_priority_class = res["priority_class"]
  350. base_rule_priority = res["priority"]
  351. if base_priority_class != priority_class:
  352. raise InconsistentRuleException(
  353. "Given priority class does not match class of relative rule"
  354. )
  355. if before:
  356. # Higher priority rules are executed first, So adding a rule before
  357. # a rule means giving it a higher priority than that rule.
  358. new_rule_priority = base_rule_priority + 1
  359. else:
  360. # We increment the priority of the existing rules to make space for
  361. # the new rule. Therefore if we want this rule to appear after
  362. # an existing rule we give it the priority of the existing rule,
  363. # and then increment the priority of the existing rule.
  364. new_rule_priority = base_rule_priority
  365. sql = (
  366. "UPDATE push_rules SET priority = priority + 1"
  367. " WHERE user_name = ? AND priority_class = ? AND priority >= ?"
  368. )
  369. txn.execute(sql, (user_id, priority_class, new_rule_priority))
  370. self._upsert_push_rule_txn(
  371. txn,
  372. stream_id,
  373. event_stream_ordering,
  374. user_id,
  375. rule_id,
  376. priority_class,
  377. new_rule_priority,
  378. conditions_json,
  379. actions_json,
  380. )
  381. def _add_push_rule_highest_priority_txn(
  382. self,
  383. txn,
  384. stream_id,
  385. event_stream_ordering,
  386. user_id,
  387. rule_id,
  388. priority_class,
  389. conditions_json,
  390. actions_json,
  391. ):
  392. # Lock the table since otherwise we'll have annoying races between the
  393. # SELECT here and the UPSERT below.
  394. self.database_engine.lock_table(txn, "push_rules")
  395. # find the highest priority rule in that class
  396. sql = (
  397. "SELECT COUNT(*), MAX(priority) FROM push_rules"
  398. " WHERE user_name = ? and priority_class = ?"
  399. )
  400. txn.execute(sql, (user_id, priority_class))
  401. res = txn.fetchall()
  402. (how_many, highest_prio) = res[0]
  403. new_prio = 0
  404. if how_many > 0:
  405. new_prio = highest_prio + 1
  406. self._upsert_push_rule_txn(
  407. txn,
  408. stream_id,
  409. event_stream_ordering,
  410. user_id,
  411. rule_id,
  412. priority_class,
  413. new_prio,
  414. conditions_json,
  415. actions_json,
  416. )
  417. def _upsert_push_rule_txn(
  418. self,
  419. txn,
  420. stream_id,
  421. event_stream_ordering,
  422. user_id,
  423. rule_id,
  424. priority_class,
  425. priority,
  426. conditions_json,
  427. actions_json,
  428. update_stream=True,
  429. ):
  430. """Specialised version of _simple_upsert_txn that picks a push_rule_id
  431. using the _push_rule_id_gen if it needs to insert the rule. It assumes
  432. that the "push_rules" table is locked"""
  433. sql = (
  434. "UPDATE push_rules"
  435. " SET priority_class = ?, priority = ?, conditions = ?, actions = ?"
  436. " WHERE user_name = ? AND rule_id = ?"
  437. )
  438. txn.execute(
  439. sql,
  440. (priority_class, priority, conditions_json, actions_json, user_id, rule_id),
  441. )
  442. if txn.rowcount == 0:
  443. # We didn't update a row with the given rule_id so insert one
  444. push_rule_id = self._push_rule_id_gen.get_next()
  445. self._simple_insert_txn(
  446. txn,
  447. table="push_rules",
  448. values={
  449. "id": push_rule_id,
  450. "user_name": user_id,
  451. "rule_id": rule_id,
  452. "priority_class": priority_class,
  453. "priority": priority,
  454. "conditions": conditions_json,
  455. "actions": actions_json,
  456. },
  457. )
  458. if update_stream:
  459. self._insert_push_rules_update_txn(
  460. txn,
  461. stream_id,
  462. event_stream_ordering,
  463. user_id,
  464. rule_id,
  465. op="ADD",
  466. data={
  467. "priority_class": priority_class,
  468. "priority": priority,
  469. "conditions": conditions_json,
  470. "actions": actions_json,
  471. },
  472. )
  473. @defer.inlineCallbacks
  474. def delete_push_rule(self, user_id, rule_id):
  475. """
  476. Delete a push rule. Args specify the row to be deleted and can be
  477. any of the columns in the push_rule table, but below are the
  478. standard ones
  479. Args:
  480. user_id (str): The matrix ID of the push rule owner
  481. rule_id (str): The rule_id of the rule to be deleted
  482. """
  483. def delete_push_rule_txn(txn, stream_id, event_stream_ordering):
  484. self._simple_delete_one_txn(
  485. txn, "push_rules", {"user_name": user_id, "rule_id": rule_id}
  486. )
  487. self._insert_push_rules_update_txn(
  488. txn, stream_id, event_stream_ordering, user_id, rule_id, op="DELETE"
  489. )
  490. with self._push_rules_stream_id_gen.get_next() as ids:
  491. stream_id, event_stream_ordering = ids
  492. yield self.runInteraction(
  493. "delete_push_rule",
  494. delete_push_rule_txn,
  495. stream_id,
  496. event_stream_ordering,
  497. )
  498. @defer.inlineCallbacks
  499. def set_push_rule_enabled(self, user_id, rule_id, enabled):
  500. with self._push_rules_stream_id_gen.get_next() as ids:
  501. stream_id, event_stream_ordering = ids
  502. yield self.runInteraction(
  503. "_set_push_rule_enabled_txn",
  504. self._set_push_rule_enabled_txn,
  505. stream_id,
  506. event_stream_ordering,
  507. user_id,
  508. rule_id,
  509. enabled,
  510. )
  511. def _set_push_rule_enabled_txn(
  512. self, txn, stream_id, event_stream_ordering, user_id, rule_id, enabled
  513. ):
  514. new_id = self._push_rules_enable_id_gen.get_next()
  515. self._simple_upsert_txn(
  516. txn,
  517. "push_rules_enable",
  518. {"user_name": user_id, "rule_id": rule_id},
  519. {"enabled": 1 if enabled else 0},
  520. {"id": new_id},
  521. )
  522. self._insert_push_rules_update_txn(
  523. txn,
  524. stream_id,
  525. event_stream_ordering,
  526. user_id,
  527. rule_id,
  528. op="ENABLE" if enabled else "DISABLE",
  529. )
  530. @defer.inlineCallbacks
  531. def set_push_rule_actions(self, user_id, rule_id, actions, is_default_rule):
  532. actions_json = json.dumps(actions)
  533. def set_push_rule_actions_txn(txn, stream_id, event_stream_ordering):
  534. if is_default_rule:
  535. # Add a dummy rule to the rules table with the user specified
  536. # actions.
  537. priority_class = -1
  538. priority = 1
  539. self._upsert_push_rule_txn(
  540. txn,
  541. stream_id,
  542. event_stream_ordering,
  543. user_id,
  544. rule_id,
  545. priority_class,
  546. priority,
  547. "[]",
  548. actions_json,
  549. update_stream=False,
  550. )
  551. else:
  552. self._simple_update_one_txn(
  553. txn,
  554. "push_rules",
  555. {"user_name": user_id, "rule_id": rule_id},
  556. {"actions": actions_json},
  557. )
  558. self._insert_push_rules_update_txn(
  559. txn,
  560. stream_id,
  561. event_stream_ordering,
  562. user_id,
  563. rule_id,
  564. op="ACTIONS",
  565. data={"actions": actions_json},
  566. )
  567. with self._push_rules_stream_id_gen.get_next() as ids:
  568. stream_id, event_stream_ordering = ids
  569. yield self.runInteraction(
  570. "set_push_rule_actions",
  571. set_push_rule_actions_txn,
  572. stream_id,
  573. event_stream_ordering,
  574. )
  575. def _insert_push_rules_update_txn(
  576. self, txn, stream_id, event_stream_ordering, user_id, rule_id, op, data=None
  577. ):
  578. values = {
  579. "stream_id": stream_id,
  580. "event_stream_ordering": event_stream_ordering,
  581. "user_id": user_id,
  582. "rule_id": rule_id,
  583. "op": op,
  584. }
  585. if data is not None:
  586. values.update(data)
  587. self._simple_insert_txn(txn, "push_rules_stream", values=values)
  588. txn.call_after(self.get_push_rules_for_user.invalidate, (user_id,))
  589. txn.call_after(self.get_push_rules_enabled_for_user.invalidate, (user_id,))
  590. txn.call_after(
  591. self.push_rules_stream_cache.entity_has_changed, user_id, stream_id
  592. )
  593. def get_all_push_rule_updates(self, last_id, current_id, limit):
  594. """Get all the push rules changes that have happend on the server"""
  595. if last_id == current_id:
  596. return defer.succeed([])
  597. def get_all_push_rule_updates_txn(txn):
  598. sql = (
  599. "SELECT stream_id, event_stream_ordering, user_id, rule_id,"
  600. " op, priority_class, priority, conditions, actions"
  601. " FROM push_rules_stream"
  602. " WHERE ? < stream_id AND stream_id <= ?"
  603. " ORDER BY stream_id ASC LIMIT ?"
  604. )
  605. txn.execute(sql, (last_id, current_id, limit))
  606. return txn.fetchall()
  607. return self.runInteraction(
  608. "get_all_push_rule_updates", get_all_push_rule_updates_txn
  609. )
  610. def get_push_rules_stream_token(self):
  611. """Get the position of the push rules stream.
  612. Returns a pair of a stream id for the push_rules stream and the
  613. room stream ordering it corresponds to."""
  614. return self._push_rules_stream_id_gen.get_current_token()
  615. def get_max_push_rules_stream_id(self):
  616. return self.get_push_rules_stream_token()[0]
  617. class RuleNotFoundException(Exception):
  618. pass
  619. class InconsistentRuleException(Exception):
  620. pass