2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { DriveFilesRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-12 21:02:26 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../../error.js';
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['drive'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:drive',
|
2018-10-23 06:59:52 +09:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Delete an existing drive file.',
|
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
|
noSuchFile: {
|
|
|
|
|
message: 'No such file.',
|
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '908939ec-e52b-4458-b395-1025195cea58',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
|
message: 'Access denied.',
|
|
|
|
|
code: 'ACCESS_DENIED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '5eb8d909-2540-4970-90b8-dd6f86088121',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
},
|
|
|
|
|
required: ['fileId'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private driveService: DriveService,
|
2023-01-12 21:02:26 +09:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (file == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
|
}
|
2018-12-15 00:09:04 +09:00
|
|
|
|
2023-01-12 21:02:26 +09:00
|
|
|
if (!await this.roleService.isModerator(me) && (file.userId !== me.id)) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
|
}
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Delete
|
|
|
|
|
await this.driveService.deleteFile(file);
|
|
|
|
|
|
|
|
|
|
// Publish fileDeleted event
|
|
|
|
|
this.globalEventService.publishDriveStream(me.id, 'fileDeleted', file.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|