Files
SnowLuma/packages/bridge/src/oidb-services/group-file/get-private-file-url.ts
T
DrSmoothl 5724f490b4 refactor(oidb): thread ctx into serialize/deserialize for self-contained namespaces
The first pass at namespace-per-cmd left a smell: namespaces that
needed identity/uid resolution had to declare `Deps` AND then rewrite
their own spec inside `invoke` via a closure-over-resolved-value:

  // before
  export const serialize = (p: Params, targetUid: string) => ({...});
  export const invoke = async (deps, params) => {
    const targetUid = await deps.resolveUserUid(params.userId);
    return invokeOidb(deps, {
      ...Self,
      serialize: p => serialize(p, targetUid),  // ← escape hatch
    }, params);
  };

Reading "what does this cmd do on the wire?" required reading the
namespace AND the closure rewrite — the namespace wasn't really
self-contained, just a forwarder of two pure functions.

Fix: thread `ctx` (the namespace's full capability surface) into
`serialize` and `deserialize` directly. `serialize` may now return
`Promise<TReq>` so identity / uid lookups happen in place:

  // after
  export const serialize = async (ctx: Deps, p: Params) => ({
    groupUin: p.groupId, type: 1,
    body: {
      targetUid: await ctx.resolveUserUid(p.userId, p.groupId),
      duration: p.duration,
    },
  });
  export const invoke = (deps, params) => invokeOidb(deps, Self, params);

OidbCallSpec gains a `TCtx extends OidbSender` generic; invokeOidb's
first arg becomes `ctx: TCtx` (the sender capability is just a slice
of ctx now). buildOidbRequest / parseOidbResponse follow the same
shape. Async serialize is awaited inside invokeOidb.

17 namespaces had closure rewrites; all 17 collapse to plain
`invokeOidb(deps, Self, params)` invokes. Only one closure remains —
`fetch-user-profile` binds `params.uin` into deserialize as a server-
echo fallback; kept as a documented exception (the rare case where
deserialize wants request context, not ctx).

Wire bytes preserved across all migrations. 326 bridge tests + 302
core tests pass; lint clean.
2026-05-23 12:27:26 +08:00

57 lines
2.0 KiB
TypeScript

// 0xE37_1200 — resolve a private (c2c) file_id → http download URL.
// Carries an opaque 4-byte `field99999` magic the server's deserializer
// requires (Lagrange has the same constant). The facade composes the
// final `http://<server>:<port><url>&isthumb=0` string.
//
// 5s timeout matches the original facade — the server batches these
// at low priority and stalls past the default 30s.
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
OidbPrivateFileDownloadReq, OidbPrivateFileDownloadResp,
OidbPrivateFileDownloadRespResult,
} from '@snowluma/proto-defs/oidb-actions/media';
import { invokeOidb, type OidbSender } from '../../oidb-service';
const TIMEOUT_MS = 5000;
export namespace GetPrivateFileUrl {
export const command = 0xE37;
export const subCommand = 1200;
export interface Params { selfUid: string; fileId: string; fileHash: string; }
export type Deps = OidbSender;
export const serialize = (_ctx: Deps, p: Params): OidbPrivateFileDownloadReq => ({
subCommand: 1200,
field2: 1,
body: {
receiverUid: p.selfUid,
fileUuid: p.fileId,
type: 2,
fileHash: p.fileHash,
t2: 0,
},
field101: 3,
field102: 103,
field200: 1,
field99999: new Uint8Array([0xC0, 0x85, 0x2C, 0x01]),
});
export const deserialize = (_ctx: Deps, body: OidbPrivateFileDownloadResp): OidbPrivateFileDownloadRespResult => {
const result = body.body?.result;
if (!result) throw new Error('private file url response invalid');
return result;
};
export const encode = (env: OidbBase<OidbPrivateFileDownloadReq>): Uint8Array =>
protobuf_encode<OidbBase<OidbPrivateFileDownloadReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<OidbPrivateFileDownloadResp> =>
protobuf_decode<OidbBase<OidbPrivateFileDownloadResp>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<OidbPrivateFileDownloadRespResult> =>
invokeOidb(deps, GetPrivateFileUrl, params, TIMEOUT_MS);
}