forked-synapse/synapse/handlers/receipts.py

213 lines
6.5 KiB
Python
Raw Normal View History

2015-07-01 12:19:31 -04:00
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket Ltd
#
# 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.
from ._base import BaseHandler
from twisted.internet import defer
from synapse.util.logcontext import PreserveLoggingContext
import logging
logger = logging.getLogger(__name__)
class ReceiptsHandler(BaseHandler):
def __init__(self, hs):
super(ReceiptsHandler, self).__init__(hs)
2015-07-07 05:55:31 -04:00
self.hs = hs
self.federation = hs.get_replication_layer()
2015-07-01 12:19:31 -04:00
self.federation.register_edu_handler(
"m.receipt", self._received_remote_receipt
)
self.clock = self.hs.get_clock()
2015-07-01 12:19:31 -04:00
self._receipt_cache = None
2015-07-01 12:19:31 -04:00
@defer.inlineCallbacks
def received_client_receipt(self, room_id, receipt_type, user_id,
event_id):
2015-07-13 08:30:43 -04:00
"""Called when a client tells us a local user has read up to the given
event_id in the room.
"""
2015-07-01 12:19:31 -04:00
receipt = {
"room_id": room_id,
"receipt_type": receipt_type,
"user_id": user_id,
"event_ids": [event_id],
"data": {
2015-07-09 08:15:34 -04:00
"ts": int(self.clock.time_msec()),
}
2015-07-01 12:19:31 -04:00
}
is_new = yield self._handle_new_receipts([receipt])
if is_new:
self._push_remotes([receipt])
2015-07-01 12:19:31 -04:00
@defer.inlineCallbacks
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.
"""
2015-07-01 12:19:31 -04:00
receipts = [
{
"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
}
for room_id, room_values in content.items()
for receipt_type, users in room_values.items()
for user_id, user_values in users.items()
2015-07-01 12:19:31 -04:00
]
yield self._handle_new_receipts(receipts)
@defer.inlineCallbacks
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.
"""
2015-07-01 12:19:31 -04: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"]
data = receipt["data"]
2015-07-01 12:19:31 -04:00
res = yield self.store.insert_receipt(
room_id, receipt_type, user_id, event_ids, data
2015-07-01 12:19:31 -04:00
)
if not res:
# res will be None if this read receipt is 'old'
defer.returnValue(False)
2015-07-01 12:19:31 -04:00
stream_id, max_persisted_id = res
2015-07-01 12:19:31 -04:00
with PreserveLoggingContext():
2015-07-02 06:40:56 -04:00
self.notifier.on_new_event(
"receipt_key", max_persisted_id, rooms=[room_id]
2015-07-01 12:19:31 -04:00
)
defer.returnValue(True)
2015-07-01 12:19:31 -04:00
@defer.inlineCallbacks
2015-07-01 12:19:31 -04:00
def _push_remotes(self, receipts):
2015-07-13 08:30:43 -04:00
"""Given a list of receipts, works out which remote servers should be
poked and pokes them.
"""
2015-07-01 12:19:31 -04:00
# 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"]
remotedomains = set()
rm_handler = self.hs.get_handlers().room_member_handler
yield rm_handler.fetch_room_distributions_into(
room_id, localusers=None, remotedomains=remotedomains
)
logger.debug("Sending receipt to: %r", remotedomains)
2015-07-01 12:19:31 -04:00
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-01 12:19:31 -04:00
}
},
},
)
2015-07-08 05:54:01 -04:00
@defer.inlineCallbacks
def get_receipts_for_room(self, room_id, to_key):
2015-07-13 08:30:43 -04:00
"""Gets all receipts for a room, upto the given key.
"""
2015-07-08 05:54:01 -04:00
result = yield self.store.get_linearized_receipts_for_room(
2015-07-14 05:19:07 -04:00
room_id,
to_key=to_key,
2015-07-08 05:54:01 -04:00
)
if not result:
defer.returnValue([])
event = {
"type": "m.receipt",
"room_id": room_id,
"content": result,
2015-07-08 05:54:01 -04:00
}
defer.returnValue([event])
class ReceiptEventSource(object):
def __init__(self, hs):
self.store = hs.get_datastore()
@defer.inlineCallbacks
def get_new_events_for_user(self, user, from_key, limit):
2015-07-22 11:13:46 -04:00
defer.returnValue(([], from_key))
from_key = int(from_key)
to_key = yield self.get_current_key()
if from_key == to_key:
defer.returnValue(([], to_key))
rooms = yield self.store.get_rooms_for_user(user.to_string())
rooms = [room.room_id for room in rooms]
events = yield self.store.get_linearized_receipts_for_rooms(
2015-07-14 05:19:07 -04:00
rooms,
from_key=from_key,
to_key=to_key,
)
defer.returnValue((events, to_key))
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 05:54:01 -04:00
to_key = int(config.from_key)
2015-07-22 11:13:46 -04:00
defer.returnValue(([], to_key))
2015-07-08 05:54:01 -04:00
if config.to_key:
from_key = int(config.to_key)
else:
from_key = None
rooms = yield self.store.get_rooms_for_user(user.to_string())
rooms = [room.room_id for room in rooms]
events = yield self.store.get_linearized_receipts_for_rooms(
2015-07-14 05:19:07 -04:00
rooms,
from_key=from_key,
to_key=to_key,
)
defer.returnValue((events, to_key))