summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes/reactions/create.ts
blob: c80c5416b1e3265c30118470cb8c09af37847559 (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
/**
 * Module dependencies
 */
import $ from 'cafy';
import Note from '../../../../../models/note';
import create from '../../../../../services/note/reaction/create';

/**
 * React to a note
 */
module.exports = (params, user) => new Promise(async (res, rej) => {
	// Get 'noteId' parameter
	const [noteId, noteIdErr] = $(params.noteId).id().$;
	if (noteIdErr) return rej('invalid noteId 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 note = await Note.findOne({
		_id: noteId
	});

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

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

	res();
});