2022-02-27 02:07:39 +00:00
|
|
|
import { publishNoteStream } from '@/services/stream.js';
|
|
|
|
import { renderLike } from '@/remote/activitypub/renderer/like.js';
|
|
|
|
import DeliverManager from '@/remote/activitypub/deliver-manager.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import { toDbReaction, decodeReaction } from '@/misc/reaction-lib.js';
|
|
|
|
import { User, IRemoteUser } from '@/models/entities/user.js';
|
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { NoteReactions, Users, NoteWatchings, Notes, Emojis, Blockings } from '@/models/index.js';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Not } from 'typeorm';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { perUserReactionsChart } from '@/services/chart/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { createNotification } from '../../create-notification.js';
|
|
|
|
import deleteReaction from './delete.js';
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
|
|
|
import { NoteReaction } from '@/models/entities/note-reaction.js';
|
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
2018-04-07 08:05:14 +00:00
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
export default async (user: { id: User['id']; host: User['host']; }, note: Note, reaction?: string) => {
|
2021-08-17 12:48:59 +00:00
|
|
|
// Check blocking
|
|
|
|
if (note.userId !== user.id) {
|
|
|
|
const block = await Blockings.findOne({
|
|
|
|
blockerId: note.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new IdentifiableError('e70412a4-7197-4726-8e74-f3e0deb92aa7');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 01:53:09 +00:00
|
|
|
// TODO: cache
|
2020-04-13 15:42:59 +00:00
|
|
|
reaction = await toDbReaction(reaction, user.host);
|
2019-03-17 15:03:57 +00:00
|
|
|
|
2021-05-11 03:41:02 +00:00
|
|
|
const record: NoteReaction = {
|
2021-03-21 12:27:09 +00:00
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
reaction,
|
2021-03-21 12:27:09 +00:00
|
|
|
};
|
2021-03-18 08:35:47 +00:00
|
|
|
|
|
|
|
// Create reaction
|
|
|
|
try {
|
2021-03-21 12:27:09 +00:00
|
|
|
await NoteReactions.insert(record);
|
2021-03-18 08:35:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (isDuplicateKeyValueError(e)) {
|
2021-05-11 03:41:02 +00:00
|
|
|
const exists = await NoteReactions.findOneOrFail({
|
2021-03-18 08:35:47 +00:00
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
2020-02-03 23:26:00 +00:00
|
|
|
|
2021-05-11 03:41:02 +00:00
|
|
|
if (exists.reaction !== reaction) {
|
2021-03-18 08:35:47 +00:00
|
|
|
// 別のリアクションがすでにされていたら置き換える
|
|
|
|
await deleteReaction(user, note);
|
2021-05-11 03:41:02 +00:00
|
|
|
await NoteReactions.insert(record);
|
2021-03-18 08:35:47 +00:00
|
|
|
} else {
|
2021-05-11 03:41:02 +00:00
|
|
|
// 同じリアクションがすでにされていたらエラー
|
|
|
|
throw new IdentifiableError('51c42bb4-931a-456b-bff7-e5a8a70dd298');
|
2021-03-18 08:35:47 +00:00
|
|
|
}
|
2020-02-03 23:26:00 +00:00
|
|
|
} else {
|
2021-03-18 08:35:47 +00:00
|
|
|
throw e;
|
2020-02-03 23:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 08:05:14 +00:00
|
|
|
// Increment reactions count
|
2019-04-07 12:50:36 +00:00
|
|
|
const sql = `jsonb_set("reactions", '{${reaction}}', (COALESCE("reactions"->>'${reaction}', '0')::int + 1)::text::jsonb)`;
|
|
|
|
await Notes.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
reactions: () => sql,
|
2021-12-09 14:58:30 +00:00
|
|
|
score: () => '"score" + 1',
|
2019-04-07 12:50:36 +00:00
|
|
|
})
|
|
|
|
.where('id = :id', { id: note.id })
|
|
|
|
.execute();
|
2019-04-14 11:28:57 +00:00
|
|
|
|
2018-10-22 20:36:35 +00:00
|
|
|
perUserReactionsChart.update(user, note);
|
2018-10-22 08:36:36 +00:00
|
|
|
|
2020-04-13 15:42:59 +00:00
|
|
|
// カスタム絵文字リアクションだったら絵文字情報も送る
|
|
|
|
const decodedReaction = decodeReaction(reaction);
|
|
|
|
|
2022-02-02 17:41:22 +00:00
|
|
|
const emoji = await Emojis.findOne({
|
2020-04-13 15:42:59 +00:00
|
|
|
where: {
|
|
|
|
name: decodedReaction.name,
|
2021-12-09 14:58:30 +00:00
|
|
|
host: decodedReaction.host,
|
2020-04-13 15:42:59 +00:00
|
|
|
},
|
2022-01-21 09:47:02 +00:00
|
|
|
select: ['name', 'host', 'originalUrl', 'publicUrl'],
|
2020-04-13 15:42:59 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
publishNoteStream(note.id, 'reacted', {
|
2020-04-15 15:47:17 +00:00
|
|
|
reaction: decodedReaction.reaction,
|
2022-01-21 09:47:02 +00:00
|
|
|
emoji: emoji != null ? {
|
|
|
|
name: emoji.host ? `${emoji.name}@${emoji.host}` : `${emoji.name}@.`,
|
|
|
|
url: emoji.publicUrl || emoji.originalUrl, // || emoji.originalUrl してるのは後方互換性のため
|
|
|
|
} : null,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2018-10-07 02:06:17 +00:00
|
|
|
});
|
2018-04-07 08:05:14 +00:00
|
|
|
|
2018-04-22 01:53:27 +00:00
|
|
|
// リアクションされたユーザーがローカルユーザーなら通知を作成
|
2019-04-07 12:50:36 +00:00
|
|
|
if (note.userHost === null) {
|
2020-03-28 09:07:41 +00:00
|
|
|
createNotification(note.userId, 'reaction', {
|
|
|
|
notifierId: user.id,
|
2019-04-07 12:50:36 +00:00
|
|
|
noteId: note.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
reaction: reaction,
|
2018-04-22 01:53:27 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-07 08:05:14 +00:00
|
|
|
|
|
|
|
// Fetch watchers
|
2019-04-07 12:50:36 +00:00
|
|
|
NoteWatchings.find({
|
|
|
|
noteId: note.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: Not(user.id),
|
2019-04-07 12:50:36 +00:00
|
|
|
}).then(watchers => {
|
|
|
|
for (const watcher of watchers) {
|
2020-03-28 09:07:41 +00:00
|
|
|
createNotification(watcher.userId, 'reaction', {
|
|
|
|
notifierId: user.id,
|
2019-04-07 12:50:36 +00:00
|
|
|
noteId: note.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
reaction: reaction,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-04-07 08:05:14 +00:00
|
|
|
|
|
|
|
//#region 配信
|
2020-02-03 23:26:00 +00:00
|
|
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
2021-03-18 08:35:47 +00:00
|
|
|
const content = renderActivity(await renderLike(record, note));
|
2020-02-03 23:26:00 +00:00
|
|
|
const dm = new DeliverManager(user, content);
|
|
|
|
if (note.userHost !== null) {
|
2020-05-10 08:25:16 +00:00
|
|
|
const reactee = await Users.findOne(note.userId);
|
2020-02-03 23:26:00 +00:00
|
|
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
|
|
|
}
|
|
|
|
dm.addFollowersRecipe();
|
|
|
|
dm.execute();
|
2018-04-07 08:05:14 +00:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 02:46:58 +00:00
|
|
|
};
|