fix(onebot): preserve group requests with unknown senders

Deliver actionable invitations even when the sender account is unavailable instead of failing the full polling batch.

Isolate malformed records, keep sibling requests flowing, and distinguish multiple anonymous invitations by their request handles.

Add regression coverage for repeated polls, batch isolation, and anonymous-request deduplication.
This commit is contained in:
DrSmoothl
2026-08-10 13:12:18 +08:00
parent da3821ced4
commit 190f156fca
2 changed files with 123 additions and 9 deletions
+36 -9
View File
@@ -93,13 +93,26 @@ export class GroupRequestPoller {
if (this.lifecycleStarted && !this.running) return;
const discovered = [...main, ...filtered]
.filter((request) => request.state === 1)
.filter((request) => request.notifyType === 1 || request.notifyType === 7)
.map((request) => ({ request, event: this.toEvent(request) }));
.filter((request) => request.notifyType === 1 || request.notifyType === 7);
const now = this.now();
this.sweepRecentFingerprints(now);
for (const { request, event } of discovered) {
for (const request of discovered) {
if (this.lifecycleStarted && !this.running) return;
let event: GroupInviteEvent;
try {
event = this.toEvent(request);
} catch (error) {
this.log.error(
'group request record rejected: group=%s sequence=%s subtype=%s reason=%s',
String(request.groupId),
String(request.sequence),
request.notifyType === 1 ? 'invite' : 'add',
error instanceof Error ? error.message : String(error),
);
continue;
}
const fingerprint = eventFingerprint(event);
if (this.seenFlags.has(event.flag)) continue;
if ((this.recentFingerprints.get(fingerprint) ?? 0) > now - RECENT_EVENT_WINDOW_MS) {
@@ -107,6 +120,15 @@ export class GroupRequestPoller {
continue;
}
if (event.fromUin === 0) {
this.log.warn(
'group request sender could not be identified: group=%d sequence=%d subtype=%s',
event.groupId,
request.sequence,
event.subType,
);
}
// Mark before emission: handlers may synchronously trigger another list
// read through a quick operation, which must not re-emit this request.
this.rememberFlag(event.flag);
@@ -141,10 +163,8 @@ export class GroupRequestPoller {
`group request has no operation type: group=${request.groupId} sequence=${request.sequence} notifyType=${notifyType ?? 0}`,
);
}
if (!Number.isSafeInteger(fromUin) || fromUin <= 0) {
throw new Error(
`group request has no sender account: group=${request.groupId} sequence=${request.sequence} subtype=${subType}`,
);
if (!Number.isSafeInteger(fromUin) || fromUin < 0) {
throw new Error(`invalid sender account on group request: ${fromUin}`);
}
if (inviteCardSequence !== undefined
&& (!Number.isSafeInteger(inviteCardSequence) || inviteCardSequence <= 0)) {
@@ -212,7 +232,14 @@ export class GroupRequestPoller {
}
}
function eventFingerprint(event: Pick<GroupInviteEvent, 'groupId' | 'subType' | 'fromUin' | 'fromUid'>): string {
const actor = event.fromUin > 0 ? `uin:${event.fromUin}` : `uid:${event.fromUid ?? ''}`;
function eventFingerprint(
event: Pick<GroupInviteEvent, 'groupId' | 'subType' | 'fromUin' | 'fromUid' | 'flag'>,
): string {
const fromUid = event.fromUid?.trim() ?? '';
const actor = event.fromUin > 0
? `uin:${event.fromUin}`
: fromUid
? `uid:${fromUid}`
: `request:${event.flag}`;
return `${event.groupId}:${event.subType}:${actor}`;
}
@@ -87,6 +87,93 @@ describe('GroupRequestPoller', () => {
})]);
});
it('emits an actionable invite when the sender account is unavailable and continues the batch', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [
request({
notifyType: 1,
eventType: 2,
sequence: 41,
invitorUid: '',
invitorUin: 0,
}),
request({
groupId: 1_000,
notifyType: 1,
eventType: 2,
sequence: 42,
}),
]);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
await poller.pollOnce();
expect(received).toEqual([
expect.objectContaining({
groupId: 999,
fromUin: 0,
fromUid: '',
subType: 'invite',
flag: 'slreq:1:41:999:2:0',
}),
expect.objectContaining({
groupId: 1_000,
fromUin: 456,
flag: 'slreq:1:42:1000:2:0',
}),
]);
});
it('does not merge separate requests whose sender identities are both unavailable', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [
request({
notifyType: 1,
eventType: 2,
sequence: 41,
invitorUid: '',
invitorUin: 0,
}),
request({
notifyType: 1,
eventType: 2,
sequence: 42,
invitorUid: '',
invitorUin: 0,
}),
]);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
expect(received).toEqual([
expect.objectContaining({ flag: 'slreq:1:41:999:2:0' }),
expect.objectContaining({ flag: 'slreq:1:42:999:2:0' }),
]);
});
it('isolates a malformed list record without suppressing valid siblings', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [
request({ sequence: 0 }),
request({ groupId: 1_000, sequence: 43 }),
]);
const { events, poller } = setup(fetchGroupRequests);
const received: unknown[] = [];
events.on('group_invite', (event) => { received.push(event); });
await poller.pollOnce();
expect(received).toEqual([
expect.objectContaining({
groupId: 1_000,
flag: 'slreq:1:43:1000:1:0',
}),
]);
});
it('preserves the private invite-card approval sequence when one is known', async () => {
const fetchGroupRequests = vi.fn(async (filtered: boolean) => filtered ? [] : [request({
notifyType: 1,