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:
DrSmoothl committed 2026-08-13 14:37:56 +08:00
1 parent d26aef86d8
commit 7d5ca5523b
16 files changed
+334 -187

No files matched your search

+34
View File
@@ -44,6 +44,8 @@ interface PendingIdentityWrite {
*/
export interface IdentityFetcher {
fetchProfile(uin: number): Promise<UserProfileInfo>;
/** Inbound UID→UIN network hop. Must not be used to refresh a group roster. */
fetchProfileByUid?(uid: string): Promise<UserProfileInfo>;
fetchGroupMemberList?(groupId: number): Promise<unknown>;
}
@@ -431,10 +433,36 @@ export class IdentityService {
throw new Error(`failed to resolve UID for UIN ${normalized}`);
}
/**
* Inbound UID→UIN. Sync cache/DB (including a numeric UID) first; on miss,
* `fetchProfileByUid`. Returns null when still unknown. Never throws for a
* miss, a fetcher error, or a missing fetcher — inbound events still emit.
* Does not pull a group member list; that remains a roster side-effect.
*/
async resolveUin(uid: string, groupId?: number): Promise<number | null> {
const cached = this.findUinByUid(uid, groupId);
if (cached !== null) return cached;
const normalized = normalizeUid(uid);
if (!normalized || !this.fetcher?.fetchProfileByUid) return null;
try {
const profile = await this.fetcher.fetchProfileByUid(normalized);
const uin = normalizeUin(profile.uin);
if (uin !== null) return uin;
} catch {
// Fall through: a fetcher that remembered then threw still counts as a hit.
}
return this.findUinByUid(normalized, groupId);
}
findUinByUid(uid: string, groupId?: number): number | null {
const normalized = normalizeUid(uid);
if (!normalized) return null;
const numeric = numericUidAsUin(normalized);
if (numeric !== null) return numeric;
if (groupId !== undefined) {
const member = this.findGroupMemberByUid(groupId, normalized);
if (member) return member.uin;
@@ -1303,6 +1331,12 @@ function normalizeUid(uid: unknown): string {
return typeof uid === 'string' ? uid.trim() : '';
}
/** A UID that is itself a decimal UIN. Protocol fact, not a packet-header guess. */
function numericUidAsUin(uid: string): number | null {
if (!/^\d+$/.test(uid)) return null;
return normalizeUin(uid);
}
function normalizeUin(uin: unknown): number | null {
if (typeof uin === 'number' && Number.isFinite(uin)) {
const n = Math.trunc(uin);