fix(onebot): match group invitations to the correct request
Report distinct invitations separately and approve only the request that belongs to the account and inviter. Reject ambiguous approvals and resolve the sender of invitations from outside the contact list. Refs #612
This commit is contained in:
16 files changed
+429
-142
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { GroupInviteTracker } from '@snowluma/protocol/group-invite-tracker';
|
||||
import { FetchDownloadRkeys } from '@snowluma/protocol/oidb-services/contacts/fetch-download-rkeys';
|
||||
import {
|
||||
FetchFriendListPage,
|
||||
@@ -109,9 +110,7 @@ export class ContactsApi {
|
||||
load: (groupId) => this.fetchGroupMemberListUncached(groupId),
|
||||
});
|
||||
private robotUinRangesPromise_: Promise<RobotUinRangeSnapshot> | null = null;
|
||||
/** groupUin → approval msgseq from a private qun.invite card. Written
|
||||
* only by IncomingPacketPipeline (and tests). OneBot reads get/find. */
|
||||
private readonly groupInviteCardSeqs_ = new Map<number, number>();
|
||||
readonly groupInvites = new GroupInviteTracker();
|
||||
|
||||
constructor(private readonly ctx: BridgeContext) {}
|
||||
|
||||
@@ -616,32 +615,16 @@ export class ContactsApi {
|
||||
}
|
||||
|
||||
/** Returns whether this live card introduces a new approval sequence. */
|
||||
rememberGroupInviteCardSequence(groupUin: number, sequence: number): boolean {
|
||||
if (this.groupInviteCardSeqs_.get(groupUin) === sequence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.groupInviteCardSeqs_.set(groupUin, sequence);
|
||||
return true;
|
||||
rememberGroupInviteCardSequence(groupId: number, sequence: number): boolean {
|
||||
return this.groupInvites.receiveCard(groupId, sequence);
|
||||
}
|
||||
|
||||
/** The approval msgseq captured from a private "qun.invite" card for this
|
||||
* group, or undefined if none was seen. `set_group_add_request` uses it to
|
||||
* approve a bot self-invite via 0x10c8 (eventType=2). See issue #125. */
|
||||
getGroupInviteCardSequence(groupId: number): number | undefined {
|
||||
return this.groupInviteCardSeqs_.get(groupId);
|
||||
return this.groupInvites.getCardSequence(groupId);
|
||||
}
|
||||
|
||||
/** Resolve the group for a private invite-card msgseq. Numeric OneBot flags
|
||||
* use this path because the card sequence is absent from 0x10C0. */
|
||||
findGroupInviteCardGroupBySequence(sequence: number): number | undefined {
|
||||
for (const [groupUin, rememberedSequence] of this.groupInviteCardSeqs_) {
|
||||
if (rememberedSequence === sequence) {
|
||||
return groupUin;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
findGroupInviteCardGroupBySequence(sequence: number, groupId?: number): number | undefined {
|
||||
return this.groupInvites.findCardGroup(sequence, groupId);
|
||||
}
|
||||
|
||||
async fetchDownloadRKeys(): Promise<DownloadRKeyInfo[]> {
|
||||
|
||||
@@ -73,8 +73,7 @@ export class Bridge implements BridgeInterface {
|
||||
},
|
||||
resolveGroupJoinRequest: (groupId, uid, subType, invitedUid) =>
|
||||
this.apis.contacts.resolveGroupJoinRequest(groupId, uid, subType, invitedUid),
|
||||
rememberGroupInviteCardSequence: (groupUin, sequence) =>
|
||||
this.apis.contacts.rememberGroupInviteCardSequence(groupUin, sequence),
|
||||
groupInvites: this.apis.contacts.groupInvites,
|
||||
});
|
||||
this.pipeline.registerCmd(MSG_PUSH_CMD, (pkt, identity) =>
|
||||
parseMsgPushOrThrow(pkt, identity, this.sysMsgDedup_),
|
||||
|
||||
@@ -761,4 +761,13 @@ describe('apis/contacts / invite-card pending application', () => {
|
||||
expect(api.findGroupInviteCardGroupBySequence(778899)).toBe(12345);
|
||||
expect(api.findGroupInviteCardGroupBySequence(1)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('requires a group when different groups have the same card sequence', () => {
|
||||
const api = new ContactsApi({ identity: { uin: '10001' } } as any);
|
||||
api.rememberGroupInviteCardSequence(12345, 778899);
|
||||
api.rememberGroupInviteCardSequence(67890, 778899);
|
||||
|
||||
expect(() => api.findGroupInviteCardGroupBySequence(778899)).toThrow('multiple groups');
|
||||
expect(api.findGroupInviteCardGroupBySequence(778899, 67890)).toBe(67890);
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { parseMsgPush, MSG_PUSH_CMD } from '@snowluma/protocol/msg-push';
|
||||
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import { GroupInviteTracker } from '@snowluma/protocol/group-invite-tracker';
|
||||
import { IncomingPacketPipeline } from '@snowluma/protocol/packet-pipeline';
|
||||
import { BridgeEventBus } from '@snowluma/protocol/event-bus';
|
||||
import type { GroupMemberInfo, QQGroupInfo } from '@snowluma/protocol/qq-info';
|
||||
@@ -529,7 +530,7 @@ describe('parseMsgPush Event0x210 subType=364 (friend remark synchronization)',
|
||||
fetchGroupList: vi.fn(async () => {}),
|
||||
fetchGroupMemberList: vi.fn(async () => {}),
|
||||
resolveGroupJoinRequest: vi.fn(async () => null),
|
||||
rememberGroupInviteCardSequence: vi.fn(() => true),
|
||||
groupInvites: new GroupInviteTracker(),
|
||||
});
|
||||
pipeline.registerCmd(MSG_PUSH_CMD, parseMsgPush);
|
||||
|
||||
@@ -1028,7 +1029,7 @@ describe('parseMsgPush PkgType 44 (group admin set/unset) keeps the member cache
|
||||
fetchGroupList: vi.fn(async () => {}),
|
||||
fetchGroupMemberList: vi.fn(async () => {}),
|
||||
resolveGroupJoinRequest: vi.fn(async () => null),
|
||||
rememberGroupInviteCardSequence: vi.fn(() => true),
|
||||
groupInvites: new GroupInviteTracker(),
|
||||
});
|
||||
pipeline.registerCmd(MSG_PUSH_CMD, parseMsgPush);
|
||||
await pipeline.process(packet);
|
||||
|
||||
Reference in new issue
Block a user