refactor(protocol): send leftover OIDB envelopes through the shared caller

Moving a friend who is already in the target category still succeeds.
Media upload preflight now uses the same envelope success and error
rules as other OIDB calls.
This commit is contained in:
DrSmoothl committed 2026-09-13 19:53:20 +08:00
1 parent 1bb8c9f46c
commit 6328890d34
12 files changed
+251 -175

No files matched your search

@@ -1,21 +1,19 @@
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase, OidbBaseMeta } from '@snowluma/proto-defs/oidb';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbSetFriendCategoryRequest } from '@snowluma/proto-defs/oidb-actions/base';
import { makeOidbEnvelope } from '../../bridge-oidb';
import { OidbError, type OidbSender } from '../../oidb-service';
import { invokeOidb, type OidbSender } from '../../oidb-service';
/**
* Move one friend into a category.
*
* The desktop client treats 2001002 as an idempotent success result, so this
* call intentionally validates the response itself instead of using the
* generic OIDB helper that rejects every non-zero application code.
* The desktop client treats 2001002 as an idempotent success result.
*/
export namespace SetFriendCategory {
export const command = 0x1255;
export const subCommand = 0;
export const uinForm = true;
export const alreadyAppliedCode = 2_001_002;
export const acceptedEnvelopeCodes = [alreadyAppliedCode] as const;
export interface Params {
uid: string;
@@ -29,35 +27,14 @@ export namespace SetFriendCategory {
categoryId: params.categoryId,
});
function accepted(code: number | null | undefined): boolean {
return code == null || code === 0 || code === alreadyAppliedCode;
}
export const deserialize = (): void => undefined;
export async function invoke(deps: Deps, params: Params): Promise<void> {
const body = serialize(deps, params);
const bytes = protobuf_encode<OidbBase<OidbSetFriendCategoryRequest>>(
makeOidbEnvelope(command, subCommand, body, uinForm),
);
const result = await deps.sendRawPacket('OidbSvcTrpcTcp.0x1255_0', bytes);
export const encode = (env: OidbBase<OidbSetFriendCategoryRequest>): Uint8Array =>
protobuf_encode<OidbBase<OidbSetFriendCategoryRequest>>(env);
if (!result.gotResponse) {
throw new Error(result.errorMessage || 'no response');
}
if (!result.success && result.errorCode !== alreadyAppliedCode) {
if (result.errorCode) {
throw new OidbError(result.errorCode, result.errorMessage, command, subCommand);
}
throw new Error(result.errorMessage || 'packet send failed');
}
if (!accepted(result.errorCode)) {
throw new OidbError(result.errorCode, result.errorMessage, command, subCommand);
}
export const decode = (bytes: Uint8Array): OidbBase<OidbSetFriendCategoryRequest> =>
protobuf_decode<OidbBase<OidbSetFriendCategoryRequest>>(bytes);
const response = result.responseData ?? new Uint8Array(0);
if (response.length === 0) return;
const meta = protobuf_decode<OidbBaseMeta>(response);
if (!accepted(meta.errorCode)) {
throw new OidbError(meta.errorCode!, meta.errorMsg ?? '', command, subCommand);
}
}
export const invoke = (deps: Deps, params: Params): Promise<void> =>
invokeOidb(deps, SetFriendCategory, params);
}
@@ -0,0 +1,83 @@
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
NTV2ExtBizInfo,
NTV2UploadInfo,
NTV2UploadRespBody,
NTV2UploadRichMediaReq,
NTV2UploadRichMediaResp,
} from '@snowluma/proto-defs/highway';
import { invokeOidb, type OidbSender } from '../../oidb-service';
/**
* NTV2 rich-media upload preflight. Image / PTT / video share one Req/Resp
* type; command (0x11C4 / 0x11C5 / …) comes from the caller. Highway PUTs
* stay in the pipeline.
*/
export namespace Ntv2UploadRequest {
export const command = 0;
export const subCommand = 100;
export const uinForm = true;
export interface Params {
oidbCmd: number;
isGroup: boolean;
targetIdOrUid: string | number;
requestId: number;
businessType: number;
uploadInfo: NTV2UploadInfo[];
compatQmsgSceneType: number;
extBizInfo: NTV2ExtBizInfo;
tryFast: boolean;
clientRandomId: bigint;
}
export type Deps = OidbSender;
export const resolveCommand = (params: Params): number => params.oidbCmd;
export const serialize = (_ctx: Deps, params: Params): NTV2UploadRichMediaReq => ({
reqHead: {
common: { requestId: params.requestId, command: 100 },
scene: {
requestType: 2,
businessType: params.businessType,
sceneType: params.isGroup ? 2 : 1,
...(params.isGroup
? { group: { groupUin: Number(params.targetIdOrUid) } }
: { c2c: { accountType: 2, targetUid: String(params.targetIdOrUid) } }),
},
client: { agentType: 2 },
},
upload: {
uploadInfo: params.uploadInfo,
tryFastUploadCompleted: params.tryFast,
srvSendMsg: false,
clientRandomId: params.clientRandomId,
compatQmsgSceneType: params.compatQmsgSceneType,
extBizInfo: params.extBizInfo,
clientSeq: 0,
noNeedCompatMsg: false,
},
});
export const deserialize = (_ctx: Deps, body: NTV2UploadRichMediaResp): NTV2UploadRespBody => {
if (!body) throw new Error('media upload response body missing');
if (body.respHead?.retCode && body.respHead.retCode !== 0) {
throw new Error(body.respHead.message ?? 'media upload failed');
}
const upload = body.upload;
if (!upload) throw new Error('media upload response body missing');
if (!upload.msgInfo) throw new Error('upload response missing msgInfo');
return upload;
};
export const encode = (env: OidbBase<NTV2UploadRichMediaReq>): Uint8Array =>
protobuf_encode<OidbBase<NTV2UploadRichMediaReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<NTV2UploadRichMediaResp> =>
protobuf_decode<OidbBase<NTV2UploadRichMediaResp>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<NTV2UploadRespBody> =>
invokeOidb(deps, Ntv2UploadRequest, params);
}