From 2981799803d8ead790be2253c388787aac2d2aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sun, 1 Sep 2024 14:11:28 +0800 Subject: [PATCH] fix: file api --- src/common/helper.ts | 22 ++++---- src/onebot/action/file/DelGroupFile.ts | 23 --------- src/onebot/action/file/DelGroupFileFolder.ts | 24 --------- src/onebot/action/file/GetGroupFileCount.ts | 23 --------- src/onebot/action/file/GetGroupFileList.ts | 50 ------------------- src/onebot/action/file/SetGroupFileFolder.ts | 23 --------- .../action/go-cqhttp/CreateGroupFileFolder.ts | 15 +----- .../action/go-cqhttp/DeleteGroupFile.ts | 18 ++----- .../action/go-cqhttp/DeleteGroupFileFolder.ts | 17 ++----- .../action/go-cqhttp/GetGroupFilesByFolder.ts | 24 ++------- .../action/go-cqhttp/GetGroupRootFiles.ts | 13 ++--- src/onebot/action/index.ts | 25 ++-------- src/onebot/entities.ts | 2 +- 13 files changed, 36 insertions(+), 243 deletions(-) delete mode 100644 src/onebot/action/file/DelGroupFile.ts delete mode 100644 src/onebot/action/file/DelGroupFileFolder.ts delete mode 100644 src/onebot/action/file/GetGroupFileCount.ts delete mode 100644 src/onebot/action/file/GetGroupFileList.ts delete mode 100644 src/onebot/action/file/SetGroupFileFolder.ts diff --git a/src/common/helper.ts b/src/common/helper.ts index aa37e0cd..d0ee50a6 100644 --- a/src/common/helper.ts +++ b/src/common/helper.ts @@ -25,29 +25,31 @@ export async function solveAsyncProblem Promise; - -export class DelGroupFile extends BaseAction { - actionName = ActionName.DelGroupFile; - payloadSchema = SchemaData; - - async _handle(payload: Payload) { - return await this.core.apis.GroupApi.DelGroupFile(payload.group_id.toString(), [payload.file_id]); - } -} diff --git a/src/onebot/action/file/DelGroupFileFolder.ts b/src/onebot/action/file/DelGroupFileFolder.ts deleted file mode 100644 index 4fe7ede1..00000000 --- a/src/onebot/action/file/DelGroupFileFolder.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; - -const SchemaData = { - type: 'object', - properties: { - group_id: { type: ['string', 'number'] }, - folder_id: { type: 'string' }, - }, - required: ['group_id', 'folder_id'], -} as const satisfies JSONSchema; - -type Payload = FromSchema; - -export class DelGroupFileFolder extends BaseAction { - actionName = ActionName.DelGroupFileFolder; - payloadSchema = SchemaData; - - async _handle(payload: Payload) { - return (await this.core.apis.GroupApi.DelGroupFileFolder( - payload.group_id.toString(), payload.folder_id)).groupFileCommonResult; - } -} diff --git a/src/onebot/action/file/GetGroupFileCount.ts b/src/onebot/action/file/GetGroupFileCount.ts deleted file mode 100644 index 0fde4908..00000000 --- a/src/onebot/action/file/GetGroupFileCount.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; - -const SchemaData = { - type: 'object', - properties: { - group_id: { type: ['string', 'number'] }, - }, - required: ['group_id'], -} as const satisfies JSONSchema; - -type Payload = FromSchema; - -export class GetGroupFileCount extends BaseAction { - actionName = ActionName.GetGroupFileCount; - payloadSchema = SchemaData; - - async _handle(payload: Payload) { - const ret = await this.core.apis.GroupApi.getGroupFileCount([payload.group_id?.toString()]); - return { count: ret.groupFileCounts[0] }; - } -} diff --git a/src/onebot/action/file/GetGroupFileList.ts b/src/onebot/action/file/GetGroupFileList.ts deleted file mode 100644 index f53b08f6..00000000 --- a/src/onebot/action/file/GetGroupFileList.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; -import { FileNapCatOneBotUUID } from '@/common/helper'; - -const SchemaData = { - type: 'object', - properties: { - group_id: { type: ['string', 'number'] }, - start_index: { type: ['string', 'number'] }, - file_count: { type: ['string', 'number'] }, - folder_id: { type: ['string', 'number'] }, - }, - required: ['group_id', 'start_index', 'file_count'], -} as const satisfies JSONSchema; - -type Payload = FromSchema; - -export class GetGroupFileList extends BaseAction }> { - actionName = ActionName.GetGroupFileList; - payloadSchema = SchemaData; - - async _handle(payload: Payload) { - let param = {}; - if (payload.folder_id) { - param = { - folderId: payload.folder_id.toString(), - }; - } - const ret = await this.core.apis.MsgApi.getGroupFileList(payload.group_id.toString(), { - sortType: 1, - fileCount: +payload.file_count, - startIndex: +payload.start_index, - sortOrder: 2, - showOnlinedocFolder: 0, - ...param - }).catch(() => { - return []; - }); - ret.forEach((e) => { - const fileModelId = e?.fileInfo?.fileModelId; - if (fileModelId) - e.fileInfo!.fileId = FileNapCatOneBotUUID.encodeModelId({ - chatType: 2, - peerUid: payload.group_id.toString() - }, fileModelId); - }); - return { FileList: ret }; - } -} diff --git a/src/onebot/action/file/SetGroupFileFolder.ts b/src/onebot/action/file/SetGroupFileFolder.ts deleted file mode 100644 index fe9f222a..00000000 --- a/src/onebot/action/file/SetGroupFileFolder.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; - -const SchemaData = { - type: 'object', - properties: { - group_id: { type: ['string', 'number'] }, - folder_name: { type: 'string' }, - }, - required: ['group_id', 'folder_name'], -} as const satisfies JSONSchema; - -type Payload = FromSchema; - -export class SetGroupFileFolder extends BaseAction { - actionName = ActionName.SetGroupFileFolder; - payloadSchema = SchemaData; - - async _handle(payload: Payload) { - return (await this.core.apis.GroupApi.CreatGroupFileFolder(payload.group_id.toString(), payload.folder_name)).resultWithGroupItem; - } -} diff --git a/src/onebot/action/go-cqhttp/CreateGroupFileFolder.ts b/src/onebot/action/go-cqhttp/CreateGroupFileFolder.ts index bd831260..eb298bf5 100644 --- a/src/onebot/action/go-cqhttp/CreateGroupFileFolder.ts +++ b/src/onebot/action/go-cqhttp/CreateGroupFileFolder.ts @@ -1,10 +1,6 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NapCatOneBot11Adapter } from '@/onebot'; -import { NapCatCore } from '@/core'; -import { SetGroupFileFolder } from '@/onebot/action/file/SetGroupFileFolder'; - const SchemaData = { type: 'object', properties: { @@ -16,17 +12,10 @@ const SchemaData = { type Payload = FromSchema; -export class CreateGroupFileFolder extends BaseAction { +export class CreateGroupFileFolder extends BaseAction { actionName = ActionName.GoCQHTTP_CreateGroupFileFolder; payloadSchema = SchemaData; - - constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore, - private ncSetGroupFileFolderImpl: SetGroupFileFolder) { - super(obContext, core); - } - async _handle(payload: Payload) { - await this.ncSetGroupFileFolderImpl._handle(payload); - return null; + return (await this.core.apis.GroupApi.CreatGroupFileFolder(payload.group_id.toString(), payload.folder_name)).resultWithGroupItem; } } diff --git a/src/onebot/action/go-cqhttp/DeleteGroupFile.ts b/src/onebot/action/go-cqhttp/DeleteGroupFile.ts index ec8df16c..67bd1ee9 100644 --- a/src/onebot/action/go-cqhttp/DeleteGroupFile.ts +++ b/src/onebot/action/go-cqhttp/DeleteGroupFile.ts @@ -1,10 +1,7 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NapCatCore } from '@/core'; -import { NapCatOneBot11Adapter } from '@/onebot'; -import { DelGroupFile } from '@/onebot/action/file/DelGroupFile'; - +import { FileNapCatOneBotUUID } from '@/common/helper'; const SchemaData = { type: 'object', properties: { @@ -16,17 +13,12 @@ const SchemaData = { type Payload = FromSchema; -export class DeleteGroupFile extends BaseAction { +export class DeleteGroupFile extends BaseAction { actionName = ActionName.GOCQHTTP_DeleteGroupFile; payloadSchema = SchemaData; - - constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore, - private ncDelGroupFileImpl: DelGroupFile) { - super(obContext, core); - } - async _handle(payload: Payload) { - await this.ncDelGroupFileImpl._handle(payload); - return null; + const data = FileNapCatOneBotUUID.decodeModelId(payload.file_id); + if (!data) throw new Error('Invalid file_id'); + return await this.core.apis.GroupApi.DelGroupFile(payload.group_id.toString(), [data.fileId]); } } diff --git a/src/onebot/action/go-cqhttp/DeleteGroupFileFolder.ts b/src/onebot/action/go-cqhttp/DeleteGroupFileFolder.ts index e9c13d64..acefd21f 100644 --- a/src/onebot/action/go-cqhttp/DeleteGroupFileFolder.ts +++ b/src/onebot/action/go-cqhttp/DeleteGroupFileFolder.ts @@ -1,9 +1,6 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NapCatCore } from '@/core'; -import { NapCatOneBot11Adapter } from '@/onebot'; -import { DelGroupFileFolder } from '@/onebot/action/file/DelGroupFileFolder'; +import BaseAction from '../BaseAction'; const SchemaData = { type: 'object', @@ -16,17 +13,11 @@ const SchemaData = { type Payload = FromSchema; -export class DeleteGroupFileFolder extends BaseAction { +export class DeleteGroupFileFolder extends BaseAction { actionName = ActionName.GoCQHTTP_DeleteGroupFileFolder; payloadSchema = SchemaData; - - constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore, - private ncDelGroupFileFolderImpl: DelGroupFileFolder) { - super(obContext, core); - } - async _handle(payload: Payload) { - await this.ncDelGroupFileFolderImpl._handle(payload); - return null; + return (await this.core.apis.GroupApi.DelGroupFileFolder( + payload.group_id.toString(), payload.folder_id)).groupFileCommonResult; } } diff --git a/src/onebot/action/go-cqhttp/GetGroupFilesByFolder.ts b/src/onebot/action/go-cqhttp/GetGroupFilesByFolder.ts index 8a0fabe9..2100c679 100644 --- a/src/onebot/action/go-cqhttp/GetGroupFilesByFolder.ts +++ b/src/onebot/action/go-cqhttp/GetGroupFilesByFolder.ts @@ -1,43 +1,27 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NapCatOneBot11Adapter, OB11GroupFile } from '@/onebot'; -import { NapCatCore } from '@/core'; -import { GetGroupRootFiles } from '@/onebot/action/go-cqhttp/GetGroupRootFiles'; import { OB11Entities } from '@/onebot/entities'; - const SchemaData = { type: 'object', properties: { group_id: { type: ['string', 'number'] }, folder_id: { type: 'string' }, + file_count: { type: ['string', 'number'] }, }, required: ['group_id', 'folder_id'], } as const satisfies JSONSchema; type Payload = FromSchema; -export class GetGroupFilesByFolder extends BaseAction { +export class GetGroupFilesByFolder extends BaseAction { actionName = ActionName.GoCQHTTP_GetGroupFilesByFolder; payloadSchema = SchemaData; - - constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore, - private getGroupRootFilesImpl: GetGroupRootFiles) { - super(obContext, core); - } - async _handle(payload: Payload) { - const folder = (await this.getGroupRootFilesImpl._handle({ group_id: payload.group_id })) - .folders.find(folder => folder.folder_id === payload.folder_id); - if (!folder) { - throw new Error('Folder not found'); - } + const ret = await this.core.apis.MsgApi.getGroupFileList(payload.group_id.toString(), { sortType: 1, - fileCount: folder.total_file_count, + fileCount: +(payload.file_count ?? 50), startIndex: 0, sortOrder: 2, showOnlinedocFolder: 0, diff --git a/src/onebot/action/go-cqhttp/GetGroupRootFiles.ts b/src/onebot/action/go-cqhttp/GetGroupRootFiles.ts index 499a9abd..dc713687 100644 --- a/src/onebot/action/go-cqhttp/GetGroupRootFiles.ts +++ b/src/onebot/action/go-cqhttp/GetGroupRootFiles.ts @@ -1,15 +1,14 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NapCatOneBot11Adapter, OB11GroupFile, OB11GroupFileFolder } from '@/onebot'; -import { NapCatCore } from '@/core'; -import { GetGroupFileCount } from '@/onebot/action/file/GetGroupFileCount'; +import { OB11GroupFile, OB11GroupFileFolder } from '@/onebot'; import { OB11Entities } from '@/onebot/entities'; const SchemaData = { type: 'object', properties: { group_id: { type: ['string', 'number'] }, + file_count: { type: ['string', 'number'] }, }, required: ['group_id'], } as const satisfies JSONSchema; @@ -22,16 +21,10 @@ export class GetGroupRootFiles extends BaseAction { actionName = ActionName.GoCQHTTP_GetGroupRootFiles; payloadSchema = SchemaData; - - constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore, - private ncGetGroupFileCountImpl: GetGroupFileCount) { - super(obContext, core); - } - async _handle(payload: Payload) { const ret = await this.core.apis.MsgApi.getGroupFileList(payload.group_id.toString(), { sortType: 1, - fileCount: (await this.ncGetGroupFileCountImpl._handle({ group_id: payload.group_id.toString() })).count, + fileCount: +(payload.file_count ?? 50), startIndex: 0, sortOrder: 2, showOnlinedocFolder: 0, diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index daab7552..b9ed7a8b 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -55,12 +55,7 @@ import { GoCQHTTPHandleQuickAction } from './go-cqhttp/QuickAction'; import { GetGroupIgnoredNotifies } from './group/GetGroupIgnoredNotifies'; import { GetOnlineClient } from './go-cqhttp/GetOnlineClient'; import { IOCRImage, OCRImage } from './extends/OCRImage'; -import { GetGroupFileCount } from './file/GetGroupFileCount'; -import { GetGroupFileList } from './file/GetGroupFileList'; import { TranslateEnWordToZn } from './extends/TranslateEnWordToZn'; -import { SetGroupFileFolder } from './file/SetGroupFileFolder'; -import { DelGroupFile } from './file/DelGroupFile'; -import { DelGroupFileFolder } from './file/DelGroupFileFolder'; import { SetQQProfile } from './go-cqhttp/SetQQProfile'; import { ShareGroupEx, SharePeer } from './extends/ShareContact'; import { CreateCollection } from './extends/CreateCollection'; @@ -92,11 +87,6 @@ import { GetGroupFilesByFolder } from '@/onebot/action/go-cqhttp/GetGroupFilesBy export type ActionMap = Map>; export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore): ActionMap { - const ncDelGroupFile = new DelGroupFile(obContext, core); - const ncSetGroupFileFolder = new SetGroupFileFolder(obContext, core); - const ncDelGroupFileFolder = new DelGroupFileFolder(obContext, core); - const ncGetGroupFileCount = new GetGroupFileCount(obContext, core); - const goCqHttpGetGroupRootFiles = new GetGroupRootFiles(obContext, core, ncGetGroupFileCount); const actionHandlers = [ new GetGroupInfoEx(obContext, core), @@ -113,11 +103,7 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new MarkPrivateMsgAsRead(obContext, core), new SetQQAvatar(obContext, core), new TranslateEnWordToZn(obContext, core), - ncGetGroupFileCount, - new GetGroupFileList(obContext, core), - ncSetGroupFileFolder, - ncDelGroupFile, - ncDelGroupFileFolder, + new GetGroupRootFiles(obContext, core), // onebot11 new SendLike(obContext, core), new GetMsg(obContext, core), @@ -185,12 +171,11 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new SetInputStatus(obContext, core), new GetCSRF(obContext, core), new DelGroupNotice(obContext, core), - new DeleteGroupFile(obContext, core, ncDelGroupFile), - new CreateGroupFileFolder(obContext, core, ncSetGroupFileFolder), - new DeleteGroupFileFolder(obContext, core, ncDelGroupFileFolder), + new DeleteGroupFile(obContext, core), + new CreateGroupFileFolder(obContext, core), + new DeleteGroupFileFolder(obContext, core), new GetGroupFileSystemInfo(obContext, core), - goCqHttpGetGroupRootFiles, - new GetGroupFilesByFolder(obContext, core, goCqHttpGetGroupRootFiles), + new GetGroupFilesByFolder(obContext, core), ]; const actionMap = new Map(); for (const action of actionHandlers) { diff --git a/src/onebot/entities.ts b/src/onebot/entities.ts index a043dd0d..989236d8 100644 --- a/src/onebot/entities.ts +++ b/src/onebot/entities.ts @@ -109,7 +109,7 @@ export class OB11Entities { static file(peerId: string, file: Exclude): OB11GroupFile { return { group_id: parseInt(peerId), - file_id: FileNapCatOneBotUUID.encodeModelId({ chatType: 2, peerUid: peerId }, file.fileModelId), + file_id: FileNapCatOneBotUUID.encodeModelId({ chatType: 2, peerUid: peerId }, file.fileModelId, file.fileId), file_name: file.fileName, busid: file.busId, size: parseInt(file.fileSize),