hippofish/src/services/note/reaction/delete.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

import { IUser, isLocalUser, isRemoteUser } from '../../../models/user';
import Note, { INote } from '../../../models/note';
2019-03-20 13:39:02 +01:00
import NoteReaction from '../../../models/note-reaction';
2019-02-05 06:14:23 +01:00
import { publishNoteStream } from '../../stream';
import renderLike from '../../../remote/activitypub/renderer/like';
import renderUndo from '../../../remote/activitypub/renderer/undo';
import { renderActivity } from '../../../remote/activitypub/renderer';
import { deliver } from '../../../queue';
import { IdentifiableError } from '../../../misc/identifiable-error';
export default async (user: IUser, note: INote) => {
// if already unreacted
2019-03-20 13:39:02 +01:00
const exist = await NoteReaction.findOne({
noteId: note._id,
userId: user._id,
deletedAt: { $exists: false }
});
if (exist === null) {
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
}
// Delete reaction
2019-03-20 13:39:02 +01:00
await NoteReaction.remove({
_id: exist._id
});
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 = renderActivity(renderUndo(renderLike(user, note, exist.reaction), user));
deliver(user, content, note._user.inbox);
}
//#endregion
return;
};