2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2018-02-16 05:17:14 -07:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2015-07-01 04:41:55 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-06-01 07:01:43 -06:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
2020-08-27 05:08:38 -06:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from twisted.internet import defer
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
|
|
|
|
from synapse.replication.tcp.streams import ReceiptsStream
|
2020-07-16 09:32:19 -06:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.database import DatabasePool
|
2021-01-18 08:47:59 -07:00
|
|
|
from synapse.storage.engines import PostgresEngine
|
|
|
|
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
|
2020-10-15 10:33:28 -06:00
|
|
|
from synapse.types import JsonDict
|
2020-08-07 06:02:55 -06:00
|
|
|
from synapse.util import json_encoder
|
2020-08-14 08:05:19 -06:00
|
|
|
from synapse.util.caches.descriptors import cached, cachedList
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
class ReceiptsWorkerStore(SQLBaseStore):
|
2020-08-05 14:38:57 -06:00
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
2021-01-18 08:47:59 -07:00
|
|
|
self._instance_name = hs.get_instance_name()
|
|
|
|
|
|
|
|
if isinstance(database.engine, PostgresEngine):
|
|
|
|
self._can_write_to_receipts = (
|
|
|
|
self._instance_name in hs.config.worker.writers.receipts
|
|
|
|
)
|
|
|
|
|
|
|
|
self._receipts_id_gen = MultiWriterIdGenerator(
|
|
|
|
db_conn=db_conn,
|
|
|
|
db=database,
|
2021-01-21 08:09:09 -07:00
|
|
|
stream_name="receipts",
|
2021-01-18 08:47:59 -07:00
|
|
|
instance_name=self._instance_name,
|
|
|
|
tables=[("receipts_linearized", "instance_name", "stream_id")],
|
|
|
|
sequence_name="receipts_sequence",
|
|
|
|
writers=hs.config.worker.writers.receipts,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._can_write_to_receipts = True
|
|
|
|
|
|
|
|
# We shouldn't be running in worker mode with SQLite, but its useful
|
|
|
|
# to support it for unit tests.
|
|
|
|
#
|
|
|
|
# If this process is the writer than we need to use
|
|
|
|
# `StreamIdGenerator`, otherwise we use `SlavedIdTracker` which gets
|
|
|
|
# updated over replication. (Multiple writers are not supported for
|
|
|
|
# SQLite).
|
2021-01-21 08:09:09 -07:00
|
|
|
if hs.get_instance_name() in hs.config.worker.writers.receipts:
|
2021-01-18 08:47:59 -07:00
|
|
|
self._receipts_id_gen = StreamIdGenerator(
|
|
|
|
db_conn, "receipts_linearized", "stream_id"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._receipts_id_gen = SlavedIdTracker(
|
|
|
|
db_conn, "receipts_linearized", "stream_id"
|
|
|
|
)
|
|
|
|
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(database, db_conn, hs)
|
2015-07-08 08:35:00 -06:00
|
|
|
|
2016-01-28 09:37:41 -07:00
|
|
|
self._receipts_stream_cache = StreamChangeCache(
|
2018-02-20 10:33:18 -07:00
|
|
|
"ReceiptsRoomChangeCache", self.get_max_receipt_stream_id()
|
2016-01-26 08:51:06 -07:00
|
|
|
)
|
2015-07-08 08:35:00 -06:00
|
|
|
|
2018-02-20 10:33:18 -07:00
|
|
|
def get_max_receipt_stream_id(self):
|
|
|
|
"""Get the current max stream ID for receipts stream
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
return self._receipts_id_gen.get_current_token()
|
2018-02-20 10:33:18 -07:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
@cached()
|
|
|
|
async def get_users_with_read_receipts_in_room(self, room_id):
|
|
|
|
receipts = await self.get_receipts_for_room(room_id, "m.read")
|
2020-02-21 05:15:07 -07:00
|
|
|
return {r["user_id"] for r in receipts}
|
2016-06-01 03:31:09 -06:00
|
|
|
|
2016-01-19 09:01:05 -07:00
|
|
|
@cached(num_args=2)
|
2020-08-27 05:08:38 -06:00
|
|
|
async def get_receipts_for_room(
|
|
|
|
self, room_id: str, receipt_type: str
|
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
return await self.db_pool.simple_select_list(
|
2016-01-19 09:01:05 -07:00
|
|
|
table="receipts_linearized",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"room_id": room_id, "receipt_type": receipt_type},
|
2016-01-19 09:01:05 -07:00
|
|
|
retcols=("user_id", "event_id"),
|
|
|
|
desc="get_receipts_for_room",
|
|
|
|
)
|
|
|
|
|
2016-02-09 06:55:59 -07:00
|
|
|
@cached(num_args=3)
|
2020-08-26 05:19:32 -06:00
|
|
|
async def get_last_receipt_event_id_for_user(
|
|
|
|
self, user_id: str, room_id: str, receipt_type: str
|
|
|
|
) -> Optional[str]:
|
|
|
|
return await self.db_pool.simple_select_one_onecol(
|
2016-02-09 06:55:59 -07:00
|
|
|
table="receipts_linearized",
|
|
|
|
keyvalues={
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
2019-04-03 03:07:29 -06:00
|
|
|
"user_id": user_id,
|
2016-02-09 06:55:59 -07:00
|
|
|
},
|
|
|
|
retcol="event_id",
|
|
|
|
desc="get_own_receipt_for_user",
|
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
@cached(num_args=2)
|
|
|
|
async def get_receipts_for_user(self, user_id, receipt_type):
|
|
|
|
rows = await self.db_pool.simple_select_list(
|
2016-03-23 10:13:05 -06:00
|
|
|
table="receipts_linearized",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"user_id": user_id, "receipt_type": receipt_type},
|
2016-03-23 10:13:05 -06:00
|
|
|
retcols=("room_id", "event_id"),
|
|
|
|
desc="get_receipts_for_user",
|
|
|
|
)
|
2016-01-20 08:30:31 -07:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {row["room_id"]: row["event_id"] for row in rows}
|
2016-01-20 08:30:31 -07:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
async def get_receipts_for_user_with_orderings(self, user_id, receipt_type):
|
2016-05-23 11:33:51 -06:00
|
|
|
def f(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT rl.room_id, rl.event_id,"
|
|
|
|
" e.topological_ordering, e.stream_ordering"
|
2016-08-18 10:53:44 -06:00
|
|
|
" FROM receipts_linearized AS rl"
|
|
|
|
" INNER JOIN events AS e USING (room_id, event_id)"
|
2016-05-23 11:33:51 -06:00
|
|
|
" WHERE rl.room_id = e.room_id"
|
|
|
|
" AND rl.event_id = e.event_id"
|
|
|
|
" AND user_id = ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_id,))
|
|
|
|
return txn.fetchall()
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
rows = await self.db_pool.runInteraction(
|
2020-08-05 14:38:57 -06:00
|
|
|
"get_receipts_for_user_with_orderings", f
|
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
return {
|
|
|
|
row[0]: {
|
|
|
|
"event_id": row[1],
|
|
|
|
"topological_ordering": row[2],
|
|
|
|
"stream_ordering": row[3],
|
2019-04-03 03:07:29 -06:00
|
|
|
}
|
2019-07-23 07:00:55 -06:00
|
|
|
for row in rows
|
|
|
|
}
|
2016-05-23 11:33:51 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
async def get_linearized_receipts_for_rooms(
|
|
|
|
self, room_ids: List[str], to_key: int, from_key: Optional[int] = None
|
|
|
|
) -> List[dict]:
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Get receipts for multiple rooms for sending to clients.
|
2015-07-14 03:19:07 -06:00
|
|
|
|
|
|
|
Args:
|
2020-08-14 08:05:19 -06:00
|
|
|
room_id: List of room_ids.
|
2021-02-12 09:01:48 -07:00
|
|
|
to_key: Max stream id to fetch receipts up to.
|
2020-08-14 08:05:19 -06:00
|
|
|
from_key: Min stream id to fetch receipts from. None fetches
|
2015-07-14 03:19:07 -06:00
|
|
|
from the start.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-14 08:05:19 -06:00
|
|
|
A list of receipts.
|
2015-07-13 06:30:43 -06:00
|
|
|
"""
|
2015-07-08 08:35:00 -06:00
|
|
|
room_ids = set(room_ids)
|
|
|
|
|
2018-07-10 11:12:39 -06:00
|
|
|
if from_key is not None:
|
|
|
|
# Only ask the database about rooms where there have been new
|
|
|
|
# receipts added since `from_key`
|
2020-08-14 08:05:19 -06:00
|
|
|
room_ids = self._receipts_stream_cache.get_entities_changed(
|
2016-01-28 08:02:37 -07:00
|
|
|
room_ids, from_key
|
2015-07-08 08:35:00 -06:00
|
|
|
)
|
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
results = await self._get_linearized_receipts_for_rooms(
|
2015-08-14 08:06:22 -06:00
|
|
|
room_ids, to_key, from_key=from_key
|
|
|
|
)
|
2015-07-08 08:35:00 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return [ev for res in results.values() for ev in res]
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
async def get_linearized_receipts_for_room(
|
|
|
|
self, room_id: str, to_key: int, from_key: Optional[int] = None
|
|
|
|
) -> List[dict]:
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Get receipts for a single room for sending to clients.
|
2015-07-14 03:19:07 -06:00
|
|
|
|
|
|
|
Args:
|
2020-08-14 08:05:19 -06:00
|
|
|
room_ids: The room id.
|
2021-02-12 09:01:48 -07:00
|
|
|
to_key: Max stream id to fetch receipts up to.
|
2020-08-14 08:05:19 -06:00
|
|
|
from_key: Min stream id to fetch receipts from. None fetches
|
2015-07-14 03:19:07 -06:00
|
|
|
from the start.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-14 08:05:19 -06:00
|
|
|
A list of receipts.
|
2015-07-13 06:30:43 -06:00
|
|
|
"""
|
2018-07-10 11:12:39 -06:00
|
|
|
if from_key is not None:
|
|
|
|
# Check the cache first to see if any new receipts have been added
|
|
|
|
# since`from_key`. If not we can no-op.
|
2018-07-10 10:21:17 -06:00
|
|
|
if not self._receipts_stream_cache.has_entity_changed(room_id, from_key):
|
2020-08-14 08:05:19 -06:00
|
|
|
return []
|
2018-07-10 10:21:17 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
return await self._get_linearized_receipts_for_room(room_id, to_key, from_key)
|
2018-07-10 10:21:17 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
@cached(num_args=3, tree=True)
|
|
|
|
async def _get_linearized_receipts_for_room(
|
|
|
|
self, room_id: str, to_key: int, from_key: Optional[int] = None
|
|
|
|
) -> List[dict]:
|
2018-07-10 10:21:17 -06:00
|
|
|
"""See get_linearized_receipts_for_room"""
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
def f(txn):
|
2015-07-08 03:54:01 -06:00
|
|
|
if from_key:
|
|
|
|
sql = (
|
|
|
|
"SELECT * FROM receipts_linearized WHERE"
|
|
|
|
" room_id = ? AND stream_id > ? AND stream_id <= ?"
|
|
|
|
)
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
txn.execute(sql, (room_id, from_key, to_key))
|
2015-07-08 03:54:01 -06:00
|
|
|
else:
|
|
|
|
sql = (
|
|
|
|
"SELECT * FROM receipts_linearized WHERE"
|
|
|
|
" room_id = ? AND stream_id <= ?"
|
|
|
|
)
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
txn.execute(sql, (room_id, to_key))
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
rows = self.db_pool.cursor_to_dict(txn)
|
2015-07-07 08:25:30 -06:00
|
|
|
|
|
|
|
return rows
|
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
rows = await self.db_pool.runInteraction("get_linearized_receipts_for_room", f)
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2015-07-08 08:35:00 -06:00
|
|
|
if not rows:
|
2019-07-23 07:00:55 -06:00
|
|
|
return []
|
2015-07-08 08:35:00 -06:00
|
|
|
|
|
|
|
content = {}
|
2015-07-01 04:41:55 -06:00
|
|
|
for row in rows:
|
2019-04-03 03:07:29 -06:00
|
|
|
content.setdefault(row["event_id"], {}).setdefault(row["receipt_type"], {})[
|
|
|
|
row["user_id"]
|
2020-07-16 09:32:19 -06:00
|
|
|
] = db_to_json(row["data"])
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return [{"type": "m.receipt", "room_id": room_id, "content": content}]
|
2019-04-03 03:07:29 -06:00
|
|
|
|
|
|
|
@cachedList(
|
|
|
|
cached_method_name="_get_linearized_receipts_for_room",
|
|
|
|
list_name="room_ids",
|
|
|
|
num_args=3,
|
|
|
|
)
|
2020-08-14 05:24:26 -06:00
|
|
|
async def _get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None):
|
2015-08-14 08:06:22 -06:00
|
|
|
if not room_ids:
|
2019-07-23 07:00:55 -06:00
|
|
|
return {}
|
2015-08-14 08:06:22 -06:00
|
|
|
|
|
|
|
def f(txn):
|
|
|
|
if from_key:
|
2019-10-02 12:07:07 -06:00
|
|
|
sql = """
|
|
|
|
SELECT * FROM receipts_linearized WHERE
|
|
|
|
stream_id > ? AND stream_id <= ? AND
|
|
|
|
"""
|
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
self.database_engine, "room_id", room_ids
|
|
|
|
)
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2019-10-02 12:07:07 -06:00
|
|
|
txn.execute(sql + clause, [from_key, to_key] + list(args))
|
2015-08-14 08:06:22 -06:00
|
|
|
else:
|
2019-10-02 12:07:07 -06:00
|
|
|
sql = """
|
|
|
|
SELECT * FROM receipts_linearized WHERE
|
|
|
|
stream_id <= ? AND
|
|
|
|
"""
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2019-10-02 12:07:07 -06:00
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
self.database_engine, "room_id", room_ids
|
|
|
|
)
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2019-10-02 12:07:07 -06:00
|
|
|
txn.execute(sql + clause, [to_key] + list(args))
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
return self.db_pool.cursor_to_dict(txn)
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2020-08-14 05:24:26 -06:00
|
|
|
txn_results = await self.db_pool.runInteraction(
|
2019-12-04 06:52:46 -07:00
|
|
|
"_get_linearized_receipts_for_rooms", f
|
|
|
|
)
|
2015-08-14 08:06:22 -06:00
|
|
|
|
|
|
|
results = {}
|
|
|
|
for row in txn_results:
|
2015-08-18 03:49:23 -06:00
|
|
|
# We want a single event per room, since we want to batch the
|
|
|
|
# receipts by room, event and type.
|
2019-04-03 03:07:29 -06:00
|
|
|
room_event = results.setdefault(
|
|
|
|
row["room_id"],
|
|
|
|
{"type": "m.receipt", "room_id": row["room_id"], "content": {}},
|
|
|
|
)
|
2015-08-18 03:49:23 -06:00
|
|
|
|
|
|
|
# The content is of the form:
|
|
|
|
# {"$foo:bar": { "read": { "@user:host": <receipt> }, .. }, .. }
|
2015-08-18 04:54:03 -06:00
|
|
|
event_entry = room_event["content"].setdefault(row["event_id"], {})
|
|
|
|
receipt_type = event_entry.setdefault(row["receipt_type"], {})
|
2015-08-18 03:49:23 -06:00
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
receipt_type[row["user_id"]] = db_to_json(row["data"])
|
2015-08-14 08:06:22 -06:00
|
|
|
|
|
|
|
results = {
|
|
|
|
room_id: [results[room_id]] if room_id in results else []
|
|
|
|
for room_id in room_ids
|
|
|
|
}
|
2019-07-23 07:00:55 -06:00
|
|
|
return results
|
2015-08-14 08:06:22 -06:00
|
|
|
|
2020-10-15 10:33:28 -06:00
|
|
|
@cached(
|
|
|
|
num_args=2,
|
|
|
|
)
|
|
|
|
async def get_linearized_receipts_for_all_rooms(
|
|
|
|
self, to_key: int, from_key: Optional[int] = None
|
|
|
|
) -> Dict[str, JsonDict]:
|
2020-11-18 11:54:09 -07:00
|
|
|
"""Get receipts for all rooms between two stream_ids, up
|
|
|
|
to a limit of the latest 100 read receipts.
|
2020-10-15 10:33:28 -06:00
|
|
|
|
|
|
|
Args:
|
2021-02-12 09:01:48 -07:00
|
|
|
to_key: Max stream id to fetch receipts up to.
|
2020-10-15 10:33:28 -06:00
|
|
|
from_key: Min stream id to fetch receipts from. None fetches
|
|
|
|
from the start.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary of roomids to a list of receipts.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def f(txn):
|
|
|
|
if from_key:
|
|
|
|
sql = """
|
|
|
|
SELECT * FROM receipts_linearized WHERE
|
|
|
|
stream_id > ? AND stream_id <= ?
|
2020-11-18 11:54:09 -07:00
|
|
|
ORDER BY stream_id DESC
|
|
|
|
LIMIT 100
|
2020-10-15 10:33:28 -06:00
|
|
|
"""
|
|
|
|
txn.execute(sql, [from_key, to_key])
|
|
|
|
else:
|
|
|
|
sql = """
|
|
|
|
SELECT * FROM receipts_linearized WHERE
|
|
|
|
stream_id <= ?
|
2020-11-18 11:54:09 -07:00
|
|
|
ORDER BY stream_id DESC
|
|
|
|
LIMIT 100
|
2020-10-15 10:33:28 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
txn.execute(sql, [to_key])
|
|
|
|
|
|
|
|
return self.db_pool.cursor_to_dict(txn)
|
|
|
|
|
|
|
|
txn_results = await self.db_pool.runInteraction(
|
|
|
|
"get_linearized_receipts_for_all_rooms", f
|
|
|
|
)
|
|
|
|
|
|
|
|
results = {}
|
|
|
|
for row in txn_results:
|
|
|
|
# We want a single event per room, since we want to batch the
|
|
|
|
# receipts by room, event and type.
|
|
|
|
room_event = results.setdefault(
|
|
|
|
row["room_id"],
|
|
|
|
{"type": "m.receipt", "room_id": row["room_id"], "content": {}},
|
|
|
|
)
|
|
|
|
|
|
|
|
# The content is of the form:
|
|
|
|
# {"$foo:bar": { "read": { "@user:host": <receipt> }, .. }, .. }
|
|
|
|
event_entry = room_event["content"].setdefault(row["event_id"], {})
|
|
|
|
receipt_type = event_entry.setdefault(row["receipt_type"], {})
|
|
|
|
|
|
|
|
receipt_type[row["user_id"]] = db_to_json(row["data"])
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2020-09-01 07:21:48 -06:00
|
|
|
async def get_users_sent_receipts_between(
|
|
|
|
self, last_id: int, current_id: int
|
|
|
|
) -> List[str]:
|
2020-06-16 10:10:28 -06:00
|
|
|
"""Get all users who sent receipts between `last_id` exclusive and
|
|
|
|
`current_id` inclusive.
|
|
|
|
|
|
|
|
Returns:
|
2020-09-01 07:21:48 -06:00
|
|
|
The list of users.
|
2020-06-16 10:10:28 -06:00
|
|
|
"""
|
|
|
|
|
2018-02-16 05:17:14 -07:00
|
|
|
if last_id == current_id:
|
|
|
|
return defer.succeed([])
|
|
|
|
|
2020-06-16 10:10:28 -06:00
|
|
|
def _get_users_sent_receipts_between_txn(txn):
|
|
|
|
sql = """
|
|
|
|
SELECT DISTINCT user_id FROM receipts_linearized
|
|
|
|
WHERE ? < stream_id AND stream_id <= ?
|
|
|
|
"""
|
|
|
|
txn.execute(sql, (last_id, current_id))
|
2018-02-16 05:17:14 -07:00
|
|
|
|
2020-06-16 10:10:28 -06:00
|
|
|
return [r[0] for r in txn]
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2020-09-01 07:21:48 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2020-06-16 10:10:28 -06:00
|
|
|
"get_users_sent_receipts_between", _get_users_sent_receipts_between_txn
|
|
|
|
)
|
|
|
|
|
|
|
|
async def get_all_updated_receipts(
|
|
|
|
self, instance_name: str, last_id: int, current_id: int, limit: int
|
|
|
|
) -> Tuple[List[Tuple[int, list]], int, bool]:
|
|
|
|
"""Get updates for receipts replication stream.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
instance_name: The writer we want to fetch updates from. Unused
|
|
|
|
here since there is only ever one writer.
|
|
|
|
last_id: The token to fetch updates from. Exclusive.
|
|
|
|
current_id: The token to fetch updates up to. Inclusive.
|
|
|
|
limit: The requested limit for the number of rows to return. The
|
|
|
|
function may return more or fewer rows.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A tuple consisting of: the updates, a token to use to fetch
|
|
|
|
subsequent updates, and whether we returned fewer rows than exists
|
|
|
|
between the requested tokens due to the limit.
|
|
|
|
|
|
|
|
The token returned can be used in a subsequent call to this
|
|
|
|
function to get further updatees.
|
|
|
|
|
|
|
|
The updates are a list of 2-tuples of stream ID and the row data
|
|
|
|
"""
|
|
|
|
|
|
|
|
if last_id == current_id:
|
|
|
|
return [], current_id, False
|
|
|
|
|
|
|
|
def get_all_updated_receipts_txn(txn):
|
|
|
|
sql = """
|
|
|
|
SELECT stream_id, room_id, receipt_type, user_id, event_id, data
|
|
|
|
FROM receipts_linearized
|
|
|
|
WHERE ? < stream_id AND stream_id <= ?
|
|
|
|
ORDER BY stream_id ASC
|
|
|
|
LIMIT ?
|
|
|
|
"""
|
|
|
|
txn.execute(sql, (last_id, current_id, limit))
|
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
updates = [(r[0], r[1:5] + (db_to_json(r[5]),)) for r in txn]
|
2020-06-16 10:10:28 -06:00
|
|
|
|
|
|
|
limited = False
|
|
|
|
upper_bound = current_id
|
|
|
|
|
|
|
|
if len(updates) == limit:
|
|
|
|
limited = True
|
|
|
|
upper_bound = updates[-1][0]
|
|
|
|
|
|
|
|
return updates, upper_bound, limited
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2018-02-16 05:17:14 -07:00
|
|
|
"get_all_updated_receipts", get_all_updated_receipts_txn
|
|
|
|
)
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
def _invalidate_get_users_with_receipts_in_room(
|
2020-08-14 08:05:19 -06:00
|
|
|
self, room_id: str, receipt_type: str, user_id: str
|
2019-04-03 03:07:29 -06:00
|
|
|
):
|
2018-02-16 05:17:14 -07:00
|
|
|
if receipt_type != "m.read":
|
|
|
|
return
|
|
|
|
|
2020-10-19 08:00:12 -06:00
|
|
|
res = self.get_users_with_read_receipts_in_room.cache.get_immediate(
|
2019-04-03 03:07:29 -06:00
|
|
|
room_id, None, update_metrics=False
|
2018-02-16 05:17:14 -07:00
|
|
|
)
|
|
|
|
|
2018-05-18 02:15:35 -06:00
|
|
|
if res and user_id in res:
|
|
|
|
# We'd only be adding to the set, so no point invalidating if the
|
|
|
|
# user is already there
|
|
|
|
return
|
2018-02-16 05:17:14 -07:00
|
|
|
|
|
|
|
self.get_users_with_read_receipts_in_room.invalidate((room_id,))
|
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
def invalidate_caches_for_receipt(self, room_id, receipt_type, user_id):
|
|
|
|
self.get_receipts_for_user.invalidate((user_id, receipt_type))
|
2021-05-27 03:33:56 -06:00
|
|
|
self._get_linearized_receipts_for_room.invalidate((room_id,))
|
2021-01-18 08:47:59 -07:00
|
|
|
self.get_last_receipt_event_id_for_user.invalidate(
|
|
|
|
(user_id, room_id, receipt_type)
|
2018-02-21 08:19:54 -07:00
|
|
|
)
|
2021-01-18 08:47:59 -07:00
|
|
|
self._invalidate_get_users_with_receipts_in_room(room_id, receipt_type, user_id)
|
|
|
|
self.get_receipts_for_room.invalidate((room_id, receipt_type))
|
|
|
|
|
|
|
|
def process_replication_rows(self, stream_name, instance_name, token, rows):
|
|
|
|
if stream_name == ReceiptsStream.NAME:
|
|
|
|
self._receipts_id_gen.advance(instance_name, token)
|
|
|
|
for row in rows:
|
|
|
|
self.invalidate_caches_for_receipt(
|
|
|
|
row.room_id, row.receipt_type, row.user_id
|
|
|
|
)
|
|
|
|
self._receipts_stream_cache.entity_has_changed(row.room_id, token)
|
2018-02-21 08:19:54 -07:00
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
return super().process_replication_rows(stream_name, instance_name, token, rows)
|
2018-02-21 08:19:54 -07:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
def insert_linearized_receipt_txn(
|
|
|
|
self, txn, room_id, receipt_type, user_id, event_id, data, stream_id
|
|
|
|
):
|
2019-03-04 11:18:11 -07:00
|
|
|
"""Inserts a read-receipt into the database if it's newer than the current RR
|
|
|
|
|
|
|
|
Returns: int|None
|
|
|
|
None if the RR is older than the current RR
|
|
|
|
otherwise, the rx timestamp of the event that the RR corresponds to
|
|
|
|
(or 0 if the event is unknown)
|
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_receipts
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
res = self.db_pool.simple_select_one_txn(
|
2018-06-01 05:18:11 -06:00
|
|
|
txn,
|
|
|
|
table="events",
|
2019-03-04 11:18:11 -07:00
|
|
|
retcols=["stream_ordering", "received_ts"],
|
2018-06-01 05:18:11 -06:00
|
|
|
keyvalues={"event_id": event_id},
|
2019-04-03 03:07:29 -06:00
|
|
|
allow_none=True,
|
2018-06-01 05:18:11 -06:00
|
|
|
)
|
|
|
|
|
2018-06-01 07:01:43 -06:00
|
|
|
stream_ordering = int(res["stream_ordering"]) if res else None
|
2019-03-04 11:18:11 -07:00
|
|
|
rx_ts = res["received_ts"] if res else 0
|
2018-06-01 05:18:11 -06:00
|
|
|
|
|
|
|
# We don't want to clobber receipts for more recent events, so we
|
|
|
|
# have to compare orderings of existing receipts
|
2018-06-01 07:01:43 -06:00
|
|
|
if stream_ordering is not None:
|
|
|
|
sql = (
|
|
|
|
"SELECT stream_ordering, event_id FROM events"
|
|
|
|
" INNER JOIN receipts_linearized as r USING (event_id, room_id)"
|
|
|
|
" WHERE r.room_id = ? AND r.receipt_type = ? AND r.user_id = ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (room_id, receipt_type, user_id))
|
|
|
|
|
|
|
|
for so, eid in txn:
|
|
|
|
if int(so) >= stream_ordering:
|
|
|
|
logger.debug(
|
|
|
|
"Ignoring new receipt for %s in favour of existing "
|
|
|
|
"one for later event %s",
|
2019-04-03 03:07:29 -06:00
|
|
|
event_id,
|
|
|
|
eid,
|
2018-06-01 07:01:43 -06:00
|
|
|
)
|
2019-03-04 11:18:11 -07:00
|
|
|
return None
|
2018-06-01 05:18:11 -06:00
|
|
|
|
2016-01-20 08:30:31 -07:00
|
|
|
txn.call_after(
|
2021-01-18 08:47:59 -07:00
|
|
|
self.invalidate_caches_for_receipt, room_id, receipt_type, user_id
|
2016-01-20 08:30:31 -07:00
|
|
|
)
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2016-01-28 08:02:37 -07:00
|
|
|
txn.call_after(
|
2019-04-03 03:07:29 -06:00
|
|
|
self._receipts_stream_cache.entity_has_changed, room_id, stream_id
|
2016-01-28 08:02:37 -07:00
|
|
|
)
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_upsert_txn(
|
2015-07-01 04:41:55 -06:00
|
|
|
txn,
|
|
|
|
table="receipts_linearized",
|
|
|
|
keyvalues={
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
|
|
|
"user_id": user_id,
|
2019-04-03 03:07:29 -06:00
|
|
|
},
|
2015-07-01 04:41:55 -06:00
|
|
|
values={
|
2015-07-07 03:55:31 -06:00
|
|
|
"stream_id": stream_id,
|
2015-07-01 04:41:55 -06:00
|
|
|
"event_id": event_id,
|
2020-08-07 06:02:55 -06:00
|
|
|
"data": json_encoder.encode(data),
|
2019-04-03 03:07:29 -06:00
|
|
|
},
|
2020-06-01 03:53:06 -06:00
|
|
|
# receipts_linearized has a unique constraint on
|
|
|
|
# (user_id, room_id, receipt_type), so no need to lock
|
|
|
|
lock=False,
|
2015-07-01 04:41:55 -06:00
|
|
|
)
|
|
|
|
|
2018-06-01 07:01:43 -06:00
|
|
|
if receipt_type == "m.read" and stream_ordering is not None:
|
2016-05-20 08:25:12 -06:00
|
|
|
self._remove_old_push_actions_before_txn(
|
2019-04-03 03:07:29 -06:00
|
|
|
txn, room_id=room_id, user_id=user_id, stream_ordering=stream_ordering
|
2016-05-03 07:22:33 -06:00
|
|
|
)
|
|
|
|
|
2019-03-04 11:18:11 -07:00
|
|
|
return rx_ts
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
async def insert_receipt(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
receipt_type: str,
|
|
|
|
user_id: str,
|
|
|
|
event_ids: List[str],
|
|
|
|
data: dict,
|
|
|
|
) -> Optional[Tuple[int, int]]:
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Insert a receipt, either from local client or remote server.
|
|
|
|
|
|
|
|
Automatically does conversion between linearized and graph
|
|
|
|
representations.
|
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_receipts
|
|
|
|
|
2015-07-01 04:41:55 -06:00
|
|
|
if not event_ids:
|
2020-08-14 08:05:19 -06:00
|
|
|
return None
|
2015-07-01 04:41:55 -06:00
|
|
|
|
|
|
|
if len(event_ids) == 1:
|
|
|
|
linearized_event_id = event_ids[0]
|
|
|
|
else:
|
|
|
|
# we need to points in graph -> linearized form.
|
2015-07-09 09:14:46 -06:00
|
|
|
# TODO: Make this better.
|
2015-07-01 04:41:55 -06:00
|
|
|
def graph_to_linear(txn):
|
2019-10-02 12:07:07 -06:00
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
self.database_engine, "event_id", event_ids
|
|
|
|
)
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
SELECT event_id WHERE room_id = ? AND stream_ordering IN (
|
|
|
|
SELECT max(stream_ordering) WHERE %s
|
|
|
|
)
|
|
|
|
""" % (
|
|
|
|
clause,
|
|
|
|
)
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2019-10-02 12:07:07 -06:00
|
|
|
txn.execute(sql, [room_id] + list(args))
|
2015-07-01 04:41:55 -06:00
|
|
|
rows = txn.fetchall()
|
|
|
|
if rows:
|
|
|
|
return rows[0][0]
|
|
|
|
else:
|
2015-07-09 09:14:46 -06:00
|
|
|
raise RuntimeError("Unrecognized event_ids: %r" % (event_ids,))
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
linearized_event_id = await self.db_pool.runInteraction(
|
2015-07-07 03:55:31 -06:00
|
|
|
"insert_receipt_conv", graph_to_linear
|
2015-07-01 04:41:55 -06:00
|
|
|
)
|
|
|
|
|
2020-09-23 09:11:18 -06:00
|
|
|
async with self._receipts_id_gen.get_next() as stream_id:
|
2020-08-14 08:05:19 -06:00
|
|
|
event_ts = await self.db_pool.runInteraction(
|
2015-07-07 03:55:31 -06:00
|
|
|
"insert_linearized_receipt",
|
2015-07-01 04:41:55 -06:00
|
|
|
self.insert_linearized_receipt_txn,
|
2019-04-03 03:07:29 -06:00
|
|
|
room_id,
|
|
|
|
receipt_type,
|
|
|
|
user_id,
|
|
|
|
linearized_event_id,
|
2015-07-09 04:39:30 -06:00
|
|
|
data,
|
2015-07-01 04:41:55 -06:00
|
|
|
stream_id=stream_id,
|
|
|
|
)
|
|
|
|
|
2019-03-04 11:18:11 -07:00
|
|
|
if event_ts is None:
|
2019-07-23 07:00:55 -06:00
|
|
|
return None
|
2019-03-04 11:18:11 -07:00
|
|
|
|
|
|
|
now = self._clock.time_msec()
|
|
|
|
logger.debug(
|
|
|
|
"RR for event %s in %s (%i ms old)",
|
2019-04-03 03:07:29 -06:00
|
|
|
linearized_event_id,
|
|
|
|
room_id,
|
|
|
|
now - event_ts,
|
2019-03-04 11:18:11 -07:00
|
|
|
)
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2020-08-14 08:05:19 -06:00
|
|
|
await self.insert_graph_receipt(room_id, receipt_type, user_id, event_ids, data)
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2016-12-08 05:06:47 -07:00
|
|
|
max_persisted_id = self._receipts_id_gen.get_current_token()
|
2016-01-20 08:30:31 -07:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return stream_id, max_persisted_id
|
2015-07-01 04:41:55 -06:00
|
|
|
|
2020-09-01 07:21:48 -06:00
|
|
|
async def insert_graph_receipt(
|
|
|
|
self, room_id, receipt_type, user_id, event_ids, data
|
|
|
|
):
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_receipts
|
|
|
|
|
2020-09-01 07:21:48 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2015-07-07 03:55:31 -06:00
|
|
|
"insert_graph_receipt",
|
2015-07-01 04:41:55 -06:00
|
|
|
self.insert_graph_receipt_txn,
|
2019-04-03 03:07:29 -06:00
|
|
|
room_id,
|
|
|
|
receipt_type,
|
|
|
|
user_id,
|
|
|
|
event_ids,
|
|
|
|
data,
|
2015-07-01 04:41:55 -06:00
|
|
|
)
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
def insert_graph_receipt_txn(
|
|
|
|
self, txn, room_id, receipt_type, user_id, event_ids, data
|
|
|
|
):
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_receipts
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
txn.call_after(self.get_receipts_for_room.invalidate, (room_id, receipt_type))
|
2016-06-01 03:31:09 -06:00
|
|
|
txn.call_after(
|
|
|
|
self._invalidate_get_users_with_receipts_in_room,
|
2019-04-03 03:07:29 -06:00
|
|
|
room_id,
|
|
|
|
receipt_type,
|
|
|
|
user_id,
|
2016-06-01 03:31:09 -06:00
|
|
|
)
|
2019-04-03 03:07:29 -06:00
|
|
|
txn.call_after(self.get_receipts_for_user.invalidate, (user_id, receipt_type))
|
|
|
|
# FIXME: This shouldn't invalidate the whole cache
|
2021-05-27 03:33:56 -06:00
|
|
|
txn.call_after(self._get_linearized_receipts_for_room.invalidate, (room_id,))
|
2016-01-20 08:30:31 -07:00
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_delete_txn(
|
2015-07-01 04:41:55 -06:00
|
|
|
txn,
|
|
|
|
table="receipts_graph",
|
|
|
|
keyvalues={
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
|
|
|
"user_id": user_id,
|
2019-04-03 03:07:29 -06:00
|
|
|
},
|
2015-07-01 04:41:55 -06:00
|
|
|
)
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_insert_txn(
|
2015-07-01 04:41:55 -06:00
|
|
|
txn,
|
|
|
|
table="receipts_graph",
|
2015-07-09 04:39:30 -06:00
|
|
|
values={
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
|
|
|
"user_id": user_id,
|
2020-08-07 06:02:55 -06:00
|
|
|
"event_ids": json_encoder.encode(event_ids),
|
|
|
|
"data": json_encoder.encode(data),
|
2019-04-03 03:07:29 -06:00
|
|
|
},
|
2015-07-01 04:41:55 -06:00
|
|
|
)
|
2021-01-18 08:47:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ReceiptsStore(ReceiptsWorkerStore):
|
|
|
|
pass
|