mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Merge pull request #552 from matrix-org/erikj/public_room_fix
Change the way we do public room list fetching
This commit is contained in:
commit
430e496050
@ -674,8 +674,8 @@ def run(hs):
|
|||||||
stats["uptime_seconds"] = uptime
|
stats["uptime_seconds"] = uptime
|
||||||
stats["total_users"] = yield hs.get_datastore().count_all_users()
|
stats["total_users"] = yield hs.get_datastore().count_all_users()
|
||||||
|
|
||||||
all_rooms = yield hs.get_datastore().get_rooms(False)
|
room_count = yield hs.get_datastore().get_room_count()
|
||||||
stats["total_room_count"] = len(all_rooms)
|
stats["total_room_count"] = room_count
|
||||||
|
|
||||||
stats["daily_active_users"] = yield hs.get_datastore().count_daily_users()
|
stats["daily_active_users"] = yield hs.get_datastore().count_daily_users()
|
||||||
daily_messages = yield hs.get_datastore().count_daily_messages()
|
daily_messages = yield hs.get_datastore().count_daily_messages()
|
||||||
|
@ -876,39 +876,71 @@ class RoomListHandler(BaseHandler):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_public_room_list(self):
|
def get_public_room_list(self):
|
||||||
chunk = yield self.store.get_rooms(is_public=True)
|
room_ids = yield self.store.get_public_room_ids()
|
||||||
|
|
||||||
room_members = yield defer.gatherResults(
|
|
||||||
[
|
|
||||||
self.store.get_users_in_room(room["room_id"])
|
|
||||||
for room in chunk
|
|
||||||
],
|
|
||||||
consumeErrors=True,
|
|
||||||
).addErrback(unwrapFirstError)
|
|
||||||
|
|
||||||
avatar_urls = yield defer.gatherResults(
|
|
||||||
[
|
|
||||||
self.get_room_avatar_url(room["room_id"])
|
|
||||||
for room in chunk
|
|
||||||
],
|
|
||||||
consumeErrors=True,
|
|
||||||
).addErrback(unwrapFirstError)
|
|
||||||
|
|
||||||
for i, room in enumerate(chunk):
|
|
||||||
room["num_joined_members"] = len(room_members[i])
|
|
||||||
if avatar_urls[i]:
|
|
||||||
room["avatar_url"] = avatar_urls[i]
|
|
||||||
|
|
||||||
# FIXME (erikj): START is no longer a valid value
|
|
||||||
defer.returnValue({"start": "START", "end": "END", "chunk": chunk})
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_room_avatar_url(self, room_id):
|
def handle_room(room_id):
|
||||||
event = yield self.hs.get_state_handler().get_current_state(
|
aliases = yield self.store.get_aliases_for_room(room_id)
|
||||||
room_id, "m.room.avatar"
|
if not aliases:
|
||||||
|
defer.returnValue(None)
|
||||||
|
|
||||||
|
state = yield self.state_handler.get_current_state(room_id)
|
||||||
|
|
||||||
|
result = {"aliases": aliases, "room_id": room_id}
|
||||||
|
|
||||||
|
name_event = state.get((EventTypes.Name, ""), None)
|
||||||
|
if name_event:
|
||||||
|
name = name_event.content.get("name", None)
|
||||||
|
if name:
|
||||||
|
result["name"] = name
|
||||||
|
|
||||||
|
topic_event = state.get((EventTypes.Topic, ""), None)
|
||||||
|
if topic_event:
|
||||||
|
topic = topic_event.content.get("topic", None)
|
||||||
|
if topic:
|
||||||
|
result["topic"] = topic
|
||||||
|
|
||||||
|
canonical_event = state.get((EventTypes.CanonicalAlias, ""), None)
|
||||||
|
if canonical_event:
|
||||||
|
canonical_alias = canonical_event.content.get("alias", None)
|
||||||
|
if canonical_alias:
|
||||||
|
result["canonical_alias"] = canonical_alias
|
||||||
|
|
||||||
|
visibility_event = state.get((EventTypes.RoomHistoryVisibility, ""), None)
|
||||||
|
visibility = None
|
||||||
|
if visibility_event:
|
||||||
|
visibility = visibility_event.content.get("history_visibility", None)
|
||||||
|
result["world_readable"] = visibility == "world_readable"
|
||||||
|
|
||||||
|
guest_event = state.get((EventTypes.GuestAccess, ""), None)
|
||||||
|
guest = None
|
||||||
|
if guest_event:
|
||||||
|
guest = guest_event.content.get("guest_access", None)
|
||||||
|
result["guest_can_join"] = guest == "can_join"
|
||||||
|
|
||||||
|
avatar_event = state.get(("m.room.avatar", ""), None)
|
||||||
|
if avatar_event:
|
||||||
|
avatar_url = avatar_event.content.get("url", None)
|
||||||
|
if avatar_url:
|
||||||
|
result["avatar_url"] = avatar_url
|
||||||
|
|
||||||
|
result["num_joined_members"] = sum(
|
||||||
|
1 for (event_type, _), ev in state.items()
|
||||||
|
if event_type == EventTypes.Member and ev.membership == Membership.JOIN
|
||||||
)
|
)
|
||||||
if event and "url" in event.content:
|
|
||||||
defer.returnValue(event.content["url"])
|
defer.returnValue(result)
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for chunk in (room_ids[i:i + 10] for i in xrange(0, len(room_ids), 10)):
|
||||||
|
chunk_result = yield defer.gatherResults([
|
||||||
|
handle_room(room_id)
|
||||||
|
for room_id in chunk
|
||||||
|
], consumeErrors=True).addErrback(unwrapFirstError)
|
||||||
|
result.extend(v for v in chunk_result if v)
|
||||||
|
|
||||||
|
# FIXME (erikj): START is no longer a valid value
|
||||||
|
defer.returnValue({"start": "START", "end": "END", "chunk": result})
|
||||||
|
|
||||||
|
|
||||||
class RoomContextHandler(BaseHandler):
|
class RoomContextHandler(BaseHandler):
|
||||||
|
@ -87,90 +87,20 @@ class RoomStore(SQLBaseStore):
|
|||||||
desc="get_public_room_ids",
|
desc="get_public_room_ids",
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
def get_room_count(self):
|
||||||
def get_rooms(self, is_public):
|
"""Retrieve a list of all rooms
|
||||||
"""Retrieve a list of all public rooms.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
is_public (bool): True if the rooms returned should be public.
|
|
||||||
Returns:
|
|
||||||
A list of room dicts containing at least a "room_id" key, a
|
|
||||||
"topic" key if one is set, and a "name" key if one is set
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def f(txn):
|
def f(txn):
|
||||||
def subquery(table_name, column_name=None):
|
sql = "SELECT count(*) FROM rooms"
|
||||||
column_name = column_name or table_name
|
txn.execute(sql)
|
||||||
return (
|
row = txn.fetchone()
|
||||||
"SELECT %(table_name)s.event_id as event_id, "
|
return row[0] or 0
|
||||||
"%(table_name)s.room_id as room_id, %(column_name)s "
|
|
||||||
"FROM %(table_name)s "
|
|
||||||
"INNER JOIN current_state_events as c "
|
|
||||||
"ON c.event_id = %(table_name)s.event_id " % {
|
|
||||||
"column_name": column_name,
|
|
||||||
"table_name": table_name,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
sql = (
|
return self.runInteraction(
|
||||||
"SELECT"
|
|
||||||
" r.room_id,"
|
|
||||||
" max(n.name),"
|
|
||||||
" max(t.topic),"
|
|
||||||
" max(v.history_visibility),"
|
|
||||||
" max(g.guest_access)"
|
|
||||||
" FROM rooms AS r"
|
|
||||||
" LEFT JOIN (%(topic)s) AS t ON t.room_id = r.room_id"
|
|
||||||
" LEFT JOIN (%(name)s) AS n ON n.room_id = r.room_id"
|
|
||||||
" LEFT JOIN (%(history_visibility)s) AS v ON v.room_id = r.room_id"
|
|
||||||
" LEFT JOIN (%(guest_access)s) AS g ON g.room_id = r.room_id"
|
|
||||||
" WHERE r.is_public = ?"
|
|
||||||
" GROUP BY r.room_id" % {
|
|
||||||
"topic": subquery("topics", "topic"),
|
|
||||||
"name": subquery("room_names", "name"),
|
|
||||||
"history_visibility": subquery("history_visibility"),
|
|
||||||
"guest_access": subquery("guest_access"),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
txn.execute(sql, (is_public,))
|
|
||||||
|
|
||||||
rows = txn.fetchall()
|
|
||||||
|
|
||||||
for i, row in enumerate(rows):
|
|
||||||
room_id = row[0]
|
|
||||||
aliases = self._simple_select_onecol_txn(
|
|
||||||
txn,
|
|
||||||
table="room_aliases",
|
|
||||||
keyvalues={
|
|
||||||
"room_id": room_id
|
|
||||||
},
|
|
||||||
retcol="room_alias",
|
|
||||||
)
|
|
||||||
|
|
||||||
rows[i] = list(row) + [aliases]
|
|
||||||
|
|
||||||
return rows
|
|
||||||
|
|
||||||
rows = yield self.runInteraction(
|
|
||||||
"get_rooms", f
|
"get_rooms", f
|
||||||
)
|
)
|
||||||
|
|
||||||
ret = [
|
|
||||||
{
|
|
||||||
"room_id": r[0],
|
|
||||||
"name": r[1],
|
|
||||||
"topic": r[2],
|
|
||||||
"world_readable": r[3] == "world_readable",
|
|
||||||
"guest_can_join": r[4] == "can_join",
|
|
||||||
"aliases": r[5],
|
|
||||||
}
|
|
||||||
for r in rows
|
|
||||||
if r[5] # We only return rooms that have at least one alias.
|
|
||||||
]
|
|
||||||
|
|
||||||
defer.returnValue(ret)
|
|
||||||
|
|
||||||
def _store_room_topic_txn(self, txn, event):
|
def _store_room_topic_txn(self, txn, event):
|
||||||
if hasattr(event, "content") and "topic" in event.content:
|
if hasattr(event, "content") and "topic" in event.content:
|
||||||
self._simple_insert_txn(
|
self._simple_insert_txn(
|
||||||
|
16
synapse/storage/schema/delta/28/public_roms_index.sql
Normal file
16
synapse/storage/schema/delta/28/public_roms_index.sql
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* Copyright 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
CREATE INDEX public_room_index on rooms(is_public);
|
@ -51,32 +51,6 @@ class RoomStoreTestCase(unittest.TestCase):
|
|||||||
(yield self.store.get_room(self.room.to_string()))
|
(yield self.store.get_room(self.room.to_string()))
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def test_get_rooms(self):
|
|
||||||
# get_rooms does an INNER JOIN on the room_aliases table :(
|
|
||||||
|
|
||||||
rooms = yield self.store.get_rooms(is_public=True)
|
|
||||||
# Should be empty before we add the alias
|
|
||||||
self.assertEquals([], rooms)
|
|
||||||
|
|
||||||
yield self.store.create_room_alias_association(
|
|
||||||
room_alias=self.alias,
|
|
||||||
room_id=self.room.to_string(),
|
|
||||||
servers=["test"]
|
|
||||||
)
|
|
||||||
|
|
||||||
rooms = yield self.store.get_rooms(is_public=True)
|
|
||||||
|
|
||||||
self.assertEquals(1, len(rooms))
|
|
||||||
self.assertEquals({
|
|
||||||
"name": None,
|
|
||||||
"room_id": self.room.to_string(),
|
|
||||||
"topic": None,
|
|
||||||
"aliases": [self.alias.to_string()],
|
|
||||||
"world_readable": False,
|
|
||||||
"guest_can_join": False,
|
|
||||||
}, rooms[0])
|
|
||||||
|
|
||||||
|
|
||||||
class RoomEventsStoreTestCase(unittest.TestCase):
|
class RoomEventsStoreTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user