Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Allow non-guests to peek on rooms using /events
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion authored and review.rocks committed Jan 20, 2016
1 parent ea5eea2 commit da417aa
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 85 deletions.
14 changes: 7 additions & 7 deletions synapse/handlers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _filter_events_for_clients(self, user_tuples, events):
row["event_id"] for rows in forgotten for row in rows
)

def allowed(event, user_id, is_guest):
def allowed(event, user_id, is_peeking):
state = event_id_to_state[event.event_id]

visibility_event = state.get((EventTypes.RoomHistoryVisibility, ""), None)
Expand All @@ -96,7 +96,7 @@ def allowed(event, user_id, is_guest):
if visibility == "world_readable":
return True

if is_guest:
if is_peeking:
return False

membership_event = state.get((EventTypes.Member, user_id), None)
Expand All @@ -112,7 +112,7 @@ def allowed(event, user_id, is_guest):
return True

if event.type == EventTypes.RoomHistoryVisibility:
return not is_guest
return not is_peeking

if visibility == "shared":
return True
Expand All @@ -127,15 +127,15 @@ def allowed(event, user_id, is_guest):
user_id: [
event
for event in events
if allowed(event, user_id, is_guest)
if allowed(event, user_id, is_peeking)
]
for user_id, is_guest in user_tuples
for user_id, is_peeking in user_tuples
})

@defer.inlineCallbacks
def _filter_events_for_client(self, user_id, events, is_guest=False):
def _filter_events_for_client(self, user_id, events, is_peeking=False):
# Assumes that user has at some point joined the room if not is_guest.
res = yield self._filter_events_for_clients([(user_id, is_guest)], events)
res = yield self._filter_events_for_clients([(user_id, is_peeking)], events)
defer.returnValue(res.get(user_id, []))

