2021-08-19 21:55:45 +09:00
|
|
|
import define from '../../../define';
|
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
|
import { GalleryPosts, GalleryLikes } from '@/models/index';
|
|
|
|
|
import { genId } from '@/misc/gen-id';
|
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-likes',
|
|
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
noSuchPost: {
|
|
|
|
|
message: 'No such post.',
|
|
|
|
|
code: 'NO_SUCH_POST',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '56c06af3-1287-442f-9701-c93f7c4a62ff',
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
yourPost: {
|
|
|
|
|
message: 'You cannot like your post.',
|
|
|
|
|
code: 'YOUR_POST',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'f78f1511-5ebc-4478-a888-1198d752da68',
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
alreadyLiked: {
|
|
|
|
|
message: 'The post has already been liked.',
|
|
|
|
|
code: 'ALREADY_LIKED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '40e9ed56-a59c-473a-bf3f-f289c54fb5a7',
|
2021-04-24 22:38:24 +09:00
|
|
|
},
|
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: {
|
|
|
|
|
postId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
},
|
|
|
|
|
required: ['postId'],
|
|
|
|
|
} 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 post = await GalleryPosts.findOne(ps.postId);
|
|
|
|
|
if (post == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchPost);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (post.userId === user.id) {
|
|
|
|
|
throw new ApiError(meta.errors.yourPost);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if already liked
|
|
|
|
|
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.alreadyLiked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create like
|
|
|
|
|
await GalleryLikes.insert({
|
|
|
|
|
id: genId(),
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
postId: post.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: user.id,
|
2021-04-24 22:38:24 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
GalleryPosts.increment({ id: post.id }, 'likedCount', 1);
|
|
|
|
|
});
|