events: Extract apply_event helper.

This mostly just saves us a level of messy indentation.
This commit is contained in:
Tim Abbott
2017-02-20 11:09:48 -08:00
parent e4fbad95c6
commit f52d812a71
2 changed files with 203 additions and 199 deletions

View File

@@ -36,7 +36,7 @@ interacting with the database in `zerver/lib/actions.py`. It should
update the database and send an event announcing the change. update the database and send an event announcing the change.
**Application state:** Modify the `fetch_initial_state_data` and **Application state:** Modify the `fetch_initial_state_data` and
`apply_events` functions in `zerver/lib/actions.py` to update the state `apply_event` functions in `zerver/lib/actions.py` to update the state
based on the event you just created. based on the event you just created.
**Backend implementation:** Make any other modifications to the backend **Backend implementation:** Make any other modifications to the backend
@@ -182,16 +182,16 @@ realm. :
You then need to add code that will handle the event and update the You then need to add code that will handle the event and update the
application state. In `zerver/lib/events.py` update the application state. In `zerver/lib/events.py` update the
`fetch_initial_state` and `apply_events` functions. : `fetch_initial_state` and `apply_event` functions. :
def fetch_initial_state_data(user_profile, event_types, queue_id): def fetch_initial_state_data(user_profile, event_types, queue_id, include_subscribers=True):
# ... # ...
state['realm_invite_by_admins_only'] = user_profile.realm.invite_by_admins_only` state['realm_invite_by_admins_only'] = user_profile.realm.invite_by_admins_only`
In this case you don't need to change `apply_events` because there is In this case you don't need to change `apply_event` because there is
already code that will correctly handle the realm update event type: : already code that will correctly handle the realm update event type: :
def apply_events(state, events, user_profile): def apply_event(state, events, user_profile, include_subscribers):
for event in events: for event in events:
# ... # ...
elif event['type'] == 'realm': elif event['type'] == 'realm':

View File

@@ -152,6 +152,10 @@ def fetch_initial_state_data(user_profile, event_types, queue_id,
def apply_events(state, events, user_profile, include_subscribers=True): def apply_events(state, events, user_profile, include_subscribers=True):
# type: (Dict[str, Any], Iterable[Dict[str, Any]], UserProfile, bool) -> None # type: (Dict[str, Any], Iterable[Dict[str, Any]], UserProfile, bool) -> None
for event in events: for event in events:
apply_event(state, event, user_profile, include_subscribers)
def apply_event(state, event, user_profile, include_subscribers):
# type: (Dict[str, Any], Dict[str, Any], UserProfile, bool) -> None
if event['type'] == "message": if event['type'] == "message":
state['max_message_id'] = max(state['max_message_id'], event['message']['id']) state['max_message_id'] = max(state['max_message_id'], event['message']['id'])
elif event['type'] == "pointer": elif event['type'] == "pointer":
@@ -241,7 +245,7 @@ def apply_events(state, events, user_profile, include_subscribers=True):
state['realm_' + key] = value state['realm_' + key] = value
elif event['type'] == "subscription": elif event['type'] == "subscription":
if not include_subscribers and event['op'] in ['peer_add', 'peer_remove']: if not include_subscribers and event['op'] in ['peer_add', 'peer_remove']:
continue return
if event['op'] in ["add"]: if event['op'] in ["add"]:
if include_subscribers: if include_subscribers: