paricafe/src/server/api/endpoints/notes/reactions/delete.ts
Aya Morisawa 9b746f3eb5
Make reactions removable
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>

Resolve #367, resolve #2260, close #3503
2018-12-26 23:05:47 +09:00

76 lines
1.5 KiB
TypeScript

import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
import Reaction from '../../../../../models/note-reaction';
import Note from '../../../../../models/note';
import define from '../../../define';
import { publishNoteStream } from '../../../../../stream';
const ms = require('ms');
export const meta = {
desc: {
'ja-JP': '指定した投稿へのリアクションを取り消します。',
'en-US': 'Unreact to a note.'
},
requireCredential: true,
kind: 'reaction-write',
limit: {
duration: ms('1hour'),
max: 5,
minInterval: ms('3sec')
},
params: {
noteId: {
validator: $.type(ID),
transform: transform,
desc: {
'ja-JP': '対象の投稿のID',
'en-US': 'Target note ID'
}
},
}
};
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
// Fetch unreactee
const note = await Note.findOne({
_id: ps.noteId
});
if (note === null) {
return rej('note not found');
}
// if already unreacted
const exist = await Reaction.findOne({
noteId: note._id,
userId: user._id,
deletedAt: { $exists: false }
});
if (exist === null) {
return rej('never reacted');
}
// Delete reaction
await Reaction.remove({
_id: exist._id
});
res();
const dec: any = {};
dec[`reactionCounts.${exist.reaction}`] = -1;
// Decrement reactions count
Note.update({ _id: note._id }, {
$inc: dec
});
publishNoteStream(note._id, 'unreacted', {
reaction: exist.reaction,
userId: user._id
});
}));