Skip to content

Commit

Permalink
Pass in current state to push action handler
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Feb 9, 2016
1 parent feb294d commit f28cc45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
31 changes: 13 additions & 18 deletions synapse/handlers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,10 @@ def __init__(self, hs):
self.event_builder_factory = hs.get_event_builder_factory()

@defer.inlineCallbacks
def _filter_events_for_clients(self, user_tuples, events):
def _filter_events_for_clients(self, user_tuples, events, event_id_to_state):
""" Returns dict of user_id -> list of events that user is allowed to
see.
"""
# If there is only one user, just get the state for that one user,
# otherwise just get all the state.
if len(user_tuples) == 1:
types = (
(EventTypes.RoomHistoryVisibility, ""),
(EventTypes.Member, user_tuples[0][0]),
)
else:
types = None

event_id_to_state = yield self.store.get_state_for_events(
frozenset(e.event_id for e in events),
types=types
)

forgotten = yield defer.gatherResults([
self.store.who_forgot_in_room(
room_id,
Expand Down Expand Up @@ -135,7 +120,17 @@ def allowed(event, user_id, is_peeking):
@defer.inlineCallbacks
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_peeking)], events)
types = (
(EventTypes.RoomHistoryVisibility, ""),
(EventTypes.Member, user_id),
)
event_id_to_state = yield self.store.get_state_for_events(
frozenset(e.event_id for e in events),
types=types
)
res = yield self._filter_events_for_clients(
[(user_id, is_peeking)], events, event_id_to_state
)
defer.returnValue(res.get(user_id, []))

def ratelimit(self, user_id):
Expand Down Expand Up @@ -275,7 +270,7 @@ def handle_new_client_event(self, event, context, extra_users=[]):

action_generator = ActionGenerator(self.hs)
yield action_generator.handle_push_actions_for_event(
event, self
event, self, context.current_state
)

destinations = set()
Expand Down
6 changes: 4 additions & 2 deletions synapse/push/action_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, hs):
# tag (ie. we just need all the users).

@defer.inlineCallbacks
def handle_push_actions_for_event(self, event, handler):
def handle_push_actions_for_event(self, event, handler, current_state):
if event.type == EventTypes.Redaction and event.redacts is not None:
yield self.store.remove_push_actions_for_event_id(
event.room_id, event.redacts
Expand All @@ -46,7 +46,9 @@ def handle_push_actions_for_event(self, event, handler):
event.room_id, self.hs, self.store
)

actions_by_user = yield bulk_evaluator.action_for_event_by_user(event, handler)
actions_by_user = yield bulk_evaluator.action_for_event_by_user(
event, handler, current_state
)

yield self.store.set_push_actions_for_event_and_users(
event,
Expand Down
10 changes: 3 additions & 7 deletions synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,21 @@ def __init__(self, room_id, rules_by_user, users_in_room, store):
self.store = store

@defer.inlineCallbacks
def action_for_event_by_user(self, event, handler):
def action_for_event_by_user(self, event, handler, current_state):
actions_by_user = {}

users_dict = yield self.store.are_guests(self.rules_by_user.keys())

filtered_by_user = yield handler._filter_events_for_clients(
users_dict.items(), [event]
users_dict.items(), [event], {event.event_id: current_state}
)

evaluator = PushRuleEvaluatorForEvent(event, len(self.users_in_room))

condition_cache = {}

member_state = yield self.store.get_state_for_event(
event.event_id,
)

display_names = {}
for ev in member_state.values():
for ev in current_state.values():
nm = ev.content.get("displayname", None)
if nm and ev.type == EventTypes.Member:
display_names[ev.state_key] = nm
Expand Down

0 comments on commit f28cc45

Please sign in to comment.