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
@@ -22,8 +22,7 @@ import type { PttTransPush } from '@snowluma/proto-defs/ptt-trans';
|
||||
import type { MsgPushContext } from '../context';
|
||||
import { Event0x210SubType } from '../enums';
|
||||
import {
|
||||
buildTemplateMap, findTemplateValue,
|
||||
parseU64OrZero,
|
||||
buildTemplateMap, c2cRoutingUin, findTemplateValue,
|
||||
resolveUidToUin,
|
||||
} from '../helpers';
|
||||
import type { MsgPushDecoder } from '../registry';
|
||||
@@ -177,7 +176,7 @@ function decodeFriendRequest(ctx: MsgPushContext): QQEventVariant[] {
|
||||
kind: 'friend_request',
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
fromUin: resolveUidToUin(ctx.identity, 0, sourceUid, ctx.fromUin),
|
||||
fromUin: resolveUidToUin(ctx.identity, 0, sourceUid),
|
||||
fromUid: sourceUid,
|
||||
message: request.info.message ?? '',
|
||||
flag: sourceUid,
|
||||
@@ -199,7 +198,7 @@ function decodeFriendRecall(ctx: MsgPushContext): QQEventVariant[] {
|
||||
kind: 'friend_recall',
|
||||
time: recall.info.time ?? ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
userUin: resolveUidToUin(ctx.identity, 0, peerUid, ctx.fromUin),
|
||||
userUin: resolveUidToUin(ctx.identity, 0, peerUid),
|
||||
msgSeq: recall.info.clientSequence ?? 0,
|
||||
clientSeq: recall.info.clientSequence ?? 0,
|
||||
recalledBySelf: isSelfRecall,
|
||||
@@ -211,7 +210,7 @@ function decodeNewFriend(ctx: MsgPushContext): QQEventVariant[] {
|
||||
const nf = protobuf_decode<NewFriend>(ctx.content);
|
||||
if (!nf?.info) return [];
|
||||
const newFriendUid = nf.info.uid ?? '';
|
||||
const newFriendUin = resolveUidToUin(ctx.identity, 0, newFriendUid, ctx.fromUin);
|
||||
const newFriendUin = c2cRoutingUin(ctx.identity, newFriendUid, ctx.fromUin);
|
||||
if (newFriendUin <= 0) return []; // can't surface a `user_id`-less friend_add to OneBot
|
||||
const ev: FriendAddEvent = {
|
||||
kind: 'friend_add',
|
||||
@@ -240,7 +239,7 @@ function decodeInputStatus(ctx: MsgPushContext): QQEventVariant[] {
|
||||
kind: 'friend_input_status',
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
userUin: resolveUidToUin(ctx.identity, 0, fromUid, ctx.fromUin),
|
||||
userUin: resolveUidToUin(ctx.identity, 0, fromUid),
|
||||
userUid: fromUid,
|
||||
eventType,
|
||||
statusText: inputStatusText(eventType),
|
||||
@@ -285,16 +284,15 @@ function decodeFriendPoke(ctx: MsgPushContext): QQEventVariant[] {
|
||||
const templates = buildTemplateMap(grayTip.msgTemplParam ?? []);
|
||||
const actor = findTemplateValue(templates, 'uin_str1');
|
||||
const target = findTemplateValue(templates, 'uin_str2');
|
||||
const senderUin = resolveUidToUin(ctx.identity, 0, actor, parseU64OrZero(actor));
|
||||
const routeFromUin = resolveUidToUin(ctx.identity, 0, ctx.fromUid, ctx.fromUin);
|
||||
const senderUin = resolveUidToUin(ctx.identity, 0, actor);
|
||||
const routeFromUin = c2cRoutingUin(ctx.identity, ctx.fromUid, ctx.fromUin);
|
||||
// The gray-tip template only identifies the two poke endpoints. The C2C
|
||||
// routing head owns the conversation peer: outbound notices use `to`,
|
||||
// while inbound notices use `from`, just like ordinary friend messages.
|
||||
const sentBySelf = routeFromUin > 0 && routeFromUin === ctx.selfUin;
|
||||
const peerUin = sentBySelf
|
||||
? resolveUidToUin(
|
||||
? c2cRoutingUin(
|
||||
ctx.identity,
|
||||
0,
|
||||
ctx.responseHead?.toUid ?? '',
|
||||
ctx.responseHead?.toUin ?? 0,
|
||||
)
|
||||
@@ -311,7 +309,7 @@ function decodeFriendPoke(ctx: MsgPushContext): QQEventVariant[] {
|
||||
selfUin: ctx.selfUin,
|
||||
peerUin,
|
||||
senderUin,
|
||||
targetUin: resolveUidToUin(ctx.identity, 0, target, parseU64OrZero(target)),
|
||||
targetUin: resolveUidToUin(ctx.identity, 0, target),
|
||||
action: findTemplateValue(templates, 'action_str', 'alt_str1'),
|
||||
suffix: findTemplateValue(templates, 'suffix_str'),
|
||||
actionImgUrl: findTemplateValue(templates, 'action_img_url'),
|
||||
|
||||
@@ -21,7 +21,6 @@ import type { MsgPushContext } from '../context';
|
||||
import { Event0x2DCSubType } from '../enums';
|
||||
import {
|
||||
buildTemplateMap, findTemplateValue,
|
||||
parseU64OrZero,
|
||||
resolveUidToUin,
|
||||
unwrapGroupNotifyPayload,
|
||||
} from '../helpers';
|
||||
@@ -50,8 +49,8 @@ function decodeGroupMute(ctx: MsgPushContext): QQEventVariant[] {
|
||||
time: mute.data.timestamp ?? ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: mute.groupUin ?? 0,
|
||||
operatorUin: resolveUidToUin(ctx.identity, mute.groupUin ?? 0, mute.operatorUid ?? '', ctx.fromUin),
|
||||
userUin: resolveUidToUin(ctx.identity, mute.groupUin ?? 0, mute.data.state.targetUid ?? '', 0),
|
||||
operatorUin: resolveUidToUin(ctx.identity, mute.groupUin ?? 0, mute.operatorUid ?? ''),
|
||||
userUin: resolveUidToUin(ctx.identity, mute.groupUin ?? 0, mute.data.state.targetUid ?? ''),
|
||||
duration: duration === 0xFFFFFFFF ? 0x7FFFFFFF : duration,
|
||||
};
|
||||
return [ev];
|
||||
@@ -69,8 +68,8 @@ function decodeGroupRecall(ctx: MsgPushContext): QQEventVariant[] {
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: notify.groupUin ?? 0,
|
||||
operatorUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0,
|
||||
notify.recall.operatorUid || notify.operatorUid || '', ctx.fromUin),
|
||||
authorUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, recalled.authorUid ?? '', ctx.fromUin),
|
||||
notify.recall.operatorUid || notify.operatorUid || ''),
|
||||
authorUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, recalled.authorUid ?? ''),
|
||||
msgSeq: recalled.sequence ?? 0,
|
||||
};
|
||||
return [ev];
|
||||
@@ -89,8 +88,8 @@ function decodeGroupGreyTip(ctx: MsgPushContext): QQEventVariant[] {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: notify.groupUin ?? 0,
|
||||
userUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, actor, parseU64OrZero(actor)),
|
||||
targetUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, target, parseU64OrZero(target)),
|
||||
userUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, actor),
|
||||
targetUin: resolveUidToUin(ctx.identity, notify.groupUin ?? 0, target),
|
||||
action: findTemplateValue(templates, 'action_str', 'alt_str1'),
|
||||
suffix: findTemplateValue(templates, 'suffix_str'),
|
||||
actionImgUrl: findTemplateValue(templates, 'action_img_url'),
|
||||
@@ -111,7 +110,7 @@ function decodeGroupEssence(ctx: MsgPushContext): QQEventVariant[] {
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: essence.groupUin ?? notify.groupUin ?? 0,
|
||||
senderUin: essence.memberUin ?? 0,
|
||||
operatorUin: essence.operatorUin ?? ctx.fromUin,
|
||||
operatorUin: essence.operatorUin ?? 0,
|
||||
msgSeq: essence.msgSequence ?? essence.msgSequence2 ?? notify.msgSequence ?? 0,
|
||||
random: essence.random ?? 0,
|
||||
set: setFlag === 1,
|
||||
@@ -200,7 +199,7 @@ function decodeGroupName(ctx: MsgPushContext): QQEventVariant[] {
|
||||
// Operator is a group member → resolves from the roster. Fall back to 0
|
||||
// (not ctx.fromUin, which on a 0x2DC push is the group id) so an
|
||||
// unresolved operator never surfaces the group id as user_id.
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, notify?.operatorUid ?? '', 0),
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, notify?.operatorUid ?? ''),
|
||||
name,
|
||||
};
|
||||
return [ev];
|
||||
@@ -235,7 +234,7 @@ function decodeGroupMsgEmojiLike(ctx: MsgPushContext): QQEventVariant[] {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId,
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, ctx.fromUin),
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid),
|
||||
operatorUid,
|
||||
msgSeq,
|
||||
emojiId,
|
||||
|
||||
@@ -19,7 +19,7 @@ export const decodeGroupAdmin: MsgPushDecoder = (ctx) => {
|
||||
if (!adminUid) return [];
|
||||
const groupId = admin.groupUin ?? 0;
|
||||
const set = !!enableUid;
|
||||
const userUin = resolveUidToUin(ctx.identity, groupId, adminUid, ctx.fromUin);
|
||||
const userUin = resolveUidToUin(ctx.identity, groupId, adminUid);
|
||||
|
||||
// Keep the in-memory member cache in step with the promotion/demotion.
|
||||
// `get_group_member_info` serves straight from this cache on its
|
||||
|
||||
@@ -15,15 +15,8 @@ export const decodeGroupJoinRequest: MsgPushDecoder = (ctx) => {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: join.groupUin ?? 0,
|
||||
// Strangers (not in our cache, not yet group members) won't resolve
|
||||
// here — pass 0 instead of ctx.fromUin (which on a group-scoped push
|
||||
// IS the group's own uin, so falling back to it produces a bogus
|
||||
// `fromUin === groupId` collision). The packet-pipeline's pre-
|
||||
// dispatch hook does an async UID-form FetchUserProfile lookup to
|
||||
// fill in the uin + nickname before the event is emitted — same
|
||||
// shape as Lagrange's flow at
|
||||
// `dev/Lagrange.Core/.../MessagingLogic.cs:215-224`.
|
||||
fromUin: resolveUidToUin(ctx.identity, join.groupUin ?? 0, join.targetUid ?? '', 0),
|
||||
// Strangers stay unresolved here. Pipeline fills UIN via Identity.resolveUin.
|
||||
fromUin: resolveUidToUin(ctx.identity, join.groupUin ?? 0, join.targetUid ?? ''),
|
||||
fromUid: join.targetUid ?? '',
|
||||
subType: 'add',
|
||||
message: '',
|
||||
@@ -41,7 +34,7 @@ export const decodeGroupInvitation: MsgPushDecoder = (ctx) => {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: inner.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(ctx.identity, inner.groupUin ?? 0, inner.invitorUid ?? '', 0),
|
||||
fromUin: resolveUidToUin(ctx.identity, inner.groupUin ?? 0, inner.invitorUid ?? ''),
|
||||
fromUid: inner.invitorUid ?? '',
|
||||
subType: 'invite',
|
||||
message: '',
|
||||
@@ -58,7 +51,7 @@ export const decodeGroupInvite: MsgPushDecoder = (ctx) => {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId: invite.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(ctx.identity, invite.groupUin ?? 0, invite.invitorUid ?? '', 0),
|
||||
fromUin: resolveUidToUin(ctx.identity, invite.groupUin ?? 0, invite.invitorUid ?? ''),
|
||||
fromUid: invite.invitorUid ?? '',
|
||||
subType: 'invite',
|
||||
message: '',
|
||||
|
||||
@@ -31,8 +31,8 @@ export const decodeGroupMemberJoin: MsgPushDecoder = (ctx) => {
|
||||
time: ctx.head.timestamp,
|
||||
selfUin: ctx.selfUin,
|
||||
groupId,
|
||||
userUin: resolveUidToUin(ctx.identity, groupId, userUid, 0),
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
|
||||
userUin: resolveUidToUin(ctx.identity, groupId, userUid),
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid),
|
||||
userUid,
|
||||
operatorUid,
|
||||
joinType: joinTypeFromOperationType(change.decreaseType ?? 0),
|
||||
@@ -66,7 +66,7 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
|
||||
const dt = change.decreaseType ?? 0;
|
||||
const groupId = change.groupUin ?? 0;
|
||||
const userUid = change.memberUid ?? '';
|
||||
const userUin = resolveUidToUin(ctx.identity, groupId, userUid, 0);
|
||||
const userUin = resolveUidToUin(ctx.identity, groupId, userUid);
|
||||
|
||||
let operatorUid: string;
|
||||
let operatorUin: number;
|
||||
@@ -83,7 +83,7 @@ export const decodeGroupMemberLeave: MsgPushDecoder = (ctx) => {
|
||||
operatorUid = dt === 3
|
||||
? decodeNestedOperatorUid(operatorBytes, 'group member decrease type=3')
|
||||
: decodeGroupChangeOperatorUid(operatorBytes, `group member decrease type=${dt}`);
|
||||
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid, 0);
|
||||
operatorUin = resolveUidToUin(ctx.identity, groupId, operatorUid);
|
||||
}
|
||||
const ev: GroupMemberLeave = {
|
||||
kind: 'group_member_leave',
|
||||
@@ -119,7 +119,7 @@ export const decodeGroupSelfJoined: MsgPushDecoder = (ctx) => {
|
||||
groupId,
|
||||
userUin: ctx.selfUin,
|
||||
userUid: ctx.identity.selfUid ?? '',
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid, 0),
|
||||
operatorUin: resolveUidToUin(ctx.identity, groupId, operatorUid),
|
||||
operatorUid,
|
||||
};
|
||||
return [ev];
|
||||
|
||||
@@ -62,28 +62,27 @@ export function decompressData(
|
||||
return { ok: true, text: Buffer.from(data).toString('utf8'), outputBytes: data.length };
|
||||
}
|
||||
|
||||
export function isNumericUin(value: string): boolean {
|
||||
return value.length > 0 && /^\d+$/.test(value);
|
||||
}
|
||||
|
||||
export function parseU64OrZero(value: string): number {
|
||||
if (!value) return 0;
|
||||
const n = parseInt(value, 10);
|
||||
return isNaN(n) ? 0 : n;
|
||||
}
|
||||
|
||||
// Cascades group-scoped lookup → in-memory map → SQLite.
|
||||
// No network fallback on the parse hot path: missing identities fall back to
|
||||
// `fallback` and downstream events drive a roster refresh asynchronously.
|
||||
export function resolveUidToUin(identity: IdentityService, groupId: number, uid: string, fallback = 0): number {
|
||||
if (!uid) return fallback;
|
||||
if (isNumericUin(uid)) {
|
||||
const n = parseInt(uid, 10);
|
||||
if (!isNaN(n)) return n;
|
||||
}
|
||||
const uin = identity.findUinByUid(uid, groupId || undefined);
|
||||
if (uin !== null) return uin;
|
||||
return fallback;
|
||||
/** Sync Identity lookup. Miss is unresolved (0). Decoder must not invent a UIN. */
|
||||
export function resolveUidToUin(identity: IdentityService, groupId: number, uid: string): number {
|
||||
if (!uid) return 0;
|
||||
return identity.findUinByUid(uid, groupId || undefined) ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* C2C routing hop. Prefer Identity on the hop UID; otherwise use that hop's
|
||||
* own wire UIN. This is not a fallback that assigns an unrelated header UIN
|
||||
* to a different UID.
|
||||
*/
|
||||
export function c2cRoutingUin(identity: IdentityService, uid: string, wireUin: number): number {
|
||||
const resolved = resolveUidToUin(identity, 0, uid);
|
||||
if (resolved > 0) return resolved;
|
||||
return wireUin > 0 ? wireUin : 0;
|
||||
}
|
||||
|
||||
const OPERATOR_BYTES_PREVIEW_LENGTH = 32;
|
||||
|
||||
Reference in new issue
Block a user