anonymousland-synapse/synapse/handlers/receipts.py

176 lines
5.9 KiB
Python
Raw Normal View History

2015-07-01 12:19:31 -04:00
# -*- coding: utf-8 -*-
2016-01-06 23:26:29 -05:00
# Copyright 2015, 2016 OpenMarket Ltd
2015-07-01 12:19:31 -04: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 02:09:20 -04:00
import logging
from typing import List, Tuple
2015-07-01 12:19:31 -04:00
from synapse.appservice import ApplicationService
from synapse.handlers._base import BaseHandler
from synapse.types import JsonDict, ReadReceipt, get_domain_from_id
2015-07-01 12:19:31 -04:00
logger = logging.getLogger(__name__)
class ReceiptsHandler(BaseHandler):
def __init__(self, hs):
super().__init__(hs)
2015-07-01 12:19:31 -04:00
self.server_name = hs.config.server_name
self.store = hs.get_datastore()
2015-07-07 05:55:31 -04:00
self.hs = hs
2016-11-16 09:28:03 -05:00
self.federation = hs.get_federation_sender()
hs.get_federation_registry().register_edu_handler(
2015-07-01 12:19:31 -04:00
"m.receipt", self._received_remote_receipt
)
self.clock = self.hs.get_clock()
self.state = hs.get_state_handler()
2015-07-01 12:19:31 -04:00
async def _received_remote_receipt(self, origin, content):
2015-07-13 08:30:43 -04:00
"""Called when we receive an EDU of type m.receipt from a remote HS.
"""
receipts = []
for room_id, room_values in content.items():
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 12:19:31 -04:00
await self._handle_new_receipts(receipts)
2015-07-01 12:19:31 -04:00
async def _handle_new_receipts(self, receipts):
2015-07-13 08:30:43 -04:00
"""Takes a list of receipts, stores them and informs the notifier.
"""
min_batch_id = None
max_batch_id = None
2015-07-01 12:19:31 -04:00
for receipt in receipts:
res = await self.store.insert_receipt(
receipt.room_id,
receipt.receipt_type,
receipt.user_id,
receipt.event_ids,
receipt.data,
2015-07-01 12:19:31 -04:00
)
if not res:
# res will be None if this read receipt is 'old'
continue
2015-07-01 12:19:31 -04:00
stream_id, max_persisted_id = res
2015-07-01 12:19:31 -04: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
if min_batch_id is None:
# no new receipts
return False
affected_room_ids = list({r.room_id for r in receipts})
2019-06-20 05:32:02 -04: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.
await self.hs.get_pusherpool().on_new_receipts(
min_batch_id, max_batch_id, affected_room_ids
)
2015-07-01 12:19:31 -04:00
return True
2015-07-01 12:19:31 -04:00
async def received_client_receipt(self, room_id, receipt_type, user_id, event_id):
"""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
"""
receipt = ReadReceipt(
room_id=room_id,
receipt_type=receipt_type,
user_id=user_id,
event_ids=[event_id],
2019-06-20 05:32:02 -04:00
data={"ts": int(self.clock.time_msec())},
)
is_new = await self._handle_new_receipts([receipt])
if not is_new:
return
await self.federation.send_read_receipt(receipt)
2015-07-08 05:54:01 -04:00
2020-09-04 06:54:56 -04:00
class ReceiptEventSource:
def __init__(self, hs):
self.store = hs.get_datastore()
async def get_new_events(self, from_key, room_ids, **kwargs):
from_key = int(from_key)
to_key = self.get_current_key()
if from_key == to_key:
return [], to_key
events = await self.store.get_linearized_receipts_for_rooms(
2019-06-20 05:32:02 -04:00
room_ids, from_key=from_key, to_key=to_key
)
return (events, to_key)
async def get_new_events_as(
self, from_key: int, service: ApplicationService
) -> Tuple[List[JsonDict], int]:
"""Returns a set of new receipt events that an appservice
may be interested in.
Args:
from_key: the stream position at which events should be fetched from
service: The appservice which may be interested
"""
from_key = int(from_key)
to_key = self.get_current_key()
if from_key == to_key:
return [], to_key
# Fetch all read receipts for all rooms, up to a limit of 100. This is ordered
# by most recent.
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)
return (events, to_key)
2019-06-20 05:32:02 -04:00
def get_current_key(self, direction="f"):
return self.store.get_max_receipt_stream_id()