2019-02-05 02:48:08 +00:00
|
|
|
import $ from 'cafy';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { publishDriveStream } from '@/services/stream';
|
|
|
|
import define from '../../../define';
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { DriveFolders } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-07-16 19:36:44 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: true as const,
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:drive',
|
2018-10-24 09:32:19 +00:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 18:32:24 +00:00
|
|
|
name: {
|
2019-04-07 12:50:36 +00:00
|
|
|
validator: $.optional.str.pipe(DriveFolders.validateFolderName),
|
2018-10-24 09:32:19 +00:00
|
|
|
default: 'Untitled',
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-10-24 09:32:19 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
parentId: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchFolder: {
|
|
|
|
message: 'No such folder.',
|
|
|
|
code: 'NO_SUCH_FOLDER',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '53326628-a00d-40a6-a3cd-8975105c0f95',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2021-12-09 14:58:30 +00:00
|
|
|
ref: 'DriveFolder',
|
|
|
|
},
|
2018-07-16 19:36:44 +00:00
|
|
|
};
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, user) => {
|
2016-12-28 22:49:51 +00:00
|
|
|
// If the parent folder is specified
|
|
|
|
let parent = null;
|
2018-10-24 09:32:19 +00:00
|
|
|
if (ps.parentId) {
|
2017-03-03 10:33:14 +00:00
|
|
|
// Fetch parent folder
|
2019-04-07 12:50:36 +00:00
|
|
|
parent = await DriveFolders.findOne({
|
|
|
|
id: ps.parentId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (parent == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create folder
|
2021-03-24 02:05:37 +00:00
|
|
|
const folder = await DriveFolders.insert({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: genId(),
|
2018-03-29 05:48:47 +00:00
|
|
|
createdAt: new Date(),
|
2018-10-24 09:32:19 +00:00
|
|
|
name: ps.name,
|
2019-04-07 12:50:36 +00:00
|
|
|
parentId: parent !== null ? parent.id : null,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2021-03-24 02:05:37 +00:00
|
|
|
}).then(x => DriveFolders.findOneOrFail(x.identifiers[0]));
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const folderObj = await DriveFolders.pack(folder);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-10-22 22:01:43 +00:00
|
|
|
// Publish folderCreated event
|
2019-04-07 12:50:36 +00:00
|
|
|
publishDriveStream(user.id, 'folderCreated', folderObj);
|
2019-02-22 02:46:58 +00:00
|
|
|
|
|
|
|
return folderObj;
|
|
|
|
});
|