From d8154dc591401f066f4cac86edaff8b6b5d6e585 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 14 Aug 2023 04:02:39 -0400 Subject: [PATCH] lookup note in scylla --- packages/backend/src/services/i/pin.ts | 33 +++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/services/i/pin.ts b/packages/backend/src/services/i/pin.ts index 97045a9fa9..4f1c1f83ad 100644 --- a/packages/backend/src/services/i/pin.ts +++ b/packages/backend/src/services/i/pin.ts @@ -10,6 +10,8 @@ import type { UserNotePining } from "@/models/entities/user-note-pining.js"; import { genId } from "@/misc/gen-id.js"; import { deliverToFollowers } from "@/remote/activitypub/deliver-manager.js"; import { deliverToRelays } from "../relay.js"; +import { parseScyllaNote, prepared, scyllaClient } from "@/db/scylla.js"; +import { userByIdCache } from "../user-cache.js"; /** * 指定した投稿をピン留めします @@ -21,12 +23,25 @@ export async function addPinned( noteId: Note["id"], ) { // Fetch pinee - const note = await Notes.findOneBy({ - id: noteId, - userId: user.id, - }); + let note: Note | null = null; + if (scyllaClient) { + const result = await scyllaClient.execute( + prepared.note.select.byId, + [noteId], + { prepare: true }, + ); + if (result.rowLength > 0) { + const candidate = parseScyllaNote(result.first()); + if (candidate.userId === user.id) note = candidate; + } + } else { + note = await Notes.findOneBy({ + id: noteId, + userId: user.id, + }); + } - if (note == null) { + if (!note) { throw new IdentifiableError( "70c4e51f-5bea-449c-a030-53bee3cce202", "No such note.", @@ -42,7 +57,7 @@ export async function addPinned( ); } - if (pinings.some((pining) => pining.noteId === note.id)) { + if (pinings.some((pining) => note && pining.noteId === note.id)) { throw new IdentifiableError( "23f0cf4e-59a3-4276-a91d-61a5891c1514", "That note has already been pinned.", @@ -100,8 +115,10 @@ export async function deliverPinnedChange( noteId: Note["id"], isAddition: boolean, ) { - const user = await Users.findOneBy({ id: userId }); - if (user == null) throw new Error("user not found"); + const user = await userByIdCache.fetchMaybe(userId, () => + Users.findOneBy({ id: userId }).then((user) => user ?? undefined), + ); + if (!user) throw new Error("user not found"); if (!Users.isLocalUser(user)) return;