2018-11-02 03:32:24 +09:00
|
|
|
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
|
2018-10-25 05:33:40 +09:00
|
|
|
import DriveFolder from '../../../../../models/drive-folder';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../../define';
|
2018-10-24 18:37:58 +09:00
|
|
|
import { publishDriveStream } from '../../../../../stream';
|
|
|
|
import DriveFile from '../../../../../models/drive-file';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
stability: 'stable',
|
|
|
|
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '指定したドライブのフォルダを削除します。',
|
|
|
|
'en-US': 'Delete specified folder of drive.'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
kind: 'drive-write',
|
|
|
|
|
|
|
|
params: {
|
2018-11-02 03:32:24 +09:00
|
|
|
folderId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
transform: transform,
|
2018-10-24 18:37:58 +09:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のフォルダID',
|
|
|
|
'en-US': 'Target folder ID'
|
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
}
|
2018-10-24 18:37:58 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-02 13:47:44 +09:00
|
|
|
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
2018-10-24 18:37:58 +09:00
|
|
|
// Get folder
|
|
|
|
const folder = await DriveFolder
|
|
|
|
.findOne({
|
|
|
|
_id: ps.folderId,
|
|
|
|
userId: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
|
|
|
return rej('folder-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const [childFoldersCount, childFilesCount] = await Promise.all([
|
|
|
|
DriveFolder.count({ parentId: folder._id }),
|
2018-10-26 15:06:27 +09:00
|
|
|
DriveFile.count({ 'metadata.folderId': folder._id })
|
2018-10-24 18:37:58 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (childFoldersCount !== 0 || childFilesCount !== 0) {
|
|
|
|
return rej('has-child-contents');
|
|
|
|
}
|
|
|
|
|
|
|
|
await DriveFolder.remove({ _id: folder._id });
|
|
|
|
|
|
|
|
// Publish folderCreated event
|
|
|
|
publishDriveStream(user._id, 'folderDeleted', folder._id);
|
|
|
|
|
|
|
|
res();
|
2018-11-02 13:47:44 +09:00
|
|
|
}));
|