2023-01-13 04:40:33 +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 type { User, IRemoteUser } from "@/models/entities/user.js";
|
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import {
|
|
|
|
NoteReactions,
|
|
|
|
Users,
|
|
|
|
NoteWatchings,
|
|
|
|
Notes,
|
|
|
|
Emojis,
|
|
|
|
Blockings,
|
|
|
|
} from "@/models/index.js";
|
|
|
|
import { IsNull, Not } from "typeorm";
|
|
|
|
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 type { NoteReaction } from "@/models/entities/note-reaction.js";
|
|
|
|
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
|
|
|
|
|
|
|
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) {
|
2022-03-26 06:34:00 +00:00
|
|
|
const block = await Blockings.findOneBy({
|
2021-08-17 12:48:59 +00:00
|
|
|
blockerId: note.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
2023-01-13 04:40:33 +00:00
|
|
|
throw new IdentifiableError("e70412a4-7197-4726-8e74-f3e0deb92aa7");
|
2021-08-17 12:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 11:40:16 +00:00
|
|
|
// check visibility
|
2023-01-13 04:40:33 +00:00
|
|
|
if (!(await Notes.isVisibleForMe(note, user.id))) {
|
|
|
|
throw new IdentifiableError(
|
|
|
|
"68e9d2d1-48bf-42c2-b90a-b20e09fd3d48",
|
|
|
|
"Note not accessible for you.",
|
|
|
|
);
|
2022-05-19 11:40:16 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
2022-03-26 06:34:00 +00:00
|
|
|
const exists = await NoteReactions.findOneByOrFail({
|
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
|
|
|
// 同じリアクションがすでにされていたらエラー
|
2023-01-13 04:40:33 +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)`;
|
2023-01-13 04:40:33 +00:00
|
|
|
await Notes.createQueryBuilder()
|
|
|
|
.update()
|
2019-04-07 12:50:36 +00:00
|
|
|
.set({
|
|
|
|
reactions: () => sql,
|
2021-12-09 14:58:30 +00:00
|
|
|
score: () => '"score" + 1',
|
2019-04-07 12:50:36 +00:00
|
|
|
})
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("id = :id", { id: note.id })
|
2019-04-07 12:50:36 +00:00
|
|
|
.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,
|
2022-03-26 06:34:00 +00:00
|
|
|
host: decodedReaction.host ?? IsNull(),
|
2020-04-13 15:42:59 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
select: ["name", "host", "originalUrl", "publicUrl"],
|
2020-04-13 15:42:59 +00:00
|
|
|
});
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
publishNoteStream(note.id, "reacted", {
|
2020-04-15 15:47:17 +00:00
|
|
|
reaction: decodedReaction.reaction,
|
2023-01-13 04:40:33 +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
|
|
|
|
2023-04-30 23:45:53 +00:00
|
|
|
// Create notification if the reaction target is a local user.
|
2023-05-01 00:22:50 +00:00
|
|
|
if (note.userHost === null) {
|
2023-01-13 04:40:33 +00:00
|
|
|
createNotification(note.userId, "reaction", {
|
2020-03-28 09:07:41 +00:00
|
|
|
notifierId: user.id,
|
2022-12-26 03:13:20 +00:00
|
|
|
note: note,
|
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
|
2022-03-26 06:34:00 +00:00
|
|
|
NoteWatchings.findBy({
|
2019-04-07 12:50:36 +00:00
|
|
|
noteId: note.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: Not(user.id),
|
2023-01-13 04:40:33 +00:00
|
|
|
}).then((watchers) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
for (const watcher of watchers) {
|
2023-01-13 04:40:33 +00:00
|
|
|
createNotification(watcher.userId, "reaction", {
|
2020-03-28 09:07:41 +00:00
|
|
|
notifierId: user.id,
|
2022-12-26 03:13:20 +00:00
|
|
|
note: note,
|
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
|
|
|
|
2023-04-30 23:45:53 +00:00
|
|
|
//#region deliver
|
2023-05-08 01:01:36 +00:00
|
|
|
if (
|
|
|
|
Users.isLocalUser(user) &&
|
|
|
|
!note.localOnly &&
|
|
|
|
note.visibility !== "hidden"
|
|
|
|
) {
|
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) {
|
2022-03-26 06:34:00 +00:00
|
|
|
const reactee = await Users.findOneBy({ id: note.userId });
|
2020-02-03 23:26:00 +00:00
|
|
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
|
|
|
}
|
2022-03-27 04:57:04 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
if (["public", "home", "followers"].includes(note.visibility)) {
|
2022-03-27 04:57:04 +00:00
|
|
|
dm.addFollowersRecipe();
|
2023-01-13 04:40:33 +00:00
|
|
|
} else if (note.visibility === "specified") {
|
|
|
|
const visibleUsers = await Promise.all(
|
|
|
|
note.visibleUserIds.map((id) => Users.findOneBy({ id })),
|
|
|
|
);
|
|
|
|
for (const u of visibleUsers.filter((u) => u && Users.isRemoteUser(u))) {
|
2022-03-27 04:57:04 +00:00
|
|
|
dm.addDirectRecipe(u as IRemoteUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:26:00 +00:00
|
|
|
dm.execute();
|
2018-04-07 08:05:14 +00:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 02:46:58 +00:00
|
|
|
};
|