ギャラリー投稿の編集と削除

This commit is contained in:
syuilo
2021-04-26 11:10:45 +09:00
parent 8bce241170
commit 42539575a6
6 changed files with 301 additions and 111 deletions

View File

@ -0,0 +1,40 @@
import $ from 'cafy';
import define from '../../../define';
import { ApiError } from '../../../error';
import { GalleryPosts } from '../../../../../models';
import { ID } from '@/misc/cafy-id';
export const meta = {
tags: ['gallery'],
requireCredential: true as const,
kind: 'write:gallery',
params: {
postId: {
validator: $.type(ID),
},
},
errors: {
noSuchPost: {
message: 'No such post.',
code: 'NO_SUCH_POST',
id: 'ae52f367-4bd7-4ecd-afc6-5672fff427f5'
},
}
};
export default define(meta, async (ps, user) => {
const post = await GalleryPosts.findOne({
id: ps.postId,
userId: user.id,
});
if (post == null) {
throw new ApiError(meta.errors.noSuchPost);
}
await GalleryPosts.delete(post.id);
});

View File

@ -0,0 +1,81 @@
import $ from 'cafy';
import * as ms from 'ms';
import define from '../../../define';
import { ID } from '../../../../../misc/cafy-id';
import { DriveFiles, GalleryPosts } from '../../../../../models';
import { GalleryPost } from '../../../../../models/entities/gallery-post';
import { ApiError } from '../../../error';
export const meta = {
tags: ['gallery'],
requireCredential: true as const,
kind: 'write:gallery',
limit: {
duration: ms('1hour'),
max: 300
},
params: {
postId: {
validator: $.type(ID),
},
title: {
validator: $.str.min(1),
},
description: {
validator: $.optional.nullable.str,
},
fileIds: {
validator: $.arr($.type(ID)).unique().range(1, 32),
},
isSensitive: {
validator: $.optional.bool,
default: false,
},
},
res: {
type: 'object' as const,
optional: false as const, nullable: false as const,
ref: 'GalleryPost',
},
errors: {
}
};
export default define(meta, async (ps, user) => {
const files = (await Promise.all(ps.fileIds.map(fileId =>
DriveFiles.findOne({
id: fileId,
userId: user.id
})
))).filter(file => file != null);
if (files.length === 0) {
throw new Error();
}
await GalleryPosts.update({
id: ps.postId,
userId: user.id,
}, {
updatedAt: new Date(),
title: ps.title,
description: ps.description,
isSensitive: ps.isSensitive,
fileIds: files.map(file => file.id)
});
const post = await GalleryPosts.findOneOrFail(ps.postId);
return await GalleryPosts.pack(post, user);
});