summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes/reactions/create.ts
blob: 8b22e532250e467f26928f438433727dfd10b072 (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
48
49
50
51
52
53
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import Note from '../../../../../models/note';
import create from '../../../../../services/note/reaction/create';
import { validateReaction } from '../../../../../models/note-reaction';
import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';

export const meta = {
	name: 'notes/reactions/create',

	desc: {
		ja: '投稿にリアクションします。'
	},

	params: {
		noteId: $.type(ID).note({
			desc: {
				ja: '対象の投稿'
			}
		}),

		reaction: $.str.pipe(validateReaction.ok).note({
			desc: {
				ja: 'リアクションの種類'
			}
		})
	}
};

/**
 * React to a note
 */
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
	const [ps, psErr] = getParams(meta, params);
	if (psErr) return rej(psErr);

	// Fetch reactee
	const note = await Note.findOne({
		_id: ps.noteId
	});

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

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

	res();
});