feat(onebot): implement rename_group_file (OIDB 0x6D6_4)

Add the file-rename slot to the group-file OIDB envelope (req/resp field 5),
a RenameGroupFile service mirroring MoveGroupFile, a groupFile.rename() api
method, and the rename_group_file action (params group_id/file_id/
current_parent_directory/new_name, NapCat-aligned), replacing the stub.
Wire fields (busId=102, parentFolder=current dir, newFileName) verified
against NapCat's Oidb.0x6D6 transformer; subCommand 4 cross-checked against
the move op (sub 5 on both sides). Serialize + envelope round-trip test locks
the pb tags.
This commit is contained in:
DrSmoothl committed 2026-06-18 00:19:00 +08:00
1 parent 0ba5eba31c
commit 07988629f1
8 files changed
+178 -9

No files matched your search

@@ -21,6 +21,7 @@ import { GetPrivateVideoUrl } from '@snowluma/protocol/oidb-services/group-file/
import { ListGroupFilesPage } from '@snowluma/protocol/oidb-services/group-file/list-group-files-page';
import { MoveGroupFile } from '@snowluma/protocol/oidb-services/group-file/move-group-file';
import { PublishGroupFile } from '@snowluma/protocol/oidb-services/group-file/publish-group-file';
import { RenameGroupFile } from '@snowluma/protocol/oidb-services/group-file/rename-group-file';
import { RenameGroupFolder } from '@snowluma/protocol/oidb-services/group-file/rename-group-folder';
import { UploadGroupFileRequest } from '@snowluma/protocol/oidb-services/group-file/upload-group-file-request';
import { UploadPrivateFileRequest } from '@snowluma/protocol/oidb-services/group-file/upload-private-file-request';
@@ -604,6 +605,10 @@ export class GroupFileApi {
return MoveGroupFile.invoke(this.ctx, { groupId, fileId, parentDirectory, targetDirectory });
}
rename(groupId: number, fileId: string, parentDirectory: string, newFileName: string): Promise<void> {
return RenameGroupFile.invoke(this.ctx, { groupId, fileId, parentDirectory, newFileName });
}
// ─────────────── folders ───────────────
createFolder(groupId: number, name: string, parentId = '/'): Promise<void> {
-9
View File
@@ -1029,15 +1029,6 @@ export const actions = [
},
}),
defineAction({
name: 'rename_group_file',
summary: '重命名群文件(未实现)',
params: {},
run: async () => {
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
},
}),
// get_file — 统一文件信息入口。SnowLuma 按 file_id 解析媒体缓存:
// 先图片、后语音(两者已带 url 重签与 file_size/file_name)。
// 局限:群文件/普通文件的 file_id 无法在此解析——OneBot `get_file` 单参
+16
View File
@@ -100,6 +100,22 @@ export const actions = [
},
}),
// rename_group_file — 0x6D6_4。NapCat 入参:file_id + current_parent_directory
// (文件当前所在目录)+ new_name。SnowLuma 的 file_id 即原始 fileId,无需 UUID 解码。
groupAction({
name: 'rename_group_file',
summary: '重命名群文件',
params: {
file_id: f.string({ allowEmpty: false }),
current_parent_directory: f.string().default('/'),
new_name: f.string({ allowEmpty: false }),
},
run: async (p, ctx) => {
await ctx.bridge.apis.groupFile.rename(p.group_id, p.file_id, p.current_parent_directory || '/', p.new_name);
return okResponse({ ok: true });
},
}),
groupAction({
name: 'create_group_file_folder',
summary: '创建群文件夹',
@@ -683,3 +683,34 @@ describe('extended-actions / get_file', () => {
expect(res).toMatchObject({ status: 'failed', retcode: 1400 });
});
});
// ─── Wave 2: rename_group_file (0x6D6_4) ───
describe('extended-actions / rename_group_file', () => {
it('renames a group file via apis.groupFile.rename', async () => {
const rename = vi.fn(async () => undefined);
const bridge = fakeBridge({ apis: { groupFile: { rename } } });
const res = await makeHandler(fakeCtx(bridge)).handle('rename_group_file', {
group_id: 12345, file_id: '/abc', current_parent_directory: '/', new_name: 'new.txt',
});
expect(res.status).toBe('ok');
expect(rename).toHaveBeenCalledWith(12345, '/abc', '/', 'new.txt');
});
it('rejects missing required params', async () => {
const rename = vi.fn();
const bridge = fakeBridge({ apis: { groupFile: { rename } } });
const res = await makeHandler(fakeCtx(bridge)).handle('rename_group_file', { group_id: 12345, file_id: '/abc' });
expect(res).toMatchObject({ status: 'failed', retcode: 1400 });
expect(rename).not.toHaveBeenCalled();
});
it('surfaces oidb errors (handler maps a thrown error to INTERNAL_ERROR, as its sibling file ops do)', async () => {
const rename = vi.fn(async () => { throw new Error('rename rejected'); });
const bridge = fakeBridge({ apis: { groupFile: { rename } } });
const res = await makeHandler(fakeCtx(bridge)).handle('rename_group_file', {
group_id: 1, file_id: '/a', current_parent_directory: '/', new_name: 'x',
});
expect(res).toMatchObject({ status: 'failed', retcode: 1200, wording: 'rename rejected' });
});
});
@@ -84,10 +84,18 @@ export interface OidbGroupFileMoveReq {
parentDirectory?: pb<5, string>;
targetDirectory?: pb<6, string>;
}
export interface OidbGroupFileRenameReq {
groupUin?: pb<1, uint_32>;
busId?: pb<3, uint_32>;
fileId?: pb<4, string>;
parentFolder?: pb<5, string>;
newFileName?: pb<6, string>;
}
export interface OidbGroupFileReq {
file?: pb<1, OidbGroupFileUploadReq>;
download?: pb<3, OidbGroupFileDownloadReq>;
delete?: pb<4, OidbGroupFileDeleteReq>;
rename?: pb<5, OidbGroupFileRenameReq>;
move?: pb<6, OidbGroupFileMoveReq>;
}
export interface OidbGroupFileUploadResp {
@@ -121,6 +129,7 @@ export interface OidbGroupFileResp {
upload?: pb<1, OidbGroupFileUploadResp>;
download?: pb<3, OidbGroupFileDownloadResp>;
delete?: pb<4, OidbGroupFileRetResp>;
rename?: pb<5, OidbGroupFileRetResp>;
move?: pb<6, OidbGroupFileRetResp>;
}
export interface OidbGroupSendFileInfo {
+4
View File
@@ -263,6 +263,10 @@
"types": "./src/oidb-services/group-file/move-group-file.ts",
"default": "./src/oidb-services/group-file/move-group-file.ts"
},
"./oidb-services/group-file/rename-group-file": {
"types": "./src/oidb-services/group-file/rename-group-file.ts",
"default": "./src/oidb-services/group-file/rename-group-file.ts"
},
"./oidb-services/group-file/create-group-folder": {
"types": "./src/oidb-services/group-file/create-group-folder.ts",
"default": "./src/oidb-services/group-file/create-group-folder.ts"
@@ -0,0 +1,49 @@
// 0x6D6_4 — rename a group file (not a folder; that is 0x6D7_2). The
// rename slot lives on the shared `OidbGroupFileReq` envelope at field 5.
// Wire fields verified against NapCat's Oidb.0x6D6 transformer
// (busId=102, parentFolder = the file's CURRENT parent directory).
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
OidbGroupFileReq, OidbGroupFileResp,
} from '@snowluma/proto-defs/oidb-actions/group-file';
import { invokeOidb, type OidbSender } from '../../oidb-service';
import { ensureRetCodeZero } from '../shared';
export namespace RenameGroupFile {
export const command = 0x6D6;
export const subCommand = 4;
export const uinForm = true;
export interface Params {
groupId: number; fileId: string;
parentDirectory: string; newFileName: string;
}
export type Deps = OidbSender;
export const serialize = (_ctx: Deps, p: Params): OidbGroupFileReq => ({
rename: {
groupUin: p.groupId,
busId: 102,
fileId: p.fileId,
parentFolder: p.parentDirectory,
newFileName: p.newFileName,
},
});
export const deserialize = (_ctx: Deps, body: OidbGroupFileResp): void => {
const result = body.rename;
if (!result) throw new Error('group file rename response missing');
ensureRetCodeZero('group file rename', result.retCode, result.retMsg, result.clientWording);
};
export const encode = (env: OidbBase<OidbGroupFileReq>): Uint8Array =>
protobuf_encode<OidbBase<OidbGroupFileReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<OidbGroupFileResp> =>
protobuf_decode<OidbBase<OidbGroupFileResp>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<void> =>
invokeOidb(deps, RenameGroupFile, params);
}
@@ -0,0 +1,64 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGroupFileReq, OidbGroupFileResp } from '@snowluma/proto-defs/oidb-actions/group-file';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { RenameGroupFile } from '../../../src/oidb-services/group-file/rename-group-file';
// A success response the real server would send: command/sub echoed, rename
// ret slot with retCode 0. Lets `invoke`'s deserialize pass so we can assert
// the request wire (cmd name + body) without it throwing on an empty body.
function okRenameResponse(): Buffer {
const env: OidbBase<OidbGroupFileResp> = {
command: 0x6D6, subCommand: 4, body: { rename: { retCode: 0 } },
};
return Buffer.from(protobuf_encode<OidbBase<OidbGroupFileResp>>(env));
}
function makeSender() {
const defaultResp: SendPacketResult = {
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
responseData: okRenameResponse(),
};
return { sendRawPacket: vi.fn(async () => defaultResp) };
}
describe('RenameGroupFile namespace', () => {
it('declares command 0x6D6 sub 4 (rename slot), uin form', () => {
expect(RenameGroupFile.command).toBe(0x6D6);
expect(RenameGroupFile.subCommand).toBe(4);
expect(RenameGroupFile.uinForm).toBe(true);
});
it('serializes into the rename slot with busId 102 and the current parent folder', () => {
const out = RenameGroupFile.serialize({} as any, {
groupId: 12345, fileId: '/abc', parentDirectory: '/sub', newFileName: 'new.txt',
});
expect(out).toEqual({
rename: {
groupUin: 12345, busId: 102, fileId: '/abc', parentFolder: '/sub', newFileName: 'new.txt',
},
});
});
it('round-trips the rename body through the OIDB envelope (locks the pb field tags)', async () => {
const sender = makeSender();
await RenameGroupFile.invoke(sender, {
groupId: 12345, fileId: '/abc', parentDirectory: '/', newFileName: 'new.txt',
});
expect(sender.sendRawPacket).toHaveBeenCalledOnce();
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(cmd).toBe('OidbSvcTrpcTcp.0x6d6_4');
const env = protobuf_decode<OidbBase<OidbGroupFileReq>>(bytes);
expect(env.command).toBe(0x6D6);
expect(env.subCommand).toBe(4);
expect(env.body.rename).toMatchObject({
groupUin: 12345, busId: 102, fileId: '/abc', parentFolder: '/', newFileName: 'new.txt',
});
});
it('deserialize throws when the rename slot is missing from the response', () => {
expect(() => RenameGroupFile.deserialize({} as any, {})).toThrow(/rename response missing/);
});
});