2018-06-21 11:35:28 +09:00
|
|
|
import { IUser, isLocalUser, isRemoteUser } from '../../../models/user';
|
|
|
|
import Note, { INote } from '../../../models/note';
|
2018-04-08 02:30:37 +09:00
|
|
|
import NoteReaction from '../../../models/note-reaction';
|
2018-07-07 19:19:00 +09:00
|
|
|
import { publishNoteStream } from '../../../stream';
|
2018-07-08 03:15:54 +09:00
|
|
|
import notify from '../../../notify';
|
2018-04-08 02:30:37 +09:00
|
|
|
import NoteWatching from '../../../models/note-watching';
|
2018-04-07 17:05:14 +09:00
|
|
|
import watch from '../watch';
|
|
|
|
import renderLike from '../../../remote/activitypub/renderer/like';
|
|
|
|
import { deliver } from '../../../queue';
|
2018-04-13 14:39:08 +09:00
|
|
|
import pack from '../../../remote/activitypub/renderer';
|
2018-10-23 05:36:35 +09:00
|
|
|
import perUserReactionsChart from '../../../chart/per-user-reactions';
|
2018-04-07 17:05:14 +09:00
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => {
|
2018-04-07 17:05:14 +09:00
|
|
|
// Myself
|
2018-04-08 02:30:37 +09:00
|
|
|
if (note.userId.equals(user._id)) {
|
|
|
|
return rej('cannot react to my note');
|
2018-04-07 17:05:14 +09:00
|
|
|
}
|
|
|
|
|
2018-04-15 17:53:25 +09:00
|
|
|
// Create reaction
|
|
|
|
try {
|
|
|
|
await NoteReaction.insert({
|
|
|
|
createdAt: new Date(),
|
|
|
|
noteId: note._id,
|
|
|
|
userId: user._id,
|
|
|
|
reaction
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// duplicate key error
|
2018-04-19 09:17:42 +09:00
|
|
|
if (e.code === 11000) {
|
2018-10-16 09:45:36 +09:00
|
|
|
return rej('already reacted');
|
2018-04-15 17:53:25 +09:00
|
|
|
}
|
2018-04-07 17:05:14 +09:00
|
|
|
|
2018-04-15 17:53:25 +09:00
|
|
|
console.error(e);
|
|
|
|
return rej('something happened');
|
2018-04-07 17:05:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
res();
|
|
|
|
|
|
|
|
// Increment reactions count
|
2018-04-08 02:30:37 +09:00
|
|
|
await Note.update({ _id: note._id }, {
|
2018-10-25 07:04:15 +09:00
|
|
|
$inc: {
|
|
|
|
[`reactionCounts.${reaction}`]: 1,
|
|
|
|
score: 1
|
|
|
|
}
|
2018-04-07 17:05:14 +09:00
|
|
|
});
|
|
|
|
|
2018-10-23 05:36:35 +09:00
|
|
|
perUserReactionsChart.update(user, note);
|
2018-10-22 17:36:36 +09:00
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
publishNoteStream(note._id, 'reacted', {
|
2018-10-09 01:26:04 +09:00
|
|
|
reaction: reaction,
|
|
|
|
userId: user._id
|
2018-10-07 11:06:17 +09:00
|
|
|
});
|
2018-04-07 17:05:14 +09:00
|
|
|
|
2018-04-22 10:53:27 +09:00
|
|
|
// リアクションされたユーザーがローカルユーザーなら通知を作成
|
|
|
|
if (isLocalUser(note._user)) {
|
|
|
|
notify(note.userId, user._id, 'reaction', {
|
|
|
|
noteId: note._id,
|
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
}
|
2018-04-07 17:05:14 +09:00
|
|
|
|
|
|
|
// Fetch watchers
|
2018-04-08 02:30:37 +09:00
|
|
|
NoteWatching
|
2018-04-07 17:05:14 +09:00
|
|
|
.find({
|
2018-04-08 02:30:37 +09:00
|
|
|
noteId: note._id,
|
2018-04-07 17:05:14 +09:00
|
|
|
userId: { $ne: user._id }
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
userId: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(watchers => {
|
|
|
|
watchers.forEach(watcher => {
|
|
|
|
notify(watcher.userId, user._id, 'reaction', {
|
2018-04-08 02:30:37 +09:00
|
|
|
noteId: note._id,
|
2018-04-07 17:05:14 +09:00
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
|
2018-04-08 03:58:11 +09:00
|
|
|
if (isLocalUser(user) && user.settings.autoWatch !== false) {
|
2018-04-08 02:30:37 +09:00
|
|
|
watch(user._id, note);
|
2018-04-07 17:05:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region 配信
|
|
|
|
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
|
2018-04-08 02:30:37 +09:00
|
|
|
if (isLocalUser(user) && isRemoteUser(note._user)) {
|
2018-04-23 15:27:01 +09:00
|
|
|
const content = pack(renderLike(user, note, reaction));
|
2018-04-15 17:53:25 +09:00
|
|
|
deliver(user, content, note._user.inbox);
|
2018-04-07 17:05:14 +09:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
});
|