From d8c72c2ebc67a7d12e6573cd315e01e0205eddf9 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 23 May 2026 22:00:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(events):=20surface=20verify-text=20on=20gro?= =?UTF-8?q?up=5Finvite=20("=E4=BD=A0=E4=BB=AC=E5=A5=BD"=20=E2=86=92=20comm?= =?UTF-8?q?ent)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User report: SnowLuma's group_invite event renders to OneBot with empty `comment`, so the bot's approval-prompt template shows nickname + uin + flag but loses the requester's verify message. NapCat's same flow shows the verify text: 【进群申请】批准/驳回: 昵称:KitaIkuyo QQ:1957003260 flag:1779543612823906 等级:7 你们好 ← only NapCat shows this line Root cause: The OIDB push (GroupRequestJoinNotice subType=84) only carries `(groupUin, targetUid)` and a few opaque numeric fields — no string for the verify text. NapCat doesn't use the push directly; they poll `nodeIKernelGroupService.getGroupNotifies` (= our OIDB 0x10C0 pending-request queue at `FetchGroupRequests`) which returns each pending row with `postscript` (the verify text) and `seq` (server sequence id). Reference: dev/NapCatQQ/packages/napcat-onebot/index.ts:483-498 (`flag = notify.seq`, `comment = notify.postscript`). Our pipeline didn't enrich. The decoder set `message: ''` and emitted, so OneBot's `comment` was empty. Fix: - Extend `PacketPipelineDeps` with a `resolveGroupJoinRequest( groupId, uid, subType)` hook. Bridge.ts wires it to `fetchGroupRequests` and matches on `(groupId, uid)` — targetUid for `subType='add'`, invitorUid for `subType='invite'`. - `dispatchAfterStrangerResolve` now fires the profile + request lookups in PARALLEL via `Promise.allSettled` so a flake on one path doesn't kill the other. The request lookup's `comment` lands on `event.message`; OneBot converts that to `comment`. Decisions: - Kept the existing `add::` flag format (consumer bots already parse it). Switching to NapCat's sequence-form flag would break callers and is a separate breaking change. - Both lookups fail-open: if either rejects, the event still emits with whatever info we did get, so the bot at least sees the request and can render the bare nickname. Regression tests: - `populates event.message from the pending-request queue` — pins the user-reported "你们好" case end-to-end through the pipeline. - `runs lookups in parallel` — profile resolves, request fails; event still ships with the resolved uin and an empty message. - `invite subtype matches the request on invitorUid` — explicit contract for the invite-forwarded-to-admin case where the pending row's REQUESTER is `invitorUid`, not `targetUid`. 345 bridge tests pass (was 342 + 3 new); typecheck + lint clean. --- packages/bridge/src/packet-pipeline.ts | 53 ++++++++--- .../packet-pipeline-stranger-resolve.test.ts | 95 ++++++++++++++++++- packages/core/src/bridge/bridge.ts | 18 ++++ 3 files changed, 154 insertions(+), 12 deletions(-) diff --git a/packages/bridge/src/packet-pipeline.ts b/packages/bridge/src/packet-pipeline.ts index d7e366b4..cfac6fec 100644 --- a/packages/bridge/src/packet-pipeline.ts +++ b/packages/bridge/src/packet-pipeline.ts @@ -43,6 +43,20 @@ export interface PacketPipelineDeps { * uid-only event. */ resolveStrangerProfile(uid: string): Promise<{ uin: number; nickname: string } | null>; + /** + * Resolve the verify message ("postscript") + server sequence number + * for a pending group-join / group-invite. The OIDB push only + * carries the requester's UID + group uin — the actual verify text + * the user typed ("你们好" etc.) lives on the pending-request queue + * fetched via OIDB 0x10C0. NapCat surfaces this as + * `notify.postscript` from `nodeIKernelGroupService.getGroupNotifies`; + * we mirror that with an `fetchGroupRequests` lookup matched on + * `(groupId, uid, subType)`. Returns null when no matching pending + * request exists (e.g. it was already handled by another client). + */ + resolveGroupJoinRequest( + groupId: number, uid: string, subType: 'add' | 'invite', + ): Promise<{ comment: string; sequence: number } | null>; } export class IncomingPacketPipeline { @@ -151,18 +165,35 @@ export class IncomingPacketPipeline { private async dispatchAfterStrangerResolve(event: Extract): Promise { const uid = event.fromUid; - try { - if (uid) { - const profile = await this.deps.resolveStrangerProfile(uid); - if (profile && profile.uin > 0) { - event.fromUin = profile.uin; - } + if (uid) { + // Fire profile + pending-request lookups in PARALLEL — they're + // independent OIDB calls (0xFE1_2 stranger profile + 0x10C0 + // pending request queue). Both `Promise.allSettled` so a flake + // on one path doesn't kill the other. + const subType = event.subType === 'invite' ? 'invite' : 'add'; + const [profileR, requestR] = await Promise.allSettled([ + this.deps.resolveStrangerProfile(uid), + this.deps.resolveGroupJoinRequest(event.groupId, uid, subType), + ]); + + if (profileR.status === 'fulfilled' && profileR.value && profileR.value.uin > 0) { + event.fromUin = profileR.value.uin; + } else if (profileR.status === 'rejected') { + this.log.warn('failed to resolve stranger profile: uid=%s err=%s', + uid, profileR.reason instanceof Error ? profileR.reason.message : String(profileR.reason)); + } + + if (requestR.status === 'fulfilled' && requestR.value) { + // NapCat surfaces the verify text as `comment` on the OneBot + // event (`postscript` server-side). Without this the bot's + // approval-prompt template renders an empty body line — see + // `dev/NapCatQQ/.../napcat-onebot/index.ts:496-498`. + event.message = requestR.value.comment; + } else if (requestR.status === 'rejected') { + this.log.warn('failed to resolve group join request: groupId=%d uid=%s err=%s', + event.groupId, uid, + requestR.reason instanceof Error ? requestR.reason.message : String(requestR.reason)); } - } catch (e) { - // Fail open — emit the event with the bare uid so bots can at - // least see the join request, even if they can't render a name. - this.log.warn('failed to resolve stranger profile: uid=%s err=%s', - uid ?? '', e instanceof Error ? e.message : String(e)); } this.handleSideEffects(event); diff --git a/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts b/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts index d70bda67..7213f218 100644 --- a/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts +++ b/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts @@ -17,21 +17,24 @@ import type { PacketInfo } from '@snowluma/common/protocol-types'; function makePipeline(opts: { resolveStrangerProfile?: vi.Mock; + resolveGroupJoinRequest?: vi.Mock; } = {}) { const identity = IdentityService.memory('10001'); const events = new BridgeEventBus(); const resolveStrangerProfile = opts.resolveStrangerProfile ?? vi.fn(async () => null); + const resolveGroupJoinRequest = opts.resolveGroupJoinRequest ?? vi.fn(async () => null); const pipeline = new IncomingPacketPipeline({ identity, events, refreshMemberCache: vi.fn(async () => false), resolveStrangerProfile, + resolveGroupJoinRequest, }); const captured: QQEventVariant[] = []; events.onAny((event) => { captured.push(event as QQEventVariant); }); - return { pipeline, events, captured, resolveStrangerProfile }; + return { pipeline, events, captured, resolveStrangerProfile, resolveGroupJoinRequest }; } describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { @@ -132,6 +135,96 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { expect(captured[0]).toMatchObject({ fromUin: 99999, fromUid: 'u_known' }); }); + it('populates event.message from the pending-request queue (NapCat parity)', async () => { + // User-reported bug: SnowLuma's group_invite event surfaces a + // bare uid + uin but the requester's verify text ("你们好") never + // lands in the OneBot `comment` field. NapCat shows the text via + // `notify.postscript` from `getGroupNotifies`; we mirror that by + // doing an `OIDB 0x10C0 fetchGroupRequests` lookup in the async + // pre-dispatch hook and patching `event.message` from the matching + // row's `comment`. + const resolveStrangerProfile = vi.fn(async () => ({ + uin: 1957003260, nickname: 'KitaIkuyo', + })); + const resolveGroupJoinRequest = vi.fn(async () => ({ + comment: '你们好', sequence: 1779543612823906, + })); + const { pipeline, captured } = makePipeline({ + resolveStrangerProfile, resolveGroupJoinRequest, + }); + + pipeline.registerCmd('test.cmd', () => [{ + kind: 'group_invite', + time: 1, selfUin: 10001, + groupId: 950929451, fromUin: 0, + fromUid: 'u_UVLHYYqba27WPUzTrdZlCA', + subType: 'add', message: '', + flag: 'add:950929451:u_UVLHYYqba27WPUzTrdZlCA', + } as QQEventVariant]); + + pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); + await new Promise(r => setTimeout(r, 10)); + + expect(resolveGroupJoinRequest).toHaveBeenCalledWith( + 950929451, 'u_UVLHYYqba27WPUzTrdZlCA', 'add'); + expect(captured).toHaveLength(1); + const ev = captured[0] as Extract; + expect(ev.message).toBe('你们好'); + expect(ev.fromUin).toBe(1957003260); + }); + + it('runs the profile + request lookups in parallel (independent failures)', async () => { + // Profile lookup succeeds, request lookup fails — event should + // still carry the resolved uin but no comment. The dispatch + // doesn't block on the slower / failing path. + const resolveStrangerProfile = vi.fn(async () => ({ + uin: 12345, nickname: 'OK', + })); + const resolveGroupJoinRequest = vi.fn(async () => { + throw new Error('queue lookup failed'); + }); + const { pipeline, captured } = makePipeline({ + resolveStrangerProfile, resolveGroupJoinRequest, + }); + + pipeline.registerCmd('test.cmd', () => [{ + kind: 'group_invite', + time: 1, selfUin: 10001, + groupId: 1, fromUin: 0, fromUid: 'u_x', + subType: 'add', message: '', flag: 'add:1:u_x', + } as QQEventVariant]); + + pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); + await new Promise(r => setTimeout(r, 10)); + + const ev = captured[0] as Extract; + expect(ev.fromUin).toBe(12345); // profile succeeded + expect(ev.message).toBe(''); // request lookup threw, message left empty + }); + + it('invite subtype matches the request on invitorUid (not targetUid)', async () => { + // For `subType: 'invite'` (group member invited the bot or another + // user), the pending-request row's REQUESTER field is + // `invitorUid` not `targetUid`. The dep contract makes this + // explicit so the bridge facade can route the lookup correctly. + const resolveGroupJoinRequest = vi.fn(async () => ({ + comment: 'come join', sequence: 999, + })); + const { pipeline } = makePipeline({ resolveGroupJoinRequest }); + + pipeline.registerCmd('test.cmd', () => [{ + kind: 'group_invite', + time: 1, selfUin: 10001, + groupId: 1, fromUin: 0, fromUid: 'u_inviter', + subType: 'invite', message: '', flag: 'invite:1:u_inviter', + } as QQEventVariant]); + + pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); + await new Promise(r => setTimeout(r, 10)); + + expect(resolveGroupJoinRequest).toHaveBeenCalledWith(1, 'u_inviter', 'invite'); + }); + it('forces re-resolve when fromUin === groupId (legacy cache pollution)', async () => { // Pre-fix builds stored ` → ` because // the decoder's fallback was `ctx.fromUin` (= group's own uin on diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index 3842073d..dbb93711 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -156,6 +156,24 @@ export class Bridge implements BridgeInterface { return null; } }, + resolveGroupJoinRequest: async (groupId, uid, subType) => { + // OIDB 0x10C0 pending-request queue holds (targetUid, + // invitorUid, comment, sequence, ...). For a plain join the + // requester is `targetUid`; for a forwarded invite it's + // `invitorUid`. Mirrors NapCat's `getGroupNotifies` → + // `notify.postscript` pipeline. + try { + const requests = await this.apis.contacts.fetchGroupRequests(); + const match = requests.find(r => { + if (r.groupId !== groupId) return false; + return subType === 'invite' ? r.invitorUid === uid : r.targetUid === uid; + }); + if (!match) return null; + return { comment: match.comment, sequence: match.sequence }; + } catch { + return null; + } + }, }); this.pipeline.registerCmd(MSG_PUSH_CMD, parseMsgPush); }