2021-04-24 22:38:24 +09:00
|
|
|
import $ from 'cafy';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import define from '../../../define';
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { GalleryPosts, GalleryLikes } from '@/models/index';
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['gallery'],
|
|
|
|
|
|
|
|
requireCredential: true as const,
|
|
|
|
|
|
|
|
kind: 'write:gallery-likes',
|
|
|
|
|
|
|
|
params: {
|
|
|
|
postId: {
|
|
|
|
validator: $.type(ID),
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchPost: {
|
|
|
|
message: 'No such post.',
|
|
|
|
code: 'NO_SUCH_POST',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'c32e6dd0-b555-4413-925e-b3757d19ed84',
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
notLiked: {
|
|
|
|
message: 'You have not liked that post.',
|
|
|
|
code: 'NOT_LIKED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'e3e8e06e-be37-41f7-a5b4-87a8250288f0',
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2021-04-24 22:38:24 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
const post = await GalleryPosts.findOne(ps.postId);
|
|
|
|
if (post == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchPost);
|
|
|
|
}
|
|
|
|
|
|
|
|
const exist = await GalleryLikes.findOne({
|
|
|
|
postId: post.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: user.id,
|
2021-04-24 22:38:24 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist == null) {
|
|
|
|
throw new ApiError(meta.errors.notLiked);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete like
|
|
|
|
await GalleryLikes.delete(exist.id);
|
|
|
|
|
|
|
|
GalleryPosts.decrement({ id: post.id }, 'likedCount', 1);
|
|
|
|
});
|