summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes/reactions/delete.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-08 02:30:37 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-08 02:30:37 +0900
commita1b490afa756a71b9cef4afa424575bc223bc612 (patch)
tree06de4d839e17b1e08e0891542af7360c701a154a /src/server/api/endpoints/notes/reactions/delete.ts
parentMerge pull request #1392 from syuilo/greenkeeper/element-ui-2.3.3 (diff)
downloadmisskey-a1b490afa756a71b9cef4afa424575bc223bc612.tar.gz
misskey-a1b490afa756a71b9cef4afa424575bc223bc612.tar.bz2
misskey-a1b490afa756a71b9cef4afa424575bc223bc612.zip
Post --> Note
Closes #1411
Diffstat (limited to 'src/server/api/endpoints/notes/reactions/delete.ts')
-rw-r--r--src/server/api/endpoints/notes/reactions/delete.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/server/api/endpoints/notes/reactions/delete.ts b/src/server/api/endpoints/notes/reactions/delete.ts
new file mode 100644
index 0000000000..b5d738b8ff
--- /dev/null
+++ b/src/server/api/endpoints/notes/reactions/delete.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Reaction from '../../../../../models/note-reaction';
+import Note from '../../../../../models/note';
+// import event from '../../../publishers/stream';
+
+/**
+ * Unreact to a note
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+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');
+
+ // Fetch unreactee
+ const note = await Note.findOne({
+ _id: 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.update({
+ _id: exist._id
+ }, {
+ $set: {
+ deletedAt: new Date()
+ }
+ });
+
+ // Send response
+ res();
+
+ const dec = {};
+ dec[`reactionCounts.${exist.reaction}`] = -1;
+
+ // Decrement reactions count
+ Note.update({ _id: note._id }, {
+ $inc: dec
+ });
+});