2019-02-05 06:14:23 +01:00
|
|
|
import { publishNoteStream } from '../../stream';
|
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';
|
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-11-08 08:49:23 +01:00
|
|
|
import { NoteReactions, Users, NoteWatchings, Notes, 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) => {
|
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
|
|
|
|
|
|
|
//#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) {
|
2020-05-10 10:25:16 +02:00
|
|
|
const reactee = await Users.findOne(note.userId);
|
2020-02-04 00:26:00 +01:00
|
|
|
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
|
|
|
};
|