diff --git a/packages/core/src/bridge/apis/group-admin.ts b/packages/core/src/bridge/apis/group-admin.ts index 048c44ef..a9bc16b9 100644 --- a/packages/core/src/bridge/apis/group-admin.ts +++ b/packages/core/src/bridge/apis/group-admin.ts @@ -54,8 +54,26 @@ export class GroupAdminApi { return KickMembers.invoke(this.ctx, { groupId, userIds, reject }); } - leave(groupId: number): Promise { - return LeaveGroup.invoke(this.ctx, { groupId }); + async leave(groupId: number): Promise { + await LeaveGroup.invoke(this.ctx, { groupId }); + // The server doesn't push a member-decrease back to the member who left of + // its own accord, so synthesize the self group_member_leave here — otherwise + // downstream never sees notice.group_decrease and keeps a "zombie" group in + // its cache (#133). Also drop the group from our own roster. + const selfUin = Number(this.ctx.identity.uin) || 0; + const selfUid = this.ctx.identity.selfUid ?? ''; + await this.ctx.events.emit({ + kind: 'group_member_leave', + time: Math.floor(Date.now() / 1000), + selfUin, + groupId, + userUin: selfUin, + operatorUin: selfUin, + userUid: selfUid, + operatorUid: selfUid, + leaveType: 'leave', + }); + this.ctx.identity.forgetGroup(groupId); } setAdmin(groupId: number, userId: number, enable: boolean): Promise { diff --git a/packages/core/tests/actions/_helpers.ts b/packages/core/tests/actions/_helpers.ts index 53b3b095..d41bc212 100644 --- a/packages/core/tests/actions/_helpers.ts +++ b/packages/core/tests/actions/_helpers.ts @@ -124,7 +124,9 @@ export interface MockBridge { findUidByUin: ReturnType; findUinByUid: ReturnType; findGroupMember: ReturnType; + forgetGroup: ReturnType; }; + events: { emit: ReturnType }; apis: MockApiHub; sendRawPacket: ReturnType; fetchFriendList: ReturnType; @@ -154,8 +156,10 @@ export function mockBridge(overrides: Partial = {}): MockBridge { findUidByUin: vi.fn(() => 'cached-uid'), findUinByUid: vi.fn(() => 0), findGroupMember: vi.fn(() => null), + forgetGroup: vi.fn(), ...(overrides.identity ?? {}), } as MockBridge['identity'], + events: overrides.events ?? { emit: vi.fn(async () => undefined) }, apis: overrides.apis ?? mockApiHub(), sendRawPacket: vi.fn(async () => defaultResp), fetchFriendList: vi.fn(async () => []), diff --git a/packages/core/tests/actions/group-admin.test.ts b/packages/core/tests/actions/group-admin.test.ts index 008d6fe8..c951f8e1 100644 --- a/packages/core/tests/actions/group-admin.test.ts +++ b/packages/core/tests/actions/group-admin.test.ts @@ -88,13 +88,24 @@ describe('apis/group-admin', () => { expect(env.body?.rejectAddRequest ?? 0).toBe(0); }); - it('leave sends 0x1097_1 with the groupUin', async () => { + it('leave sends 0x1097_1, emits a self group_member_leave, and forgets the group (#133)', async () => { const bridge = mockBridge(); await new GroupAdminApi(bridge as any).leave(12345); const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!; expect(cmd).toBe('OidbSvcTrpcTcp.0x1097_1'); const env = protobuf_decode>(bytes); expect(env.body).toMatchObject({ groupUin: 12345 }); + + // The server never pushes a member-decrease for our own voluntary leave, so + // the bridge synthesizes it (self user_id/operator_id) and drops the group. + expect(bridge.events.emit).toHaveBeenCalledWith(expect.objectContaining({ + kind: 'group_member_leave', + groupId: 12345, + userUin: 10001, + operatorUin: 10001, + leaveType: 'leave', + })); + expect(bridge.identity.forgetGroup).toHaveBeenCalledWith(12345); }); it('setAdmin resolves UID and sends 0x1096_1', async () => { diff --git a/packages/core/tests/actions/group-file.test.ts b/packages/core/tests/actions/group-file.test.ts index 2894ffa8..adf18667 100644 --- a/packages/core/tests/actions/group-file.test.ts +++ b/packages/core/tests/actions/group-file.test.ts @@ -233,6 +233,7 @@ describe('apis/group-file', () => { findUidByUin: vi.fn(() => 'cached-uid'), findUinByUid: vi.fn(() => 0), findGroupMember: vi.fn(() => null), + forgetGroup: vi.fn(), }, }); vi.mocked(bridge.resolveUserUid) diff --git a/packages/protocol/src/identity-service.ts b/packages/protocol/src/identity-service.ts index 7e7ee814..244b5d91 100644 --- a/packages/protocol/src/identity-service.ts +++ b/packages/protocol/src/identity-service.ts @@ -345,6 +345,20 @@ export class IdentityService { this.hydrateActiveMembersForGroups(groups.map((group) => group.groupId)); } + /** + * Drop a single group from the roster after the bot leaves/is removed, so + * `get_group_list` and member lookups stop returning a group we're no longer + * in (#133). Removes the in-memory entry and marks the DB row inactive so it + * doesn't resurrect on the next load (the server refetch won't include it). + */ + forgetGroup(groupId: number): void { + this.groups_.delete(groupId); + this.runWrite('forget group', () => { + this.pstmt('UPDATE groups SET active = 0, updated_at = ? WHERE group_id = ?') + .run(nowSeconds(), groupId); + }); + } + rememberGroupMembers(groupId: number, members: GroupMemberInfo[]): void { this.setGroupMembersInMemory(groupId, members); for (const member of members) this.rememberUidUin(member.uid, member.uin);