fix(events): resolve stranger nickname on group_invite (no more 未知昵称)
User report: when a stranger requests to join a group, the bot
receives `user_id=<groupId>` (instead of the requester's actual
uin) and `get_stranger_info` returns no nickname → bot displays
"未知昵称".
Two layered bugs:
1. **Decoder fallback was wrong.** `decodeGroupJoinRequest` /
`decodeGroupInvitation` / `decodeGroupInvite` all passed
`ctx.fromUin` as the fallback uin when `resolveUidToUin` missed
the local cache. But for group-scoped pushes `ctx.fromUin` is
the GROUP's own uin — falling back to it produces the bogus
`fromUin === groupId` collision the user saw. Fix: pass 0.
2. **No async resolve for stranger UIDs.** The push only carries
the requester's UID string. When the UID isn't in our cache
(true for any stranger who isn't yet a group member), we have
no UIN — and our UIN-form `FetchUserProfile` can't help. The
pipeline never tried to look up the stranger by UID, so the
event went out with `fromUin=0` (or, with the old fallback,
the group's own uin).
Cross-checked against Lagrange.Core's flow:
dev/Lagrange.Core/.../MessagingLogic.cs:215-224
case GroupSysRequestJoinEvent join:
var fetchUidEvent = FetchUserInfoEvent.Create(join.TargetUid);
var results = await Collection.Business.SendEvent(fetchUidEvent);
uint targetUin = results.Count == 0 ? 0
: ((FetchUserInfoEvent)results[0]).UserInfo.Uin;
var joinArgs = new GroupJoinRequestEvent(join.GroupUin, targetUin);
dev/Lagrange.Core/.../OidbSvcTrpcTcp0xFE1_2.cs:9-26
— UID-form variant of the same OIDB 0xFE1_2: field 1 is `uid:
string`, envelope `reserved=0` (no uinForm flag).
dev/Lagrange.Core/.../FetchUserInfoService.cs:66-78
— picks UID vs UIN form based on which input is set.
Changes:
- `proto-defs/oidb-actions/base.ts`: add `OidbUserInfoByUidRequest`
(same shape as the UIN variant but field 1 is `uid: string`).
- `bridge/oidb-services/contacts/fetch-user-profile-by-uid.ts`:
new namespace at 0xFE1_2 with UID-form encoding, no `uinForm`.
- `bridge/msg-push/decoders/group-join-request.ts`: 3 decoders'
`resolveUidToUin` fallbacks change `ctx.fromUin` → 0.
- `bridge/packet-pipeline.ts`: new `resolveStrangerProfile(uid)`
dep + `needsStrangerResolve` check + async dispatch that fires
the lookup before emitting the event. Fail open if the lookup
rejects (event still ships with `fromUin=0` so the OneBot bot
can fall back to the uid in the flag).
- `core/bridge/apis/contacts.ts`: `fetchUserProfileByUid(uid)`
facade method.
- `core/bridge/bridge.ts`: wire `resolveStrangerProfile` into the
pipeline deps so it routes through the new namespace.
Tests:
- UID-form namespace: declares the right cmd/subcmd, packages uid
as field 1, decodes nickname + uin so the pipeline patch works.
- Pipeline stranger resolve: patches event.fromUin from the
resolver response; fails open on null / throw; skips the wire
call when fromUin is already populated or fromUid is empty.
All 655 tests pass (was 645 + 10 new); typecheck + lint clean.
This commit is contained in:
9 files changed
+415
-3
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbUserInfoByUidRequest, OidbUserInfoResponse,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { FetchUserProfileByUid } from '../../../src/oidb-services/contacts/fetch-user-profile-by-uid';
|
||||
|
||||
function makeSender(body?: OidbUserInfoResponse) {
|
||||
const responseData = body !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbUserInfoResponse>>({ body }))
|
||||
: Buffer.alloc(0);
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
describe('FetchUserProfileByUid namespace (UID form)', () => {
|
||||
it('declares 0xFE1_2 WITHOUT uinForm (reserved stays 0)', () => {
|
||||
expect(FetchUserProfileByUid.command).toBe(0xFE1);
|
||||
expect(FetchUserProfileByUid.subCommand).toBe(2);
|
||||
// No `uinForm` exposed — matches Lagrange's isUid=false path on
|
||||
// the UID variant. Important because the server distinguishes
|
||||
// UIN vs UID requests via the envelope's `reserved` field.
|
||||
expect((FetchUserProfileByUid as any).uinForm).toBeUndefined();
|
||||
});
|
||||
|
||||
it('packages the uid as field 1 (string), routes to OidbSvcTrpcTcp.0xfe1_2', async () => {
|
||||
const sender = makeSender({ body: { uin: 950929451, uid: 'u_abc' } as any });
|
||||
await FetchUserProfileByUid.invoke(sender, { uid: 'u_abc' });
|
||||
const [wireName, bytes] = sender.sendRawPacket.mock.calls[0]!;
|
||||
expect(wireName).toBe('OidbSvcTrpcTcp.0xfe1_2');
|
||||
const env = protobuf_decode<OidbBase<OidbUserInfoByUidRequest>>(bytes);
|
||||
expect(env.body?.uid).toBe('u_abc');
|
||||
// reserved=0 (default, omitted on wire) because this is the UID
|
||||
// variant — Lagrange's `OidbSvcTrpcTcp0xFE1_2.cs:9` does the same.
|
||||
expect(env.reserved ?? 0).toBe(0);
|
||||
});
|
||||
|
||||
it('requests the same property key catalogue as the UIN variant', async () => {
|
||||
const sender = makeSender({ body: { uin: 1, uid: 'u' } as any });
|
||||
await FetchUserProfileByUid.invoke(sender, { uid: 'u' });
|
||||
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
|
||||
const env = protobuf_decode<OidbBase<OidbUserInfoByUidRequest>>(bytes);
|
||||
const keys = env.body?.keys?.map(k => k.key);
|
||||
expect(keys).toContain(20002); // nickname — the whole point
|
||||
expect(keys).toContain(105); // QQ level
|
||||
});
|
||||
|
||||
it('decodes nickname + uin from the response so the pipeline can fill them into the event', async () => {
|
||||
// This is the actual point of the namespace — the stranger's
|
||||
// nickname + uin lands here so the pipeline can patch them onto
|
||||
// the in-flight group_invite event.
|
||||
const enc = (s: string) => new TextEncoder().encode(s);
|
||||
const sender = makeSender({
|
||||
body: {
|
||||
uin: 950929451, uid: 'u_abc',
|
||||
properties: {
|
||||
bytesProperties: [{ code: 20002, value: enc('小明') }],
|
||||
numberProperties: [],
|
||||
},
|
||||
} as any,
|
||||
});
|
||||
const out = await FetchUserProfileByUid.invoke(sender, { uid: 'u_abc' });
|
||||
expect(out.uin).toBe(950929451);
|
||||
expect(out.nickname).toBe('小明');
|
||||
});
|
||||
|
||||
it('throws when the response body is missing entirely', async () => {
|
||||
const sender = makeSender({});
|
||||
await expect(FetchUserProfileByUid.invoke(sender, { uid: 'u_abc' }))
|
||||
.rejects.toThrow('user info response body missing');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
// Pre-dispatch stranger resolve regression — when a group join
|
||||
// request comes in, the push only carries the requester's UID. The
|
||||
// pipeline must do an async UID-form FetchUserProfile lookup BEFORE
|
||||
// emitting the event so the OneBot layer's `user_id` field is
|
||||
// populated and the consumer bot's `get_stranger_info` lookup works.
|
||||
//
|
||||
// Cross-checked against Lagrange's flow:
|
||||
// dev/Lagrange.Core/.../MessagingLogic.cs:215-224
|
||||
// (FetchUserInfoEvent by uid → resolved uin → posted event)
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { IncomingPacketPipeline } from '@snowluma/bridge/packet-pipeline';
|
||||
import { BridgeEventBus } from '@snowluma/bridge/event-bus';
|
||||
import { IdentityService } from '@snowluma/bridge/identity-service';
|
||||
import type { QQEventVariant } from '@snowluma/bridge/events';
|
||||
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||
|
||||
function makePipeline(opts: {
|
||||
resolveStrangerProfile?: vi.Mock;
|
||||
} = {}) {
|
||||
const identity = IdentityService.memory('10001');
|
||||
const events = new BridgeEventBus();
|
||||
const resolveStrangerProfile = opts.resolveStrangerProfile ?? vi.fn(async () => null);
|
||||
const pipeline = new IncomingPacketPipeline({
|
||||
identity,
|
||||
events,
|
||||
refreshMemberCache: vi.fn(async () => false),
|
||||
resolveStrangerProfile,
|
||||
});
|
||||
|
||||
const captured: QQEventVariant[] = [];
|
||||
events.onAny((event) => { captured.push(event as QQEventVariant); });
|
||||
|
||||
return { pipeline, events, captured, resolveStrangerProfile };
|
||||
}
|
||||
|
||||
describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
it('calls resolveStrangerProfile(uid) when group_invite has fromUin=0 + fromUid', async () => {
|
||||
const resolveStrangerProfile = vi.fn(async (_uid: string) => ({
|
||||
uin: 950929451, nickname: '小明',
|
||||
}));
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
|
||||
// Plant a parser that emits the user's exact bug-report shape:
|
||||
// groupId set, fromUid is a string UID, fromUin=0 (decoder fix).
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1700000000,
|
||||
selfUin: 10001,
|
||||
groupId: 123456789,
|
||||
fromUin: 0,
|
||||
fromUid: 'u_stranger_abc',
|
||||
subType: 'add',
|
||||
message: '',
|
||||
flag: 'add:123456789:u_stranger_abc',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
|
||||
// Let the async hook flush.
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).toHaveBeenCalledWith('u_stranger_abc');
|
||||
expect(captured).toHaveLength(1);
|
||||
const event = captured[0] as Extract<QQEventVariant, { kind: 'group_invite' }>;
|
||||
expect(event.fromUin).toBe(950929451); // ← patched in by the async resolve
|
||||
expect(event.fromUid).toBe('u_stranger_abc');
|
||||
expect(event.groupId).toBe(123456789);
|
||||
});
|
||||
|
||||
it('still emits the event when resolveStrangerProfile returns null (fail open)', async () => {
|
||||
const resolveStrangerProfile = vi.fn(async () => null);
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1, selfUin: 10001,
|
||||
groupId: 12345, fromUin: 0, fromUid: 'u_no_such_user',
|
||||
subType: 'add', message: '', flag: 'add:12345:u_no_such_user',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).toHaveBeenCalledOnce();
|
||||
expect(captured).toHaveLength(1);
|
||||
const event = captured[0] as Extract<QQEventVariant, { kind: 'group_invite' }>;
|
||||
// fromUin stays 0 — but the event still goes out so the bot can
|
||||
// see the join request and at least respond using the uid in the
|
||||
// flag.
|
||||
expect(event.fromUin).toBe(0);
|
||||
expect(event.fromUid).toBe('u_no_such_user');
|
||||
});
|
||||
|
||||
it('still emits the event when resolveStrangerProfile throws', async () => {
|
||||
const resolveStrangerProfile = vi.fn(async () => { throw new Error('network down'); });
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1, selfUin: 10001,
|
||||
groupId: 12345, fromUin: 0, fromUid: 'u_x',
|
||||
subType: 'add', message: '', flag: 'add:12345:u_x',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(captured).toHaveLength(1);
|
||||
expect(captured[0]).toMatchObject({ fromUin: 0, fromUid: 'u_x' });
|
||||
});
|
||||
|
||||
it('skips the stranger resolve when fromUin is already populated (cache hit)', async () => {
|
||||
// If the decoder's `resolveUidToUin` already found the uin in
|
||||
// the local cache (e.g. group member roster), we shouldn't waste
|
||||
// a wire call.
|
||||
const resolveStrangerProfile = vi.fn(async () => null);
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1, selfUin: 10001,
|
||||
groupId: 12345, fromUin: 99999, fromUid: 'u_known',
|
||||
subType: 'invite', message: '', flag: 'invite:12345:u_known',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).not.toHaveBeenCalled();
|
||||
expect(captured).toHaveLength(1);
|
||||
expect(captured[0]).toMatchObject({ fromUin: 99999, fromUid: 'u_known' });
|
||||
});
|
||||
|
||||
it('skips the stranger resolve when fromUid is empty (no uid to look up by)', async () => {
|
||||
const resolveStrangerProfile = vi.fn(async () => null);
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1, selfUin: 10001,
|
||||
groupId: 12345, fromUin: 0, fromUid: '',
|
||||
subType: 'add', message: '', flag: 'add:12345:',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).not.toHaveBeenCalled();
|
||||
expect(captured).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user