2015-07-01 10:19:31 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
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
|
2015-07-01 10:19:31 -06:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-08-26 07:54:30 -06:00
|
|
|
from synapse.types import get_domain_from_id
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.util import logcontext
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from ._base import BaseHandler
|
2015-07-01 10:19:31 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ReceiptsHandler(BaseHandler):
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(ReceiptsHandler, self).__init__(hs)
|
|
|
|
|
2016-05-16 12:48:07 -06:00
|
|
|
self.server_name = hs.config.server_name
|
|
|
|
self.store = hs.get_datastore()
|
2015-07-07 03:55:31 -06:00
|
|
|
self.hs = hs
|
2016-11-16 07:28:03 -07:00
|
|
|
self.federation = hs.get_federation_sender()
|
2018-03-12 10:17:08 -06:00
|
|
|
hs.get_federation_registry().register_edu_handler(
|
2015-07-01 10:19:31 -06:00
|
|
|
"m.receipt", self._received_remote_receipt
|
|
|
|
)
|
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
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def received_client_receipt(self, room_id, receipt_type, user_id,
|
|
|
|
event_id):
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Called when a client tells us a local user has read up to the given
|
|
|
|
event_id in the room.
|
|
|
|
"""
|
2015-07-01 10:19:31 -06:00
|
|
|
receipt = {
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
|
|
|
"user_id": user_id,
|
|
|
|
"event_ids": [event_id],
|
2015-07-09 04:39:30 -06:00
|
|
|
"data": {
|
2015-07-09 06:15:34 -06:00
|
|
|
"ts": int(self.clock.time_msec()),
|
2015-07-09 04:39:30 -06:00
|
|
|
}
|
2015-07-01 10:19:31 -06:00
|
|
|
}
|
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
is_new = yield self._handle_new_receipts([receipt])
|
|
|
|
|
|
|
|
if is_new:
|
2017-10-11 07:37:20 -06:00
|
|
|
# fire off a process in the background to send the receipt to
|
|
|
|
# remote servers
|
2015-07-07 08:25:30 -06:00
|
|
|
self._push_remotes([receipt])
|
2015-07-01 10:19:31 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _received_remote_receipt(self, origin, content):
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Called when we receive an EDU of type m.receipt from a remote HS.
|
|
|
|
"""
|
2015-07-01 10:19:31 -06:00
|
|
|
receipts = [
|
|
|
|
{
|
|
|
|
"room_id": room_id,
|
|
|
|
"receipt_type": receipt_type,
|
|
|
|
"user_id": user_id,
|
2015-07-09 04:39:30 -06:00
|
|
|
"event_ids": user_values["event_ids"],
|
|
|
|
"data": user_values.get("data", {}),
|
2015-07-01 10:19:31 -06:00
|
|
|
}
|
|
|
|
for room_id, room_values in content.items()
|
2015-07-09 04:39:30 -06:00
|
|
|
for receipt_type, users in room_values.items()
|
|
|
|
for user_id, user_values in users.items()
|
2015-07-01 10:19:31 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
yield self._handle_new_receipts(receipts)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _handle_new_receipts(self, receipts):
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Takes a list of receipts, stores them and informs the notifier.
|
|
|
|
"""
|
2016-04-07 08:39:53 -06:00
|
|
|
min_batch_id = None
|
|
|
|
max_batch_id = None
|
|
|
|
|
2015-07-01 10:19:31 -06:00
|
|
|
for receipt in receipts:
|
|
|
|
room_id = receipt["room_id"]
|
|
|
|
receipt_type = receipt["receipt_type"]
|
|
|
|
user_id = receipt["user_id"]
|
|
|
|
event_ids = receipt["event_ids"]
|
2015-07-09 04:39:30 -06:00
|
|
|
data = receipt["data"]
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
res = yield self.store.insert_receipt(
|
2015-07-09 04:39:30 -06:00
|
|
|
room_id, receipt_type, user_id, event_ids, 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
|
|
|
|
|
2016-12-08 05:13:01 -07:00
|
|
|
if min_batch_id is None:
|
|
|
|
# no new receipts
|
|
|
|
defer.returnValue(False)
|
|
|
|
|
2016-04-07 08:39:53 -06:00
|
|
|
affected_room_ids = list(set([r["room_id"] for r in receipts]))
|
|
|
|
|
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.
|
2018-10-22 09:12:11 -06:00
|
|
|
yield self.hs.get_pusherpool().on_new_receipts(
|
2018-08-16 17:32:39 -06:00
|
|
|
min_batch_id, max_batch_id, affected_room_ids,
|
|
|
|
)
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2018-08-16 17:32:39 -06:00
|
|
|
defer.returnValue(True)
|
2015-07-01 10:19:31 -06:00
|
|
|
|
2017-10-11 07:37:20 -06:00
|
|
|
@logcontext.preserve_fn # caller should not yield on this
|
2015-07-07 08:25:30 -06:00
|
|
|
@defer.inlineCallbacks
|
2015-07-01 10:19:31 -06:00
|
|
|
def _push_remotes(self, receipts):
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Given a list of receipts, works out which remote servers should be
|
|
|
|
poked and pokes them.
|
|
|
|
"""
|
2018-04-27 04:07:40 -06:00
|
|
|
try:
|
|
|
|
# TODO: Some of this stuff should be coallesced.
|
|
|
|
for receipt in receipts:
|
|
|
|
room_id = receipt["room_id"]
|
|
|
|
receipt_type = receipt["receipt_type"]
|
|
|
|
user_id = receipt["user_id"]
|
|
|
|
event_ids = receipt["event_ids"]
|
|
|
|
data = receipt["data"]
|
|
|
|
|
|
|
|
users = yield self.state.get_current_user_in_room(room_id)
|
|
|
|
remotedomains = set(get_domain_from_id(u) for u in users)
|
|
|
|
remotedomains = remotedomains.copy()
|
|
|
|
remotedomains.discard(self.server_name)
|
|
|
|
|
|
|
|
logger.debug("Sending receipt to: %r", remotedomains)
|
|
|
|
|
|
|
|
for domain in remotedomains:
|
|
|
|
self.federation.send_edu(
|
|
|
|
destination=domain,
|
|
|
|
edu_type="m.receipt",
|
|
|
|
content={
|
|
|
|
room_id: {
|
|
|
|
receipt_type: {
|
|
|
|
user_id: {
|
|
|
|
"event_ids": event_ids,
|
|
|
|
"data": data,
|
|
|
|
}
|
2015-07-09 04:39:30 -06:00
|
|
|
}
|
2018-04-27 04:07:40 -06:00
|
|
|
},
|
2015-07-01 10:19:31 -06:00
|
|
|
},
|
2018-04-27 04:07:40 -06:00
|
|
|
key=(room_id, receipt_type, user_id),
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Error pushing receipts to remote servers")
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2015-07-08 03:54:01 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_receipts_for_room(self, room_id, to_key):
|
2015-07-13 06:30:43 -06:00
|
|
|
"""Gets all receipts for a room, upto the given key.
|
|
|
|
"""
|
2015-07-08 03:54:01 -06:00
|
|
|
result = yield self.store.get_linearized_receipts_for_room(
|
2015-07-14 03:19:07 -06:00
|
|
|
room_id,
|
|
|
|
to_key=to_key,
|
2015-07-08 03:54:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if not result:
|
|
|
|
defer.returnValue([])
|
|
|
|
|
2015-10-22 09:17:12 -06:00
|
|
|
defer.returnValue(result)
|
2015-07-08 03:54:01 -06:00
|
|
|
|
2015-07-07 08:25:30 -06:00
|
|
|
|
|
|
|
class ReceiptEventSource(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2015-11-05 07:32:26 -07:00
|
|
|
def get_new_events(self, from_key, room_ids, **kwargs):
|
2015-07-07 08:25:30 -06:00
|
|
|
from_key = int(from_key)
|
|
|
|
to_key = yield self.get_current_key()
|
|
|
|
|
2015-07-18 12:07:12 -06:00
|
|
|
if from_key == to_key:
|
|
|
|
defer.returnValue(([], to_key))
|
|
|
|
|
2015-07-08 08:35:00 -06:00
|
|
|
events = yield self.store.get_linearized_receipts_for_rooms(
|
2015-11-05 07:32:26 -07:00
|
|
|
room_ids,
|
2015-07-14 03:19:07 -06:00
|
|
|
from_key=from_key,
|
|
|
|
to_key=to_key,
|
2015-07-08 08:35:00 -06:00
|
|
|
)
|
2015-07-07 08:25:30 -06:00
|
|
|
|
2015-07-08 04:36:05 -06:00
|
|
|
defer.returnValue((events, to_key))
|
2015-07-07 08:25:30 -06:00
|
|
|
|
|
|
|
def get_current_key(self, direction='f'):
|
|
|
|
return self.store.get_max_receipt_stream_id()
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_pagination_rows(self, user, config, key):
|
2015-07-08 03:54:01 -06:00
|
|
|
to_key = int(config.from_key)
|
|
|
|
|
|
|
|
if config.to_key:
|
|
|
|
from_key = int(config.to_key)
|
|
|
|
else:
|
|
|
|
from_key = None
|
|
|
|
|
2017-03-16 05:51:46 -06:00
|
|
|
room_ids = yield self.store.get_rooms_for_user(user.to_string())
|
2015-07-08 08:35:00 -06:00
|
|
|
events = yield self.store.get_linearized_receipts_for_rooms(
|
2017-03-16 05:51:46 -06:00
|
|
|
room_ids,
|
2015-07-14 03:19:07 -06:00
|
|
|
from_key=from_key,
|
|
|
|
to_key=to_key,
|
2015-07-08 08:35:00 -06:00
|
|
|
)
|
2015-07-08 04:36:05 -06:00
|
|
|
|
|
|
|
defer.returnValue((events, to_key))
|