From ab02fd3ec4abf59051edff4769b61935e3d394bf Mon Sep 17 00:00:00 2001 From: tamaina Date: Fri, 16 Jun 2023 06:49:29 +0000 Subject: [PATCH] drive/folders --- .../api/endpoints/drive/folders/create.ts | 43 +---- .../api/endpoints/drive/folders/delete.ts | 39 +--- .../api/endpoints/drive/folders/find.ts | 32 +--- .../api/endpoints/drive/folders/show.ts | 37 +--- .../api/endpoints/drive/folders/update.ts | 57 +----- packages/misskey-js/src/endpoints.ts | 176 ++++++++++++++++++ 6 files changed, 199 insertions(+), 185 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/drive/folders/create.ts b/packages/backend/src/server/api/endpoints/drive/folders/create.ts index 39c9c6bc58..268e5cd0ab 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/create.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/create.ts @@ -8,45 +8,10 @@ import { GlobalEventService } from '@/core/GlobalEventService.js'; import { DI } from '@/di-symbols.js'; import { ApiError } from '../../../error.js'; -export const meta = { - tags: ['drive'], - - requireCredential: true, - - kind: 'write:drive', - - limit: { - duration: ms('1hour'), - max: 10, - }, - - errors: { - noSuchFolder: { - message: 'No such folder.', - code: 'NO_SUCH_FOLDER', - id: '53326628-a00d-40a6-a3cd-8975105c0f95', - }, - }, - - res: { - type: 'object' as const, - optional: false as const, nullable: false as const, - ref: 'DriveFolder', - }, -} as const; - -export const paramDef = { - type: 'object', - properties: { - name: { type: 'string', default: 'Untitled', maxLength: 200 }, - parentId: { type: 'string', format: 'misskey:id', nullable: true }, - }, - required: [], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'drive/folders/create'> { + name = 'drive/folders/create' as const; constructor( @Inject(DI.driveFoldersRepository) private driveFoldersRepository: DriveFoldersRepository, @@ -55,7 +20,7 @@ export default class extends Endpoint { private idService: IdService, private globalEventService: GlobalEventService, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { // If the parent folder is specified let parent = null; if (ps.parentId) { @@ -66,7 +31,7 @@ export default class extends Endpoint { }); if (parent == null) { - throw new ApiError(meta.errors.noSuchFolder); + throw new ApiError(this.meta.errors.noSuchFolder); } } diff --git a/packages/backend/src/server/api/endpoints/drive/folders/delete.ts b/packages/backend/src/server/api/endpoints/drive/folders/delete.ts index d921bc1b17..0e77524fc2 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/delete.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/delete.ts @@ -5,39 +5,10 @@ import { GlobalEventService } from '@/core/GlobalEventService.js'; import { DI } from '@/di-symbols.js'; import { ApiError } from '../../../error.js'; -export const meta = { - tags: ['drive'], - - requireCredential: true, - - kind: 'write:drive', - - errors: { - noSuchFolder: { - message: 'No such folder.', - code: 'NO_SUCH_FOLDER', - id: '1069098f-c281-440f-b085-f9932edbe091', - }, - - hasChildFilesOrFolders: { - message: 'This folder has child files or folders.', - code: 'HAS_CHILD_FILES_OR_FOLDERS', - id: 'b0fc8a17-963c-405d-bfbc-859a487295e1', - }, - }, -} as const; - -export const paramDef = { - type: 'object', - properties: { - folderId: { type: 'string', format: 'misskey:id' }, - }, - required: ['folderId'], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'drive/folders/delete'> { + name = 'drive/folders/delete' as const; constructor( @Inject(DI.driveFilesRepository) private driveFilesRepository: DriveFilesRepository, @@ -47,7 +18,7 @@ export default class extends Endpoint { private globalEventService: GlobalEventService, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { // Get folder const folder = await this.driveFoldersRepository.findOneBy({ id: ps.folderId, @@ -55,7 +26,7 @@ export default class extends Endpoint { }); if (folder == null) { - throw new ApiError(meta.errors.noSuchFolder); + throw new ApiError(this.meta.errors.noSuchFolder); } const [childFoldersCount, childFilesCount] = await Promise.all([ @@ -64,7 +35,7 @@ export default class extends Endpoint { ]); if (childFoldersCount !== 0 || childFilesCount !== 0) { - throw new ApiError(meta.errors.hasChildFilesOrFolders); + throw new ApiError(this.meta.errors.hasChildFilesOrFolders); } await this.driveFoldersRepository.delete(folder.id); diff --git a/packages/backend/src/server/api/endpoints/drive/folders/find.ts b/packages/backend/src/server/api/endpoints/drive/folders/find.ts index ee24db11f2..410764093a 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/find.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/find.ts @@ -5,43 +5,17 @@ import type { DriveFoldersRepository } from '@/models/index.js'; import { DriveFolderEntityService } from '@/core/entities/DriveFolderEntityService.js'; import { DI } from '@/di-symbols.js'; -export const meta = { - tags: ['drive'], - - requireCredential: true, - - kind: 'read:drive', - - res: { - type: 'array', - optional: false, nullable: false, - items: { - type: 'object', - optional: false, nullable: false, - ref: 'DriveFolder', - }, - }, -} as const; - -export const paramDef = { - type: 'object', - properties: { - name: { type: 'string' }, - parentId: { type: 'string', format: 'misskey:id', nullable: true, default: null }, - }, - required: ['name'], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'drive/folders/find'> { + name = 'drive/folders/find' as const; constructor( @Inject(DI.driveFoldersRepository) private driveFoldersRepository: DriveFoldersRepository, private driveFolderEntityService: DriveFolderEntityService, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { const folders = await this.driveFoldersRepository.findBy({ name: ps.name, userId: me.id, diff --git a/packages/backend/src/server/api/endpoints/drive/folders/show.ts b/packages/backend/src/server/api/endpoints/drive/folders/show.ts index c06263b902..b197d1c026 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/show.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/show.ts @@ -5,46 +5,17 @@ import { DriveFolderEntityService } from '@/core/entities/DriveFolderEntityServi import { DI } from '@/di-symbols.js'; import { ApiError } from '../../../error.js'; -export const meta = { - tags: ['drive'], - - requireCredential: true, - - kind: 'read:drive', - - res: { - type: 'object', - optional: false, nullable: false, - ref: 'DriveFolder', - }, - - errors: { - noSuchFolder: { - message: 'No such folder.', - code: 'NO_SUCH_FOLDER', - id: 'd74ab9eb-bb09-4bba-bf24-fb58f761e1e9', - }, - }, -} as const; - -export const paramDef = { - type: 'object', - properties: { - folderId: { type: 'string', format: 'misskey:id' }, - }, - required: ['folderId'], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'drive/folders/show'> { + name = 'drive/folders/show' as const; constructor( @Inject(DI.driveFoldersRepository) private driveFoldersRepository: DriveFoldersRepository, private driveFolderEntityService: DriveFolderEntityService, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { // Get folder const folder = await this.driveFoldersRepository.findOneBy({ id: ps.folderId, @@ -52,7 +23,7 @@ export default class extends Endpoint { }); if (folder == null) { - throw new ApiError(meta.errors.noSuchFolder); + throw new ApiError(this.meta.errors.noSuchFolder); } return await this.driveFolderEntityService.pack(folder, { diff --git a/packages/backend/src/server/api/endpoints/drive/folders/update.ts b/packages/backend/src/server/api/endpoints/drive/folders/update.ts index ff0a78b929..1a7e99a6f6 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/update.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/update.ts @@ -6,53 +6,10 @@ import { GlobalEventService } from '@/core/GlobalEventService.js'; import { DI } from '@/di-symbols.js'; import { ApiError } from '../../../error.js'; -export const meta = { - tags: ['drive'], - - requireCredential: true, - - kind: 'write:drive', - - errors: { - noSuchFolder: { - message: 'No such folder.', - code: 'NO_SUCH_FOLDER', - id: 'f7974dac-2c0d-4a27-926e-23583b28e98e', - }, - - noSuchParentFolder: { - message: 'No such parent folder.', - code: 'NO_SUCH_PARENT_FOLDER', - id: 'ce104e3a-faaf-49d5-b459-10ff0cbbcaa1', - }, - - recursiveNesting: { - message: 'It can not be structured like nesting folders recursively.', - code: 'RECURSIVE_NESTING', - id: 'dbeb024837894013aed44279f9199740', - }, - }, - - res: { - type: 'object', - optional: false, nullable: false, - ref: 'DriveFolder', - }, -} as const; - -export const paramDef = { - type: 'object', - properties: { - folderId: { type: 'string', format: 'misskey:id' }, - name: { type: 'string', maxLength: 200 }, - parentId: { type: 'string', format: 'misskey:id', nullable: true }, - }, - required: ['folderId'], -} as const; - // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'drive/folders/update'> { + name = 'drive/folders/update' as const; constructor( @Inject(DI.driveFoldersRepository) private driveFoldersRepository: DriveFoldersRepository, @@ -60,7 +17,7 @@ export default class extends Endpoint { private driveFolderEntityService: DriveFolderEntityService, private globalEventService: GlobalEventService, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { // Fetch folder const folder = await this.driveFoldersRepository.findOneBy({ id: ps.folderId, @@ -68,14 +25,14 @@ export default class extends Endpoint { }); if (folder == null) { - throw new ApiError(meta.errors.noSuchFolder); + throw new ApiError(this.meta.errors.noSuchFolder); } if (ps.name) folder.name = ps.name; if (ps.parentId !== undefined) { if (ps.parentId === folder.id) { - throw new ApiError(meta.errors.recursiveNesting); + throw new ApiError(this.meta.errors.recursiveNesting); } else if (ps.parentId === null) { folder.parentId = null; } else { @@ -86,7 +43,7 @@ export default class extends Endpoint { }); if (parent == null) { - throw new ApiError(meta.errors.noSuchParentFolder); + throw new ApiError(this.meta.errors.noSuchParentFolder); } // Check if the circular reference will occur @@ -107,7 +64,7 @@ export default class extends Endpoint { if (parent.parentId !== null) { if (await checkCircle(parent.parentId)) { - throw new ApiError(meta.errors.recursiveNesting); + throw new ApiError(this.meta.errors.recursiveNesting); } } diff --git a/packages/misskey-js/src/endpoints.ts b/packages/misskey-js/src/endpoints.ts index b694941459..11da1aeaa1 100644 --- a/packages/misskey-js/src/endpoints.ts +++ b/packages/misskey-js/src/endpoints.ts @@ -3914,6 +3914,182 @@ export const endpoints = { res: undefined, }], }, + 'drive/folders/create': { + tags: ['drive'], + + requireCredential: true, + + kind: 'write:drive', + + limit: { + duration: ms('1hour'), + max: 10, + }, + + errors: { + noSuchFolder: { + message: 'No such folder.', + code: 'NO_SUCH_FOLDER', + id: '53326628-a00d-40a6-a3cd-8975105c0f95', + }, + }, + + defines: [{ + req: { + type: 'object', + properties: { + name: { type: 'string', default: 'Untitled', maxLength: 200 }, + parentId: { + oneOf: [ + { type: 'string', format: 'misskey:id' }, + { type: 'null' }, + ], + }, + }, + required: [], + }, + res: { + $ref: 'https://misskey-hub.net/api/schemas/DriveFolder', + }, + }], + }, + 'drive/folders/delete': { + tags: ['drive'], + + requireCredential: true, + + kind: 'write:drive', + + errors: { + noSuchFolder: { + message: 'No such folder.', + code: 'NO_SUCH_FOLDER', + id: '1069098f-c281-440f-b085-f9932edbe091', + }, + + hasChildFilesOrFolders: { + message: 'This folder has child files or folders.', + code: 'HAS_CHILD_FILES_OR_FOLDERS', + id: 'b0fc8a17-963c-405d-bfbc-859a487295e1', + }, + }, + + defines: [{ + req: { + type: 'object', + properties: { + folderId: { type: 'string', format: 'misskey:id' }, + }, + required: ['folderId'], + }, + res: undefined, + }], + }, + 'drive/folders/find': { + tags: ['drive'], + + requireCredential: true, + + kind: 'read:drive', + + defines: [{ + req: { + type: 'object', + properties: { + name: { type: 'string' }, + parentId: { + oneOf: [ + { type: 'string', format: 'misskey:id' }, + { type: 'null' }, + ], + default: null, + }, + }, + required: ['name'], + }, + res: { + type: 'array', + items: { + $ref: 'https://misskey-hub.net/api/schemas/DriveFolder', + }, + } + }] + }, + 'drive/folders/show': { + tags: ['drive'], + + requireCredential: true, + + kind: 'read:drive', + + errors: { + noSuchFolder: { + message: 'No such folder.', + code: 'NO_SUCH_FOLDER', + id: 'd74ab9eb-bb09-4bba-bf24-fb58f761e1e9', + }, + }, + + defines: [{ + req: { + type: 'object', + properties: { + folderId: { type: 'string', format: 'misskey:id' }, + }, + required: ['folderId'], + }, + res: { + $ref: 'https://misskey-hub.net/api/schemas/DriveFolder', + } + }], + }, + 'drive/folders/update': { + tags: ['drive'], + + requireCredential: true, + + kind: 'write:drive', + + errors: { + noSuchFolder: { + message: 'No such folder.', + code: 'NO_SUCH_FOLDER', + id: 'f7974dac-2c0d-4a27-926e-23583b28e98e', + }, + + noSuchParentFolder: { + message: 'No such parent folder.', + code: 'NO_SUCH_PARENT_FOLDER', + id: 'ce104e3a-faaf-49d5-b459-10ff0cbbcaa1', + }, + + recursiveNesting: { + message: 'It can not be structured like nesting folders recursively.', + code: 'RECURSIVE_NESTING', + id: 'dbeb024837894013aed44279f9199740', + }, + }, + + defines: [{ + req: { + type: 'object', + properties: { + folderId: { type: 'string', format: 'misskey:id' }, + name: { type: 'string', maxLength: 200 }, + parentId: { + oneOf: [ + { type: 'string', format: 'misskey:id' }, + { type: 'null' }, + ], + }, + }, + required: ['folderId'], + }, + res: { + $ref: 'https://misskey-hub.net/api/schemas/DriveFolder', + } + }], + }, //#endregion } as const satisfies { [x: string]: IEndpointMeta; };