event_push_actions.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 OpenMarket 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 ._base import SQLBaseStore
  16. from twisted.internet import defer
  17. from synapse.util.caches.descriptors import cachedInlineCallbacks
  18. import logging
  19. import ujson as json
  20. logger = logging.getLogger(__name__)
  21. class EventPushActionsStore(SQLBaseStore):
  22. def _set_push_actions_for_event_and_users_txn(self, txn, event, tuples):
  23. """
  24. Args:
  25. event: the event set actions for
  26. tuples: list of tuples of (user_id, actions)
  27. """
  28. values = []
  29. for uid, actions in tuples:
  30. values.append({
  31. 'room_id': event.room_id,
  32. 'event_id': event.event_id,
  33. 'user_id': uid,
  34. 'actions': json.dumps(actions),
  35. 'stream_ordering': event.internal_metadata.stream_ordering,
  36. 'topological_ordering': event.depth,
  37. 'notif': 1,
  38. 'highlight': 1 if _action_has_highlight(actions) else 0,
  39. })
  40. for uid, __ in tuples:
  41. txn.call_after(
  42. self.get_unread_event_push_actions_by_room_for_user.invalidate_many,
  43. (event.room_id, uid)
  44. )
  45. self._simple_insert_many_txn(txn, "event_push_actions", values)
  46. @cachedInlineCallbacks(num_args=3, lru=True, tree=True, max_entries=5000)
  47. def get_unread_event_push_actions_by_room_for_user(
  48. self, room_id, user_id, last_read_event_id
  49. ):
  50. def _get_unread_event_push_actions_by_room(txn):
  51. sql = (
  52. "SELECT stream_ordering, topological_ordering"
  53. " FROM events"
  54. " WHERE room_id = ? AND event_id = ?"
  55. )
  56. txn.execute(
  57. sql, (room_id, last_read_event_id)
  58. )
  59. results = txn.fetchall()
  60. if len(results) == 0:
  61. return {"notify_count": 0, "highlight_count": 0}
  62. stream_ordering = results[0][0]
  63. topological_ordering = results[0][1]
  64. sql = (
  65. "SELECT sum(notif), sum(highlight)"
  66. " FROM event_push_actions ea"
  67. " WHERE"
  68. " user_id = ?"
  69. " AND room_id = ?"
  70. " AND ("
  71. " topological_ordering > ?"
  72. " OR (topological_ordering = ? AND stream_ordering > ?)"
  73. ")"
  74. )
  75. txn.execute(sql, (
  76. user_id, room_id,
  77. topological_ordering, topological_ordering, stream_ordering
  78. ))
  79. row = txn.fetchone()
  80. if row:
  81. return {
  82. "notify_count": row[0] or 0,
  83. "highlight_count": row[1] or 0,
  84. }
  85. else:
  86. return {"notify_count": 0, "highlight_count": 0}
  87. ret = yield self.runInteraction(
  88. "get_unread_event_push_actions_by_room",
  89. _get_unread_event_push_actions_by_room
  90. )
  91. defer.returnValue(ret)
  92. def _remove_push_actions_for_event_id_txn(self, txn, room_id, event_id):
  93. # Sad that we have to blow away the cache for the whole room here
  94. txn.call_after(
  95. self.get_unread_event_push_actions_by_room_for_user.invalidate_many,
  96. (room_id,)
  97. )
  98. txn.execute(
  99. "DELETE FROM event_push_actions WHERE room_id = ? AND event_id = ?",
  100. (room_id, event_id)
  101. )
  102. def _action_has_highlight(actions):
  103. for action in actions:
  104. try:
  105. if action.get("set_tweak", None) == "highlight":
  106. return action.get("value", True)
  107. except AttributeError:
  108. pass
  109. return False