def ratelimit(self, user_id):
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_stream(self, auth_user_id, pagin_config, timeout=0,
events, tokens = yield self.notifier.get_events_for(
auth_user, pagin_config, timeout,
only_room_events=only_room_events,
is_guest=is_guest, guest_room_id=room_id
is_guest=is_guest, explicit_room_id=room_id
)

time_now = self.clock.time_msec()
Expand Down
93 changes: 46 additions & 47 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@ def get_message(self, msg_id=None, room_id=None, sender_id=None,
defer.returnValue(None)

@defer.inlineCallbacks
def get_messages(self, user_id=None, room_id=None, pagin_config=None,
as_client_event=True, is_guest=False):
def get_messages(self, requester, room_id=None, pagin_config=None,
as_client_event=True):
"""Get messages in a room.
Args:
user_id (str): The user requesting messages.
requester (Requester): The user requesting messages.
room_id (str): The room they want messages from.
pagin_config (synapse.api.streams.PaginationConfig): The pagination
config rules to apply, if any.
as_client_event (bool): True to get events in client-server format.
is_guest (bool): Whether the requesting user is a guest (as opposed
to a fully registered user).
Returns:
dict: Pagination API results
"""
user_id = requester.user.to_string()
data_source = self.hs.get_event_sources().sources["room"]

if pagin_config.from_token:
Expand All @@ -115,36 +114,33 @@ def get_messages(self, user_id=None, room_id=None, pagin_config=None,

source_config = pagin_config.get_source_config("room")

if not is_guest:
member_event = yield self.auth.check_user_was_in_room(room_id, user_id)
if member_event.membership == Membership.LEAVE:
# If they have left the room then clamp the token to be before
# they left the room.
# If they're a guest, we'll just 403 them if they're asking for
# events they can't see.
leave_token = yield self.store.get_topological_token_for_event(
member_event.event_id
)
leave_token = RoomStreamToken.parse(leave_token)
if leave_token.topological < room_token.topological:
source_config.from_key = str(leave_token)

if source_config.direction == "f":
if source_config.to_key is None:
membership, member_event_id = yield self._check_in_room_or_world_readable(
room_id, user_id
)
if membership == Membership.LEAVE:
# If they have left the room then clamp the token to be before
# they left the room.
leave_token = yield self.store.get_topological_token_for_event(
member_event_id
)
leave_token = RoomStreamToken.parse(leave_token)
if leave_token.topological < room_token.topological:
source_config.from_key = str(leave_token)

if source_config.direction == "f":
if source_config.to_key is None:
source_config.to_key = str(leave_token)
else:
to_token = RoomStreamToken.parse(source_config.to_key)
if leave_token.topological < to_token.topological:
source_config.to_key = str(leave_token)
else:
to_token = RoomStreamToken.parse(source_config.to_key)
if leave_token.topological < to_token.topological:
source_config.to_key = str(leave_token)

yield self.hs.get_handlers().federation_handler.maybe_backfill(
room_id, room_token.topological
)

user = UserID.from_string(user_id)

events, next_key = yield data_source.get_pagination_rows(
user, source_config, room_id
requester.user, source_config, room_id
)

next_token = pagin_config.from_token.copy_and_replace(
Expand All @@ -158,7 +154,11 @@ def get_messages(self, user_id=None, room_id=None, pagin_config=None,
"end": next_token.to_string(),
})

events = yield self._filter_events_for_client(user_id, events, is_guest=is_guest)
events = yield self._filter_events_for_client(
user_id,
events,
is_peeking=(member_event_id is None),
)

time_now = self.clock.time_msec()

Expand Down Expand Up @@ -289,7 +289,7 @@ def get_room_data(self, user_id=None, room_id=None,
SynapseError if something went wrong.
"""
membership, membership_event_id = yield self._check_in_room_or_world_readable(
room_id, user_id, is_guest
room_id, user_id
)

if membership == Membership.JOIN:
Expand All @@ -306,7 +306,7 @@ def get_room_data(self, user_id=None, room_id=None,
defer.returnValue(data)

@defer.inlineCallbacks
def _check_in_room_or_world_readable(self, room_id, user_id, is_guest):
def _check_in_room_or_world_readable(self, room_id, user_id):
try:
# check_user_was_in_room will return the most recent membership
# event for the user if:
Expand All @@ -316,7 +316,7 @@ def _check_in_room_or_world_readable(self, room_id, user_id, is_guest):
member_event = yield self.auth.check_user_was_in_room(room_id, user_id)
defer.returnValue((member_event.membership, member_event.event_id))
return
except AuthError, auth_error:
except AuthError:
visibility = yield self.state_handler.get_current_state(
room_id, EventTypes.RoomHistoryVisibility, ""
)
Expand All @@ -326,8 +326,6 @@ def _check_in_room_or_world_readable(self, room_id, user_id, is_guest):
):
defer.returnValue((Membership.JOIN, None))
return
if not is_guest:
raise auth_error
raise AuthError(
403, "Guest access not allowed", errcode=Codes.GUEST_ACCESS_FORBIDDEN
)
Expand All @@ -345,7 +343,7 @@ def get_state_events(self, user_id, room_id, is_guest=False):
A list of dicts representing state events. [{}, {}, {}]
"""
membership, membership_event_id = yield self._check_in_room_or_world_readable(
room_id, user_id, is_guest
room_id, user_id
)

if membership == Membership.JOIN:
Expand Down Expand Up @@ -556,13 +554,13 @@ def handle_room(event):
defer.returnValue(ret)

@defer.inlineCallbacks
def room_initial_sync(self, user_id, room_id, pagin_config=None, is_guest=False):
def room_initial_sync(self, requester, room_id, pagin_config=None):
"""Capture the a snapshot of a room. If user is currently a member of
the room this will be what is currently in the room. If the user left
the room this will be what was in the room when they left.
Args:
user_id(str): The user to get a snapshot for.
requester(Requester): The user to get a snapshot for.
room_id(str): The room to get a snapshot of.
pagin_config(synapse.streams.config.PaginationConfig):
The pagination config used to determine how many messages to
Expand All @@ -573,19 +571,20 @@ def room_initial_sync(self, user_id, room_id, pagin_config=None, is_guest=False)
A JSON serialisable dict with the snapshot of the room.
"""

user_id = requester.user.to_string()

membership, member_event_id = yield self._check_in_room_or_world_readable(
room_id,
user_id,
is_guest
room_id, user_id,
)
is_peeking = member_event_id is None

if membership == Membership.JOIN:
result = yield self._room_initial_sync_joined(
user_id, room_id, pagin_config, membership, is_guest
user_id, room_id, pagin_config, membership, is_peeking
)
elif membership == Membership.LEAVE:
result = yield self._room_initial_sync_parted(
user_id, room_id, pagin_config, membership, member_event_id, is_guest
user_id, room_id, pagin_config, membership, member_event_id, is_peeking
)

account_data_events = []
Expand All @@ -609,7 +608,7 @@ def room_initial_sync(self, user_id, room_id, pagin_config=None, is_guest=False)

@defer.inlineCallbacks
def _room_initial_sync_parted(self, user_id, room_id, pagin_config,
membership, member_event_id, is_guest):
membership, member_event_id, is_peeking):
room_state = yield self.store.get_state_for_events(
[member_event_id], None
)
Expand All @@ -631,7 +630,7 @@ def _room_initial_sync_parted(self, user_id, room_id, pagin_config,
)

messages = yield self._filter_events_for_client(
user_id, messages, is_guest=is_guest
user_id, messages, is_peeking=is_peeking
)

start_token = StreamToken(token[0], 0, 0, 0, 0)
Expand All @@ -654,7 +653,7 @@ def _room_initial_sync_parted(self, user_id, room_id, pagin_config,

@defer.inlineCallbacks
def _room_initial_sync_joined(self, user_id, room_id, pagin_config,
membership, is_guest):
membership, is_peeking):
current_state = yield self.state.get_current_state(
room_id=room_id,
)
Expand Down Expand Up @@ -718,7 +717,7 @@ def get_receipts():
).addErrback(unwrapFirstError)

messages = yield self._filter_events_for_client(
user_id, messages, is_guest=is_guest,
user_id, messages, is_peeking=is_peeking,
)

start_token = now_token.copy_and_replace("room_key", token[0])
Expand All @@ -737,7 +736,7 @@ def get_receipts():
"presence": presence,
"receipts": receipts,
}
if not is_guest:
if not is_peeking:
ret["membership"] = membership

defer.returnValue(ret)
2 changes: 1 addition & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ def filter_evts(events):
return self._filter_events_for_client(
user.to_string(),
events,
is_guest=is_guest)
is_peeking=is_guest)

event = yield self.store.get_event(event_id, get_prev_content=True,
allow_none=True)
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def current_sync_callback(before_token, after_token):
return self.current_sync_for_user(sync_config, since_token)

result = yield self.notifier.wait_for_events(
sync_config.user, timeout, current_sync_callback,
sync_config.user.to_string(), timeout, current_sync_callback,
from_token=since_token
)
defer.returnValue(result)
Expand Down Expand Up @@ -640,7 +640,7 @@ def load_filtered_recents(self, room_id, sync_config, now_token,
loaded_recents = yield self._filter_events_for_client(
sync_config.user.to_string(),
loaded_recents,
is_guest=sync_config.is_guest,
is_peeking=sync_config.is_guest,
)
loaded_recents.extend(recents)
recents = loaded_recents
Expand Down
Loading

0 comments on commit da417aa

Please sign in to comment.