From 61371cbe9a841d9ba493b4e6b1bd9f3214d2afd8 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Tue, 21 May 2019 22:17:30 +0530 Subject: [PATCH] user_info: Add `bot_owner_id` to user info dataset. Modifies the dict with the user info to include the key `bot_owner_id` so it can be displayed in the user info popover. Tests concerned with changing bot owner have been modified to have number of events=2 because while updating the bot info, two events are fired -- updating the `realm_bot` and `realm_user` since the key `bot_owner_id` is a part of realm user info. --- static/js/user_events.js | 4 ++++ zerver/lib/actions.py | 34 ++++++++++++++++++++++++---------- zerver/lib/cache.py | 2 +- zerver/lib/events.py | 5 ++++- zerver/tests/test_events.py | 33 ++++++++++++++++++++++++--------- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/static/js/user_events.js b/static/js/user_events.js index 5cf3226ad8..97a7f387dd 100644 --- a/static/js/user_events.js +++ b/static/js/user_events.js @@ -91,6 +91,10 @@ exports.update_person = function update(person) { if (_.has(person, 'timezone')) { person_obj.timezone = person.timezone; } + + if (_.has(person, 'bot_owner_id')) { + person_obj.bot_owner_id = person.bot_owner_id; + } }; return exports; diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 7a118b5324..c9bc26a0da 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -407,16 +407,18 @@ def process_new_human_user(user_profile: UserProfile, lambda event: None) def notify_created_user(user_profile: UserProfile) -> None: - event = dict(type="realm_user", op="add", - person=dict(email=user_profile.email, - user_id=user_profile.id, - is_admin=user_profile.is_realm_admin, - full_name=user_profile.full_name, - avatar_url=avatar_url(user_profile), - timezone=user_profile.timezone, - date_joined=user_profile.date_joined.isoformat(), - is_guest=user_profile.is_guest, - is_bot=user_profile.is_bot)) # type: Dict[str, Any] + person = dict(email=user_profile.email, + user_id=user_profile.id, + is_admin=user_profile.is_realm_admin, + full_name=user_profile.full_name, + avatar_url=avatar_url(user_profile), + timezone=user_profile.timezone, + date_joined=user_profile.date_joined.isoformat(), + is_guest=user_profile.is_guest, + is_bot=user_profile.is_bot) # type: Dict[str, Any] + if user_profile.is_bot and user_profile.bot_owner_id is not None: + person["bot_owner_id"] = user_profile.bot_owner_id + event = dict(type="realm_user", op="add", person=person) # type: Dict[str, Any] if not user_profile.is_bot: event["person"]["profile_data"] = {} send_event(user_profile.realm, event, active_user_ids(user_profile.realm_id)) @@ -3229,6 +3231,18 @@ def do_change_bot_owner(user_profile: UserProfile, bot_owner: UserProfile, )), update_users) + # Since `bot_owner_id` is included in the user profile dict we need + # to update the users dict with the new bot owner id + event = dict( + type="realm_user", + op="update", + person=dict( + user_id=user_profile.id, + bot_owner_id=user_profile.bot_owner.id, + ), + ) # type: Dict[str, Any] + send_event(user_profile.realm, event, active_user_ids(user_profile.realm_id)) + def do_change_tos_version(user_profile: UserProfile, tos_version: str) -> None: user_profile.tos_version = tos_version user_profile.save(update_fields=["tos_version"]) diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 003f06b342..f69b9dd103 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -325,7 +325,7 @@ realm_user_dict_fields = [ 'id', 'full_name', 'short_name', 'email', 'avatar_source', 'avatar_version', 'is_active', 'is_realm_admin', 'is_bot', 'realm_id', 'timezone', - 'date_joined', 'is_guest' + 'date_joined', 'is_guest', 'bot_owner_id' ] # type: List[str] def realm_user_dicts_cache_key(realm_id: int) -> str: diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 361b398dd5..ef547174ee 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -101,7 +101,10 @@ def get_raw_user_data(realm: Realm, client_gravatar: bool) -> Dict[int, Dict[str is_active = row['is_active'], date_joined = row['date_joined'].isoformat(), ) - if not is_bot: + if is_bot: + if row['bot_owner_id'] is not None: + result['bot_owner_id'] = row['bot_owner_id'] + else: result['profile_data'] = profiles_by_user_id.get(row['id'], {}) return result diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 632a312445..5da8bc8ef3 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -2138,7 +2138,16 @@ class EventsRegisterTest(ZulipTestCase): self.assert_on_error(error) def test_change_bot_owner(self) -> None: - change_bot_owner_checker = self.check_events_dict([ + change_bot_owner_checker_user = self.check_events_dict([ + ('type', equals('realm_user')), + ('op', equals('update')), + ('person', check_dict_only([ + ('user_id', check_int), + ('bot_owner_id', check_int), + ])), + ]) + + change_bot_owner_checker_bot = self.check_events_dict([ ('type', equals('realm_bot')), ('op', equals('update')), ('bot', check_dict_only([ @@ -2151,11 +2160,13 @@ class EventsRegisterTest(ZulipTestCase): owner = self.example_user('hamlet') bot = self.create_bot('test') action = lambda: do_change_bot_owner(bot, owner, self.user_profile) - events = self.do_test(action) - error = change_bot_owner_checker('events[0]', events[0]) + events = self.do_test(action, num_events=2) + error = change_bot_owner_checker_bot('events[0]', events[0]) + self.assert_on_error(error) + error = change_bot_owner_checker_user('events[1]', events[1]) self.assert_on_error(error) - change_bot_owner_checker = self.check_events_dict([ + change_bot_owner_checker_bot = self.check_events_dict([ ('type', equals('realm_bot')), ('op', equals('delete')), ('bot', check_dict_only([ @@ -2167,12 +2178,14 @@ class EventsRegisterTest(ZulipTestCase): owner = self.example_user('hamlet') bot = self.create_bot('test1', full_name='Test1 Testerson') action = lambda: do_change_bot_owner(bot, owner, self.user_profile) - events = self.do_test(action) - error = change_bot_owner_checker('events[0]', events[0]) + events = self.do_test(action, num_events=2) + error = change_bot_owner_checker_bot('events[0]', events[0]) + self.assert_on_error(error) + error = change_bot_owner_checker_user('events[1]', events[1]) self.assert_on_error(error) check_services = check_list(sub_validator=None, length=0) - change_bot_owner_checker = self.check_events_dict([ + change_bot_owner_checker_bot = self.check_events_dict([ ('type', equals('realm_bot')), ('op', equals('add')), ('bot', check_dict_only([ @@ -2194,8 +2207,10 @@ class EventsRegisterTest(ZulipTestCase): self.user_profile = self.example_user('hamlet') bot = self.create_test_bot('test2', previous_owner, full_name='Test2 Testerson') action = lambda: do_change_bot_owner(bot, self.user_profile, previous_owner) - events = self.do_test(action) - error = change_bot_owner_checker('events[0]', events[0]) + events = self.do_test(action, num_events=2) + error = change_bot_owner_checker_bot('events[0]', events[0]) + self.assert_on_error(error) + error = change_bot_owner_checker_user('events[1]', events[1]) self.assert_on_error(error) def test_do_update_outgoing_webhook_service(self):