Files
SnowLuma/packages/protocol/src/self-uid.ts
T
DrSmoothl 3db89d0bd0 refactor(protocol,core): converge self-uid resolution to one impl
「取自己 uid」原本散成 5 份、3 种错误文案:core/apis/shared.ts::resolveSelfUid、
group-file/message 各内联手抄一份、get-like 跨包再抄一份。收敛成 protocol 单一
resolveSelfUid(ctx: Pick<BridgeContext,'identity'|'resolveUserUid'>)。

阶段 1(行为不变):
- 新增 protocol/self-uid.ts(依赖方向 core→protocol 要求权威实现下沉 protocol;
  Bridge 与 OIDB service Deps 都满足 Pick 切片)。
- core/apis/shared.ts 改为 re-export 它(现有 resolveSelfUid(bridge) 调用点不变)。
- get-like/group-file/message 三处内联手抄 → 全调权威实现。
- 唯一副作用:message 的错误文案统一为 'self uid is unavailable'(无测试依赖旧文案)。

flash-transfer/forward 的 ?? '' 空串 bug 留待阶段 2;group-admin 合成本地事件保持不动。
全仓 12 包 typecheck Done、protocol/core(471)/onebot(516) 全绿;对抗性 Agent 确认
toInt/Number 边界无实际差异(identity.uin 恒为正整数串)、5 处等价。
2026-07-01 19:12:05 +08:00

30 lines
1.2 KiB
TypeScript

import type { BridgeContext } from './bridge-context';
/**
* Resolve the bot's own UID — the single source of truth for "what is my uid".
*
* Fast path: once warmup populates `selfProfile`, `identity.selfUid` is set and
* returns immediately. Before that, resolve via `resolveUserUid(selfUin)`.
* Throws when there is no self identity at all (uin missing / <= 0).
*
* Callers that put the uid on an outbound packet must route here rather than
* fall back to an empty string: an empty uid ships a broken packet. This used
* to be re-inlined in ContactsApi shared helpers, group-file, message, and
* get-like — the copies had drifted into three error messages and two silent
* `?? ''` empty-uid bugs before being converged here.
*
* Takes just the `BridgeContext` slice it needs, so both a full `Bridge` and a
* bare context (e.g. an OIDB service's Deps) satisfy it.
*/
export async function resolveSelfUid(
ctx: Pick<BridgeContext, 'identity' | 'resolveUserUid'>,
): Promise<string> {
const cached = ctx.identity.selfUid;
if (cached) return cached;
const selfUin = Number(ctx.identity.uin);
if (!Number.isFinite(selfUin) || selfUin <= 0) {
throw new Error('self uid is unavailable');
}
return ctx.resolveUserUid(selfUin);
}