2023-01-13 04:40:33 +00:00
|
|
|
import { publishNoteStream } from "@/services/stream.js";
|
|
|
|
import { renderLike } from "@/remote/activitypub/renderer/like.js";
|
|
|
|
import renderUndo from "@/remote/activitypub/renderer/undo.js";
|
|
|
|
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
|
|
|
|
import DeliverManager from "@/remote/activitypub/deliver-manager.js";
|
|
|
|
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
|
|
|
import type { User, IRemoteUser } from "@/models/entities/user.js";
|
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import { NoteReactions, Users, Notes } from "@/models/index.js";
|
|
|
|
import { decodeReaction } from "@/misc/reaction-lib.js";
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
user: { id: User["id"]; host: User["host"] },
|
|
|
|
note: Note,
|
|
|
|
) => {
|
2019-01-19 18:07:12 +00:00
|
|
|
// if already unreacted
|
2022-03-26 06:34:00 +00:00
|
|
|
const exist = await NoteReactions.findOneBy({
|
2019-04-07 12:50:36 +00:00
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
2019-01-19 18:07:12 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (exist == null) {
|
2023-01-13 04:40:33 +00:00
|
|
|
throw new IdentifiableError(
|
|
|
|
"60527ec9-b4cb-4a88-a6bd-32d3ad26817d",
|
|
|
|
"not reacted",
|
|
|
|
);
|
2019-01-19 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete reaction
|
2020-04-13 14:58:38 +00:00
|
|
|
const result = await NoteReactions.delete(exist.id);
|
|
|
|
|
|
|
|
if (result.affected !== 1) {
|
2023-01-13 04:40:33 +00:00
|
|
|
throw new IdentifiableError(
|
|
|
|
"60527ec9-b4cb-4a88-a6bd-32d3ad26817d",
|
|
|
|
"not reacted",
|
|
|
|
);
|
2020-04-13 14:58:38 +00:00
|
|
|
}
|
2019-01-19 18:07:12 +00:00
|
|
|
|
|
|
|
// Decrement reactions count
|
2019-04-07 12:50:36 +00:00
|
|
|
const sql = `jsonb_set("reactions", '{${exist.reaction}}', (COALESCE("reactions"->>'${exist.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,
|
|
|
|
})
|
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
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
Notes.decrement({ id: note.id }, "score", 1);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
publishNoteStream(note.id, "unreacted", {
|
2020-04-15 15:47:17 +00:00
|
|
|
reaction: decodeReaction(exist.reaction).reaction,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2019-01-19 18:07:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//#region 配信
|
2020-02-03 23:26:00 +00:00
|
|
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const content = renderActivity(
|
|
|
|
renderUndo(await renderLike(exist, note), user),
|
|
|
|
);
|
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);
|
|
|
|
}
|
|
|
|
dm.addFollowersRecipe();
|
|
|
|
dm.execute();
|
2019-01-19 18:07:12 +00:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 02:46:58 +00:00
|
|
|
};
|