forked-synapse/synapse/handlers/sync.py

148 lines
4.8 KiB
Python
Raw Normal View History

# -*- 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 synapse.streams.config import PaginationConfig
from synapse.api.constants import Membership
from twisted.internet import defer
2015-01-26 15:46:31 +00:00
import collections
import logging
logger = logging.getLogger(__name__)
2015-01-26 15:46:31 +00:00
SyncConfig = collections.namedtuple("SyncConfig", [
"user",
"device",
"limit",
"gap",
"sort",
"backfill",
2015-01-26 15:46:31 +00:00
"filter",
])
2015-01-26 15:46:31 +00:00
RoomSyncResult = collections.namedtuple("RoomSyncResult", [
"room_id",
"limited",
"published",
"events", # dict of event
2015-01-26 15:46:31 +00:00
"state",
"prev_batch",
2015-01-26 15:46:31 +00:00
])
class SyncResult(collections.namedtuple("SyncResult", [
"next_batch", # Token for the next sync
"private_user_data", # List of private events for the user.
"public_user_data", # List of public events for all users.
"rooms", # RoomSyncResult for each room.
])):
__slots__ = []
def __nonzero__(self):
return self.private_user_data or self.public_user_data or self.rooms
class SyncHandler(BaseHandler):
def __init__(self, hs):
super(SyncHandler, self).__init__(hs)
self.event_sources = hs.get_event_sources()
self.clock = hs.get_clock()
2015-01-26 15:46:31 +00:00
def wait_for_sync_for_user(self, sync_config, since_token=None, timeout=0):
if timeout == 0:
return self.current_sync_for_user(sync_config, since_token)
2015-01-26 15:46:31 +00:00
else:
def current_sync_callback(since_token):
return self.current_sync_for_user(
self, since_token, sync_config
)
return self.notifier.wait_for_events(
sync_config.filter, since_token, current_sync_callback
)
def current_sync_for_user(self, sync_config, since_token=None):
if since_token is None:
return self.initial_sync(sync_config)
2015-01-26 15:46:31 +00:00
else:
return self.incremental_sync(sync_config)
@defer.inlineCallbacks
def initial_sync(self, sync_config):
if sync_config.sort == "timeline,desc":
# TODO(mjark): Handle going through events in reverse order?.
# What does "most recent events" mean when applying the limits mean
# in this case?
raise NotImplementedError()
2015-01-26 15:46:31 +00:00
now_token = yield self.event_sources.get_current_token()
presence_stream = self.event_sources.sources["presence"]
# TODO (mjark): This looks wrong, shouldn't we be getting the presence
2015-01-26 15:46:31 +00:00
# UP to the present rather than after the present?
pagination_config = PaginationConfig(from_token=now_token)
presence, _ = yield presence_stream.get_pagination_rows(
user=sync_config.user,
pagination_config=pagination_config.get_source_config("presence"),
key=None
2015-01-26 15:46:31 +00:00
)
room_list = yield self.store.get_rooms_for_user_where_membership_is(
user_id=sync_config.user.to_string(),
2015-01-26 15:46:31 +00:00
membership_list=[Membership.INVITE, Membership.JOIN]
)
# TODO (mjark): Does public mean "published"?
2015-01-26 15:46:31 +00:00
published_rooms = yield self.store.get_rooms(is_public=True)
published_room_ids = set(r["room_id"] for r in published_rooms)
2015-01-26 15:46:31 +00:00
rooms = []
2015-01-26 15:46:31 +00:00
for event in room_list:
#TODO (mjark): Apply the event filter in sync_config.
recent_events, token = yield self.store.get_recent_events_for_room(
2015-01-26 15:46:31 +00:00
event.room_id,
limit=sync_config.limit,
end_token=now_token.room_key,
)
prev_batch_token = now_token.copy_and_replace("room_key", token[0])
current_state_events = yield self.state_handler.get_current_state(
2015-01-26 15:46:31 +00:00
event.room_id
)
rooms.append(RoomSyncResult(
room_id=event.room_id,
published=event.room_id in published_room_ids,
events=recent_events,
prev_batch=prev_batch_token,
state=current_state_events,
limited=True,
))
2015-01-26 15:46:31 +00:00
defer.returnValue(SyncResult(
public_user_data=presence,
private_user_data=[],
rooms=rooms,
next_batch=now_token,
))
2015-01-26 15:46:31 +00:00
@defer.inlineCallbacks
def incremental_sync(self, sync_config):
pass