2019-02-05 03:48:08 +01:00
|
|
|
import $ from 'cafy';
|
2021-03-23 09:43:07 +01:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../../define';
|
2019-02-05 06:14:23 +01:00
|
|
|
import { publishDriveStream } from '../../../../../services/stream';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../../error';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { DriveFolders, DriveFiles } from '../../../../../models';
|
2018-10-24 11:37:58 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: true as const,
|
2018-10-24 11:37:58 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'write:drive',
|
2018-10-24 11:37:58 +02:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 19:32:24 +01:00
|
|
|
folderId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
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'
|
|
|
|
},
|
2018-10-24 11:37:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, user) => {
|
2018-10-24 11:37:58 +02:00
|
|
|
// Get folder
|
2019-04-07 14:50:36 +02:00
|
|
|
const folder = await DriveFolders.findOne({
|
|
|
|
id: ps.folderId,
|
|
|
|
userId: user.id
|
|
|
|
});
|
2018-10-24 11:37:58 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (folder == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
2018-10-24 11:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const [childFoldersCount, childFilesCount] = await Promise.all([
|
2019-04-07 14:50:36 +02:00
|
|
|
DriveFolders.count({ parentId: folder.id }),
|
|
|
|
DriveFiles.count({ folderId: folder.id })
|
2018-10-24 11:37:58 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (childFoldersCount !== 0 || childFilesCount !== 0) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.hasChildFilesOrFolders);
|
2018-10-24 11:37:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await DriveFolders.delete(folder.id);
|
2018-10-24 11:37:58 +02:00
|
|
|
|
|
|
|
// Publish folderCreated event
|
2019-04-07 14:50:36 +02:00
|
|
|
publishDriveStream(user.id, 'folderDeleted', folder.id);
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|