diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 287173e448..dad0202421 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -4362,6 +4362,13 @@ def remove_members_from_user_group(user_group, user_profiles): user_ids = [up.id for up in user_profiles] do_send_user_group_members_update_event('remove_members', user_group, user_ids) +def do_send_delete_user_group_event(user_group_id: int, realm_id: int) -> None: + event = dict(type="user_group", + op="remove", + group_id=user_group_id) + send_event(event, active_user_ids(realm_id)) + def check_delete_user_group(user_group_id: int, realm: Realm) -> None: user_group = access_user_group_by_id(user_group_id, realm) user_group.delete() + do_send_delete_user_group_event(user_group_id, realm.id) diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 6d948c6305..bd24152ecc 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -571,6 +571,9 @@ def apply_event(state, event, user_profile, client_gravatar, include_subscribers members = set(user_group['members']) user_group['members'] = list(members - set(event['user_ids'])) user_group['members'].sort() + elif event['op'] == 'remove': + state['realm_user_groups'] = [ug for ug in state['realm_user_groups'] + if ug['id'] != event['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 cedd91a7c9..427214e5b6 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -80,6 +80,7 @@ from zerver.lib.actions import ( do_update_user_group_description, bulk_add_members_to_user_group, remove_members_from_user_group, + check_delete_user_group, ) from zerver.lib.events import ( apply_events, @@ -1061,6 +1062,16 @@ class EventsRegisterTest(ZulipTestCase): error = user_group_remove_member_checker('events[0]', events[0]) self.assert_on_error(error) + # Test delete event + user_group_remove_checker = self.check_events_dict([ + ('type', equals('user_group')), + ('op', equals('remove')), + ('group_id', check_int), + ]) + events = self.do_test(lambda: check_delete_user_group(backend.id, backend.realm)) + error = user_group_remove_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([