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
@@ -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