2018-06-21 04:35:28 +02:00
|
|
|
import { IUser, isLocalUser, isRemoteUser } from '../../../models/user';
|
|
|
|
import Note, { INote } from '../../../models/note';
|
2018-04-07 19:30:37 +02:00
|
|
|
import NoteReaction from '../../../models/note-reaction';
|
2019-02-05 06:14:23 +01:00
|
|
|
import { publishNoteStream } from '../../stream';
|
|
|
|
import notify from '../../create-notification';
|
2018-04-07 19:30:37 +02:00
|
|
|
import NoteWatching from '../../../models/note-watching';
|
2018-04-07 10:05:14 +02:00
|
|
|
import watch from '../watch';
|
|
|
|
import renderLike from '../../../remote/activitypub/renderer/like';
|
|
|
|
import { deliver } from '../../../queue';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2019-02-07 20:31:33 +01:00
|
|
|
import perUserReactionsChart from '../../../services/chart/per-user-reactions';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { IdentifiableError } from '../../../misc/identifiable-error';
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default async (user: IUser, note: INote, reaction: string) => {
|
2018-04-07 10:05:14 +02:00
|
|
|
// Myself
|
2018-04-07 19:30:37 +02:00
|
|
|
if (note.userId.equals(user._id)) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new IdentifiableError('2d8e7297-1873-4c00-8404-792c68d7bef0', 'cannot react to my note');
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-15 10:53:25 +02:00
|
|
|
// Create reaction
|
2019-02-22 03:46:58 +01:00
|
|
|
await NoteReaction.insert({
|
|
|
|
createdAt: new Date(),
|
|
|
|
noteId: note._id,
|
|
|
|
userId: user._id,
|
|
|
|
reaction
|
|
|
|
}).catch(e => {
|
2018-04-15 10:53:25 +02:00
|
|
|
// duplicate key error
|
2018-04-19 02:17:42 +02:00
|
|
|
if (e.code === 11000) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new IdentifiableError('51c42bb4-931a-456b-bff7-e5a8a70dd298', 'already reacted');
|
2018-04-15 10:53:25 +02:00
|
|
|
}
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
throw e;
|
|
|
|
});
|
2018-04-07 10:05:14 +02:00
|
|
|
|
|
|
|
// Increment reactions count
|
2018-04-07 19:30:37 +02:00
|
|
|
await Note.update({ _id: note._id }, {
|
2018-10-25 00:04:15 +02:00
|
|
|
$inc: {
|
|
|
|
[`reactionCounts.${reaction}`]: 1,
|
|
|
|
score: 1
|
|
|
|
}
|
2018-04-07 10:05:14 +02:00
|
|
|
});
|
|
|
|
|
2018-10-22 22:36:35 +02:00
|
|
|
perUserReactionsChart.update(user, note);
|
2018-10-22 10:36:36 +02:00
|
|
|
|
2018-10-07 04:06:17 +02:00
|
|
|
publishNoteStream(note._id, 'reacted', {
|
2018-10-08 18:26:04 +02:00
|
|
|
reaction: reaction,
|
|
|
|
userId: user._id
|
2018-10-07 04:06:17 +02:00
|
|
|
});
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2018-04-22 03:53:27 +02:00
|
|
|
// リアクションされたユーザーがローカルユーザーなら通知を作成
|
|
|
|
if (isLocalUser(note._user)) {
|
|
|
|
notify(note.userId, user._id, 'reaction', {
|
|
|
|
noteId: note._id,
|
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
}
|
2018-04-07 10:05:14 +02:00
|
|
|
|
|
|
|
// Fetch watchers
|
2018-04-07 19:30:37 +02:00
|
|
|
NoteWatching
|
2018-04-07 10:05:14 +02:00
|
|
|
.find({
|
2018-04-07 19:30:37 +02:00
|
|
|
noteId: note._id,
|
2018-04-07 10:05:14 +02:00
|
|
|
userId: { $ne: user._id }
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
userId: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(watchers => {
|
2018-12-11 12:36:55 +01:00
|
|
|
for (const watcher of watchers) {
|
2018-04-07 10:05:14 +02:00
|
|
|
notify(watcher.userId, user._id, 'reaction', {
|
2018-04-07 19:30:37 +02:00
|
|
|
noteId: note._id,
|
2018-04-07 10:05:14 +02:00
|
|
|
reaction: reaction
|
|
|
|
});
|
2018-12-11 12:36:55 +01:00
|
|
|
}
|
2018-04-07 10:05:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
|
2018-04-07 20:58:11 +02:00
|
|
|
if (isLocalUser(user) && user.settings.autoWatch !== false) {
|
2018-04-07 19:30:37 +02:00
|
|
|
watch(user._id, note);
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region 配信
|
|
|
|
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
|
2018-04-07 19:30:37 +02:00
|
|
|
if (isLocalUser(user) && isRemoteUser(note._user)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderLike(user, note, reaction));
|
2018-04-15 10:53:25 +02:00
|
|
|
deliver(user, content, note._user.inbox);
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 03:46:58 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
};
|