2022-02-27 02:07:39 +00:00
|
|
|
import { publishDriveStream } from '@/services/stream.js';
|
|
|
|
import { DriveFolders } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
2022-08-03 11:18:33 +00:00
|
|
|
import define from '../../../define.js';
|
|
|
|
import { ApiError } from '../../../error.js';
|
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'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
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
|
|
|
|
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',
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
2022-08-02 21:25:36 +00:00
|
|
|
name: { type: 'string', default: 'Untitled', maxLength: 200 },
|
2022-02-19 05:05:32 +00:00
|
|
|
parentId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, 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
|
2022-03-26 06:34:00 +00:00
|
|
|
parent = await DriveFolders.findOneBy({
|
2019-04-07 12:50:36 +00:00
|
|
|
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,
|
2022-10-10 21:03:24 +00:00
|
|
|
parentId: parent?.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2022-03-26 06:34:00 +00:00
|
|
|
}).then(x => DriveFolders.findOneByOrFail(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;
|
|
|
|
});
|