read from scylla and dragonfly in getNote if enabled

This commit is contained in:
Namekuji 2023-07-26 21:46:17 -04:00
parent e4fef88e4e
commit a7fdf68bd2
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -3,7 +3,8 @@ import type { User } from "@/models/entities/user.js";
import type { Note } from "@/models/entities/note.js"; import type { Note } from "@/models/entities/note.js";
import { Notes, Users } from "@/models/index.js"; import { Notes, Users } from "@/models/index.js";
import { generateVisibilityQuery } from "./generate-visibility-query.js"; import { generateVisibilityQuery } from "./generate-visibility-query.js";
import { prepared, scyllaClient } from "@/db/scylla.js"; import { parseScyllaNote, prepared, scyllaClient } from "@/db/scylla.js";
import { FollowingsCache } from "@/misc/cache.js";
/** /**
* Get note for API processing, taking into account visibility. * Get note for API processing, taking into account visibility.
@ -20,21 +21,39 @@ export async function getNote(
{ prepare: true }, { prepare: true },
); );
if (result.rowLength > 0) { if (result.rowLength > 0) {
const visibility: string = result.rows[0].get("visibility"); const candidate = parseScyllaNote(result.first());
if (!me) { let valid = false;
if (
["public", "home"].includes(candidate.visibility) // public post
) {
valid = true;
} else if (me) {
const cache = await FollowingsCache.init(me.id);
valid =
candidate.userId === me.id || // my own post
candidate.visibleUserIds.includes(me.id) || // visible to me
candidate.mentions.includes(me.id) || // mentioned me
(candidate.visibility === "followers" &&
(await cache.isFollowing(candidate.userId)));
}
if (valid) {
note = candidate;
} }
} }
} else {
const query = Notes.createQueryBuilder("note").where("note.id = :id", {
id: noteId,
});
generateVisibilityQuery(query, me);
note = await query.getOne();
} }
const query = Notes.createQueryBuilder("note").where("note.id = :id", { if (!note) {
id: noteId,
});
generateVisibilityQuery(query, me);
note = await query.getOne();
if (note == null) {
throw new IdentifiableError( throw new IdentifiableError(
"9725d0ce-ba28-4dde-95a7-2cbb2c15de24", "9725d0ce-ba28-4dde-95a7-2cbb2c15de24",
"No such note.", "No such note.",