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
@@ -809,6 +809,7 @@ describe('IdentityService', () => {
|
|||||||
['group requests', () => identity.rememberGroupRequests([request])],
|
['group requests', () => identity.rememberGroupRequests([request])],
|
||||||
['request identity', () => identity.rememberRequestIdentity({ uid: 'u_request', uin: 88888 })],
|
['request identity', () => identity.rememberRequestIdentity({ uid: 'u_request', uin: 88888 })],
|
||||||
['group member identity', () => identity.rememberGroupMemberIdentity(GROUP_ID, { uid: 'u_new', uin: 99999 })],
|
['group member identity', () => identity.rememberGroupMemberIdentity(GROUP_ID, { uid: 'u_new', uin: 99999 })],
|
||||||
|
['group member joined', () => identity.rememberGroupMemberJoined(GROUP_ID, { uid: 'u_new', uin: 99999 })],
|
||||||
['group member inactive', () => identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin })],
|
['group member inactive', () => identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin })],
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -823,6 +824,73 @@ describe('IdentityService', () => {
|
|||||||
expect(identity.findUinByUid('u_request')).toBeNull();
|
expect(identity.findUinByUid('u_request')).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not treat identity-only sightings as live group members', () => {
|
||||||
|
const identity = IdentityService.memory(SELF_UIN);
|
||||||
|
identity.rememberGroups([makeGroup()]);
|
||||||
|
identity.rememberGroupMemberIdentity(GROUP_ID, { uid: 'u_seen', uin: 88888 });
|
||||||
|
|
||||||
|
expect(identity.findGroupMember(GROUP_ID, 88888)).toBeNull();
|
||||||
|
expect(identity.findUinByUid('u_seen')).toBe(88888);
|
||||||
|
identity.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('records a thin live member on join without inventing roster details', () => {
|
||||||
|
const identity = IdentityService.memory(SELF_UIN);
|
||||||
|
identity.rememberGroups([makeGroup()]);
|
||||||
|
const existing = makeMember(33333, 'u_existing', 'card');
|
||||||
|
identity.rememberGroupMembers(GROUP_ID, [existing]);
|
||||||
|
|
||||||
|
identity.rememberGroupMemberJoined(GROUP_ID, { uid: 'u_join', uin: 44444 });
|
||||||
|
identity.rememberGroupMemberJoined(GROUP_ID, { uid: 'u_join', uin: 44444 });
|
||||||
|
identity.rememberGroupMemberJoined(GROUP_ID, { uid: 'u_ignored', uin: 0 });
|
||||||
|
|
||||||
|
const joined = identity.findGroupMember(GROUP_ID, 44444);
|
||||||
|
expect(joined).toMatchObject({
|
||||||
|
uin: 44444,
|
||||||
|
uid: 'u_join',
|
||||||
|
nickname: '',
|
||||||
|
card: '',
|
||||||
|
role: 'member',
|
||||||
|
});
|
||||||
|
expect(joined?.isRobot).toBeUndefined();
|
||||||
|
expect(identity.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
|
||||||
|
identity.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops a leaver from the live roster and keeps their identity mapping', () => {
|
||||||
|
const identity = IdentityService.memory(SELF_UIN);
|
||||||
|
identity.rememberGroups([makeGroup()]);
|
||||||
|
const member = makeMember(33333, 'u_member', 'card');
|
||||||
|
identity.rememberGroupMembers(GROUP_ID, [member]);
|
||||||
|
|
||||||
|
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
|
||||||
|
|
||||||
|
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
|
||||||
|
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
|
||||||
|
identity.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps a leaver out of the live roster after reopen', () => {
|
||||||
|
const dbPath = tempDbPath('leave-live-roster');
|
||||||
|
const member = makeMember(33333, 'u_member', 'card');
|
||||||
|
|
||||||
|
{
|
||||||
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
||||||
|
identity.rememberGroups([makeGroup()]);
|
||||||
|
identity.rememberGroupMembers(GROUP_ID, [member]);
|
||||||
|
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
|
||||||
|
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
|
||||||
|
identity.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
||||||
|
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
|
||||||
|
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
|
||||||
|
identity.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('marks missing members inactive only after a successful full refresh', () => {
|
it('marks missing members inactive only after a successful full refresh', () => {
|
||||||
const dbPath = tempDbPath('inactive-refresh');
|
const dbPath = tempDbPath('inactive-refresh');
|
||||||
const first = makeMember(33333, 'u_first');
|
const first = makeMember(33333, 'u_first');
|
||||||
|
|||||||
@@ -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 {
|
markGroupMemberInactive(groupId: number, identity: { uid?: string; uin?: number }): void {
|
||||||
this.beginObservation('group member inactive');
|
this.beginObservation('group member inactive');
|
||||||
const observed = { ...identity };
|
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', () => {
|
this.runWrite('group member inactive', () => {
|
||||||
const uid = normalizeUid(observed.uid);
|
|
||||||
const uin = normalizeUin(observed.uin);
|
|
||||||
if (!uid && uin === null) return;
|
if (!uid && uin === null) return;
|
||||||
const rows = this.findMemberRows(groupId, uid, uin);
|
const rows = this.findMemberRows(groupId, uid, uin);
|
||||||
const updatedAt = nowSeconds();
|
const updatedAt = nowSeconds();
|
||||||
|
|||||||
@@ -678,6 +678,10 @@ export class IncomingPacketPipeline {
|
|||||||
uid: event.operatorUid,
|
uid: event.operatorUid,
|
||||||
uin: event.operatorUin,
|
uin: event.operatorUin,
|
||||||
});
|
});
|
||||||
|
this.deps.identity.rememberGroupMemberJoined(event.groupId, {
|
||||||
|
uid: event.userUid,
|
||||||
|
uin: event.userUin,
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case 'group_member_leave':
|
case 'group_member_leave':
|
||||||
this.deps.identity.markGroupMemberInactive(event.groupId, {
|
this.deps.identity.markGroupMemberInactive(event.groupId, {
|
||||||
|
|||||||
@@ -138,6 +138,29 @@ describe('IncomingPacketPipeline roster refresh', () => {
|
|||||||
expect(fetchGroupMemberList).toHaveBeenCalledWith(GROUP_ID);
|
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 () => {
|
it('known-group join / leave / admin fetch only the member list', async () => {
|
||||||
for (const event of [join(), leave(), admin()]) {
|
for (const event of [join(), leave(), admin()]) {
|
||||||
const { pipeline, fetchGroupList, fetchGroupMemberList } = makePipeline({ plantGroup: true });
|
const { pipeline, fetchGroupList, fetchGroupMemberList } = makePipeline({ plantGroup: true });
|
||||||
|
|||||||
Reference in new issue
Block a user