lookup note in scylla

This commit is contained in:
Namekuji 2023-08-14 04:02:39 -04:00
parent 2d1a6d8ab0
commit d8154dc591
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -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;