refactor: export channel filter

This commit is contained in:
Namekuji 2023-08-04 03:07:38 -04:00
parent f3e545057d
commit e6076602df
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
2 changed files with 27 additions and 15 deletions

View file

@ -4,7 +4,7 @@ import type { Note } from "@/models/entities/note.js";
import type { NoteReaction } from "@/models/entities/note-reaction.js";
import { Client, types } from "cassandra-driver";
import type { User } from "@/models/entities/user.js";
import { LocalFollowingsCache } from "@/misc/cache.js";
import { ChannelFollowingsCache, LocalFollowingsCache } from "@/misc/cache.js";
function newClient(): Client | null {
if (!config.scylla) {
@ -209,3 +209,25 @@ export async function isVisible(
return visible;
}
export async function filterChannel(
notes: ScyllaNote[],
user: { id: User["id"] } | null,
): Promise<ScyllaNote[]> {
let foundNotes = notes;
if (!user) {
foundNotes = foundNotes.filter((note) => !note.channelId);
} else {
const channelNotes = foundNotes.filter((note) => !!note.channelId);
if (channelNotes.length > 0) {
const cache = await ChannelFollowingsCache.init(user.id);
const followingIds = await cache.getAll();
foundNotes = foundNotes.filter(
(note) => !note.channelId || followingIds.includes(note.channelId),
);
}
}
return foundNotes;
}

View file

@ -1,5 +1,5 @@
import { Brackets } from "typeorm";
import { Notes, Followings, ChannelFollowings } from "@/models/index.js";
import { Notes, Followings } from "@/models/index.js";
import { activeUsersChart } from "@/services/chart/index.js";
import define from "../../define.js";
import { makePaginationQuery } from "../../common/make-pagination-query.js";
@ -16,8 +16,9 @@ import {
parseScyllaNote,
prepared,
scyllaClient,
filterChannel,
} from "@/db/scylla.js";
import { ChannelFollowingsCache, LocalFollowingsCache } from "@/misc/cache.js";
import { LocalFollowingsCache } from "@/misc/cache.js";
export const meta = {
tags: ["notes"],
@ -103,18 +104,7 @@ export default define(meta, paramDef, async (ps, user) => {
}
// Filter channels
if (!user) {
foundNotes = foundNotes.filter((note) => !note.channelId);
} else {
const channelNotes = foundNotes.filter((note) => !!note.channelId);
if (channelNotes.length > 0) {
const cache = await ChannelFollowingsCache.init(user.id);
const followingIds = await cache.getAll();
foundNotes = foundNotes.filter(
(note) => !note.channelId || followingIds.includes(note.channelId),
);
}
}
foundNotes = await filterChannel(foundNotes, user);
return Notes.packMany(foundNotes, user);
}