This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow ThirdPartyEventRules modules to manipulate public room state #8292
Merged
anoadragon453
merged 23 commits into
develop
from
anoa/third_party_event_rules_public_rooms
Oct 5, 2020
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
68391e4
Add a store method for checking whether a room is in the public dir
anoadragon453 0503eee
Allow ThirdPartyEventRules modules to block publishing a room
anoadragon453 9eae87d
Query ThirdPartyEventRules about public rooms during room creation
anoadragon453 833883e
Query ThirdPartyEventRules while trying to add to the public room dir
anoadragon453 8690c86
Add tests
anoadragon453 6eae2c1
Changelog
anoadragon453 431e3e3
Replace verbose type with StateMap
anoadragon453 529863a
Remove get_room_is_public and use get_room instead
anoadragon453 699fa5b
Address review comments
anoadragon453 f5b6246
Axe the PublicRoomsManager and switch to passing the ModuleApi
anoadragon453 e768fae
Move PublicRoomsManager tests to ModuleApi
anoadragon453 04ab21c
Create a PublicRoomListManager as part of ModuleApi
anoadragon453 06deecf
Backwards incompatibility warning for ThirdPartyEventRules in UPGRADE…
anoadragon453 6ed082d
Some minor fixes
anoadragon453 83a20a2
Merge branch 'develop' of github.com:matrix-org/synapse into anoa/thi…
anoadragon453 0f4c1bb
Update unit tests
anoadragon453 3bde820
Fix call to old method name
anoadragon453 8d9b166
Update UPGRADE.rst, remove extra entry, convert to rst
anoadragon453 1ff7df9
Move PublicRoomListManager below ModuleApi
anoadragon453 e5b2d20
Remove hs param from the PublicRoomListManager docstring
anoadragon453 5730d17
Expose http_client and public_room_list_manager as properties
anoadragon453 d46396d
Merge branch 'develop' of github.com:matrix-org/synapse into anoa/thi…
anoadragon453 49e71a8
Move upgrade notes comment from v1.21.0 to v1.22.0
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow ThirdPartyEventRules modules to query and manipulate whether a room is in the public rooms directory. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,45 @@ | |
# 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 typing import Dict, Tuple | ||
|
||
from synapse.events import EventBase | ||
from synapse.events.snapshot import EventContext | ||
from synapse.types import Requester | ||
|
||
|
||
class PublicRoomsManager: | ||
def __init__(self, hs): | ||
self._store = hs.get_datastore() | ||
|
||
async def room_is_in_public_directory(self, room_id: str) -> bool: | ||
"""Checks whether a room is in the public rooms directory. | ||
|
||
Args: | ||
room_id: The ID of the room. | ||
|
||
Returns: | ||
Whether the room is in the public rooms directory. | ||
""" | ||
return await self._store.get_room_is_public(room_id) | ||
|
||
async def add_room_to_public_directory(self, room_id: str) -> None: | ||
"""Publishes a room to the public rooms directory. | ||
|
||
Args: | ||
room_id: The ID of the room. | ||
""" | ||
await self._store.set_room_is_public(room_id, True) | ||
|
||
async def remove_room_from_public_directory(self, room_id: str) -> None: | ||
"""Removes a room from the public rooms directory. | ||
|
||
Args: | ||
room_id: The ID of the room. | ||
""" | ||
await self._store.set_room_is_public(room_id, False) | ||
|
||
|
||
class ThirdPartyEventRules: | ||
"""Allows server admins to provide a Python module implementing an extra | ||
set of rules to apply when processing events. | ||
|
@@ -38,7 +71,9 @@ def __init__(self, hs): | |
|
||
if module is not None: | ||
self.third_party_rules = module( | ||
config=config, http_client=hs.get_simple_http_client() | ||
config=config, | ||
http_client=hs.get_simple_http_client(), | ||
public_room_manager=PublicRoomsManager(hs), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break compat with classes which don't have a maybe in this case it's best to just document this as a breaking change. But if we're going to do so can we make it future-proof by passing in a moduleapi and getting rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, need to stick a note in about the breaking change. |
||
) | ||
|
||
async def check_event_allowed( | ||
|
@@ -106,14 +141,49 @@ async def check_threepid_can_be_invited( | |
if self.third_party_rules is None: | ||
return True | ||
|
||
state_events = await self._get_state_events_dict_for_room(room_id) | ||
|
||
ret = await self.third_party_rules.check_threepid_can_be_invited( | ||
medium, address, state_events | ||
) | ||
return ret | ||
|
||
async def check_room_can_be_added_to_public_rooms_directory( | ||
self, room_id: str | ||
) -> bool: | ||
"""Check if a room is allowed to be published to the public rooms directory. | ||
|
||
Args: | ||
room_id: The ID of the room. | ||
|
||
Returns: | ||
Whether the room is allowed to be published to the public rooms directory. | ||
""" | ||
if self.third_party_rules is None: | ||
return True | ||
|
||
state_events = await self._get_state_events_dict_for_room(room_id) | ||
|
||
return await self.third_party_rules.check_room_can_be_added_to_public_rooms_directory( | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
room_id, state_events | ||
) | ||
|
||
async def _get_state_events_dict_for_room( | ||
self, room_id: str | ||
) -> Dict[Tuple[str, str], EventBase]: | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Given a room ID, return the state events of that room. | ||
|
||
Args: | ||
room_id: The ID of the room. | ||
|
||
Returns: | ||
A dict mapping (event type, state key) to state event. | ||
""" | ||
state_ids = await self.store.get_filtered_current_state_ids(room_id) | ||
room_state_events = await self.store.get_events(state_ids.values()) | ||
|
||
state_events = {} | ||
for key, event_id in state_ids.items(): | ||
state_events[key] = room_state_events[event_id] | ||
|
||
ret = await self.third_party_rules.check_threepid_can_be_invited( | ||
medium, address, state_events | ||
) | ||
return ret | ||
return state_events |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.