fix(identity): keep live group membership in sync with join and leave
A join now counts the member as in the group right away. A leave drops them from the live roster without forgetting who they are. Other sightings still do not mean the person is in the group.
This commit is contained in:
4 files changed
+132
-2
No files matched your search
@@ -682,12 +682,47 @@ export class IdentityService {
|
||||
})));
|
||||
}
|
||||
|
||||
/** Live 花名册: the joiner is in this group now. Does not invent card/role/isRobot.
|
||||
* No-op when the UIN is missing, the group is unknown, or the member is already present. */
|
||||
rememberGroupMemberJoined(
|
||||
groupId: number,
|
||||
identity: { uid?: string; uin?: number },
|
||||
): void {
|
||||
this.beginObservation('group member joined');
|
||||
const uin = normalizeUin(identity.uin);
|
||||
if (uin === null) return;
|
||||
const g = this.groups_.get(groupId);
|
||||
if (!g || g.members.has(uin)) return;
|
||||
const uid = normalizeUid(identity.uid) ?? '';
|
||||
this.rememberUidUin(uid, uin);
|
||||
g.members.set(uin, {
|
||||
uin,
|
||||
uid,
|
||||
nickname: '',
|
||||
card: '',
|
||||
role: 'member',
|
||||
level: 0,
|
||||
title: '',
|
||||
joinTime: 0,
|
||||
lastSentTime: 0,
|
||||
shutUpTime: 0,
|
||||
});
|
||||
}
|
||||
|
||||
markGroupMemberInactive(groupId: number, identity: { uid?: string; uin?: number }): void {
|
||||
this.beginObservation('group member inactive');
|
||||
const observed = { ...identity };
|
||||
const uid = normalizeUid(observed.uid);
|
||||
const uin = normalizeUin(observed.uin);
|
||||
const g = this.groups_.get(groupId);
|
||||
if (g) {
|
||||
if (uin !== null) g.members.delete(uin);
|
||||
else if (uid) {
|
||||
const member = this.findGroupMemberByUid(groupId, uid);
|
||||
if (member) g.members.delete(member.uin);
|
||||
}
|
||||
}
|
||||
this.runWrite('group member inactive', () => {
|
||||
const uid = normalizeUid(observed.uid);
|
||||
const uin = normalizeUin(observed.uin);
|
||||
if (!uid && uin === null) return;
|
||||
const rows = this.findMemberRows(groupId, uid, uin);
|
||||
const updatedAt = nowSeconds();
|
||||
|
||||
@@ -678,6 +678,10 @@ export class IncomingPacketPipeline {
|
||||
uid: event.operatorUid,
|
||||
uin: event.operatorUin,
|
||||
});
|
||||
this.deps.identity.rememberGroupMemberJoined(event.groupId, {
|
||||
uid: event.userUid,
|
||||
uin: event.userUin,
|
||||
});
|
||||
break;
|
||||
case 'group_member_leave':
|
||||
this.deps.identity.markGroupMemberInactive(event.groupId, {
|
||||
|
||||
@@ -138,6 +138,29 @@ describe('IncomingPacketPipeline roster refresh', () => {
|
||||
expect(fetchGroupMemberList).toHaveBeenCalledWith(GROUP_ID);
|
||||
});
|
||||
|
||||
it('known-group join records the joiner immediately and does not invent the operator', async () => {
|
||||
const { pipeline, identity, fetchGroupList, fetchGroupMemberList } = makePipeline({ plantGroup: true });
|
||||
pipeline.registerCmd('test.roster', () => [join({ userUin: 33333, userUid: 'u_new', operatorUin: 44444, operatorUid: 'u_op' })]);
|
||||
await pipeline.process(pkt());
|
||||
|
||||
expect(identity.findGroupMember(GROUP_ID, 33333)?.uid).toBe('u_new');
|
||||
expect(identity.findGroupMember(GROUP_ID, 33333)?.isRobot).toBeUndefined();
|
||||
expect(identity.findGroupMember(GROUP_ID, 44444)).toBeNull();
|
||||
expect(identity.findUinByUid('u_op')).toBe(44444);
|
||||
expect(fetchGroupList).not.toHaveBeenCalled();
|
||||
expect(fetchGroupMemberList).toHaveBeenCalledWith(GROUP_ID);
|
||||
});
|
||||
|
||||
it('known-group leave drops the member immediately and keeps their identity mapping', async () => {
|
||||
const { pipeline, identity } = makePipeline({ plantGroup: true });
|
||||
identity.rememberGroupMembers(GROUP_ID, [makeMember(22222, 'u_member')]);
|
||||
pipeline.registerCmd('test.roster', () => [leave()]);
|
||||
await pipeline.process(pkt());
|
||||
|
||||
expect(identity.findGroupMember(GROUP_ID, 22222)).toBeNull();
|
||||
expect(identity.findUidByUin(22222, GROUP_ID)).toBe('u_member');
|
||||
});
|
||||
|
||||
it('known-group join / leave / admin fetch only the member list', async () => {
|
||||
for (const event of [join(), leave(), admin()]) {
|
||||
const { pipeline, fetchGroupList, fetchGroupMemberList } = makePipeline({ plantGroup: true });
|
||||
|
||||
Reference in new issue
Block a user