From e6076602dfb05617d819bbfef154d77b827da671 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 4 Aug 2023 03:07:38 -0400 Subject: [PATCH] refactor: export channel filter --- packages/backend/src/db/scylla.ts | 24 ++++++++++++++++++- .../server/api/endpoints/notes/timeline.ts | 18 ++++---------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index d329765201..301d28d0e2 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -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 { + 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; +} diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts index 63eb9138c8..aad7b03483 100644 --- a/packages/backend/src/server/api/endpoints/notes/timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts @@ -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); }