2019-02-05 06:14:23 +01:00
|
|
|
import { publishNoteStream } from '../../stream';
|
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-22 03:46:58 +01:00
|
|
|
import { IdentifiableError } from '../../../misc/identifiable-error';
|
2019-03-17 16:03:57 +01:00
|
|
|
import { toDbReaction } from '../../../misc/reaction-lib';
|
|
|
|
import fetchMeta from '../../../misc/fetch-meta';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { User } from '../../../models/entities/user';
|
|
|
|
import { Note } from '../../../models/entities/note';
|
2019-04-10 08:04:27 +02:00
|
|
|
import { NoteReactions, Users, NoteWatchings, Notes, UserProfiles } from '../../../models';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Not } from 'typeorm';
|
|
|
|
import { perUserReactionsChart } from '../../chart';
|
|
|
|
import { genId } from '../../../misc/gen-id';
|
|
|
|
import { NoteReaction } from '../../../models/entities/note-reaction';
|
|
|
|
import { createNotification } from '../../create-notification';
|
|
|
|
import { isDuplicateKeyValueError } from '../../../misc/is-duplicate-key-value-error';
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async (user: User, note: Note, reaction: string) => {
|
2018-04-07 10:05:14 +02:00
|
|
|
// Myself
|
2019-04-07 14:50:36 +02:00
|
|
|
if (note.userId === 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
|
|
|
}
|
|
|
|
|
2019-03-17 16:03:57 +01:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
reaction = await toDbReaction(reaction, meta.enableEmojiReaction);
|
|
|
|
|
2018-04-15 10:53:25 +02:00
|
|
|
// Create reaction
|
2019-04-07 14:50:36 +02:00
|
|
|
await NoteReactions.save({
|
|
|
|
id: genId(),
|
2019-02-22 03:46:58 +01:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
2019-02-22 03:46:58 +01:00
|
|
|
reaction
|
2019-04-07 14:50:36 +02:00
|
|
|
} as NoteReaction).catch(e => {
|
2018-04-15 10:53:25 +02:00
|
|
|
// duplicate key error
|
2019-04-07 14:50:36 +02:00
|
|
|
if (isDuplicateKeyValueError(e)) {
|
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
|
2019-04-07 14:50:36 +02:00
|
|
|
const sql = `jsonb_set("reactions", '{${reaction}}', (COALESCE("reactions"->>'${reaction}', '0')::int + 1)::text::jsonb)`;
|
|
|
|
await Notes.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
reactions: () => sql,
|
|
|
|
})
|
|
|
|
.where('id = :id', { id: note.id })
|
|
|
|
.execute();
|
|
|
|
// v11 inc score
|
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
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
publishNoteStream(note.id, 'reacted', {
|
2018-10-08 18:26:04 +02:00
|
|
|
reaction: reaction,
|
2019-04-07 14:50:36 +02:00
|
|
|
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
|
|
|
// リアクションされたユーザーがローカルユーザーなら通知を作成
|
2019-04-07 14:50:36 +02:00
|
|
|
if (note.userHost === null) {
|
|
|
|
createNotification(note.userId, user.id, 'reaction', {
|
|
|
|
noteId: note.id,
|
2018-04-22 03:53:27 +02:00
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
}
|
2018-04-07 10:05:14 +02:00
|
|
|
|
|
|
|
// Fetch watchers
|
2019-04-07 14:50:36 +02:00
|
|
|
NoteWatchings.find({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: Not(user.id)
|
|
|
|
}).then(watchers => {
|
|
|
|
for (const watcher of watchers) {
|
|
|
|
createNotification(watcher.userId, user.id, 'reaction', {
|
|
|
|
noteId: note.id,
|
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-04-10 08:04:27 +02:00
|
|
|
const profile = await UserProfiles.findOne({ userId: user.id });
|
|
|
|
|
2018-04-07 10:05:14 +02:00
|
|
|
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
|
2019-04-10 08:04:27 +02:00
|
|
|
if (Users.isLocalUser(user) && profile.autoWatch) {
|
2019-04-07 14:50:36 +02:00
|
|
|
watch(user.id, note);
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region 配信
|
|
|
|
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(user) && note.userHost !== null) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderLike(user, note, reaction));
|
2019-04-07 14:50:36 +02:00
|
|
|
deliver(user, content, note.userInbox);
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|