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