2015-07-01 12:19:31 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2015-07-01 12:19:31 -04:00
|
|
|
#
|
|
|
|
#
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2023-02-10 18:29:00 -05:00
|
|
|
from typing import TYPE_CHECKING, Iterable, List, Optional, Sequence, Tuple
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2022-05-27 07:14:36 -04:00
|
|
|
from synapse.api.constants import EduTypes, ReceiptTypes
|
2020-10-15 12:33:28 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
2021-09-21 13:34:26 -04:00
|
|
|
from synapse.streams import EventSource
|
2022-05-16 11:35:31 -04:00
|
|
|
from synapse.types import (
|
|
|
|
JsonDict,
|
2023-09-19 15:26:44 -04:00
|
|
|
JsonMapping,
|
2023-10-25 11:16:19 -04:00
|
|
|
MultiWriterStreamToken,
|
2022-05-16 11:35:31 -04:00
|
|
|
ReadReceipt,
|
|
|
|
StreamKeyType,
|
|
|
|
UserID,
|
|
|
|
get_domain_from_id,
|
|
|
|
)
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2021-01-04 10:05:12 -05:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2021-01-04 10:05:12 -05:00
|
|
|
|
2015-07-01 12:19:31 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-08 07:44:43 -04:00
|
|
|
class ReceiptsHandler:
|
2021-01-04 10:05:12 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-10-08 07:44:43 -04:00
|
|
|
self.notifier = hs.get_notifier()
|
2021-09-13 13:07:12 -04:00
|
|
|
self.server_name = hs.config.server.server_name
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-07-06 09:31:13 -04:00
|
|
|
self.event_auth_handler = hs.get_event_auth_handler()
|
2023-09-18 09:02:12 -04:00
|
|
|
self.event_handler = hs.get_event_handler()
|
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-07-06 09:31:13 -04:00
|
|
|
|
2015-07-07 05:55:31 -04:00
|
|
|
self.hs = hs
|
2021-01-18 10:47:59 -05:00
|
|
|
|
|
|
|
# We only need to poke the federation sender explicitly if its on the
|
|
|
|
# same instance. Other federation sender instances will get notified by
|
|
|
|
# `synapse.app.generic_worker.FederationSenderHandler` when it sees it
|
|
|
|
# in the receipts stream.
|
|
|
|
self.federation_sender = None
|
|
|
|
if hs.should_send_federation():
|
|
|
|
self.federation_sender = hs.get_federation_sender()
|
|
|
|
|
|
|
|
# If we can handle the receipt EDUs we do so, otherwise we route them
|
|
|
|
# to the appropriate worker.
|
|
|
|
if hs.get_instance_name() in hs.config.worker.writers.receipts:
|
|
|
|
hs.get_federation_registry().register_edu_handler(
|
2022-05-27 07:14:36 -04:00
|
|
|
EduTypes.RECEIPT, self._received_remote_receipt
|
2021-01-18 10:47:59 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
hs.get_federation_registry().register_instances_for_edu(
|
2022-05-27 07:14:36 -04:00
|
|
|
EduTypes.RECEIPT,
|
2021-01-18 10:47:59 -05:00
|
|
|
hs.config.worker.writers.receipts,
|
|
|
|
)
|
|
|
|
|
2015-07-09 06:39:30 -04:00
|
|
|
self.clock = self.hs.get_clock()
|
2016-08-26 09:54:30 -04:00
|
|
|
self.state = hs.get_state_handler()
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2021-01-04 10:05:12 -05:00
|
|
|
async def _received_remote_receipt(self, origin: str, content: JsonDict) -> None:
|
2015-07-13 08:30:43 -04:00
|
|
|
"""Called when we receive an EDU of type m.receipt from a remote HS."""
|
2019-07-23 08:31:03 -04:00
|
|
|
receipts = []
|
|
|
|
for room_id, room_values in content.items():
|
2021-07-06 09:31:13 -04:00
|
|
|
# If we're not in the room just ditch the event entirely. This is
|
|
|
|
# probably an old server that has come back and thinks we're still in
|
|
|
|
# the room (or we've been rejoined to the room by a state reset).
|
2022-09-23 06:47:16 -04:00
|
|
|
is_in_room = await self.event_auth_handler.is_host_in_room(
|
2021-07-06 09:31:13 -04:00
|
|
|
room_id, self.server_name
|
|
|
|
)
|
|
|
|
if not is_in_room:
|
|
|
|
logger.info(
|
2021-08-03 09:35:49 -04:00
|
|
|
"Ignoring receipt for room %r from server %s as we're not in the room",
|
|
|
|
room_id,
|
2021-07-06 09:31:13 -04:00
|
|
|
origin,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2023-09-18 09:02:12 -04:00
|
|
|
# Let's check that the origin server is in the room before accepting the receipt.
|
|
|
|
# We don't want to block waiting on a partial state so take an
|
|
|
|
# approximation if needed.
|
|
|
|
domains = await self._storage_controllers.state.get_current_hosts_in_room_or_partial_state_approximation(
|
|
|
|
room_id
|
|
|
|
)
|
|
|
|
if origin not in domains:
|
|
|
|
logger.info(
|
|
|
|
"Ignoring receipt for room %r from server %s as they're not in the room",
|
|
|
|
room_id,
|
|
|
|
origin,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2019-07-23 08:31:03 -04:00
|
|
|
for receipt_type, users in room_values.items():
|
|
|
|
for user_id, user_values in users.items():
|
|
|
|
if get_domain_from_id(user_id) != origin:
|
|
|
|
logger.info(
|
|
|
|
"Received receipt for user %r from server %s, ignoring",
|
|
|
|
user_id,
|
|
|
|
origin,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2022-09-23 10:33:28 -04:00
|
|
|
# Check if these receipts apply to a thread.
|
|
|
|
data = user_values.get("data", {})
|
2022-10-07 09:26:40 -04:00
|
|
|
thread_id = data.get("thread_id")
|
|
|
|
# If the thread ID is invalid, consider it missing.
|
|
|
|
if not isinstance(thread_id, str):
|
|
|
|
thread_id = None
|
2022-09-23 10:33:28 -04:00
|
|
|
|
2019-07-23 08:31:03 -04:00
|
|
|
receipts.append(
|
|
|
|
ReadReceipt(
|
|
|
|
room_id=room_id,
|
|
|
|
receipt_type=receipt_type,
|
|
|
|
user_id=user_id,
|
|
|
|
event_ids=user_values["event_ids"],
|
2022-09-23 10:33:28 -04:00
|
|
|
thread_id=thread_id,
|
|
|
|
data=data,
|
2019-07-23 08:31:03 -04:00
|
|
|
)
|
|
|
|
)
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2019-10-29 11:08:22 -04:00
|
|
|
await self._handle_new_receipts(receipts)
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2021-01-04 10:05:12 -05:00
|
|
|
async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
|
2015-07-13 08:30:43 -04:00
|
|
|
"""Takes a list of receipts, stores them and informs the notifier."""
|
2016-04-07 10:39:53 -04:00
|
|
|
|
2023-10-04 11:28:40 -04:00
|
|
|
receipts_persisted: List[ReadReceipt] = []
|
2015-07-01 12:19:31 -04:00
|
|
|
for receipt in receipts:
|
2023-10-04 11:28:40 -04:00
|
|
|
stream_id = await self.store.insert_receipt(
|
2019-03-12 12:50:58 -04:00
|
|
|
receipt.room_id,
|
|
|
|
receipt.receipt_type,
|
|
|
|
receipt.user_id,
|
|
|
|
receipt.event_ids,
|
2022-09-23 10:33:28 -04:00
|
|
|
receipt.thread_id,
|
2019-03-12 12:50:58 -04:00
|
|
|
receipt.data,
|
2015-07-01 12:19:31 -04:00
|
|
|
)
|
|
|
|
|
2023-10-04 11:28:40 -04:00
|
|
|
if stream_id is None:
|
|
|
|
# stream_id will be None if this receipt is 'old'
|
2016-12-08 07:13:01 -05:00
|
|
|
continue
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2023-10-04 11:28:40 -04:00
|
|
|
receipts_persisted.append(receipt)
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2023-10-04 11:28:40 -04:00
|
|
|
if not receipts_persisted:
|
2016-12-08 07:13:01 -05:00
|
|
|
# no new receipts
|
2019-07-23 09:00:55 -04:00
|
|
|
return False
|
2016-12-08 07:13:01 -05:00
|
|
|
|
2023-10-04 11:28:40 -04:00
|
|
|
max_batch_id = self.store.get_max_receipt_stream_id()
|
|
|
|
|
|
|
|
affected_room_ids = list({r.room_id for r in receipts_persisted})
|
2016-04-07 10:39:53 -04:00
|
|
|
|
2022-05-16 11:35:31 -04:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
StreamKeyType.RECEIPT, max_batch_id, rooms=affected_room_ids
|
|
|
|
)
|
2018-08-16 19:32:39 -04:00
|
|
|
# Note that the min here shouldn't be relied upon to be accurate.
|
2020-12-11 14:05:15 -05:00
|
|
|
await self.hs.get_pusherpool().on_new_receipts(
|
2023-10-04 11:28:40 -04:00
|
|
|
{r.user_id for r in receipts_persisted}
|
2018-08-16 19:32:39 -04:00
|
|
|
)
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2015-07-01 12:19:31 -04:00
|
|
|
|
2021-01-04 10:05:12 -05:00
|
|
|
async def received_client_receipt(
|
2022-09-23 10:33:28 -04:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
receipt_type: str,
|
2023-09-18 09:02:12 -04:00
|
|
|
user_id: UserID,
|
2022-09-23 10:33:28 -04:00
|
|
|
event_id: str,
|
|
|
|
thread_id: Optional[str],
|
2023-02-12 08:02:13 -05:00
|
|
|
extra_content: Optional[JsonDict] = None,
|
2021-01-04 10:05:12 -05:00
|
|
|
) -> None:
|
2019-03-04 13:11:26 -05:00
|
|
|
"""Called when a client tells us a local user has read up to the given
|
|
|
|
event_id in the room.
|
2015-07-13 08:30:43 -04:00
|
|
|
"""
|
2023-09-18 09:02:12 -04:00
|
|
|
|
|
|
|
# Ensure the room/event exists, this will raise an error if the user
|
|
|
|
# cannot view the event.
|
|
|
|
if not await self.event_handler.get_event(user_id, room_id, event_id):
|
|
|
|
return
|
|
|
|
|
2019-03-12 12:50:58 -04:00
|
|
|
receipt = ReadReceipt(
|
|
|
|
room_id=room_id,
|
|
|
|
receipt_type=receipt_type,
|
2023-09-18 09:02:12 -04:00
|
|
|
user_id=user_id.to_string(),
|
2019-03-12 12:50:58 -04:00
|
|
|
event_ids=[event_id],
|
2022-09-23 10:33:28 -04:00
|
|
|
thread_id=thread_id,
|
2023-02-12 08:02:13 -05:00
|
|
|
data={"ts": int(self.clock.time_msec()), **(extra_content or {})},
|
2019-03-12 12:50:58 -04:00
|
|
|
)
|
2019-02-21 12:50:30 -05:00
|
|
|
|
2019-10-29 11:08:22 -04:00
|
|
|
is_new = await self._handle_new_receipts([receipt])
|
2019-03-04 13:11:26 -05:00
|
|
|
if not is_new:
|
|
|
|
return
|
|
|
|
|
2022-09-01 08:31:54 -04:00
|
|
|
if self.federation_sender and receipt_type != ReceiptTypes.READ_PRIVATE:
|
2021-01-18 10:47:59 -05:00
|
|
|
await self.federation_sender.send_read_receipt(receipt)
|
2015-07-08 05:54:01 -04:00
|
|
|
|
2015-07-07 10:25:30 -04:00
|
|
|
|
2023-10-25 11:16:19 -04:00
|
|
|
class ReceiptEventSource(EventSource[MultiWriterStreamToken, JsonMapping]):
|
2021-01-04 10:05:12 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-07-28 04:05:11 -04:00
|
|
|
self.config = hs.config
|
|
|
|
|
|
|
|
@staticmethod
|
2022-05-16 11:06:23 -04:00
|
|
|
def filter_out_private_receipts(
|
2023-09-19 15:26:44 -04:00
|
|
|
rooms: Sequence[JsonMapping], user_id: str
|
|
|
|
) -> List[JsonMapping]:
|
2022-05-04 11:59:22 -04:00
|
|
|
"""
|
2022-05-16 11:06:23 -04:00
|
|
|
Filters a list of serialized receipts (as returned by /sync and /initialSync)
|
|
|
|
and removes private read receipts of other users.
|
2021-07-28 04:05:11 -04:00
|
|
|
|
2022-05-16 11:06:23 -04:00
|
|
|
This operates on the return value of get_linearized_receipts_for_rooms(),
|
|
|
|
which is wrapped in a cache. Care must be taken to ensure that the input
|
|
|
|
values are not modified.
|
2021-07-28 04:05:11 -04:00
|
|
|
|
2022-05-16 11:06:23 -04:00
|
|
|
Args:
|
|
|
|
rooms: A list of mappings, each mapping has a `content` field, which
|
|
|
|
is a map of event ID -> receipt type -> user ID -> receipt information.
|
2021-07-28 04:05:11 -04:00
|
|
|
|
2022-05-16 11:06:23 -04:00
|
|
|
Returns:
|
|
|
|
The same as rooms, but filtered.
|
|
|
|
"""
|
2021-07-28 04:05:11 -04:00
|
|
|
|
2023-09-19 15:26:44 -04:00
|
|
|
result: List[JsonMapping] = []
|
2022-05-16 11:06:23 -04:00
|
|
|
|
|
|
|
# Iterate through each room's receipt content.
|
|
|
|
for room in rooms:
|
|
|
|
# The receipt content with other user's private read receipts removed.
|
|
|
|
content = {}
|
|
|
|
|
|
|
|
# Iterate over each event ID / receipts for that event.
|
|
|
|
for event_id, orig_event_content in room.get("content", {}).items():
|
|
|
|
event_content = orig_event_content
|
|
|
|
# If there are private read receipts, additional logic is necessary.
|
2022-09-01 08:31:54 -04:00
|
|
|
if ReceiptTypes.READ_PRIVATE in event_content:
|
2022-05-16 11:06:23 -04:00
|
|
|
# Make a copy without private read receipts to avoid leaking
|
|
|
|
# other user's private read receipts..
|
|
|
|
event_content = {
|
|
|
|
receipt_type: receipt_value
|
|
|
|
for receipt_type, receipt_value in event_content.items()
|
2022-09-01 08:31:54 -04:00
|
|
|
if receipt_type != ReceiptTypes.READ_PRIVATE
|
2022-05-16 11:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Copy the current user's private read receipt from the
|
|
|
|
# original content, if it exists.
|
2022-09-01 08:31:54 -04:00
|
|
|
user_private_read_receipt = orig_event_content[
|
|
|
|
ReceiptTypes.READ_PRIVATE
|
|
|
|
].get(user_id, None)
|
2022-05-16 11:06:23 -04:00
|
|
|
if user_private_read_receipt:
|
|
|
|
event_content[ReceiptTypes.READ_PRIVATE] = {
|
|
|
|
user_id: user_private_read_receipt
|
|
|
|
}
|
|
|
|
|
|
|
|
# Include the event if there is at least one non-private read
|
|
|
|
# receipt or the current user has a private read receipt.
|
|
|
|
if event_content:
|
|
|
|
content[event_id] = event_content
|
|
|
|
|
|
|
|
# Include the event if there is at least one non-private read receipt
|
|
|
|
# or the current user has a private read receipt.
|
|
|
|
if content:
|
|
|
|
# Build a new event to avoid mutating the cache.
|
|
|
|
new_room = {k: v for k, v in room.items() if k != "content"}
|
|
|
|
new_room["content"] = content
|
|
|
|
result.append(new_room)
|
|
|
|
|
|
|
|
return result
|
2015-07-07 10:25:30 -04:00
|
|
|
|
2021-01-04 10:05:12 -05:00
|
|
|
async def get_new_events(
|
2021-09-21 13:34:26 -04:00
|
|
|
self,
|
|
|
|
user: UserID,
|
2023-10-25 11:16:19 -04:00
|
|
|
from_key: MultiWriterStreamToken,
|
2022-10-14 08:30:05 -04:00
|
|
|
limit: int,
|
2021-09-21 13:34:26 -04:00
|
|
|
room_ids: Iterable[str],
|
|
|
|
is_guest: bool,
|
|
|
|
explicit_room_id: Optional[str] = None,
|
2023-10-25 11:16:19 -04:00
|
|
|
) -> Tuple[List[JsonMapping], MultiWriterStreamToken]:
|
2020-07-17 07:08:30 -04:00
|
|
|
to_key = self.get_current_key()
|
2015-07-07 10:25:30 -04:00
|
|
|
|
2015-07-18 14:07:12 -04:00
|
|
|
if from_key == to_key:
|
2019-08-30 11:28:26 -04:00
|
|
|
return [], to_key
|
2015-07-18 14:07:12 -04:00
|
|
|
|
2020-07-17 07:08:30 -04:00
|
|
|
events = await self.store.get_linearized_receipts_for_rooms(
|
2015-07-14 05:19:07 -04:00
|
|
|
room_ids, from_key=from_key, to_key=to_key
|
2015-07-08 10:35:00 -04:00
|
|
|
)
|
2015-07-07 10:25:30 -04:00
|
|
|
|
2022-08-05 11:09:33 -04:00
|
|
|
events = ReceiptEventSource.filter_out_private_receipts(
|
|
|
|
events, user.to_string()
|
|
|
|
)
|
2021-07-28 04:05:11 -04:00
|
|
|
|
2021-09-23 06:59:07 -04:00
|
|
|
return events, to_key
|
2015-07-07 10:25:30 -04:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
async def get_new_events_as(
|
2023-10-25 11:16:19 -04:00
|
|
|
self,
|
|
|
|
from_key: MultiWriterStreamToken,
|
|
|
|
to_key: MultiWriterStreamToken,
|
|
|
|
service: ApplicationService,
|
|
|
|
) -> Tuple[List[JsonMapping], MultiWriterStreamToken]:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""Returns a set of new read receipt events that an appservice
|
2020-10-15 12:33:28 -04:00
|
|
|
may be interested in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
from_key: the stream position at which events should be fetched from
|
2022-04-26 15:14:21 -04:00
|
|
|
to_key: the stream position up to which events should be fetched to
|
2020-10-15 12:33:28 -04:00
|
|
|
service: The appservice which may be interested
|
2021-10-21 12:42:25 -04:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A two-tuple containing the following:
|
|
|
|
* A list of json dictionaries derived from read receipts that the
|
|
|
|
appservice may be interested in.
|
|
|
|
* The current read receipt stream token.
|
2020-10-15 12:33:28 -04:00
|
|
|
"""
|
|
|
|
if from_key == to_key:
|
|
|
|
return [], to_key
|
|
|
|
|
2020-11-18 13:54:09 -05:00
|
|
|
# Fetch all read receipts for all rooms, up to a limit of 100. This is ordered
|
|
|
|
# by most recent.
|
2020-10-15 12:33:28 -04:00
|
|
|
rooms_to_events = await self.store.get_linearized_receipts_for_all_rooms(
|
|
|
|
from_key=from_key, to_key=to_key
|
|
|
|
)
|
|
|
|
|
|
|
|
# Then filter down to rooms that the AS can read
|
|
|
|
events = []
|
|
|
|
for room_id, event in rooms_to_events.items():
|
2022-03-03 13:14:09 -05:00
|
|
|
if not await service.is_interested_in_room(room_id, self.store):
|
2020-10-15 12:33:28 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
events.append(event)
|
|
|
|
|
2021-09-23 06:59:07 -04:00
|
|
|
return events, to_key
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2023-10-25 11:16:19 -04:00
|
|
|
def get_current_key(self) -> MultiWriterStreamToken:
|
2015-07-07 10:25:30 -04:00
|
|
|
return self.store.get_max_receipt_stream_id()
|