2016-09-14 07:07:37 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2014 - 2016 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 twisted.internet import defer
|
|
|
|
|
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
from synapse.api.constants import (
|
2016-09-14 10:17:07 -06:00
|
|
|
EventTypes, JoinRules,
|
2016-09-14 07:07:37 -06:00
|
|
|
)
|
|
|
|
from synapse.util.async import concurrently_execute
|
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
|
|
|
|
2016-09-15 02:08:57 -06:00
|
|
|
from collections import namedtuple
|
|
|
|
from unpaddedbase64 import encode_base64, decode_base64
|
|
|
|
|
2016-09-14 07:07:37 -06:00
|
|
|
import logging
|
2016-09-15 02:08:57 -06:00
|
|
|
import msgpack
|
2016-09-14 07:07:37 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
REMOTE_ROOM_LIST_POLL_INTERVAL = 60 * 1000
|
|
|
|
|
|
|
|
|
|
|
|
class RoomListHandler(BaseHandler):
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(RoomListHandler, self).__init__(hs)
|
|
|
|
self.response_cache = ResponseCache(hs)
|
2016-09-16 03:31:59 -06:00
|
|
|
self.remote_response_cache = ResponseCache(hs, timeout_ms=30 * 1000)
|
2016-09-14 07:07:37 -06:00
|
|
|
|
2016-09-15 10:35:20 -06:00
|
|
|
def get_local_public_room_list(self, limit=None, since_token=None,
|
|
|
|
search_filter=None):
|
2016-09-16 02:05:11 -06:00
|
|
|
if search_filter:
|
|
|
|
# We explicitly don't bother caching searches.
|
|
|
|
return self._get_public_room_list(limit, since_token, search_filter)
|
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
result = self.response_cache.get((limit, since_token))
|
2016-09-14 07:07:37 -06:00
|
|
|
if not result:
|
2016-09-15 02:08:57 -06:00
|
|
|
result = self.response_cache.set(
|
2016-09-15 03:36:19 -06:00
|
|
|
(limit, since_token),
|
2016-09-16 02:05:11 -06:00
|
|
|
self._get_public_room_list(limit, since_token)
|
2016-09-15 02:08:57 -06:00
|
|
|
)
|
2016-09-14 07:07:37 -06:00
|
|
|
return result
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-09-15 10:35:20 -06:00
|
|
|
def _get_public_room_list(self, limit=None, since_token=None,
|
|
|
|
search_filter=None):
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token and since_token != "END":
|
|
|
|
since_token = RoomListNextBatch.from_token(since_token)
|
2016-09-15 02:08:57 -06:00
|
|
|
else:
|
2016-09-15 03:36:19 -06:00
|
|
|
since_token = None
|
2016-09-15 02:08:57 -06:00
|
|
|
|
2016-09-14 10:28:52 -06:00
|
|
|
rooms_to_order_value = {}
|
|
|
|
rooms_to_num_joined = {}
|
|
|
|
rooms_to_latest_event_ids = {}
|
|
|
|
|
2016-09-15 04:27:04 -06:00
|
|
|
newly_visible = []
|
|
|
|
newly_unpublished = []
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token:
|
2016-09-15 04:27:04 -06:00
|
|
|
stream_token = since_token.stream_ordering
|
|
|
|
current_public_id = yield self.store.get_current_public_room_stream_id()
|
|
|
|
public_room_stream_id = since_token.public_room_stream_id
|
|
|
|
newly_visible, newly_unpublished = yield self.store.get_public_room_changes(
|
|
|
|
public_room_stream_id, current_public_id
|
|
|
|
)
|
2016-09-15 02:08:57 -06:00
|
|
|
else:
|
2016-09-15 04:27:04 -06:00
|
|
|
stream_token = yield self.store.get_room_max_stream_ordering()
|
|
|
|
public_room_stream_id = yield self.store.get_current_public_room_stream_id()
|
|
|
|
|
|
|
|
room_ids = yield self.store.get_public_room_ids_at_stream_id(
|
|
|
|
public_room_stream_id
|
|
|
|
)
|
2016-09-14 10:28:52 -06:00
|
|
|
|
|
|
|
# We want to return rooms in a particular order: the number of joined
|
|
|
|
# users. We then arbitrarily use the room_id as a tie breaker.
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_order_for_room(room_id):
|
|
|
|
latest_event_ids = rooms_to_latest_event_ids.get(room_id, None)
|
|
|
|
if not latest_event_ids:
|
|
|
|
latest_event_ids = yield self.store.get_forward_extremeties_for_room(
|
2016-09-15 04:27:04 -06:00
|
|
|
room_id, stream_token
|
2016-09-14 10:28:52 -06:00
|
|
|
)
|
|
|
|
rooms_to_latest_event_ids[room_id] = latest_event_ids
|
|
|
|
|
|
|
|
if not latest_event_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
joined_users = yield self.state_handler.get_current_user_in_room(
|
|
|
|
room_id, latest_event_ids,
|
|
|
|
)
|
|
|
|
num_joined_users = len(joined_users)
|
|
|
|
rooms_to_num_joined[room_id] = num_joined_users
|
|
|
|
|
|
|
|
if num_joined_users == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# We want larger rooms to be first, hence negating num_joined_users
|
|
|
|
rooms_to_order_value[room_id] = (-num_joined_users, room_id)
|
|
|
|
|
|
|
|
yield concurrently_execute(get_order_for_room, room_ids, 10)
|
|
|
|
|
|
|
|
sorted_entries = sorted(rooms_to_order_value.items(), key=lambda e: e[1])
|
|
|
|
sorted_rooms = [room_id for room_id, _ in sorted_entries]
|
|
|
|
|
2016-09-17 07:40:28 -06:00
|
|
|
# `sorted_rooms` should now be a list of all public room ids that is
|
|
|
|
# stable across pagination. Therefore, we can use indices into this
|
|
|
|
# list as our pagination tokens.
|
|
|
|
|
|
|
|
# Filter out rooms that we don't want to return
|
|
|
|
rooms_to_scan = [
|
|
|
|
r for r in sorted_rooms
|
|
|
|
if r not in newly_unpublished and rooms_to_num_joined[room_id] > 0
|
|
|
|
]
|
|
|
|
|
2016-09-21 06:17:08 -06:00
|
|
|
total_room_count = len(rooms_to_scan)
|
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token:
|
2016-09-17 07:40:28 -06:00
|
|
|
# Filter out rooms we've already returned previously
|
|
|
|
# `since_token.current_limit` is the index of the last room we
|
|
|
|
# sent down, so we exclude it and everything before/after it.
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token.direction_is_forward:
|
2016-09-17 07:40:28 -06:00
|
|
|
rooms_to_scan = rooms_to_scan[since_token.current_limit + 1:]
|
2016-09-15 03:15:37 -06:00
|
|
|
else:
|
2016-09-17 07:40:28 -06:00
|
|
|
rooms_to_scan = rooms_to_scan[:since_token.current_limit]
|
|
|
|
rooms_to_scan.reverse()
|
2016-09-15 02:08:57 -06:00
|
|
|
|
2016-09-17 07:40:28 -06:00
|
|
|
# Actually generate the entries. _generate_room_entry will append to
|
|
|
|
# chunk but will stop if len(chunk) > limit
|
2016-09-15 03:15:37 -06:00
|
|
|
chunk = []
|
2016-09-17 11:01:54 -06:00
|
|
|
if limit and not search_filter:
|
|
|
|
step = limit + 1
|
2016-09-17 11:09:22 -06:00
|
|
|
for i in xrange(0, len(rooms_to_scan), step):
|
2016-09-17 11:01:54 -06:00
|
|
|
# We iterate here because the vast majority of cases we'll stop
|
|
|
|
# at first iteration, but occaisonally _generate_room_entry
|
|
|
|
# won't append to the chunk and so we need to loop again.
|
|
|
|
# We don't want to scan over the entire range either as that
|
|
|
|
# would potentially waste a lot of work.
|
|
|
|
yield concurrently_execute(
|
|
|
|
lambda r: self._generate_room_entry(
|
|
|
|
r, rooms_to_num_joined[r],
|
|
|
|
chunk, limit, search_filter
|
|
|
|
),
|
|
|
|
rooms_to_scan[i:i + step], 10
|
|
|
|
)
|
|
|
|
if len(chunk) >= limit + 1:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield concurrently_execute(
|
|
|
|
lambda r: self._generate_room_entry(
|
|
|
|
r, rooms_to_num_joined[r],
|
|
|
|
chunk, limit, search_filter
|
|
|
|
),
|
|
|
|
rooms_to_scan, 5
|
|
|
|
)
|
2016-09-14 07:07:37 -06:00
|
|
|
|
2016-09-15 03:15:37 -06:00
|
|
|
chunk.sort(key=lambda e: (-e["num_joined_members"], e["room_id"]))
|
2016-09-15 02:08:57 -06:00
|
|
|
|
2016-09-16 04:00:29 -06:00
|
|
|
# Work out the new limit of the batch for pagination, or None if we
|
|
|
|
# know there are no more results that would be returned.
|
2016-09-17 07:40:28 -06:00
|
|
|
# i.e., [since_token.current_limit..new_limit] is the batch of rooms
|
|
|
|
# we've returned (or the reverse if we paginated backwards)
|
|
|
|
# We tried to pull out limit + 1 rooms above, so if we have <= limit
|
|
|
|
# then we know there are no more results to return
|
2016-09-15 10:35:20 -06:00
|
|
|
new_limit = None
|
2016-09-15 10:50:16 -06:00
|
|
|
if chunk and (not limit or len(chunk) > limit):
|
2016-09-15 10:35:20 -06:00
|
|
|
|
|
|
|
if not since_token or since_token.direction_is_forward:
|
2016-09-17 07:40:28 -06:00
|
|
|
if limit:
|
|
|
|
chunk = chunk[:limit]
|
2016-09-15 10:35:20 -06:00
|
|
|
last_room_id = chunk[-1]["room_id"]
|
|
|
|
else:
|
2016-09-17 07:40:28 -06:00
|
|
|
if limit:
|
|
|
|
chunk = chunk[-limit:]
|
2016-09-15 10:35:20 -06:00
|
|
|
last_room_id = chunk[0]["room_id"]
|
|
|
|
|
2016-09-17 07:40:28 -06:00
|
|
|
new_limit = sorted_rooms.index(last_room_id)
|
2016-09-15 10:35:20 -06:00
|
|
|
|
2016-09-15 03:15:37 -06:00
|
|
|
results = {
|
|
|
|
"chunk": chunk,
|
2016-09-21 06:17:08 -06:00
|
|
|
"total_room_count_estimate": total_room_count,
|
2016-09-15 03:15:37 -06:00
|
|
|
}
|
|
|
|
|
2016-09-15 04:27:04 -06:00
|
|
|
if since_token:
|
|
|
|
results["new_rooms"] = bool(newly_visible)
|
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
if not since_token or since_token.direction_is_forward:
|
2016-09-17 07:40:28 -06:00
|
|
|
if new_limit is not None:
|
2016-09-15 03:15:37 -06:00
|
|
|
results["next_batch"] = RoomListNextBatch(
|
2016-09-15 04:27:04 -06:00
|
|
|
stream_ordering=stream_token,
|
|
|
|
public_room_stream_id=public_room_stream_id,
|
2016-09-15 03:15:37 -06:00
|
|
|
current_limit=new_limit,
|
|
|
|
direction_is_forward=True,
|
|
|
|
).to_token()
|
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token:
|
|
|
|
results["prev_batch"] = since_token.copy_and_replace(
|
2016-09-15 03:15:37 -06:00
|
|
|
direction_is_forward=False,
|
2016-09-17 07:40:28 -06:00
|
|
|
current_limit=since_token.current_limit + 1,
|
2016-09-15 03:15:37 -06:00
|
|
|
).to_token()
|
2016-09-15 02:08:57 -06:00
|
|
|
else:
|
2016-09-17 07:40:28 -06:00
|
|
|
if new_limit is not None:
|
2016-09-15 03:15:37 -06:00
|
|
|
results["prev_batch"] = RoomListNextBatch(
|
2016-09-15 04:27:04 -06:00
|
|
|
stream_ordering=stream_token,
|
|
|
|
public_room_stream_id=public_room_stream_id,
|
2016-09-15 03:15:37 -06:00
|
|
|
current_limit=new_limit,
|
|
|
|
direction_is_forward=False,
|
|
|
|
).to_token()
|
2016-09-15 02:08:57 -06:00
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
if since_token:
|
|
|
|
results["next_batch"] = since_token.copy_and_replace(
|
2016-09-15 03:15:37 -06:00
|
|
|
direction_is_forward=True,
|
2016-09-17 07:40:28 -06:00
|
|
|
current_limit=since_token.current_limit - 1,
|
2016-09-15 03:15:37 -06:00
|
|
|
).to_token()
|
|
|
|
|
|
|
|
defer.returnValue(results)
|
2016-09-14 07:07:37 -06:00
|
|
|
|
2016-09-17 07:40:28 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _generate_room_entry(self, room_id, num_joined_users, chunk, limit,
|
|
|
|
search_filter):
|
|
|
|
if limit and len(chunk) > limit + 1:
|
|
|
|
# We've already got enough, so lets just drop it.
|
|
|
|
return
|
|
|
|
|
|
|
|
result = {
|
|
|
|
"room_id": room_id,
|
|
|
|
"num_joined_members": num_joined_users,
|
|
|
|
}
|
|
|
|
|
|
|
|
current_state_ids = yield self.state_handler.get_current_state_ids(room_id)
|
|
|
|
|
|
|
|
event_map = yield self.store.get_events([
|
|
|
|
event_id for key, event_id in current_state_ids.items()
|
|
|
|
if key[0] in (
|
|
|
|
EventTypes.JoinRules,
|
|
|
|
EventTypes.Name,
|
|
|
|
EventTypes.Topic,
|
|
|
|
EventTypes.CanonicalAlias,
|
|
|
|
EventTypes.RoomHistoryVisibility,
|
|
|
|
EventTypes.GuestAccess,
|
|
|
|
"m.room.avatar",
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
current_state = {
|
|
|
|
(ev.type, ev.state_key): ev
|
|
|
|
for ev in event_map.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
# Double check that this is actually a public room.
|
|
|
|
join_rules_event = current_state.get((EventTypes.JoinRules, ""))
|
|
|
|
if join_rules_event:
|
|
|
|
join_rule = join_rules_event.content.get("join_rule", None)
|
|
|
|
if join_rule and join_rule != JoinRules.PUBLIC:
|
|
|
|
defer.returnValue(None)
|
|
|
|
|
|
|
|
aliases = yield self.store.get_aliases_for_room(room_id)
|
|
|
|
if aliases:
|
|
|
|
result["aliases"] = aliases
|
|
|
|
|
|
|
|
name_event = yield current_state.get((EventTypes.Name, ""))
|
|
|
|
if name_event:
|
|
|
|
name = name_event.content.get("name", None)
|
|
|
|
if name:
|
|
|
|
result["name"] = name
|
|
|
|
|
|
|
|
topic_event = current_state.get((EventTypes.Topic, ""))
|
|
|
|
if topic_event:
|
|
|
|
topic = topic_event.content.get("topic", None)
|
|
|
|
if topic:
|
|
|
|
result["topic"] = topic
|
|
|
|
|
|
|
|
canonical_event = current_state.get((EventTypes.CanonicalAlias, ""))
|
|
|
|
if canonical_event:
|
|
|
|
canonical_alias = canonical_event.content.get("alias", None)
|
|
|
|
if canonical_alias:
|
|
|
|
result["canonical_alias"] = canonical_alias
|
|
|
|
|
|
|
|
visibility_event = current_state.get((EventTypes.RoomHistoryVisibility, ""))
|
|
|
|
visibility = None
|
|
|
|
if visibility_event:
|
|
|
|
visibility = visibility_event.content.get("history_visibility", None)
|
|
|
|
result["world_readable"] = visibility == "world_readable"
|
|
|
|
|
|
|
|
guest_event = current_state.get((EventTypes.GuestAccess, ""))
|
|
|
|
guest = None
|
|
|
|
if guest_event:
|
|
|
|
guest = guest_event.content.get("guest_access", None)
|
|
|
|
result["guest_can_join"] = guest == "can_join"
|
|
|
|
|
|
|
|
avatar_event = current_state.get(("m.room.avatar", ""))
|
|
|
|
if avatar_event:
|
|
|
|
avatar_url = avatar_event.content.get("url", None)
|
|
|
|
if avatar_url:
|
|
|
|
result["avatar_url"] = avatar_url
|
|
|
|
|
|
|
|
if _matches_room_entry(result, search_filter):
|
|
|
|
chunk.append(result)
|
|
|
|
|
2016-09-14 07:07:37 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-09-15 10:35:20 -06:00
|
|
|
def get_remote_public_room_list(self, server_name, limit=None, since_token=None,
|
|
|
|
search_filter=None):
|
2016-09-16 03:24:15 -06:00
|
|
|
if search_filter:
|
|
|
|
# We currently don't support searching across federation, so we have
|
|
|
|
# to do it manually without pagination
|
|
|
|
limit = None
|
|
|
|
since_token = None
|
|
|
|
|
2016-09-16 03:31:59 -06:00
|
|
|
res = yield self._get_remote_list_cached(
|
2016-09-15 03:36:19 -06:00
|
|
|
server_name, limit=limit, since_token=since_token,
|
2016-09-14 07:07:37 -06:00
|
|
|
)
|
|
|
|
|
2016-09-16 03:19:32 -06:00
|
|
|
if search_filter:
|
2016-09-16 03:24:15 -06:00
|
|
|
res = {"chunk": [
|
2016-09-16 03:19:32 -06:00
|
|
|
entry
|
2016-09-16 03:24:15 -06:00
|
|
|
for entry in list(res.get("chunk", []))
|
2016-09-16 03:19:32 -06:00
|
|
|
if _matches_room_entry(entry, search_filter)
|
2016-09-16 03:24:15 -06:00
|
|
|
]}
|
2016-09-16 03:19:32 -06:00
|
|
|
|
2016-09-15 03:36:19 -06:00
|
|
|
defer.returnValue(res)
|
2016-09-14 07:07:37 -06:00
|
|
|
|
2016-09-16 03:31:59 -06:00
|
|
|
def _get_remote_list_cached(self, server_name, limit=None, since_token=None,
|
|
|
|
search_filter=None):
|
|
|
|
repl_layer = self.hs.get_replication_layer()
|
|
|
|
if search_filter:
|
|
|
|
# We can't cache when asking for search
|
|
|
|
return repl_layer.get_public_rooms(
|
|
|
|
server_name, limit=limit, since_token=since_token,
|
|
|
|
search_filter=search_filter,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = self.remote_response_cache.get((server_name, limit, since_token))
|
|
|
|
if not result:
|
|
|
|
result = self.remote_response_cache.set(
|
|
|
|
(server_name, limit, since_token),
|
|
|
|
repl_layer.get_public_rooms(
|
|
|
|
server_name, limit=limit, since_token=since_token,
|
|
|
|
search_filter=search_filter,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
|
2016-09-15 02:08:57 -06:00
|
|
|
|
|
|
|
class RoomListNextBatch(namedtuple("RoomListNextBatch", (
|
|
|
|
"stream_ordering", # stream_ordering of the first public room list
|
2016-09-15 04:27:04 -06:00
|
|
|
"public_room_stream_id", # public room stream id for first public room list
|
2016-09-15 02:08:57 -06:00
|
|
|
"current_limit", # The number of previous rooms returned
|
2016-09-15 03:15:37 -06:00
|
|
|
"direction_is_forward", # Bool if this is a next_batch, false if prev_batch
|
2016-09-15 02:08:57 -06:00
|
|
|
))):
|
|
|
|
|
|
|
|
KEY_DICT = {
|
|
|
|
"stream_ordering": "s",
|
2016-09-15 04:27:04 -06:00
|
|
|
"public_room_stream_id": "p",
|
2016-09-15 02:08:57 -06:00
|
|
|
"current_limit": "n",
|
2016-09-15 03:15:37 -06:00
|
|
|
"direction_is_forward": "d",
|
2016-09-15 02:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
REVERSE_KEY_DICT = {v: k for k, v in KEY_DICT.items()}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_token(cls, token):
|
|
|
|
return RoomListNextBatch(**{
|
|
|
|
cls.REVERSE_KEY_DICT[key]: val
|
|
|
|
for key, val in msgpack.loads(decode_base64(token)).items()
|
|
|
|
})
|
|
|
|
|
|
|
|
def to_token(self):
|
|
|
|
return encode_base64(msgpack.dumps({
|
|
|
|
self.KEY_DICT[key]: val
|
|
|
|
for key, val in self._asdict().items()
|
|
|
|
}))
|
2016-09-15 03:15:37 -06:00
|
|
|
|
|
|
|
def copy_and_replace(self, **kwds):
|
|
|
|
return self._replace(
|
|
|
|
**kwds
|
|
|
|
)
|
2016-09-16 03:19:32 -06:00
|
|
|
|
|
|
|
|
|
|
|
def _matches_room_entry(room_entry, search_filter):
|
|
|
|
if search_filter and search_filter.get("generic_search_term", None):
|
2016-09-16 12:02:42 -06:00
|
|
|
generic_search_term = search_filter["generic_search_term"].upper()
|
|
|
|
if generic_search_term in room_entry.get("name", "").upper():
|
2016-09-16 03:19:32 -06:00
|
|
|
return True
|
2016-09-16 12:02:42 -06:00
|
|
|
elif generic_search_term in room_entry.get("topic", "").upper():
|
2016-09-16 03:19:32 -06:00
|
|
|
return True
|
2016-09-16 12:02:42 -06:00
|
|
|
elif generic_search_term in room_entry.get("canonical_alias", "").upper():
|
2016-09-16 03:19:32 -06:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|