Skip to content

Commit c5d0795

Browse files
committed
user-groups: Remove deactivated users from groups
As newly required to support Zulip Server 10+; see zulip#5899. Fixes: zulip#5899
1 parent 5107a89 commit c5d0795

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/user-groups/__tests__/userGroupsReducer-test.js

+33
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import deepFreeze from 'deep-freeze';
44

55
import * as eg from '../../__tests__/lib/exampleData';
66
import {
7+
EVENT,
78
EVENT_USER_GROUP_ADD,
89
EVENT_USER_GROUP_REMOVE,
910
EVENT_USER_GROUP_UPDATE,
1011
EVENT_USER_GROUP_ADD_MEMBERS,
1112
EVENT_USER_GROUP_REMOVE_MEMBERS,
1213
} from '../../actionConstants';
14+
import { EventTypes } from '../../api/eventTypes';
1315
import userGroupsReducer from '../userGroupsReducer';
1416

1517
describe('userGroupsReducer', () => {
@@ -192,4 +194,35 @@ describe('userGroupsReducer', () => {
192194
).toEqual([{ ...group1, members: [user1.user_id, user4.user_id] }, group2]);
193195
});
194196
});
197+
198+
describe('realm_user op: update', () => {
199+
test('a user is deactivated', () => {
200+
const user1 = eg.makeUser();
201+
const user2 = eg.makeUser();
202+
const user3 = eg.makeUser();
203+
204+
const group1 = eg.makeUserGroup({ members: [user1.user_id, user2.user_id, user3.user_id] });
205+
const group2 = eg.makeUserGroup({ members: [user2.user_id, user1.user_id] });
206+
const group3 = eg.makeUserGroup({ members: [user1.user_id] });
207+
208+
const person = { userToDeactivate: user1 };
209+
const action = deepFreeze({
210+
type: EVENT,
211+
event: { id: 1, type: EventTypes.realm_user, op: 'update', person },
212+
});
213+
214+
const prevState = deepFreeze([group1, group2, group3]);
215+
216+
const actualState = userGroupsReducer(prevState, action);
217+
218+
expect(actualState).toEqual([
219+
{ ...group1, members: [user2.user_id, user3.user_id] },
220+
{ ...group2, members: [user2.user_id] },
221+
222+
// A newly-empty group is not pruned; when a group is deactivated,
223+
// we expect a user_group/remove event.
224+
{ ...group3, members: [] },
225+
]);
226+
});
227+
});
195228
});

src/user-groups/userGroupsReducer.js

+27
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import type { UserGroupsState, PerAccountApplicableAction } from '../types';
33
import {
44
REGISTER_COMPLETE,
5+
EVENT,
56
EVENT_USER_GROUP_ADD,
67
EVENT_USER_GROUP_REMOVE,
78
EVENT_USER_GROUP_UPDATE,
89
EVENT_USER_GROUP_ADD_MEMBERS,
910
EVENT_USER_GROUP_REMOVE_MEMBERS,
1011
RESET_ACCOUNT_DATA,
1112
} from '../actionConstants';
13+
import { EventTypes } from '../api/eventTypes';
1214
import { NULL_ARRAY } from '../nullObjects';
1315

1416
const initialState: UserGroupsState = NULL_ARRAY;
@@ -69,6 +71,31 @@ export default (
6971
case EVENT_USER_GROUP_REMOVE_MEMBERS:
7072
return eventUserGroupRemoveMembers(state, action);
7173

74+
case EVENT: {
75+
const { event } = action;
76+
switch (event.type) {
77+
case EventTypes.realm_user: {
78+
switch (event.op) {
79+
case 'update': {
80+
const { person } = event;
81+
if (person.userToDeactivate != null) {
82+
const userId = person.userToDeactivate.user_id;
83+
return state.map(g => ({ ...g, members: g.members.filter(m => m !== userId) }));
84+
}
85+
86+
return state;
87+
}
88+
89+
default:
90+
return state;
91+
}
92+
}
93+
94+
default:
95+
return state;
96+
}
97+
}
98+
7299
default:
73100
return state;
74101
}

0 commit comments

Comments
 (0)