From 48aa7c2a7b51dc09219820965d50096962941c98 Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 26 Dec 2018 23:05:47 +0900 Subject: [PATCH] Make reactions removable Co-authored-by: syuilo Resolve #367, resolve #2260, close #3503 --- .../app/common/scripts/note-subscriber.ts | 21 +++++++++ .../views/components/reactions-viewer.vue | 44 ++++++++++++------- .../api/endpoints/notes/reactions/delete.ts | 13 ++++++ 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/client/app/common/scripts/note-subscriber.ts b/src/client/app/common/scripts/note-subscriber.ts index bc434c436..9545b5406 100644 --- a/src/client/app/common/scripts/note-subscriber.ts +++ b/src/client/app/common/scripts/note-subscriber.ts @@ -95,6 +95,7 @@ export default prop => ({ Vue.set(this.$_ns_target.reactionCounts, reaction, 0); } + // Increment the count this.$_ns_target.reactionCounts[reaction]++; if (body.userId == this.$store.state.i.id) { @@ -103,6 +104,26 @@ export default prop => ({ break; } + case 'unreacted': { + const reaction = body.reaction; + + if (this.$_ns_target.reactionCounts == null) { + return; + } + + if (this.$_ns_target.reactionCounts[reaction] == null) { + return; + } + + // Decrement the count + if (this.$_ns_target.reactionCounts[reaction] > 0) this.$_ns_target.reactionCounts[reaction]--; + + if (body.userId == this.$store.state.i.id) { + Vue.set(this.$_ns_target, 'myReaction', null); + } + break; + } + case 'pollVoted': { if (body.userId == this.$store.state.i.id) return; const choice = body.choice; diff --git a/src/client/app/common/views/components/reactions-viewer.vue b/src/client/app/common/views/components/reactions-viewer.vue index 73267347b..a3c421bd3 100644 --- a/src/client/app/common/views/components/reactions-viewer.vue +++ b/src/client/app/common/views/components/reactions-viewer.vue @@ -1,16 +1,16 @@ @@ -60,11 +60,25 @@ export default Vue.extend({ } }, methods: { - react(reaction: string) { - this.$root.api('notes/reactions/create', { - noteId: this.note.id, - reaction: reaction - }); + toggleReaction(reaction: string) { + const oldReaction = this.note.myReaction; + if (oldReaction) { + this.$root.api('notes/reactions/delete', { + noteId: this.note.id + }).then(() => { + if (oldReaction !== reaction) { + this.$root.api('notes/reactions/create', { + noteId: this.note.id, + reaction: reaction + }); + } + }); + } else { + this.$root.api('notes/reactions/create', { + noteId: this.note.id, + reaction: reaction + }); + } }, anime(reaction: string) { if (this.$store.state.device.reduceMotion) return; diff --git a/src/server/api/endpoints/notes/reactions/delete.ts b/src/server/api/endpoints/notes/reactions/delete.ts index 367538bed..40139afee 100644 --- a/src/server/api/endpoints/notes/reactions/delete.ts +++ b/src/server/api/endpoints/notes/reactions/delete.ts @@ -2,6 +2,8 @@ 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: { @@ -13,6 +15,12 @@ export const meta = { kind: 'reaction-write', + limit: { + duration: ms('1hour'), + max: 5, + minInterval: ms('3sec') + }, + params: { noteId: { validator: $.type(ID), @@ -60,4 +68,9 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => { Note.update({ _id: note._id }, { $inc: dec }); + + publishNoteStream(note._id, 'unreacted', { + reaction: exist.reaction, + userId: user._id + }); }));