2019-02-05 06:14:23 +01:00
|
|
|
import { publishNoteStream } from '../../stream';
|
2018-04-07 10:05:14 +02:00
|
|
|
import watch from '../watch';
|
2020-02-06 09:07:37 +01:00
|
|
|
import { renderLike } from '../../../remote/activitypub/renderer/like';
|
2020-02-04 00:26:00 +01:00
|
|
|
import DeliverManager from '../../../remote/activitypub/deliver-manager';
|
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';
|
2020-04-13 17:42:59 +02:00
|
|
|
import { toDbReaction, decodeReaction } from '../../../misc/reaction-lib';
|
2020-02-04 00:26:00 +01:00
|
|
|
import { User, IRemoteUser } from '../../../models/entities/user';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Note } from '../../../models/entities/note';
|
2020-04-13 17:42:59 +02:00
|
|
|
import { NoteReactions, Users, NoteWatchings, Notes, UserProfiles, Emojis } 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 { createNotification } from '../../create-notification';
|
2020-02-04 00:26:00 +01:00
|
|
|
import deleteReaction from './delete';
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-04-13 11:58:29 +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
|
|
|
}
|
|
|
|
|
2020-04-13 17:42:59 +02:00
|
|
|
reaction = await toDbReaction(reaction, user.host);
|
2019-03-17 16:03:57 +01:00
|
|
|
|
2020-02-04 00:26:00 +01:00
|
|
|
const exist = await NoteReactions.findOne({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exist) {
|
|
|
|
if (exist.reaction !== reaction) {
|
|
|
|
// 別のリアクションがすでにされていたら置き換える
|
|
|
|
await deleteReaction(user, note);
|
|
|
|
} else {
|
|
|
|
// 同じリアクションがすでにされていたら何もしない
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-15 10:53:25 +02:00
|
|
|
// Create reaction
|
2020-02-06 09:07:37 +01:00
|
|
|
const inserted = await NoteReactions.save({
|
2019-04-07 14:50:36 +02:00
|
|
|
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
|
|
|
|
});
|
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();
|
2019-04-14 13:28:57 +02:00
|
|
|
|
|
|
|
Notes.increment({ id: note.id }, '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
|
|
|
|
2020-04-13 17:42:59 +02:00
|
|
|
// カスタム絵文字リアクションだったら絵文字情報も送る
|
|
|
|
const decodedReaction = decodeReaction(reaction);
|
|
|
|
|
|
|
|
let emoji = await Emojis.findOne({
|
|
|
|
where: {
|
|
|
|
name: decodedReaction.name,
|
|
|
|
host: decodedReaction.host
|
|
|
|
},
|
|
|
|
select: ['name', 'host', 'url']
|
|
|
|
});
|
|
|
|
|
|
|
|
if (emoji) {
|
|
|
|
emoji = {
|
2020-04-15 17:47:17 +02:00
|
|
|
name: emoji.host ? `${emoji.name}@${emoji.host}` : `${emoji.name}@.`,
|
2020-04-13 17:42:59 +02:00
|
|
|
url: emoji.url
|
|
|
|
} as any;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
publishNoteStream(note.id, 'reacted', {
|
2020-04-15 17:47:17 +02:00
|
|
|
reaction: decodedReaction.reaction,
|
2020-04-13 17:42:59 +02:00
|
|
|
emoji: emoji,
|
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) {
|
2020-03-28 10:07:41 +01:00
|
|
|
createNotification(note.userId, 'reaction', {
|
|
|
|
notifierId: user.id,
|
2019-04-07 14:50:36 +02:00
|
|
|
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) {
|
2020-03-28 10:07:41 +01:00
|
|
|
createNotification(watcher.userId, 'reaction', {
|
|
|
|
notifierId: user.id,
|
2019-04-07 14:50:36 +02:00
|
|
|
noteId: note.id,
|
|
|
|
reaction: reaction
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-04-07 10:05:14 +02:00
|
|
|
|
2019-04-17 07:32:59 +02:00
|
|
|
const profile = await UserProfiles.findOne(user.id);
|
2019-04-10 08:04:27 +02:00
|
|
|
|
2018-04-07 10:05:14 +02:00
|
|
|
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
|
2019-04-12 18:43:22 +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 配信
|
2020-02-04 00:26:00 +01:00
|
|
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
2020-04-13 17:42:59 +02:00
|
|
|
const content = renderActivity(await renderLike(inserted, note));
|
2020-02-04 00:26:00 +01:00
|
|
|
const dm = new DeliverManager(user, content);
|
|
|
|
if (note.userHost !== null) {
|
|
|
|
const reactee = await Users.findOne(note.userId)
|
|
|
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
|
|
|
}
|
|
|
|
dm.addFollowersRecipe();
|
|
|
|
dm.execute();
|
2018-04-07 10:05:14 +02:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|