summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/posts/reactions/create.ts
blob: 71fa6a295587c1efa2c44574c0192030c1f692d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Module dependencies
 */
import $ from 'cafy';
import Reaction from '../../../../../models/post-reaction';
import Post from '../../../../../models/post';
import create from '../../../../../services/post/reaction/create';

/**
 * React to a post
 */
module.exports = (params, user) => new Promise(async (res, rej) => {
	// Get 'postId' parameter
	const [postId, postIdErr] = $(params.postId).id().$;
	if (postIdErr) return rej('invalid postId param');

	// Get 'reaction' parameter
	const [reaction, reactionErr] = $(params.reaction).string().or([
		'like',
		'love',
		'laugh',
		'hmm',
		'surprise',
		'congrats',
		'angry',
		'confused',
		'pudding'
	]).$;
	if (reactionErr) return rej('invalid reaction param');

	// Fetch reactee
	const post = await Post.findOne({
		_id: postId
	});

	if (post === null) {
		return rej('post not found');
	}

	try {
		await create(user, post, reaction);
	} catch (e) {
		rej(e);
	}

	res();
});