refactor(protocol): own inbound UID-to-UIN resolution in Identity
Inbound person lookup used to be split across decoders, the packet pipeline, and Bridge closures. Missing identities were filled from unrelated packet headers, and "who is this person" was coupled to roster refresh and pending-request enrichment. Identity now owns both directions of resolution. The hot path stays synchronous and emits unresolved events on a miss. Only the paths that already waited before dispatch perform a network lookup, and that lookup is a profile fetch by UID rather than a member-list refresh. Pending-request comments and approval flags stay outside Identity. Decoders no longer invent a UIN from a group routing header. Private chat routing still uses a hop's own wire UIN when the hop is named that way. Inbound misses stay availability-first: they do not throw and do not mark Identity degraded.
This commit is contained in:
16 files changed
+334
-187
No files matched your search
@@ -4,16 +4,14 @@ import type { GeneralGrayTipInfo } from '@snowluma/proto-defs/notify';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { IdentityService } from '../../src/identity-service';
|
||||
import { IdentityService } from '../../src/identity-service';
|
||||
import { parseMsgPush } from '../../src/msg-push';
|
||||
|
||||
const SELF_UIN = 10001;
|
||||
const FRIEND_A = 20001;
|
||||
const FRIEND_B = 20002;
|
||||
|
||||
const identity = {
|
||||
findUinByUid: () => null,
|
||||
} as unknown as IdentityService;
|
||||
const identity = IdentityService.memory(String(SELF_UIN));
|
||||
|
||||
function friendPokePacket(
|
||||
peerUin: number,
|
||||
|
||||
@@ -20,7 +20,7 @@ import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { PushMsg, PushMsgBody } from '@snowluma/proto-defs/message';
|
||||
import type { GroupChange, OnlineDeviceNotify } from '@snowluma/proto-defs/notify';
|
||||
import type { IdentityService } from '../../src/identity-service';
|
||||
import { IdentityService } from '../../src/identity-service';
|
||||
import {
|
||||
deriveSysMsgDedupIdentity,
|
||||
parseMsgPush,
|
||||
@@ -28,7 +28,7 @@ import {
|
||||
type SysMsgDedupIdentity,
|
||||
} from '../../src/msg-push';
|
||||
|
||||
const identity = { findUinByUid: () => null, findFriend: () => undefined } as unknown as IdentityService;
|
||||
const identity = IdentityService.memory('2000000001');
|
||||
|
||||
function pushPacket(message: PushMsgBody): PacketInfo {
|
||||
return {
|
||||
@@ -43,7 +43,7 @@ function pushPacket(message: PushMsgBody): PacketInfo {
|
||||
}
|
||||
|
||||
// A type-33 group member-increase push (the #137 shape). `memberUid` is a
|
||||
// numeric string so resolveUidToUin returns it without touching identity.
|
||||
// numeric string so Identity.findUinByUid returns it without a map lookup.
|
||||
function memberIncreasePush(opts: { groupId: number; memberUin: number; sequence: number; msgId: number }): PacketInfo {
|
||||
return pushPacket({
|
||||
responseHead: { fromUin: opts.groupId, fromUid: '' },
|
||||
|
||||
@@ -13,24 +13,31 @@ import { IncomingPacketPipeline } from '@snowluma/protocol/packet-pipeline';
|
||||
import { BridgeEventBus } from '@snowluma/protocol/event-bus';
|
||||
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import type { QQEventVariant } from '@snowluma/protocol/events';
|
||||
import type { GroupMemberInfo, QQGroupInfo } from '@snowluma/protocol/qq-info';
|
||||
import type { GroupMemberInfo, QQGroupInfo, UserProfileInfo } from '@snowluma/protocol/qq-info';
|
||||
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||
|
||||
function makeProfile(uin: number, uid = '', nickname = ''): UserProfileInfo {
|
||||
return { uin, uid, nickname, remark: '', qid: '', sex: 'unknown', age: 0, sign: '', avatar: '', level: 0 };
|
||||
}
|
||||
|
||||
function makePipeline(opts: {
|
||||
resolveStrangerProfile?: vi.Mock;
|
||||
fetchProfileByUid?: vi.Mock;
|
||||
resolveGroupJoinRequest?: vi.Mock;
|
||||
resolveGroupInviteCardSequence?: vi.Mock;
|
||||
} = {}) {
|
||||
const identity = IdentityService.memory('10001');
|
||||
const events = new BridgeEventBus();
|
||||
const resolveStrangerProfile = opts.resolveStrangerProfile ?? vi.fn(async () => null);
|
||||
const fetchProfileByUid = opts.fetchProfileByUid ?? vi.fn(async () => makeProfile(0));
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0),
|
||||
fetchProfileByUid,
|
||||
});
|
||||
const resolveGroupJoinRequest = opts.resolveGroupJoinRequest ?? vi.fn(async () => null);
|
||||
const resolveGroupInviteCardSequence = opts.resolveGroupInviteCardSequence ?? vi.fn(async () => null);
|
||||
const pipeline = new IncomingPacketPipeline({
|
||||
identity,
|
||||
events,
|
||||
refreshMemberCache: vi.fn(async () => false),
|
||||
resolveStrangerProfile,
|
||||
resolveGroupJoinRequest,
|
||||
resolveGroupInviteCardSequence,
|
||||
});
|
||||
@@ -40,16 +47,14 @@ function makePipeline(opts: {
|
||||
|
||||
return {
|
||||
identity, pipeline, events, captured,
|
||||
resolveStrangerProfile, resolveGroupJoinRequest, resolveGroupInviteCardSequence,
|
||||
fetchProfileByUid, resolveGroupJoinRequest, resolveGroupInviteCardSequence,
|
||||
};
|
||||
}
|
||||
|
||||
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 });
|
||||
it('calls fetchProfileByUid when group_invite has fromUin=0 + fromUid', async () => {
|
||||
const fetchProfileByUid = vi.fn(async (_uid: string) => makeProfile(950929451, 'u_stranger_abc', '小明'));
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid });
|
||||
|
||||
// Plant a parser that emits the user's exact bug-report shape:
|
||||
// groupId set, fromUid is a string UID, fromUin=0 (decoder fix).
|
||||
@@ -70,7 +75,7 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
// Let the async hook flush.
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).toHaveBeenCalledWith('u_stranger_abc');
|
||||
expect(fetchProfileByUid).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
|
||||
@@ -78,9 +83,9 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
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 });
|
||||
it('still emits the event when fetchProfileByUid returns no uin (fail open)', async () => {
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(0));
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
@@ -92,7 +97,7 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).toHaveBeenCalledOnce();
|
||||
expect(fetchProfileByUid).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
|
||||
@@ -102,9 +107,9 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
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 });
|
||||
it('still emits the event when fetchProfileByUid throws', async () => {
|
||||
const fetchProfileByUid = vi.fn(async () => { throw new Error('network down'); });
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
@@ -128,9 +133,9 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
// and the uin resolve are now decoupled: the comment is fetched
|
||||
// unconditionally; only the (wasteful) profile lookup is skipped on
|
||||
// a cache hit.
|
||||
const resolveStrangerProfile = vi.fn(async () => null);
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(0));
|
||||
const resolveGroupJoinRequest = vi.fn(async () => ({ comment: '求通过', sequence: 42 }));
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile, resolveGroupJoinRequest });
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid, resolveGroupJoinRequest });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
@@ -143,7 +148,7 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
// Profile lookup is skipped — the uin is already known, no wasted call…
|
||||
expect(resolveStrangerProfile).not.toHaveBeenCalled();
|
||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||
// …but the comment IS still fetched from the pending-request queue
|
||||
// and applied to the emitted event.
|
||||
expect(resolveGroupJoinRequest).toHaveBeenCalledWith(12345, 'u_known', 'add');
|
||||
@@ -161,14 +166,12 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
// 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 fetchProfileByUid = vi.fn(async () => makeProfile(1957003260, 'u_UVLHYYqba27WPUzTrdZlCA', 'KitaIkuyo'));
|
||||
const resolveGroupJoinRequest = vi.fn(async () => ({
|
||||
comment: '你们好', sequence: 1779543612823906,
|
||||
}));
|
||||
const { pipeline, captured } = makePipeline({
|
||||
resolveStrangerProfile, resolveGroupJoinRequest,
|
||||
fetchProfileByUid, resolveGroupJoinRequest,
|
||||
});
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
@@ -262,14 +265,12 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
// 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 fetchProfileByUid = vi.fn(async () => makeProfile(12345, 'u_x', 'OK'));
|
||||
const resolveGroupJoinRequest = vi.fn(async () => {
|
||||
throw new Error('queue lookup failed');
|
||||
});
|
||||
const { pipeline, captured } = makePipeline({
|
||||
resolveStrangerProfile, resolveGroupJoinRequest,
|
||||
fetchProfileByUid, resolveGroupJoinRequest,
|
||||
});
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
@@ -310,25 +311,15 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
expect(resolveGroupJoinRequest).toHaveBeenCalledWith(1, 'u_inviter', 'invite');
|
||||
});
|
||||
|
||||
it('forces re-resolve when fromUin === groupId (legacy cache pollution)', async () => {
|
||||
// Pre-fix builds stored `<requester_uid> → <groupUin>` because
|
||||
// the decoder's fallback was `ctx.fromUin` (= group's own uin on
|
||||
// a group-scoped push). After upgrade, the decoder's
|
||||
// resolveUidToUin would re-read that polluted mapping and emit
|
||||
// event.fromUin === groupId. The `fromUin <= 0` check alone
|
||||
// wouldn't fire the async resolve, so the bug would persist.
|
||||
// This guard catches that signature and forces a re-lookup, so
|
||||
// the cache self-heals on the next event.
|
||||
const resolveStrangerProfile = vi.fn(async () => ({
|
||||
uin: 1957003260, nickname: 'KitaIkuyo',
|
||||
}));
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile });
|
||||
it('does not treat fromUin === groupId as an unresolved identity', async () => {
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(1957003260, 'u_kitaikuyo', 'KitaIkuyo'));
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
time: 1, selfUin: 10001,
|
||||
groupId: 950929451,
|
||||
fromUin: 950929451, // ← POLLUTED: equals groupId
|
||||
fromUin: 950929451,
|
||||
fromUid: 'u_kitaikuyo',
|
||||
subType: 'add', message: '', flag: 'add:950929451:u_kitaikuyo',
|
||||
} as QQEventVariant]);
|
||||
@@ -336,19 +327,19 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveStrangerProfile).toHaveBeenCalledWith('u_kitaikuyo');
|
||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||
expect(captured).toHaveLength(1);
|
||||
expect(captured[0]).toMatchObject({
|
||||
fromUin: 1957003260, // ← corrected by the async resolve
|
||||
fromUin: 950929451,
|
||||
fromUid: 'u_kitaikuyo',
|
||||
groupId: 950929451,
|
||||
});
|
||||
});
|
||||
|
||||
it('skips both lookups when fromUid is empty (no uid to look up by)', async () => {
|
||||
const resolveStrangerProfile = vi.fn(async () => null);
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(0));
|
||||
const resolveGroupJoinRequest = vi.fn(async () => null);
|
||||
const { pipeline, captured } = makePipeline({ resolveStrangerProfile, resolveGroupJoinRequest });
|
||||
const { pipeline, captured } = makePipeline({ fetchProfileByUid, resolveGroupJoinRequest });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite',
|
||||
@@ -361,7 +352,7 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
// No uid → nothing to query by → neither lookup runs, event still emits.
|
||||
expect(resolveStrangerProfile).not.toHaveBeenCalled();
|
||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||
expect(resolveGroupJoinRequest).not.toHaveBeenCalled();
|
||||
expect(captured).toHaveLength(1);
|
||||
});
|
||||
@@ -397,7 +388,6 @@ describe('IncomingPacketPipeline / #1 group card self-heal', () => {
|
||||
identity,
|
||||
events,
|
||||
refreshMemberCache: vi.fn(async () => false),
|
||||
resolveStrangerProfile: vi.fn(async () => null),
|
||||
resolveGroupJoinRequest: vi.fn(async () => null),
|
||||
});
|
||||
return { identity, pipeline };
|
||||
@@ -463,7 +453,6 @@ describe('IncomingPacketPipeline / group_card_change from message traffic', () =
|
||||
const pipeline = new IncomingPacketPipeline({
|
||||
identity, events,
|
||||
refreshMemberCache: vi.fn(async () => false),
|
||||
resolveStrangerProfile: vi.fn(async () => null),
|
||||
resolveGroupJoinRequest: vi.fn(async () => null),
|
||||
});
|
||||
const captured: QQEventVariant[] = [];
|
||||
|
||||
@@ -75,15 +75,23 @@ function msgPushContext(
|
||||
|
||||
function makePipeline(opts: {
|
||||
refreshMemberCache?: () => Promise<boolean>;
|
||||
resolveStrangerProfile?: () => Promise<{ uin: number; nickname: string } | null>;
|
||||
fetchProfileByUid?: () => Promise<{ uin: number; uid: string; nickname: string; remark: string; qid: string; sex: string; age: number; sign: string; avatar: string; level: number }>;
|
||||
} = {}) {
|
||||
const identity = IdentityService.memory('10001');
|
||||
if (opts.fetchProfileByUid) {
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => ({
|
||||
uin: 0, uid: '', nickname: '', remark: '', qid: '', sex: 'unknown',
|
||||
age: 0, sign: '', avatar: '', level: 0,
|
||||
}),
|
||||
fetchProfileByUid: opts.fetchProfileByUid,
|
||||
});
|
||||
}
|
||||
const events = new BridgeEventBus();
|
||||
const pipeline = new IncomingPacketPipeline({
|
||||
identity,
|
||||
events,
|
||||
refreshMemberCache: opts.refreshMemberCache ?? vi.fn(async () => false),
|
||||
resolveStrangerProfile: opts.resolveStrangerProfile ?? vi.fn(async () => null),
|
||||
resolveGroupJoinRequest: vi.fn(async () => null),
|
||||
});
|
||||
const dispatched: Array<{ event: QQEventVariant; req: number | undefined }> = [];
|
||||
@@ -200,7 +208,7 @@ describe('IncomingPacketPipeline TRACE lifecycle', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('records degraded enrichment but still completes after dispatch', async () => {
|
||||
it('completes join identity enrichment without treating roster refresh as the UIN hop', async () => {
|
||||
const entries: LogEntry[] = [];
|
||||
setLogLevel('trace');
|
||||
const unsubscribe = subscribeLogs((entry) => entries.push(entry));
|
||||
@@ -227,7 +235,6 @@ describe('IncomingPacketPipeline TRACE lifecycle', () => {
|
||||
expect(packetTrace(entries).map((entry) => entry.message)).toEqual([
|
||||
'packet_branch serviceCmd="Trace.Command" seqId=77 parser=1 branch=parser_events events=1',
|
||||
'packet_branch serviceCmd="Trace.Command" seqId=77 branch=enrichment_started eventKind="group_member_join" enrichment="identity_refresh"',
|
||||
'packet_branch serviceCmd="Trace.Command" seqId=77 branch=enrichment_degraded eventKind="group_member_join" enrichment="identity_refresh" error="fixture refresh failed"',
|
||||
'packet_branch serviceCmd="Trace.Command" seqId=77 branch=dispatch eventKind="group_member_join" mode=enriched',
|
||||
expect.stringMatching(/^packet_terminal serviceCmd="Trace\.Command" seqId=77 outcome=completed reason=dispatch_complete events=1 dispatched=1 parserErrors=0 elapsedMs=\d+$/),
|
||||
]);
|
||||
@@ -305,7 +312,21 @@ describe('IncomingPacketPipeline TRACE lifecycle', () => {
|
||||
const unsubscribe = subscribeLogs((entry) => entries.push(entry));
|
||||
try {
|
||||
const { pipeline, dispatched } = makePipeline({
|
||||
resolveStrangerProfile: () => profile,
|
||||
fetchProfileByUid: async () => {
|
||||
const resolved = await profile;
|
||||
return {
|
||||
uin: resolved.uin,
|
||||
uid: 'u_stranger',
|
||||
nickname: resolved.nickname,
|
||||
remark: '',
|
||||
qid: '',
|
||||
sex: 'unknown',
|
||||
age: 0,
|
||||
sign: '',
|
||||
avatar: '',
|
||||
level: 0,
|
||||
};
|
||||
},
|
||||
});
|
||||
pipeline.registerCmd('Trace.Command', () => [{
|
||||
kind: 'group_invite',
|
||||
|
||||
Reference in new issue
Block a user