diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index dab3e964f1..eee28c6b46 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -4299,10 +4299,22 @@ def do_update_user_custom_profile_data(user_profile, data): field_id=field['id'], defaults={'value': field['value']}) +def do_send_create_user_group_event(user_group: UserGroup, members: List[UserProfile]) -> None: + event = dict(type="user_group", + op="add", + group=dict(name=user_group.name, + members=[member.id for member in members], + description=user_group.description, + id=user_group.id, + ), + ) + send_event(event, active_user_ids(user_group.realm_id)) + def check_add_user_group(realm, name, initial_members, description): # type: (Realm, Text, List[UserProfile], Text) -> None try: - create_user_group(name, initial_members, realm, description=description) + user_group = create_user_group(name, initial_members, realm, description=description) + do_send_create_user_group_event(user_group, initial_members) except django.db.utils.IntegrityError: raise JsonableError(_("User group '%s' already exists." % (name,))) diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 40911306df..01f0631256 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -552,6 +552,10 @@ def apply_event(state, event, user_profile, client_gravatar, include_subscribers elif event['type'] == "update_global_notifications": assert event['notification_name'] in UserProfile.notification_setting_types state[event['notification_name']] = event['setting'] + elif event['type'] == "user_group": + if event['op'] == 'add': + state['realm_user_groups'].append(event['group']) + state['realm_user_groups'].sort(key=lambda group: group['id']) else: raise AssertionError("Unexpected event type %s" % (event['type'],)) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index bd531dbae4..89f874d386 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -75,6 +75,7 @@ from zerver.lib.actions import ( log_event, lookup_default_stream_groups, notify_realm_custom_profile_fields, + check_add_user_group, ) from zerver.lib.events import ( apply_events, @@ -986,6 +987,24 @@ class EventsRegisterTest(ZulipTestCase): error = alert_words_checker('events[0]', events[0]) self.assert_on_error(error) + def test_user_group_events(self) -> None: + user_group_add_checker = self.check_events_dict([ + ('type', equals('user_group')), + ('op', equals('add')), + ('group', check_dict_only([ + ('id', check_int), + ('name', check_string), + ('members', check_list(check_int)), + ('description', check_string), + ])), + ]) + othello = self.example_user('othello') + zulip = get_realm('zulip') + events = self.do_test(lambda: check_add_user_group(zulip, 'backend', [othello], + 'Backend team')) + error = user_group_add_checker('events[0]', events[0]) + self.assert_on_error(error) + def test_default_stream_groups_events(self): # type: () -> None default_stream_groups_checker = self.check_events_dict([