2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-01 10:19:31 -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-07-09 00:09:20 -06:00
|
|
|
import logging
|
2021-09-21 11:34:26 -06:00
|
|
|
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2021-12-08 10:26:29 -07:00
|
|
|
from synapse.api.constants import ReadReceiptEventFields, ReceiptTypes
|
2020-10-15 10:33:28 -06:00
|
|
|
from synapse.appservice import ApplicationService
|
2021-09-21 11:34:26 -06:00
|
|
|
from synapse.streams import EventSource
|
2021-07-28 02:05:11 -06:00
|
|
|
from synapse.types import JsonDict, ReadReceipt, UserID, get_domain_from_id
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2021-01-04 08:05:12 -07:00
|
|
|
|
2015-07-01 10:19:31 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-08 05:44:43 -06:00
|
|
|
class ReceiptsHandler:
|
2021-01-04 08:05:12 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-10-08 05:44:43 -06:00
|
|
|
self.notifier = hs.get_notifier()
|
2021-09-13 11:07:12 -06:00
|
|
|
self.server_name = hs.config.server.server_name
|
2016-05-16 12:48:07 -06:00
|
|
|
self.store = hs.get_datastore()
|
2021-07-06 07:31:13 -06:00
|
|
|
self.event_auth_handler = hs.get_event_auth_handler()
|
|
|
|
|
2015-07-07 03:55:31 -06:00
|
|
|
self.hs = hs
|
2021-01-18 08:47:59 -07: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(
|
|
|
|
"m.receipt", self._received_remote_receipt
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
hs.get_federation_registry().register_instances_for_edu(
|
|
|
|
"m.receipt",
|
|
|
|
hs.config.worker.writers.receipts,
|
|
|
|
)
|
|
|
|
|
2015-07-09 04:39:30 -06:00
|
|
|
self.clock = self.hs.get_clock()
|
2016-08-26 07:54:30 -06:00
|
|
|
self.state = hs.get_state_handler()
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
async def _received_remote_receipt(self, origin: str, content: JsonDict) -> None:
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Called when we receive an EDU of type m.receipt from a remote HS."""
|
2019-07-23 06:31:03 -06:00
|
|
|
receipts = []
|
|
|
|
for room_id, room_values in content.items():
|
2021-07-06 07:31:13 -06: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).
|
|
|
|
is_in_room = await self.event_auth_handler.check_host_in_room(
|
|
|
|
room_id, self.server_name
|
|
|
|
)
|
|
|
|
if not is_in_room:
|
|
|
|
logger.info(
|
2021-08-03 07:35:49 -06:00
|
|
|
"Ignoring receipt for room %r from server %s as we're not in the room",
|
|
|
|
room_id,
|
2021-07-06 07:31:13 -06:00
|
|
|
origin,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2019-07-23 06:31:03 -06: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
|
|
|
|
|
|
|
|
receipts.append(
|
|
|
|
ReadReceipt(
|
|
|
|
room_id=room_id,
|
|
|
|
receipt_type=receipt_type,
|
|
|
|
user_id=user_id,
|
|
|
|
event_ids=user_values["event_ids"],
|
|
|
|
data=user_values.get("data", {}),
|
|
|
|
)
|
|
|
|
)
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2019-10-29 09:08:22 -06:00
|
|
|
await self._handle_new_receipts(receipts)
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Takes a list of receipts, stores them and informs the notifier."""
|
2021-07-16 11:22:36 -06:00
|
|
|
min_batch_id: Optional[int] = None
|
|
|
|
max_batch_id: Optional[int] = None
|
2016-04-07 08:39:53 -06:00
|
|
|
|
2015-07-01 10:19:31 -06:00
|
|
|
for receipt in receipts:
|
2019-10-29 09:08:22 -06:00
|
|
|
res = await self.store.insert_receipt(
|
2019-03-12 10:50:58 -06:00
|
|
|
receipt.room_id,
|
|
|
|
receipt.receipt_type,
|
|
|
|
receipt.user_id,
|
|
|
|
receipt.event_ids,
|
|
|
|
receipt.data,
|
2015-07-01 10:19:31 -06:00
|
|
|
)
|
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
if not res:
|
|
|
|
# res will be None if this read receipt is 'old'
|
2016-12-08 05:13:01 -07:00
|
|
|
continue
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
stream_id, max_persisted_id = res
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2016-04-07 08:39:53 -06:00
|
|
|
if min_batch_id is None or stream_id < min_batch_id:
|
|
|
|
min_batch_id = stream_id
|
|
|
|
if max_batch_id is None or max_persisted_id > max_batch_id:
|
|
|
|
max_batch_id = max_persisted_id
|
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
# Either both of these should be None or neither.
|
|
|
|
if min_batch_id is None or max_batch_id is None:
|
2016-12-08 05:13:01 -07:00
|
|
|
# no new receipts
|
2019-07-23 07:00:55 -06:00
|
|
|
return False
|
2016-12-08 05:13:01 -07:00
|
|
|
|
2020-02-21 05:15:07 -07:00
|
|
|
affected_room_ids = list({r.room_id for r in receipts})
|
2016-04-07 08:39:53 -06:00
|
|
|
|
2018-08-16 17:32:39 -06:00
|
|
|
self.notifier.on_new_event("receipt_key", max_batch_id, rooms=affected_room_ids)
|
|
|
|
# Note that the min here shouldn't be relied upon to be accurate.
|
2020-12-11 12:05:15 -07:00
|
|
|
await self.hs.get_pusherpool().on_new_receipts(
|
|
|
|
min_batch_id, max_batch_id, affected_room_ids
|
2018-08-16 17:32:39 -06:00
|
|
|
)
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return True
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
async def received_client_receipt(
|
2021-07-28 02:05:11 -06:00
|
|
|
self, room_id: str, receipt_type: str, user_id: str, event_id: str, hidden: bool
|
2021-01-04 08:05:12 -07:00
|
|
|
) -> None:
|
2019-03-04 11:11:26 -07:00
|
|
|
"""Called when a client tells us a local user has read up to the given
|
|
|
|
event_id in the room.
|
2015-07-13 06:30:43 -06:00
|
|
|
"""
|
2019-03-12 10:50:58 -06:00
|
|
|
receipt = ReadReceipt(
|
|
|
|
room_id=room_id,
|
|
|
|
receipt_type=receipt_type,
|
|
|
|
user_id=user_id,
|
|
|
|
event_ids=[event_id],
|
2021-07-28 02:05:11 -06:00
|
|
|
data={"ts": int(self.clock.time_msec()), "hidden": hidden},
|
2019-03-12 10:50:58 -06:00
|
|
|
)
|
2019-02-21 10:50:30 -07:00
|
|
|
|
2019-10-29 09:08:22 -06:00
|
|
|
is_new = await self._handle_new_receipts([receipt])
|
2019-03-04 11:11:26 -07:00
|
|
|
if not is_new:
|
|
|
|
return
|
|
|
|
|
2021-07-28 02:05:11 -06:00
|
|
|
if self.federation_sender and not (
|
|
|
|
self.hs.config.experimental.msc2285_enabled and hidden
|
|
|
|
):
|
2021-01-18 08:47:59 -07:00
|
|
|
await self.federation_sender.send_read_receipt(receipt)
|
2015-07-08 03:54:01 -06:00
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2021-09-21 11:34:26 -06:00
|
|
|
class ReceiptEventSource(EventSource[int, JsonDict]):
|
2021-01-04 08:05:12 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2015-07-07 08:25:30 -06:00
|
|
|
self.store = hs.get_datastore()
|
2021-07-28 02:05:11 -06:00
|
|
|
self.config = hs.config
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def filter_out_hidden(events: List[JsonDict], user_id: str) -> List[JsonDict]:
|
|
|
|
visible_events = []
|
|
|
|
|
|
|
|
# filter out hidden receipts the user shouldn't see
|
|
|
|
for event in events:
|
|
|
|
content = event.get("content", {})
|
|
|
|
new_event = event.copy()
|
|
|
|
new_event["content"] = {}
|
|
|
|
|
|
|
|
for event_id in content.keys():
|
|
|
|
event_content = content.get(event_id, {})
|
2021-12-08 10:26:29 -07:00
|
|
|
m_read = event_content.get(ReceiptTypes.READ, {})
|
2021-07-28 02:05:11 -06:00
|
|
|
|
|
|
|
# If m_read is missing copy over the original event_content as there is nothing to process here
|
|
|
|
if not m_read:
|
|
|
|
new_event["content"][event_id] = event_content.copy()
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_users = {}
|
|
|
|
for rr_user_id, user_rr in m_read.items():
|
2021-08-16 05:22:38 -06:00
|
|
|
try:
|
|
|
|
hidden = user_rr.get("hidden")
|
|
|
|
except AttributeError:
|
|
|
|
# Due to https://github.com/matrix-org/synapse/issues/10376
|
|
|
|
# there are cases where user_rr is a string, in those cases
|
|
|
|
# we just ignore the read receipt
|
|
|
|
continue
|
|
|
|
|
2021-07-28 02:05:11 -06:00
|
|
|
if hidden is not True or rr_user_id == user_id:
|
|
|
|
new_users[rr_user_id] = user_rr.copy()
|
|
|
|
# If hidden has a value replace hidden with the correct prefixed key
|
|
|
|
if hidden is not None:
|
|
|
|
new_users[rr_user_id].pop("hidden")
|
|
|
|
new_users[rr_user_id][
|
|
|
|
ReadReceiptEventFields.MSC2285_HIDDEN
|
|
|
|
] = hidden
|
|
|
|
|
|
|
|
# Set new users unless empty
|
|
|
|
if len(new_users.keys()) > 0:
|
2021-12-08 10:26:29 -07:00
|
|
|
new_event["content"][event_id] = {ReceiptTypes.READ: new_users}
|
2021-07-28 02:05:11 -06:00
|
|
|
|
|
|
|
# Append new_event to visible_events unless empty
|
|
|
|
if len(new_event["content"].keys()) > 0:
|
|
|
|
visible_events.append(new_event)
|
|
|
|
|
|
|
|
return visible_events
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
async def get_new_events(
|
2021-09-21 11:34:26 -06:00
|
|
|
self,
|
|
|
|
user: UserID,
|
|
|
|
from_key: int,
|
|
|
|
limit: Optional[int],
|
|
|
|
room_ids: Iterable[str],
|
|
|
|
is_guest: bool,
|
|
|
|
explicit_room_id: Optional[str] = None,
|
2021-01-04 08:05:12 -07:00
|
|
|
) -> Tuple[List[JsonDict], int]:
|
2015-07-07 08:25:30 -06:00
|
|
|
from_key = int(from_key)
|
2020-07-17 05:08:30 -06:00
|
|
|
to_key = self.get_current_key()
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2015-07-18 12:07:12 -06:00
|
|
|
if from_key == to_key:
|
2019-08-30 09:28:26 -06:00
|
|
|
return [], to_key
|
2015-07-18 12:07:12 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
events = await self.store.get_linearized_receipts_for_rooms(
|
2015-07-14 03:19:07 -06:00
|
|
|
room_ids, from_key=from_key, to_key=to_key
|
2015-07-08 08:35:00 -06:00
|
|
|
)
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2021-07-28 02:05:11 -06:00
|
|
|
if self.config.experimental.msc2285_enabled:
|
|
|
|
events = ReceiptEventSource.filter_out_hidden(events, user.to_string())
|
|
|
|
|
2021-09-23 04:59:07 -06:00
|
|
|
return events, to_key
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2020-10-15 10:33:28 -06:00
|
|
|
async def get_new_events_as(
|
|
|
|
self, from_key: int, service: ApplicationService
|
|
|
|
) -> Tuple[List[JsonDict], int]:
|
2021-10-21 10:42:25 -06:00
|
|
|
"""Returns a set of new read receipt events that an appservice
|
2020-10-15 10:33:28 -06:00
|
|
|
may be interested in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
from_key: the stream position at which events should be fetched from
|
|
|
|
service: The appservice which may be interested
|
2021-10-21 10:42:25 -06: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 10:33:28 -06:00
|
|
|
"""
|
|
|
|
from_key = int(from_key)
|
|
|
|
to_key = self.get_current_key()
|
|
|
|
|
|
|
|
if from_key == to_key:
|
|
|
|
return [], to_key
|
|
|
|
|
2020-11-18 11:54:09 -07:00
|
|
|
# Fetch all read receipts for all rooms, up to a limit of 100. This is ordered
|
|
|
|
# by most recent.
|
2020-10-15 10:33:28 -06: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():
|
|
|
|
if not await service.matches_user_in_member_list(room_id, self.store):
|
|
|
|
continue
|
|
|
|
|
|
|
|
events.append(event)
|
|
|
|
|
2021-09-23 04:59:07 -06:00
|
|
|
return events, to_key
|
2020-10-15 10:33:28 -06:00
|
|
|
|
2021-01-04 08:05:12 -07:00
|
|
|
def get_current_key(self, direction: str = "f") -> int:
|
2015-07-07 08:25:30 -06:00
|
|
|
return self.store.get_max_receipt_stream_id()
|