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