receipts.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 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 synapse.util.caches.descriptors import cachedInlineCallbacks, cachedList, cached
  17. from synapse.util.caches.stream_change_cache import StreamChangeCache
  18. from twisted.internet import defer
  19. import logging
  20. import ujson as json
  21. logger = logging.getLogger(__name__)
  22. class ReceiptsStore(SQLBaseStore):
  23. def __init__(self, hs):
  24. super(ReceiptsStore, self).__init__(hs)
  25. self._receipts_stream_cache = StreamChangeCache(
  26. "ReceiptsRoomChangeCache", self._receipts_id_gen.get_current_token()
  27. )
  28. @cachedInlineCallbacks()
  29. def get_users_with_read_receipts_in_room(self, room_id):
  30. receipts = yield self.get_receipts_for_room(room_id, "m.read")
  31. defer.returnValue(set(r['user_id'] for r in receipts))
  32. def _invalidate_get_users_with_receipts_in_room(self, room_id, receipt_type,
  33. user_id):
  34. if receipt_type != "m.read":
  35. return
  36. # Returns an ObservableDeferred
  37. res = self.get_users_with_read_receipts_in_room.cache.get((room_id,), None)
  38. if res and res.called and user_id in res.result:
  39. # We'd only be adding to the set, so no point invalidating if the
  40. # user is already there
  41. return
  42. self.get_users_with_read_receipts_in_room.invalidate((room_id,))
  43. @cached(num_args=2)
  44. def get_receipts_for_room(self, room_id, receipt_type):
  45. return self._simple_select_list(
  46. table="receipts_linearized",
  47. keyvalues={
  48. "room_id": room_id,
  49. "receipt_type": receipt_type,
  50. },
  51. retcols=("user_id", "event_id"),
  52. desc="get_receipts_for_room",
  53. )
  54. @cached(num_args=3)
  55. def get_last_receipt_event_id_for_user(self, user_id, room_id, receipt_type):
  56. return self._simple_select_one_onecol(
  57. table="receipts_linearized",
  58. keyvalues={
  59. "room_id": room_id,
  60. "receipt_type": receipt_type,
  61. "user_id": user_id
  62. },
  63. retcol="event_id",
  64. desc="get_own_receipt_for_user",
  65. allow_none=True,
  66. )
  67. @cachedInlineCallbacks(num_args=2)
  68. def get_receipts_for_user(self, user_id, receipt_type):
  69. rows = yield self._simple_select_list(
  70. table="receipts_linearized",
  71. keyvalues={
  72. "user_id": user_id,
  73. "receipt_type": receipt_type,
  74. },
  75. retcols=("room_id", "event_id"),
  76. desc="get_receipts_for_user",
  77. )
  78. defer.returnValue({row["room_id"]: row["event_id"] for row in rows})
  79. @defer.inlineCallbacks
  80. def get_receipts_for_user_with_orderings(self, user_id, receipt_type):
  81. def f(txn):
  82. sql = (
  83. "SELECT rl.room_id, rl.event_id,"
  84. " e.topological_ordering, e.stream_ordering"
  85. " FROM receipts_linearized AS rl"
  86. " INNER JOIN events AS e USING (room_id, event_id)"
  87. " WHERE rl.room_id = e.room_id"
  88. " AND rl.event_id = e.event_id"
  89. " AND user_id = ?"
  90. )
  91. txn.execute(sql, (user_id,))
  92. return txn.fetchall()
  93. rows = yield self.runInteraction(
  94. "get_receipts_for_user_with_orderings", f
  95. )
  96. defer.returnValue({
  97. row[0]: {
  98. "event_id": row[1],
  99. "topological_ordering": row[2],
  100. "stream_ordering": row[3],
  101. } for row in rows
  102. })
  103. @defer.inlineCallbacks
  104. def get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None):
  105. """Get receipts for multiple rooms for sending to clients.
  106. Args:
  107. room_ids (list): List of room_ids.
  108. to_key (int): Max stream id to fetch receipts upto.
  109. from_key (int): Min stream id to fetch receipts from. None fetches
  110. from the start.
  111. Returns:
  112. list: A list of receipts.
  113. """
  114. room_ids = set(room_ids)
  115. if from_key:
  116. room_ids = yield self._receipts_stream_cache.get_entities_changed(
  117. room_ids, from_key
  118. )
  119. results = yield self._get_linearized_receipts_for_rooms(
  120. room_ids, to_key, from_key=from_key
  121. )
  122. defer.returnValue([ev for res in results.values() for ev in res])
  123. @cachedInlineCallbacks(num_args=3, tree=True)
  124. def get_linearized_receipts_for_room(self, room_id, to_key, from_key=None):
  125. """Get receipts for a single room for sending to clients.
  126. Args:
  127. room_ids (str): The room id.
  128. to_key (int): Max stream id to fetch receipts upto.
  129. from_key (int): Min stream id to fetch receipts from. None fetches
  130. from the start.
  131. Returns:
  132. list: A list of receipts.
  133. """
  134. def f(txn):
  135. if from_key:
  136. sql = (
  137. "SELECT * FROM receipts_linearized WHERE"
  138. " room_id = ? AND stream_id > ? AND stream_id <= ?"
  139. )
  140. txn.execute(
  141. sql,
  142. (room_id, from_key, to_key)
  143. )
  144. else:
  145. sql = (
  146. "SELECT * FROM receipts_linearized WHERE"
  147. " room_id = ? AND stream_id <= ?"
  148. )
  149. txn.execute(
  150. sql,
  151. (room_id, to_key)
  152. )
  153. rows = self.cursor_to_dict(txn)
  154. return rows
  155. rows = yield self.runInteraction(
  156. "get_linearized_receipts_for_room", f
  157. )
  158. if not rows:
  159. defer.returnValue([])
  160. content = {}
  161. for row in rows:
  162. content.setdefault(
  163. row["event_id"], {}
  164. ).setdefault(
  165. row["receipt_type"], {}
  166. )[row["user_id"]] = json.loads(row["data"])
  167. defer.returnValue([{
  168. "type": "m.receipt",
  169. "room_id": room_id,
  170. "content": content,
  171. }])
  172. @cachedList(cached_method_name="get_linearized_receipts_for_room",
  173. list_name="room_ids", num_args=3, inlineCallbacks=True)
  174. def _get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None):
  175. if not room_ids:
  176. defer.returnValue({})
  177. def f(txn):
  178. if from_key:
  179. sql = (
  180. "SELECT * FROM receipts_linearized WHERE"
  181. " room_id IN (%s) AND stream_id > ? AND stream_id <= ?"
  182. ) % (
  183. ",".join(["?"] * len(room_ids))
  184. )
  185. args = list(room_ids)
  186. args.extend([from_key, to_key])
  187. txn.execute(sql, args)
  188. else:
  189. sql = (
  190. "SELECT * FROM receipts_linearized WHERE"
  191. " room_id IN (%s) AND stream_id <= ?"
  192. ) % (
  193. ",".join(["?"] * len(room_ids))
  194. )
  195. args = list(room_ids)
  196. args.append(to_key)
  197. txn.execute(sql, args)
  198. return self.cursor_to_dict(txn)
  199. txn_results = yield self.runInteraction(
  200. "_get_linearized_receipts_for_rooms", f
  201. )
  202. results = {}
  203. for row in txn_results:
  204. # We want a single event per room, since we want to batch the
  205. # receipts by room, event and type.
  206. room_event = results.setdefault(row["room_id"], {
  207. "type": "m.receipt",
  208. "room_id": row["room_id"],
  209. "content": {},
  210. })
  211. # The content is of the form:
  212. # {"$foo:bar": { "read": { "@user:host": <receipt> }, .. }, .. }
  213. event_entry = room_event["content"].setdefault(row["event_id"], {})
  214. receipt_type = event_entry.setdefault(row["receipt_type"], {})
  215. receipt_type[row["user_id"]] = json.loads(row["data"])
  216. results = {
  217. room_id: [results[room_id]] if room_id in results else []
  218. for room_id in room_ids
  219. }
  220. defer.returnValue(results)
  221. def get_max_receipt_stream_id(self):
  222. return self._receipts_id_gen.get_current_token()
  223. def insert_linearized_receipt_txn(self, txn, room_id, receipt_type,
  224. user_id, event_id, data, stream_id):
  225. txn.call_after(
  226. self.get_receipts_for_room.invalidate, (room_id, receipt_type)
  227. )
  228. txn.call_after(
  229. self._invalidate_get_users_with_receipts_in_room,
  230. room_id, receipt_type, user_id,
  231. )
  232. txn.call_after(
  233. self.get_receipts_for_user.invalidate, (user_id, receipt_type)
  234. )
  235. # FIXME: This shouldn't invalidate the whole cache
  236. txn.call_after(self.get_linearized_receipts_for_room.invalidate_many, (room_id,))
  237. txn.call_after(
  238. self._receipts_stream_cache.entity_has_changed,
  239. room_id, stream_id
  240. )
  241. txn.call_after(
  242. self.get_last_receipt_event_id_for_user.invalidate,
  243. (user_id, room_id, receipt_type)
  244. )
  245. res = self._simple_select_one_txn(
  246. txn,
  247. table="events",
  248. retcols=["topological_ordering", "stream_ordering"],
  249. keyvalues={"event_id": event_id},
  250. allow_none=True
  251. )
  252. topological_ordering = int(res["topological_ordering"]) if res else None
  253. stream_ordering = int(res["stream_ordering"]) if res else None
  254. # We don't want to clobber receipts for more recent events, so we
  255. # have to compare orderings of existing receipts
  256. sql = (
  257. "SELECT topological_ordering, stream_ordering, event_id FROM events"
  258. " INNER JOIN receipts_linearized as r USING (event_id, room_id)"
  259. " WHERE r.room_id = ? AND r.receipt_type = ? AND r.user_id = ?"
  260. )
  261. txn.execute(sql, (room_id, receipt_type, user_id))
  262. results = txn.fetchall()
  263. if results and topological_ordering:
  264. for to, so, _ in results:
  265. if int(to) > topological_ordering:
  266. return False
  267. elif int(to) == topological_ordering and int(so) >= stream_ordering:
  268. return False
  269. self._simple_delete_txn(
  270. txn,
  271. table="receipts_linearized",
  272. keyvalues={
  273. "room_id": room_id,
  274. "receipt_type": receipt_type,
  275. "user_id": user_id,
  276. }
  277. )
  278. self._simple_insert_txn(
  279. txn,
  280. table="receipts_linearized",
  281. values={
  282. "stream_id": stream_id,
  283. "room_id": room_id,
  284. "receipt_type": receipt_type,
  285. "user_id": user_id,
  286. "event_id": event_id,
  287. "data": json.dumps(data),
  288. }
  289. )
  290. if receipt_type == "m.read" and topological_ordering:
  291. self._remove_old_push_actions_before_txn(
  292. txn,
  293. room_id=room_id,
  294. user_id=user_id,
  295. topological_ordering=topological_ordering,
  296. )
  297. return True
  298. @defer.inlineCallbacks
  299. def insert_receipt(self, room_id, receipt_type, user_id, event_ids, data):
  300. """Insert a receipt, either from local client or remote server.
  301. Automatically does conversion between linearized and graph
  302. representations.
  303. """
  304. if not event_ids:
  305. return
  306. if len(event_ids) == 1:
  307. linearized_event_id = event_ids[0]
  308. else:
  309. # we need to points in graph -> linearized form.
  310. # TODO: Make this better.
  311. def graph_to_linear(txn):
  312. query = (
  313. "SELECT event_id WHERE room_id = ? AND stream_ordering IN ("
  314. " SELECT max(stream_ordering) WHERE event_id IN (%s)"
  315. ")"
  316. ) % (",".join(["?"] * len(event_ids)))
  317. txn.execute(query, [room_id] + event_ids)
  318. rows = txn.fetchall()
  319. if rows:
  320. return rows[0][0]
  321. else:
  322. raise RuntimeError("Unrecognized event_ids: %r" % (event_ids,))
  323. linearized_event_id = yield self.runInteraction(
  324. "insert_receipt_conv", graph_to_linear
  325. )
  326. stream_id_manager = self._receipts_id_gen.get_next()
  327. with stream_id_manager as stream_id:
  328. have_persisted = yield self.runInteraction(
  329. "insert_linearized_receipt",
  330. self.insert_linearized_receipt_txn,
  331. room_id, receipt_type, user_id, linearized_event_id,
  332. data,
  333. stream_id=stream_id,
  334. )
  335. if not have_persisted:
  336. defer.returnValue(None)
  337. yield self.insert_graph_receipt(
  338. room_id, receipt_type, user_id, event_ids, data
  339. )
  340. max_persisted_id = self._receipts_id_gen.get_current_token()
  341. defer.returnValue((stream_id, max_persisted_id))
  342. def insert_graph_receipt(self, room_id, receipt_type, user_id, event_ids,
  343. data):
  344. return self.runInteraction(
  345. "insert_graph_receipt",
  346. self.insert_graph_receipt_txn,
  347. room_id, receipt_type, user_id, event_ids, data
  348. )
  349. def insert_graph_receipt_txn(self, txn, room_id, receipt_type,
  350. user_id, event_ids, data):
  351. txn.call_after(
  352. self.get_receipts_for_room.invalidate, (room_id, receipt_type)
  353. )
  354. txn.call_after(
  355. self._invalidate_get_users_with_receipts_in_room,
  356. room_id, receipt_type, user_id,
  357. )
  358. txn.call_after(
  359. self.get_receipts_for_user.invalidate, (user_id, receipt_type)
  360. )
  361. # FIXME: This shouldn't invalidate the whole cache
  362. txn.call_after(self.get_linearized_receipts_for_room.invalidate_many, (room_id,))
  363. self._simple_delete_txn(
  364. txn,
  365. table="receipts_graph",
  366. keyvalues={
  367. "room_id": room_id,
  368. "receipt_type": receipt_type,
  369. "user_id": user_id,
  370. }
  371. )
  372. self._simple_insert_txn(
  373. txn,
  374. table="receipts_graph",
  375. values={
  376. "room_id": room_id,
  377. "receipt_type": receipt_type,
  378. "user_id": user_id,
  379. "event_ids": json.dumps(event_ids),
  380. "data": json.dumps(data),
  381. }
  382. )
  383. def get_all_updated_receipts(self, last_id, current_id, limit=None):
  384. if last_id == current_id:
  385. return defer.succeed([])
  386. def get_all_updated_receipts_txn(txn):
  387. sql = (
  388. "SELECT stream_id, room_id, receipt_type, user_id, event_id, data"
  389. " FROM receipts_linearized"
  390. " WHERE ? < stream_id AND stream_id <= ?"
  391. " ORDER BY stream_id ASC"
  392. )
  393. args = [last_id, current_id]
  394. if limit is not None:
  395. sql += " LIMIT ?"
  396. args.append(limit)
  397. txn.execute(sql, args)
  398. return txn.fetchall()
  399. return self.runInteraction(
  400. "get_all_updated_receipts", get_all_updated_receipts_txn
  401. )