2021-11-12 19:47:04 +09:00
|
|
|
import ms from 'ms';
|
2022-02-27 11:07:39 +09:00
|
|
|
import define from '../../../define.js';
|
|
|
|
|
import { DriveFiles, GalleryPosts } from '@/models/index.js';
|
|
|
|
|
import { genId } from '../../../../../misc/gen-id.js';
|
|
|
|
|
import { GalleryPost } from '@/models/entities/gallery-post.js';
|
|
|
|
|
import { ApiError } from '../../../error.js';
|
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
|
tags: ['gallery'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
|
kind: 'write:gallery',
|
|
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 23:58:30 +09:00
|
|
|
max: 300,
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-04-24 22:38:24 +09:00
|
|
|
ref: 'GalleryPost',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2021-04-24 22:38:24 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
title: { type: 'string', minLength: 1 },
|
|
|
|
|
description: { type: 'string', nullable: true },
|
|
|
|
|
fileIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 32, items: {
|
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
|
} },
|
|
|
|
|
isSensitive: { type: 'boolean', default: false },
|
|
|
|
|
},
|
|
|
|
|
required: ['title', 'fileIds'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2021-04-24 22:38:24 +09:00
|
|
|
const files = (await Promise.all(ps.fileIds.map(fileId =>
|
2022-03-26 15:34:00 +09:00
|
|
|
DriveFiles.findOneBy({
|
2021-04-24 22:38:24 +09:00
|
|
|
id: fileId,
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: user.id,
|
2021-04-24 22:38:24 +09:00
|
|
|
})
|
2021-05-28 09:34:42 +09:00
|
|
|
))).filter((file): file is DriveFile => file != null);
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const post = await GalleryPosts.insert(new GalleryPost({
|
|
|
|
|
id: genId(),
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
title: ps.title,
|
|
|
|
|
description: ps.description,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
isSensitive: ps.isSensitive,
|
2021-12-09 23:58:30 +09:00
|
|
|
fileIds: files.map(file => file.id),
|
2022-03-26 15:34:00 +09:00
|
|
|
})).then(x => GalleryPosts.findOneByOrFail(x.identifiers[0]));
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
|
return await GalleryPosts.pack(post, user);
|
|
|
|
|
});
|