diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 2b6dd800ef..eacb7fc95f 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -1,6 +1,7 @@ import config from "@/config/index.js"; -import { DriveFile } from "@/models/entities/drive-file.js"; -import { Client } from "cassandra-driver"; +import type { PopulatedEmoji } from "@/misc/populate-emojis.js"; +import type { NoteReaction } from "@/models/entities/note-reaction.js"; +import { Client, types } from "cassandra-driver"; function newClient(): Client | null { if (!config.scylla) { @@ -52,11 +53,11 @@ export const prepared = { VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, select: { - byDate: `SELECT * FROM note WHERE "createdAtDate" = ? AND "createdAt" < ?`, + byDate: `SELECT * FROM note WHERE "createdAtDate" IN ?`, byId: `SELECT * FROM note WHERE "id" IN ?`, - byUri: `SELECT * FROM note WHERE "uri" = ?`, - byUrl: `SELECT * FROM note WHERE "url" = ?`, - byUserId: `SELECT * FROM note_by_userid WHERE "userId" = ? AND "createdAt" < ?`, + byUri: `SELECT * FROM note WHERE "uri" IN ?`, + byUrl: `SELECT * FROM note WHERE "url" IN ?`, + byUserId: `SELECT * FROM note_by_userid WHERE "userId" IN ?`, }, delete: `DELETE FROM note WHERE "createdAtDate" = ? AND "createdAt" = ?`, update: { @@ -68,6 +69,13 @@ export const prepared = { insert: `INSERT INTO reaction ("id", "noteId", "userId", "reaction", "emoji", "createdAt") VALUES (?, ?, ?, ?, ?, ?)`, + select: { + byNoteId: `SELECT * FROM reaction WHERE "noteId" IN ?`, + byUserId: `SELECT * FROM reaction_by_userid WHERE "userId" IN ?`, + byNoteAndUser: `SELECT * FROM reaction WHERE "noteId" = ? AND "userId" = ?`, + byId: `SELECT * FROM reaction WHERE "id" IN ?`, + }, + delete: `DELETE FROM reaction WHERE "noteId" = ? AND "userId" = ?`, }, }; @@ -87,3 +95,18 @@ export interface ScyllaDriveFile { width: number; height: number; } + +export type ScyllaNoteReaction = NoteReaction & { + emoji: PopulatedEmoji +} + +export function parseScyllaReaction(row: types.Row): ScyllaNoteReaction { + return { + id: row.get("id"), + noteId: row.get("noteId"), + userId: row.get("userId"), + reaction: row.get("reaction"), + createdAt: row.get("createdAt"), + emoji: row.get("emoji"), + } +} diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index b53e065892..c9ba9e323d 100644 --- a/packages/backend/src/misc/populate-emojis.ts +++ b/packages/backend/src/misc/populate-emojis.ts @@ -14,7 +14,7 @@ export const EmojiCache = new Cache("populateEmojis", 60 * 60 * 12 /** * 添付用絵文字情報 */ -type PopulatedEmoji = { +export type PopulatedEmoji = { name: string; url: string; width: number | null; diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index ce5827642a..e396b0b232 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -67,10 +67,8 @@ import { shouldSilenceInstance } from "@/misc/should-block-instance.js"; import meilisearch from "../../db/meilisearch.js"; import { redisClient } from "@/db/redis.js"; import { Mutex } from "redis-semaphore"; -import { prepared, scyllaClient, ScyllaDriveFile } from "@/db/scylla.js"; +import { prepared, scyllaClient } from "@/db/scylla.js"; import { populateEmojis } from "@/misc/populate-emojis.js"; -import { decodeReaction } from "@/misc/reaction-lib.js"; -import { types } from "cassandra-driver"; const mutedWordsCache = new Cache< { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[] diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts index 90175ccdc4..b810744bee 100644 --- a/packages/backend/src/services/note/delete.ts +++ b/packages/backend/src/services/note/delete.ts @@ -22,6 +22,7 @@ import { countSameRenotes } from "@/misc/count-same-renotes.js"; import { registerOrFetchInstanceDoc } from "../register-or-fetch-instance-doc.js"; import { deliverToRelays } from "../relay.js"; import meilisearch from "@/db/meilisearch.js"; +import { prepared, scyllaClient } from "@/db/scylla.js"; /** * 投稿を削除します。 @@ -116,6 +117,13 @@ export default async function ( } } + if (scyllaClient) { + const date = new Date(note.createdAt.getTime()); + await scyllaClient.execute(prepared.note.delete, [date, date], { + prepare: true, + }); + } + await Notes.delete({ id: note.id, userId: user.id, diff --git a/packages/backend/src/services/note/reaction/create.ts b/packages/backend/src/services/note/reaction/create.ts index 038e904db5..893f239632 100644 --- a/packages/backend/src/services/note/reaction/create.ts +++ b/packages/backend/src/services/note/reaction/create.ts @@ -68,7 +68,14 @@ export default async ( // Thus, a reaction by the same user will be replaced if exists. await scyllaClient.execute( prepared.reaction.insert, - [record.id, record.noteId, record.userId, _reaction, emojiData, record.createdAt], + [ + record.id, + record.noteId, + record.userId, + _reaction, + emojiData, + record.createdAt, + ], { prepare: true }, ); } else { diff --git a/packages/backend/src/services/note/reaction/delete.ts b/packages/backend/src/services/note/reaction/delete.ts index 15c6d1cf62..c044596274 100644 --- a/packages/backend/src/services/note/reaction/delete.ts +++ b/packages/backend/src/services/note/reaction/delete.ts @@ -8,28 +8,39 @@ 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"; +import { parseScyllaReaction, prepared, scyllaClient } from "@/db/scylla"; +import type { NoteReaction } from "@/models/entities/note-reaction.js"; export default async ( user: { id: User["id"]; host: User["host"] }, note: Note, ) => { - const reaction = await NoteReactions.findOneBy({ - noteId: note.id, - userId: user.id, - }); - - // if already unreacted - if (reaction == null) { - throw new IdentifiableError( - "60527ec9-b4cb-4a88-a6bd-32d3ad26817d", - "not reacted", + let reaction: NoteReaction | null; + if (scyllaClient) { + const result = await scyllaClient.execute( + prepared.reaction.select.byNoteAndUser, + [note.id, user.id], + { prepare: true }, ); + reaction = + result.rowLength > 0 ? parseScyllaReaction(result.rows[0]) : null; + } else { + reaction = await NoteReactions.findOneBy({ + noteId: note.id, + userId: user.id, + }); } // Delete reaction - const result = await NoteReactions.delete(reaction.id); - - if (result.affected !== 1) { + if (reaction) { + if (scyllaClient) { + await scyllaClient.execute(prepared.reaction.delete, [note.id, user.id], { + prepare: true, + }); + } else { + await NoteReactions.delete(reaction.id); + } + } else { throw new IdentifiableError( "60527ec9-b4cb-4a88-a6bd-32d3ad26817d", "not reacted", @@ -37,16 +48,31 @@ export default async ( } // Decrement reactions count - const sql = `jsonb_set("reactions", '{${reaction.reaction}}', (COALESCE("reactions"->>'${reaction.reaction}', '0')::int - 1)::text::jsonb)`; - await Notes.createQueryBuilder() - .update() - .set({ - reactions: () => sql, - }) - .where("id = :id", { id: note.id }) - .execute(); + if (scyllaClient) { + const count = Math.max((note.reactions[reaction.reaction] ?? 0) - 1, 0); + if (count === 0) { + delete note.reactions[reaction.reaction]; + } else { + note.reactions[reaction.reaction] = count; + } + const date = new Date(note.createdAt.getTime()); + await scyllaClient.execute( + prepared.note.update.reactions, + [note.reactions, Math.max((note.score ?? 0) - 1, 0), date, date], + { prepare: true }, + ); + } else { + const sql = `jsonb_set("reactions", '{${reaction.reaction}}', (COALESCE("reactions"->>'${reaction.reaction}', '0')::int - 1)::text::jsonb)`; + await Notes.createQueryBuilder() + .update() + .set({ + reactions: () => sql, + }) + .where("id = :id", { id: note.id }) + .execute(); - Notes.decrement({ id: note.id }, "score", 1); + Notes.decrement({ id: note.id }, "score", 1); + } publishNoteStream(note.id, "unreacted", { reaction: decodeReaction(reaction.reaction).reaction,