This repository has been 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
Make room alias lists peekable #6949
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 @@ | ||
Implement `GET /_matrix/client/r0/rooms/{roomId}/aliases` endpoint as per [MSC2432](https://github.com/matrix-org/matrix-doc/pull/2432). |
This file contains 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
# limitations under the License. | ||
|
||
import logging | ||
from typing import Optional | ||
|
||
from six import itervalues | ||
|
||
|
@@ -35,6 +36,7 @@ | |
) | ||
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS | ||
from synapse.config.server import is_threepid_reserved | ||
from synapse.events import EventBase | ||
from synapse.types import StateMap, UserID | ||
from synapse.util.caches import CACHE_SIZE_FACTOR, register_cache | ||
from synapse.util.caches.lrucache import LruCache | ||
|
@@ -92,71 +94,61 @@ def check_from_context(self, room_version: str, event, context, do_sig_check=Tru | |
) | ||
|
||
@defer.inlineCallbacks | ||
def check_joined_room(self, room_id, user_id, current_state=None): | ||
"""Check if the user is currently joined in the room | ||
def check_user_in_room( | ||
self, | ||
room_id: str, | ||
user_id: str, | ||
current_state: Optional[StateMap[EventBase]] = None, | ||
allow_departed_users: bool = False, | ||
): | ||
"""Check if the user is in the room, or was at some point. | ||
Args: | ||
room_id(str): The room to check. | ||
user_id(str): The user to check. | ||
current_state(dict): Optional map of the current state of the room. | ||
room_id: The room to check. | ||
|
||
user_id: The user to check. | ||
|
||
current_state: Optional map of the current state of the room. | ||
If provided then that map is used to check whether they are a | ||
member of the room. Otherwise the current membership is | ||
loaded from the database. | ||
|
||
allow_departed_users: if True, accept users that were previously | ||
members but have now departed. | ||
|
||
Raises: | ||
AuthError if the user is not in the room. | ||
AuthError if the user is/was not in the room. | ||
Returns: | ||
A deferred membership event for the user if the user is in | ||
the room. | ||
Deferred[Optional[EventBase]]: | ||
Membership event for the user if the user was in the | ||
room. This will be the join event if they are currently joined to | ||
the room. This will be the leave event if they have left the room. | ||
""" | ||
if current_state: | ||
member = current_state.get((EventTypes.Member, user_id), None) | ||
else: | ||
member = yield self.state.get_current_state( | ||
room_id=room_id, event_type=EventTypes.Member, state_key=user_id | ||
) | ||
|
||
self._check_joined_room(member, user_id, room_id) | ||
return member | ||
|
||
@defer.inlineCallbacks | ||
def check_user_was_in_room(self, room_id, user_id): | ||
"""Check if the user was in the room at some point. | ||
Args: | ||
room_id(str): The room to check. | ||
user_id(str): The user to check. | ||
Raises: | ||
AuthError if the user was never in the room. | ||
Returns: | ||
A deferred membership event for the user if the user was in the | ||
room. This will be the join event if they are currently joined to | ||
the room. This will be the leave event if they have left the room. | ||
""" | ||
member = yield self.state.get_current_state( | ||
room_id=room_id, event_type=EventTypes.Member, state_key=user_id | ||
) | ||
membership = member.membership if member else None | ||
|
||
if membership not in (Membership.JOIN, Membership.LEAVE): | ||
raise AuthError(403, "User %s not in room %s" % (user_id, room_id)) | ||
if membership == Membership.JOIN: | ||
return member | ||
|
||
if membership == Membership.LEAVE: | ||
# XXX this looks totally bogus. Why do we not allow users who have been banned, | ||
# or those who were members previously and have been re-invited? | ||
if allow_departed_users and membership == Membership.LEAVE: | ||
forgot = yield self.store.did_forget(user_id, room_id) | ||
if forgot: | ||
raise AuthError(403, "User %s not in room %s" % (user_id, room_id)) | ||
if not forgot: | ||
return member | ||
|
||
return member | ||
raise AuthError(403, "User %s not in room %s" % (user_id, room_id)) | ||
|
||
@defer.inlineCallbacks | ||
def check_host_in_room(self, room_id, host): | ||
with Measure(self.clock, "check_host_in_room"): | ||
latest_event_ids = yield self.store.is_host_joined(room_id, host) | ||
return latest_event_ids | ||
|
||
def _check_joined_room(self, member, user_id, room_id): | ||
if not member or member.membership != Membership.JOIN: | ||
raise AuthError( | ||
403, "User %s not in room %s (%s)" % (user_id, room_id, repr(member)) | ||
) | ||
|
||
def can_federate(self, event, auth_events): | ||
creation_event = auth_events.get((EventTypes.Create, "")) | ||
|
||
|
@@ -560,7 +552,7 @@ def check_can_change_room_list(self, room_id, user): | |
return True | ||
|
||
user_id = user.to_string() | ||
yield self.check_joined_room(room_id, user_id) | ||
yield self.check_user_in_room(room_id, user_id) | ||
|
||
# We currently require the user is a "moderator" in the room. We do this | ||
# by checking if they would (theoretically) be able to change the | ||
|
@@ -633,10 +625,18 @@ def get_access_token_from_request(request): | |
return query_params[0].decode("ascii") | ||
|
||
@defer.inlineCallbacks | ||
def check_in_room_or_world_readable(self, room_id, user_id): | ||
def check_user_in_room_or_world_readable( | ||
self, room_id: str, user_id: str, allow_departed_users: bool = False | ||
): | ||
"""Checks that the user is or was in the room or the room is world | ||
readable. If it isn't then an exception is raised. | ||
|
||
Args: | ||
room_id: room to check | ||
user_id: user to check | ||
allow_departed_users: if True, accept users that were previously | ||
members but have now departed | ||
|
||
Returns: | ||
Deferred[tuple[str, str|None]]: Resolves to the current membership of | ||
the user in the room and the membership event ID of the user. If | ||
|
@@ -645,12 +645,14 @@ def check_in_room_or_world_readable(self, room_id, user_id): | |
""" | ||
|
||
try: | ||
# check_user_was_in_room will return the most recent membership | ||
# check_user_in_room will return the most recent membership | ||
# event for the user if: | ||
# * The user is a non-guest user, and was ever in the room | ||
# * The user is a guest user, and has joined the room | ||
# else it will throw. | ||
member_event = yield self.check_user_was_in_room(room_id, user_id) | ||
member_event = yield self.check_user_in_room( | ||
room_id, user_id, allow_departed_users=allow_departed_users | ||
) | ||
return member_event.membership, member_event.event_id | ||
except AuthError: | ||
visibility = yield self.state.get_current_state( | ||
|
@@ -662,7 +664,9 @@ def check_in_room_or_world_readable(self, room_id, user_id): | |
): | ||
return Membership.JOIN, None | ||
raise AuthError( | ||
403, "Guest access not allowed", errcode=Codes.GUEST_ACCESS_FORBIDDEN | ||
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 was never anything to do with guest access, so the error message was wrong and confusing; I've fixed it up while I'm here. |
||
403, | ||
"User %s not in room %s, and room previews are disabled" | ||
% (user_id, room_id), | ||
) | ||
|
||
@defer.inlineCallbacks | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
from twisted.internet import defer | ||
|
||
from synapse.api.constants import EventTypes, Membership | ||
from synapse.api.errors import AuthError, Codes, SynapseError | ||
from synapse.api.errors import SynapseError | ||
from synapse.events.validator import EventValidator | ||
from synapse.handlers.presence import format_user_presence_state | ||
from synapse.logging.context import make_deferred_yieldable, run_in_background | ||
|
@@ -274,8 +274,11 @@ async def room_initial_sync(self, requester, room_id, pagin_config=None): | |
|
||
user_id = requester.user.to_string() | ||
|
||
membership, member_event_id = await self._check_in_room_or_world_readable( | ||
room_id, user_id | ||
( | ||
membership, | ||
member_event_id, | ||
) = await self.auth.check_user_in_room_or_world_readable( | ||
room_id, user_id, allow_departed_users=True, | ||
) | ||
is_peeking = member_event_id is None | ||
|
||
|
@@ -433,25 +436,3 @@ async def get_receipts(): | |
ret["membership"] = membership | ||
|
||
return ret | ||
|
||
async def _check_in_room_or_world_readable(self, room_id, user_id): | ||
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 was an exact cut-and-paste of |
||
try: | ||
# check_user_was_in_room will return the most recent membership | ||
# event for the user if: | ||
# * The user is a non-guest user, and was ever in the room | ||
# * The user is a guest user, and has joined the room | ||
# else it will throw. | ||
member_event = await self.auth.check_user_was_in_room(room_id, user_id) | ||
return member_event.membership, member_event.event_id | ||
except AuthError: | ||
visibility = await self.state_handler.get_current_state( | ||
room_id, EventTypes.RoomHistoryVisibility, "" | ||
) | ||
if ( | ||
visibility | ||
and visibility.content["history_visibility"] == "world_readable" | ||
): | ||
return Membership.JOIN, None | ||
raise AuthError( | ||
403, "Guest access not allowed", errcode=Codes.GUEST_ACCESS_FORBIDDEN | ||
) |
This file contains 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 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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this defaults to
False
for consistency withcheck_user_in_room
, and for safety (if you forget to set it, it's safer if we deny access to users that should have it than grant access to users that should).