diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2019-01-20 03:07:12 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2019-01-20 03:07:12 +0900 |
| commit | 22830965e341c9c961e072b2e92cb7c404f8624d (patch) | |
| tree | 761859da2df4d3893ae47f5957281a6076724818 /src/services/note | |
| parent | Refactor (diff) | |
| download | sharkey-22830965e341c9c961e072b2e92cb7c404f8624d.tar.gz sharkey-22830965e341c9c961e072b2e92cb7c404f8624d.tar.bz2 sharkey-22830965e341c9c961e072b2e92cb7c404f8624d.zip | |
AP Undo Like (#3933)
* AP Undo Like
* rename
Diffstat (limited to 'src/services/note')
| -rw-r--r-- | src/services/note/reaction/delete.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/services/note/reaction/delete.ts b/src/services/note/reaction/delete.ts new file mode 100644 index 0000000000..b108f0ba75 --- /dev/null +++ b/src/services/note/reaction/delete.ts @@ -0,0 +1,49 @@ +import { IUser, isLocalUser, isRemoteUser } from '../../../models/user'; +import Note, { INote } from '../../../models/note'; +import Reaction from '../../../models/note-reaction'; +import { publishNoteStream } from '../../../stream'; +import renderLike from '../../../remote/activitypub/renderer/like'; +import renderUndo from '../../../remote/activitypub/renderer/undo'; +import pack from '../../../remote/activitypub/renderer'; +import { deliver } from '../../../queue'; + +export default async (user: IUser, note: INote) => new Promise(async (res, rej) => { + // 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 + }); + + //#region 配信 + // リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送 + if (isLocalUser(user) && isRemoteUser(note._user)) { + const content = pack(renderUndo(renderLike(user, note, exist.reaction), user)); + deliver(user, content, note._user.inbox); + } + //#endregion +}); |