From 3c10c80c1aafd8e154086f8f8d25954483718aaf Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Tue, 3 May 2022 19:25:14 -0700 Subject: [PATCH 01/15] remove constantly lib use and switch to enums. Signed-off-by: Andrew Do --- mypy.ini | 3 --- synapse/handlers/events.py | 2 +- synapse/handlers/federation.py | 4 ++-- synapse/handlers/federation_event.py | 2 +- synapse/handlers/message.py | 4 ++-- synapse/state/__init__.py | 2 +- .../storage/databases/main/events_worker.py | 24 +++++++++---------- synapse/storage/databases/main/search.py | 8 +++---- synapse/storage/databases/main/signatures.py | 2 +- synapse/storage/persist_events.py | 4 ++-- 10 files changed, 26 insertions(+), 29 deletions(-) diff --git a/mypy.ini b/mypy.ini index ef28216418a5..cb76b82dbe75 100644 --- a/mypy.ini +++ b/mypy.ini @@ -247,9 +247,6 @@ ignore_missing_imports = True [mypy-canonicaljson] ignore_missing_imports = True -[mypy-constantly] -ignore_missing_imports = True - [mypy-daemonize] ignore_missing_imports = True diff --git a/synapse/handlers/events.py b/synapse/handlers/events.py index 5b94b00bc3e7..82a5aac3dda6 100644 --- a/synapse/handlers/events.py +++ b/synapse/handlers/events.py @@ -164,7 +164,7 @@ async def get_event( event. """ redact_behaviour = ( - EventRedactBehaviour.AS_IS if show_redacted else EventRedactBehaviour.REDACT + EventRedactBehaviour.as_is if show_redacted else EventRedactBehaviour.redact ) event = await self.store.get_event( event_id, check_room_id=room_id, redact_behaviour=redact_behaviour diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index d2ba70a814d0..38dc5b1f6edf 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -316,7 +316,7 @@ async def _maybe_backfill_inner( events_to_check = await self.store.get_events_as_list( event_ids_to_check, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, get_prev_content=False, ) @@ -1494,7 +1494,7 @@ async def _sync_partial_state_room( events = await self.store.get_events_as_list( batch, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, allow_rejected=True, ) for event in events: diff --git a/synapse/handlers/federation_event.py b/synapse/handlers/federation_event.py index 693b544286ec..6cf927e4ff7b 100644 --- a/synapse/handlers/federation_event.py +++ b/synapse/handlers/federation_event.py @@ -860,7 +860,7 @@ async def _resolve_state_at_missing_prevs( evs = await self._store.get_events( list(state_map.values()), get_prev_content=False, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, ) event_map.update(evs) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 1b092e900eb5..95a89ac01f4b 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -1407,7 +1407,7 @@ async def persist_and_notify_client_event( original_event = await self.store.get_event( event.redacts, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, get_prev_content=False, allow_rejected=False, allow_none=True, @@ -1504,7 +1504,7 @@ async def persist_and_notify_client_event( original_event = await self.store.get_event( event.redacts, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, get_prev_content=False, allow_rejected=False, allow_none=True, diff --git a/synapse/state/__init__.py b/synapse/state/__init__.py index fbf7ba4600f8..cad3b4264007 100644 --- a/synapse/state/__init__.py +++ b/synapse/state/__init__.py @@ -800,7 +800,7 @@ def get_events( return self.store.get_events( event_ids, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, get_prev_content=False, allow_rejected=allow_rejected, ) diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index c31fc00eaace..334befc4bc0a 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -14,6 +14,7 @@ import logging import threading +from enum import Enum, auto from typing import ( TYPE_CHECKING, Any, @@ -30,7 +31,6 @@ ) import attr -from constantly import NamedConstant, Names from prometheus_client import Gauge from typing_extensions import Literal @@ -150,14 +150,14 @@ class _EventRow: outlier: bool -class EventRedactBehaviour(Names): +class EventRedactBehaviour(Enum): """ What to do when retrieving a redacted event from the database. """ - AS_IS = NamedConstant() - REDACT = NamedConstant() - BLOCK = NamedConstant() + as_is = auto() + redact = auto() + block = auto() class EventsWorkerStore(SQLBaseStore): @@ -327,7 +327,7 @@ async def have_censored_event(self, event_id: str) -> bool: async def get_event( self, event_id: str, - redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, + redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact, get_prev_content: bool = ..., allow_rejected: bool = ..., allow_none: Literal[False] = ..., @@ -339,7 +339,7 @@ async def get_event( async def get_event( self, event_id: str, - redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, + redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact, get_prev_content: bool = ..., allow_rejected: bool = ..., allow_none: Literal[True] = ..., @@ -350,7 +350,7 @@ async def get_event( async def get_event( self, event_id: str, - redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, + redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact, get_prev_content: bool = False, allow_rejected: bool = False, allow_none: bool = False, @@ -406,7 +406,7 @@ async def get_event( async def get_events( self, event_ids: Collection[str], - redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, + redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact, get_prev_content: bool = False, allow_rejected: bool = False, ) -> Dict[str, EventBase]: @@ -442,7 +442,7 @@ async def get_events( async def get_events_as_list( self, event_ids: Collection[str], - redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, + redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact, get_prev_content: bool = False, allow_rejected: bool = False, ) -> List[EventBase]: @@ -568,10 +568,10 @@ async def get_events_as_list( event = entry.event if entry.redacted_event: - if redact_behaviour == EventRedactBehaviour.BLOCK: + if redact_behaviour == EventRedactBehaviour.block: # Skip this event continue - elif redact_behaviour == EventRedactBehaviour.REDACT: + elif redact_behaviour == EventRedactBehaviour.redact: event = entry.redacted_event events.append(event) diff --git a/synapse/storage/databases/main/search.py b/synapse/storage/databases/main/search.py index d4482c06db66..3c49e7ec98e2 100644 --- a/synapse/storage/databases/main/search.py +++ b/synapse/storage/databases/main/search.py @@ -494,11 +494,11 @@ async def search_msgs( results = list(filter(lambda row: row["room_id"] in room_ids, results)) - # We set redact_behaviour to BLOCK here to prevent redacted events being returned in + # We set redact_behaviour to block here to prevent redacted events being returned in # search results (which is a data leak) events = await self.get_events_as_list( # type: ignore[attr-defined] [r["event_id"] for r in results], - redact_behaviour=EventRedactBehaviour.BLOCK, + redact_behaviour=EventRedactBehaviour.block, ) event_map = {ev.event_id: ev for ev in events} @@ -652,11 +652,11 @@ async def search_rooms( results = list(filter(lambda row: row["room_id"] in room_ids, results)) - # We set redact_behaviour to BLOCK here to prevent redacted events being returned in + # We set redact_behaviour to block here to prevent redacted events being returned in # search results (which is a data leak) events = await self.get_events_as_list( # type: ignore[attr-defined] [r["event_id"] for r in results], - redact_behaviour=EventRedactBehaviour.BLOCK, + redact_behaviour=EventRedactBehaviour.block, ) event_map = {ev.event_id: ev for ev in events} diff --git a/synapse/storage/databases/main/signatures.py b/synapse/storage/databases/main/signatures.py index 95148fd2273a..05da15074a73 100644 --- a/synapse/storage/databases/main/signatures.py +++ b/synapse/storage/databases/main/signatures.py @@ -48,7 +48,7 @@ async def get_event_reference_hashes( """ events = await self.get_events( event_ids, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, allow_rejected=True, ) diff --git a/synapse/storage/persist_events.py b/synapse/storage/persist_events.py index e496ba7bed6e..97118045a1ad 100644 --- a/synapse/storage/persist_events.py +++ b/synapse/storage/persist_events.py @@ -943,7 +943,7 @@ async def _prune_extremities( dropped_events = await self.main_store.get_events( dropped_extrems, allow_rejected=True, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, ) new_senders = {get_domain_from_id(e.sender) for e, _ in events_context} @@ -974,7 +974,7 @@ async def _prune_extremities( prev_events = await self.main_store.get_events( new_events, allow_rejected=True, - redact_behaviour=EventRedactBehaviour.AS_IS, + redact_behaviour=EventRedactBehaviour.as_is, ) events_to_check = prev_events.values() From 9a450fc2473231239b852a79fe657acd1be9e67b Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Tue, 3 May 2022 19:39:21 -0700 Subject: [PATCH 02/15] add changlog for pr 12624. --- changelog.d/12624.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/12624.misc diff --git a/changelog.d/12624.misc b/changelog.d/12624.misc new file mode 100644 index 000000000000..3f847651c35a --- /dev/null +++ b/changelog.d/12624.misc @@ -0,0 +1 @@ +remove use of constantly library and switch to enums for EventRedactBehaviour. From 317065a0b2ad3a5c5a304cef0cfbf2d7dbb39ad8 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Tue, 3 May 2022 19:45:24 -0700 Subject: [PATCH 03/15] Fix capitalization of sentence and add credit. --- changelog.d/12624.misc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.d/12624.misc b/changelog.d/12624.misc index 3f847651c35a..309186bb834d 100644 --- a/changelog.d/12624.misc +++ b/changelog.d/12624.misc @@ -1 +1,2 @@ -remove use of constantly library and switch to enums for EventRedactBehaviour. +Remove use of constantly library and switch to enums for EventRedactBehaviour. +Contributed by @andrewdoh. From 2b1fe9ae04c424c75805445b0df9efc43f7d0386 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Tue, 3 May 2022 20:24:16 -0700 Subject: [PATCH 04/15] update function docstring description to new updated enum values. --- .../storage/databases/main/events_worker.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index a98b90834bfa..a4a604a49915 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -362,9 +362,9 @@ async def get_event( event_id: The event_id of the event to fetch redact_behaviour: Determine what to do with a redacted event. Possible values: - * AS_IS - Return the full event body with no redacted content - * REDACT - Return the event but with a redacted body - * DISALLOW - Do not return redacted events (behave as per allow_none + * as_is - Return the full event body with no redacted content + * redact - Return the event but with a redacted body + * block - Do not return redacted events (behave as per allow_none if the event is redacted) get_prev_content: If True and event is a state event, @@ -417,9 +417,9 @@ async def get_events( redact_behaviour: Determine what to do with a redacted event. Possible values: - * AS_IS - Return the full event body with no redacted content - * REDACT - Return the event but with a redacted body - * DISALLOW - Do not return redacted events (omit them from the response) + * as_is - Return the full event body with no redacted content + * redact - Return the event but with a redacted body + * block - Do not return redacted events (omit them from the response) get_prev_content: If True and event is a state event, include the previous states content in the unsigned field. @@ -455,9 +455,9 @@ async def get_events_as_list( event_ids: The event_ids of the events to fetch redact_behaviour: Determine what to do with a redacted event. Possible values: - * AS_IS - Return the full event body with no redacted content - * REDACT - Return the event but with a redacted body - * DISALLOW - Do not return redacted events (omit them from the response) + * as_is - Return the full event body with no redacted content + * redact - Return the event but with a redacted body + * block - Do not return redacted events (omit them from the response) get_prev_content: If True and event is a state event, include the previous states content in the unsigned field. From 7c9e5db8b9152c4116bc88478f475b24b1231b05 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 4 May 2022 07:01:25 -0400 Subject: [PATCH 05/15] Update changelog. --- changelog.d/12624.misc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/changelog.d/12624.misc b/changelog.d/12624.misc index 309186bb834d..8772d40fa70f 100644 --- a/changelog.d/12624.misc +++ b/changelog.d/12624.misc @@ -1,2 +1 @@ -Remove use of constantly library and switch to enums for EventRedactBehaviour. -Contributed by @andrewdoh. +Remove use of constantly library and switch to enums for EventRedactBehaviour. Contributed by @andrewdoh. From 32759dc381575fd0b5c543e8bfc041fe70983401 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Wed, 6 Jul 2022 17:09:45 -0700 Subject: [PATCH 06/15] add room_type to list room and room details API response. Signed-off-by: Andrew Doh --- changelog.d/11904.misc | 1 + synapse/storage/databases/main/room.py | 5 +++-- tests/rest/admin/test_room.py | 13 +++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 changelog.d/11904.misc diff --git a/changelog.d/11904.misc b/changelog.d/11904.misc new file mode 100644 index 000000000000..56d3df319feb --- /dev/null +++ b/changelog.d/11904.misc @@ -0,0 +1 @@ +add room_type to response in list room and room details API. Contributed by @andrewdoh. diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index 13d6a1d5c0d9..83b0aef1acb1 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -175,7 +175,7 @@ def get_room_with_stats_txn( rooms.creator, state.encryption, state.is_federatable AS federatable, rooms.is_public AS public, state.join_rules, state.guest_access, state.history_visibility, curr.current_state_events AS state_events, - state.avatar, state.topic + state.avatar, state.topic, state.room_type FROM rooms LEFT JOIN room_stats_state state USING (room_id) LEFT JOIN room_stats_current curr USING (room_id) @@ -596,7 +596,7 @@ async def get_rooms_paginate( SELECT state.room_id, state.name, state.canonical_alias, curr.joined_members, curr.local_users_in_room, rooms.room_version, rooms.creator, state.encryption, state.is_federatable, rooms.is_public, state.join_rules, - state.guest_access, state.history_visibility, curr.current_state_events + state.guest_access, state.history_visibility, curr.current_state_events, state.room_type FROM room_stats_state state INNER JOIN room_stats_current curr USING (room_id) INNER JOIN rooms USING (room_id) @@ -646,6 +646,7 @@ def _get_rooms_paginate_txn( "guest_access": room[11], "history_visibility": room[12], "state_events": room[13], + "room_type": room[14], } ) diff --git a/tests/rest/admin/test_room.py b/tests/rest/admin/test_room.py index 230dc76f723c..e7085c2d6e20 100644 --- a/tests/rest/admin/test_room.py +++ b/tests/rest/admin/test_room.py @@ -21,7 +21,7 @@ from twisted.test.proto_helpers import MemoryReactor import synapse.rest.admin -from synapse.api.constants import EventTypes, Membership +from synapse.api.constants import EventTypes, Membership, RoomTypes from synapse.api.errors import Codes from synapse.handlers.pagination import PaginationHandler from synapse.rest.client import directory, events, login, room @@ -1130,7 +1130,7 @@ def test_list_rooms(self) -> None: self.assertIn("guest_access", r) self.assertIn("history_visibility", r) self.assertIn("state_events", r) - + self.assertIn("room_type", r) # Check that the correct number of total rooms was returned self.assertEqual(channel.json_body["total_rooms"], total_rooms) @@ -1229,7 +1229,11 @@ def test_list_rooms_pagination(self) -> None: def test_correct_room_attributes(self) -> None: """Test the correct attributes for a room are returned""" # Create a test room - room_id = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok) + room_id = self.helper.create_room_as( + self.admin_user, + tok=self.admin_user_tok, + extra_content={"creation_content": {"type": RoomTypes.SPACE}}, + ) test_alias = "#test:test" test_room_name = "something" @@ -1306,6 +1310,7 @@ def test_correct_room_attributes(self) -> None: self.assertEqual(room_id, r["room_id"]) self.assertEqual(test_room_name, r["name"]) self.assertEqual(test_alias, r["canonical_alias"]) + self.assertEqual(RoomTypes.SPACE, r["room_type"]) def test_room_list_sort_order(self) -> None: """Test room list sort ordering. alphabetical name versus number of members, @@ -1630,7 +1635,7 @@ def test_single_room(self) -> None: self.assertIn("guest_access", channel.json_body) self.assertIn("history_visibility", channel.json_body) self.assertIn("state_events", channel.json_body) - + self.assertIn("room_type", channel.json_body) self.assertEqual(room_id_1, channel.json_body["room_id"]) def test_single_room_devices(self) -> None: From db7010bb3277bcea80d3381ac99db992c1f20e68 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Wed, 6 Jul 2022 17:42:52 -0700 Subject: [PATCH 07/15] refactor changelog file to expected number. Signed-off-by: Andrew Doh --- changelog.d/{11904.misc => 13208.misc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog.d/{11904.misc => 13208.misc} (100%) diff --git a/changelog.d/11904.misc b/changelog.d/13208.misc similarity index 100% rename from changelog.d/11904.misc rename to changelog.d/13208.misc From 24004a71e148f4cdceb6fa62b2c8d9a9bcb82911 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Wed, 6 Jul 2022 23:05:51 -0700 Subject: [PATCH 08/15] add room_type definition and examples to documentation. Signed-off-by: Andrew Doh --- docs/admin_api/rooms.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index d4873f94905f..8d8f37d1715d 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -59,6 +59,7 @@ The following fields are possible in the JSON response body: - `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. - `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. - `state_events` - Total number of state_events of a room. Complexity of the room. + - `room_type` - the type of the room. * `offset` - The current pagination offset in rooms. This parameter should be used instead of `next_token` for room offset as `next_token` is not intended to be parsed. @@ -101,7 +102,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 93534 + "state_events": 93534, + "room_type": "m.space" }, ... (8 hidden items) ... { @@ -118,7 +120,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 8345 + "state_events": 8345, + "room_type": null } ], "offset": 0, @@ -151,7 +154,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 8 + "state_events": 8, + "room_type": null } ], "offset": 0, @@ -184,7 +188,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 93534 + "state_events": 93534, + "room_type": null }, ... (98 hidden items) ... { @@ -201,7 +206,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 8345 + "state_events": 8345, + "room_type": "m.space" } ], "offset": 0, @@ -238,7 +244,9 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 93534 + "state_events": 93534, + "room_type": "m.space" + }, ... (48 hidden items) ... { @@ -255,7 +263,9 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 8345 + "state_events": 8345, + "room_type": null + } ], "offset": 100, @@ -290,6 +300,7 @@ The following fields are possible in the JSON response body: * `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. * `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. * `state_events` - Total number of state_events of a room. Complexity of the room. +* `room_type` - the type of the room. The API is: @@ -317,7 +328,8 @@ A response body like the following is returned: "join_rules": "invite", "guest_access": null, "history_visibility": "shared", - "state_events": 93534 + "state_events": 93534, + "room_type": "m.space" } ``` From 453321a1e419dfd829a651b0e6e5ccd2c56580b0 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Wed, 6 Jul 2022 23:34:53 -0700 Subject: [PATCH 09/15] make room_type definition in documentation more specific. Signed-off-by: Andrew Doh --- docs/admin_api/rooms.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index 8d8f37d1715d..23b5824853d8 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -59,7 +59,7 @@ The following fields are possible in the JSON response body: - `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. - `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. - `state_events` - Total number of state_events of a room. Complexity of the room. - - `room_type` - the type of the room. + - `room_type` - the type of the room. It is `null` for normal rooms and `m.space` for spaces. * `offset` - The current pagination offset in rooms. This parameter should be used instead of `next_token` for room offset as `next_token` is not intended to be parsed. @@ -300,7 +300,7 @@ The following fields are possible in the JSON response body: * `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. * `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. * `state_events` - Total number of state_events of a room. Complexity of the room. -* `room_type` - the type of the room. +* `room_type` - the type of the room. It is `null` for normal rooms and `m.space` for spaces. The API is: From 8cc95ae11d01dfcf9cc6c1681d58e8dba530b652 Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Wed, 6 Jul 2022 23:37:16 -0700 Subject: [PATCH 10/15] remove old changelog file. Signed-off-by: Andrew Doh --- changelog.d/12624.misc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 changelog.d/12624.misc diff --git a/changelog.d/12624.misc b/changelog.d/12624.misc deleted file mode 100644 index 8772d40fa70f..000000000000 --- a/changelog.d/12624.misc +++ /dev/null @@ -1 +0,0 @@ -Remove use of constantly library and switch to enums for EventRedactBehaviour. Contributed by @andrewdoh. From 13e015ae821e63e03dcce9c379ec0d5a7eb79a2a Mon Sep 17 00:00:00 2001 From: Andrew Doh Date: Thu, 7 Jul 2022 10:59:14 -0700 Subject: [PATCH 11/15] refactored changelog file to features, and made various updates to definitions and code per PR comments. Signed-off-by: Andrew Doh --- changelog.d/13208.feature | 1 + changelog.d/13208.misc | 1 - docs/admin_api/rooms.md | 4 ++-- synapse/storage/databases/main/room.py | 3 ++- tests/rest/admin/test_room.py | 2 ++ 5 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 changelog.d/13208.feature delete mode 100644 changelog.d/13208.misc diff --git a/changelog.d/13208.feature b/changelog.d/13208.feature new file mode 100644 index 000000000000..b0c5f090ee7b --- /dev/null +++ b/changelog.d/13208.feature @@ -0,0 +1 @@ +Add a `room_type` field in the responses for the list room and room details admin API. Contributed by @andrewdoh. \ No newline at end of file diff --git a/changelog.d/13208.misc b/changelog.d/13208.misc deleted file mode 100644 index 56d3df319feb..000000000000 --- a/changelog.d/13208.misc +++ /dev/null @@ -1 +0,0 @@ -add room_type to response in list room and room details API. Contributed by @andrewdoh. diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index 23b5824853d8..12898dea6f98 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -59,7 +59,7 @@ The following fields are possible in the JSON response body: - `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. - `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. - `state_events` - Total number of state_events of a room. Complexity of the room. - - `room_type` - the type of the room. It is `null` for normal rooms and `m.space` for spaces. + - `room_type` - The type of the room. It is specified as `m.space` for spaces and `null` otherwise. However, unspecificed room types are also permitted. * `offset` - The current pagination offset in rooms. This parameter should be used instead of `next_token` for room offset as `next_token` is not intended to be parsed. @@ -300,7 +300,7 @@ The following fields are possible in the JSON response body: * `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. * `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. * `state_events` - Total number of state_events of a room. Complexity of the room. -* `room_type` - the type of the room. It is `null` for normal rooms and `m.space` for spaces. +* `room_type` - The type of the room. It is specified as `m.space` for spaces and `null` otherwise. However, unspecificed room types are also permitted. The API is: diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index 83b0aef1acb1..d6d485507b17 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -596,7 +596,8 @@ async def get_rooms_paginate( SELECT state.room_id, state.name, state.canonical_alias, curr.joined_members, curr.local_users_in_room, rooms.room_version, rooms.creator, state.encryption, state.is_federatable, rooms.is_public, state.join_rules, - state.guest_access, state.history_visibility, curr.current_state_events, state.room_type + state.guest_access, state.history_visibility, curr.current_state_events, + state.room_type FROM room_stats_state state INNER JOIN room_stats_current curr USING (room_id) INNER JOIN rooms USING (room_id) diff --git a/tests/rest/admin/test_room.py b/tests/rest/admin/test_room.py index e7085c2d6e20..2526136ff813 100644 --- a/tests/rest/admin/test_room.py +++ b/tests/rest/admin/test_room.py @@ -1131,6 +1131,8 @@ def test_list_rooms(self) -> None: self.assertIn("history_visibility", r) self.assertIn("state_events", r) self.assertIn("room_type", r) + self.assertIsNone(r["room_type"]) + # Check that the correct number of total rooms was returned self.assertEqual(channel.json_body["total_rooms"], total_rooms) From 01c97a995e719573f97db3a8862918faa21b1b48 Mon Sep 17 00:00:00 2001 From: andrew do Date: Mon, 11 Jul 2022 11:00:54 -0700 Subject: [PATCH 12/15] Update docs/admin_api/rooms.md Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> --- docs/admin_api/rooms.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index 12898dea6f98..4c0252d98fda 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -59,7 +59,7 @@ The following fields are possible in the JSON response body: - `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. - `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. - `state_events` - Total number of state_events of a room. Complexity of the room. - - `room_type` - The type of the room. It is specified as `m.space` for spaces and `null` otherwise. However, unspecificed room types are also permitted. + - `room_type` - The type of the room taken from the room's creation event; for example "m.space" if the room is a space. If the room does not define a type, the value will be `null`. * `offset` - The current pagination offset in rooms. This parameter should be used instead of `next_token` for room offset as `next_token` is not intended to be parsed. From 2162421657bc4aa77265df462d4bc2ef19b0ef70 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 12 Jul 2022 13:51:26 +0100 Subject: [PATCH 13/15] update the other room_type entry as well --- docs/admin_api/rooms.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index 4c0252d98fda..9aa489e4a32a 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -300,7 +300,8 @@ The following fields are possible in the JSON response body: * `guest_access` - Whether guests can join the room. One of: ["can_join", "forbidden"]. * `history_visibility` - Who can see the room history. One of: ["invited", "joined", "shared", "world_readable"]. * `state_events` - Total number of state_events of a room. Complexity of the room. -* `room_type` - The type of the room. It is specified as `m.space` for spaces and `null` otherwise. However, unspecificed room types are also permitted. +* `room_type` - The type of the room taken from the room's creation event; for example "m.space" if the room is a space. + If the room does not define a type, the value will be `null`. The API is: From 7dd8b26b9472c391b442de30d3e0cc89daaa47b4 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 12 Jul 2022 14:27:15 +0100 Subject: [PATCH 14/15] Make use of the newly exposed room_type in _build_room_entry --- synapse/handlers/room_summary.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/synapse/handlers/room_summary.py b/synapse/handlers/room_summary.py index 13098f56ed26..852c4214f23a 100644 --- a/synapse/handlers/room_summary.py +++ b/synapse/handlers/room_summary.py @@ -20,7 +20,6 @@ import attr from synapse.api.constants import ( - EventContentFields, EventTypes, HistoryVisibility, JoinRules, @@ -706,9 +705,6 @@ async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDic current_state_ids = await self._storage_controllers.state.get_current_state_ids( room_id ) - create_event = await self._store.get_event( - current_state_ids[(EventTypes.Create, "")] - ) entry = { "room_id": stats["room_id"], @@ -722,7 +718,7 @@ async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDic stats["history_visibility"] == HistoryVisibility.WORLD_READABLE ), "guest_can_join": stats["guest_access"] == "can_join", - "room_type": create_event.content.get(EventContentFields.ROOM_TYPE), + "room_type": stats["room_type"], } if self._msc3266_enabled: From 3fca4c08dacdc4ff83d9ba2031e907827f26198e Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 12 Jul 2022 14:33:14 +0100 Subject: [PATCH 15/15] Revert "Make use of the newly exposed room_type in _build_room_entry" This reverts commit 7dd8b26b9472c391b442de30d3e0cc89daaa47b4. --- synapse/handlers/room_summary.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/synapse/handlers/room_summary.py b/synapse/handlers/room_summary.py index 852c4214f23a..13098f56ed26 100644 --- a/synapse/handlers/room_summary.py +++ b/synapse/handlers/room_summary.py @@ -20,6 +20,7 @@ import attr from synapse.api.constants import ( + EventContentFields, EventTypes, HistoryVisibility, JoinRules, @@ -705,6 +706,9 @@ async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDic current_state_ids = await self._storage_controllers.state.get_current_state_ids( room_id ) + create_event = await self._store.get_event( + current_state_ids[(EventTypes.Create, "")] + ) entry = { "room_id": stats["room_id"], @@ -718,7 +722,7 @@ async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDic stats["history_visibility"] == HistoryVisibility.WORLD_READABLE ), "guest_can_join": stats["guest_access"] == "can_join", - "room_type": stats["room_type"], + "room_type": create_event.content.get(EventContentFields.ROOM_TYPE), } if self._msc3266_enabled: