Skip to content

Commit

Permalink
model: Add function to handle realm_user update events.
Browse files Browse the repository at this point in the history
This commit contains Model._handle_realm_user_event() which updates the meta
data of the respective user in Model.initial_data["realm_users"].

A test is added for this in test_model.

Function and test logic slightly adjusted by neiljp to support differing event
and user data fields, as is necessary for new_email/email, without adjusting
the original API data-structure.

Note that after such an event, we currently rely upon the regular update of
users to propagate most of the new data around the application.
  • Loading branch information
mounilKshah authored and neiljp committed Sep 25, 2022
1 parent 8571383 commit 01b3853
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
"update_display_settings",
"user_settings",
"realm_emoji",
"realm_user",
]
fetch_event_types = [
"realm",
Expand Down Expand Up @@ -3346,6 +3347,57 @@ def test__handle_subscription_event_subscribers_one_user_multiple_streams(
new_subscribers = model.stream_dict[stream_id]["subscribers"]
assert new_subscribers == expected_subscribers

@pytest.mark.parametrize(
"person, event_field, updated_field_if_different",
[
(
{"full_name": "New Full Name"},
"full_name",
None,
),
({"timezone": "New Timezone"}, "timezone", None),
({"is_billing_admin": False}, "is_billing_admin", None),
({"role": 10}, "role", None),
(
{"avatar_url": "new_avatar_url", "avatar_version": 21},
"avatar_url",
None,
),
({"new_email": "new_display@email.com"}, "new_email", "email"),
(
{"delivery_email": "new_delivery@email.com"},
"delivery_email",
None,
),
],
ids=[
"full_name",
"timezone",
"billing_admin_role",
"role",
"avatar",
"display_email",
"delivery_email",
],
)
def test__handle_realm_user_event(
self, person, event_field, updated_field_if_different, model, initial_data
):
# id 11 matches initial_data["realm_users"][1] in the initial_data fixture
person["user_id"] = 11
event = {"type": "realm_user", "op": "update", "id": 1000, "person": person}

model._handle_realm_user_event(event)

if updated_field_if_different is not None:
new_data_field = updated_field_if_different
else:
new_data_field = event_field
assert (
initial_data["realm_users"][1][new_data_field]
== event["person"][event_field]
)

@pytest.mark.parametrize("value", [True, False])
def test__handle_user_settings_event(self, mocker, model, value):
setting = "send_private_typing_notifications"
Expand Down
17 changes: 17 additions & 0 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(self, controller: Any) -> None:
("update_display_settings", self._handle_update_display_settings_event),
("user_settings", self._handle_user_settings_event),
("realm_emoji", self._handle_update_emoji_event),
("realm_user", self._handle_realm_user_event),
]
)

Expand Down Expand Up @@ -1830,6 +1831,22 @@ def _handle_update_display_settings_event(self, event: Event) -> None:
view.message_view.log[msg_pos] = msg_w_list[0]
self.controller.update_screen()

def _handle_realm_user_event(self, event: Event) -> None:
"""
Handle change to user(s) metadata (Eg: full_name, timezone, etc.)
"""
assert event["type"] == "realm_user"
if event["op"] == "update":
updated_details = event["person"]
for realm_user in self.initial_data["realm_users"]:
if realm_user["user_id"] == updated_details["user_id"]:
# realm_users has 'email' attribute and not 'new_email'
if "new_email" in updated_details:
realm_user["email"] = updated_details["new_email"]
else:
realm_user.update(updated_details)
break

def _register_desired_events(self, *, fetch_data: bool = False) -> str:
fetch_types = None if not fetch_data else self.initial_data_to_fetch
event_types = list(self.event_actions)
Expand Down

0 comments on commit 01b3853

Please sign in to comment.