export visibility check

This commit is contained in:
Namekuji 2023-07-27 07:15:58 -04:00
parent f4dffc6d72
commit c7040b77b5
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
3 changed files with 38 additions and 22 deletions

View file

@ -3,7 +3,8 @@ import type { PopulatedEmoji } from "@/misc/populate-emojis.js";
import type { Note } from "@/models/entities/note.js";
import type { NoteReaction } from "@/models/entities/note-reaction.js";
import { Client, types } from "cassandra-driver";
import { noteVisibilities } from "@/types.js";
import type { User } from "@/models/entities/user.js";
import { LocalFollowingsCache } from "@/misc/cache.js";
function newClient(): Client | null {
if (!config.scylla) {
@ -182,3 +183,28 @@ export function parseScyllaReaction(row: types.Row): ScyllaNoteReaction {
emoji: row.get("emoji"),
};
}
export async function isVisible(
note: ScyllaNote,
user: { id: User["id"] } | null,
): Promise<boolean> {
let visible = false;
if (
["public", "home"].includes(note.visibility) // public post
) {
visible = true;
} else if (user) {
const cache = await LocalFollowingsCache.init(user.id);
visible =
note.userId === user.id || // my own post
note.visibleUserIds.includes(user.id) || // visible to me
note.mentions.includes(user.id) || // mentioned me
(note.visibility === "followers" &&
(await cache.isFollowing(note.userId))) || // following
note.replyUserId === user.id; // replied to myself
}
return visible;
}

View file

@ -3,8 +3,12 @@ import type { User } from "@/models/entities/user.js";
import type { Note } from "@/models/entities/note.js";
import { Notes, Users } from "@/models/index.js";
import { generateVisibilityQuery } from "./generate-visibility-query.js";
import { parseScyllaNote, prepared, scyllaClient } from "@/db/scylla.js";
import { LocalFollowingsCache } from "@/misc/cache.js";
import {
isVisible,
parseScyllaNote,
prepared,
scyllaClient,
} from "@/db/scylla.js";
/**
* Get note for API processing, taking into account visibility.
@ -22,25 +26,7 @@ export async function getNote(
);
if (result.rowLength > 0) {
const candidate = parseScyllaNote(result.first());
let valid = false;
if (
["public", "home"].includes(candidate.visibility) // public post
) {
valid = true;
} else if (me) {
const cache = await LocalFollowingsCache.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))) || // following
candidate.replyUserId === me.id; // replied to myself
}
if (valid) {
if (await isVisible(candidate, me)) {
note = candidate;
}
}

View file

@ -20,6 +20,10 @@ export const uriPersonCache = new Cache<CacheableUser | null>(
"uriPerson",
60 * 30,
);
export const userDenormalizedCache = new Cache<CacheableUser>(
"userDenormalized",
60 * 30,
);
subscriber.on("message", async (_, data) => {
const obj = JSON.parse(data);