This commit is contained in:
syuilo 2018-07-06 23:19:47 +09:00
parent 4179685bb6
commit 42d4e6610f
1 changed files with 27 additions and 9 deletions

View File

@ -3,22 +3,40 @@ import Note from '../../../../../models/note';
import create from '../../../../../services/note/reaction/create'; import create from '../../../../../services/note/reaction/create';
import { validateReaction } from '../../../../../models/note-reaction'; import { validateReaction } from '../../../../../models/note-reaction';
import { ILocalUser } from '../../../../../models/user'; 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 * React to a note
*/ */
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'noteId' parameter const [ps, psErr] = getParams(meta, params);
const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (psErr) return rej(psErr);
if (noteIdErr) return rej('invalid noteId param');
// Get 'reaction' parameter
const [reaction, reactionErr] = $.str.pipe(validateReaction.ok).get(params.reaction);
if (reactionErr) return rej('invalid reaction param');
// Fetch reactee // Fetch reactee
const note = await Note.findOne({ const note = await Note.findOne({
_id: noteId _id: ps.noteId
}); });
if (note === null) { if (note === null) {
@ -26,7 +44,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
} }
try { try {
await create(user, note, reaction); await create(user, note, ps.reaction);
} catch (e) { } catch (e) {
rej(e); rej(e);
} }