Skip to content

Commit

Permalink
user-groups: Remove deactivated users from groups
Browse files Browse the repository at this point in the history
As newly required to support Zulip Server 10+; see zulip#5899.

Fixes: zulip#5899
  • Loading branch information
chrisbobbe committed Nov 12, 2024
1 parent 5107a89 commit c5d0795
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/user-groups/__tests__/userGroupsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import deepFreeze from 'deep-freeze';

import * as eg from '../../__tests__/lib/exampleData';
import {
EVENT,
EVENT_USER_GROUP_ADD,
EVENT_USER_GROUP_REMOVE,
EVENT_USER_GROUP_UPDATE,
EVENT_USER_GROUP_ADD_MEMBERS,
EVENT_USER_GROUP_REMOVE_MEMBERS,
} from '../../actionConstants';
import { EventTypes } from '../../api/eventTypes';
import userGroupsReducer from '../userGroupsReducer';

describe('userGroupsReducer', () => {
Expand Down Expand Up @@ -192,4 +194,35 @@ describe('userGroupsReducer', () => {
).toEqual([{ ...group1, members: [user1.user_id, user4.user_id] }, group2]);
});
});

describe('realm_user op: update', () => {
test('a user is deactivated', () => {
const user1 = eg.makeUser();
const user2 = eg.makeUser();
const user3 = eg.makeUser();

const group1 = eg.makeUserGroup({ members: [user1.user_id, user2.user_id, user3.user_id] });
const group2 = eg.makeUserGroup({ members: [user2.user_id, user1.user_id] });
const group3 = eg.makeUserGroup({ members: [user1.user_id] });

const person = { userToDeactivate: user1 };
const action = deepFreeze({
type: EVENT,
event: { id: 1, type: EventTypes.realm_user, op: 'update', person },
});

const prevState = deepFreeze([group1, group2, group3]);

const actualState = userGroupsReducer(prevState, action);

expect(actualState).toEqual([
{ ...group1, members: [user2.user_id, user3.user_id] },
{ ...group2, members: [user2.user_id] },

// A newly-empty group is not pruned; when a group is deactivated,
// we expect a user_group/remove event.
{ ...group3, members: [] },
]);
});
});
});
27 changes: 27 additions & 0 deletions src/user-groups/userGroupsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import type { UserGroupsState, PerAccountApplicableAction } from '../types';
import {
REGISTER_COMPLETE,
EVENT,
EVENT_USER_GROUP_ADD,
EVENT_USER_GROUP_REMOVE,
EVENT_USER_GROUP_UPDATE,
EVENT_USER_GROUP_ADD_MEMBERS,
EVENT_USER_GROUP_REMOVE_MEMBERS,
RESET_ACCOUNT_DATA,
} from '../actionConstants';
import { EventTypes } from '../api/eventTypes';
import { NULL_ARRAY } from '../nullObjects';

const initialState: UserGroupsState = NULL_ARRAY;
Expand Down Expand Up @@ -69,6 +71,31 @@ export default (
case EVENT_USER_GROUP_REMOVE_MEMBERS:
return eventUserGroupRemoveMembers(state, action);

case EVENT: {
const { event } = action;
switch (event.type) {
case EventTypes.realm_user: {
switch (event.op) {
case 'update': {
const { person } = event;
if (person.userToDeactivate != null) {
const userId = person.userToDeactivate.user_id;
return state.map(g => ({ ...g, members: g.members.filter(m => m !== userId) }));
}

return state;
}

default:
return state;
}
}

default:
return state;
}
}

default:
return state;
}
Expand Down

0 comments on commit c5d0795

Please sign in to comment.