From e6df07afb0285033c627d8cc947a4b6018510bf3 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Tue, 22 Aug 2023 12:31:45 -0400 Subject: [PATCH] fix: filter featured notes via origin --- .../cql/1689400417034_timeline/up.cql | 27 +++++++----------- packages/backend/src/db/cql.ts | 2 +- .../server/api/endpoints/notes/featured.ts | 28 +++++++++++++++++-- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql b/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql index a56bfa7376..ad86cf9f3d 100644 --- a/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql +++ b/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql @@ -76,7 +76,7 @@ CREATE TABLE note ( -- Store all posts "reactions" map, -- Reactions "noteEdit" set>, -- Edit History "updatedAt" timestamp, - PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility") ) WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE INDEX note_by_uri ON note ("uri"); @@ -91,8 +91,7 @@ CREATE MATERIALIZED VIEW note_by_id AS AND "userId" IS NOT NULL AND "userHost" IS NOT NULL AND "visibility" IS NOT NULL - AND "score" IS NOT NULL - PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW note_by_user_id AS @@ -102,8 +101,7 @@ CREATE MATERIALIZED VIEW note_by_user_id AS AND "createdAtDate" IS NOT NULL AND "userHost" IS NOT NULL AND "visibility" IS NOT NULL - AND "score" IS NOT NULL - PRIMARY KEY ("userId", "createdAt", "createdAtDate", "userHost", "visibility", "score") + PRIMARY KEY ("userId", "createdAt", "createdAtDate", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW note_by_renote_id AS @@ -114,8 +112,7 @@ CREATE MATERIALIZED VIEW note_by_renote_id AS AND "userId" IS NOT NULL AND "userHost" IS NOT NULL AND "visibility" IS NOT NULL - AND "score" IS NOT NULL - PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW note_by_renote_id_and_user_id AS @@ -126,8 +123,7 @@ CREATE MATERIALIZED VIEW note_by_renote_id_and_user_id AS AND "userId" IS NOT NULL AND "userHost" IS NOT NULL AND "visibility" IS NOT NULL - AND "score" IS NOT NULL - PRIMARY KEY (("renoteId", "userId"), "createdAt", "createdAtDate", "userHost", "visibility", "score") + PRIMARY KEY (("renoteId", "userId"), "createdAt", "createdAtDate", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW note_by_channel_id AS @@ -138,8 +134,7 @@ CREATE MATERIALIZED VIEW note_by_channel_id AS AND "userId" IS NOT NULL AND "userHost" IS NOT NULL AND "visibility" IS NOT NULL - AND "score" IS NOT NULL - PRIMARY KEY ("channelId", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("channelId", "createdAt", "createdAtDate", "userId", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW global_timeline AS @@ -148,9 +143,8 @@ CREATE MATERIALIZED VIEW global_timeline AS AND "createdAt" IS NOT NULL AND "userId" IS NOT NULL AND "userHost" IS NOT NULL - AND "score" IS NOT NULL AND "visibility" = 'public' - PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW local_timeline AS @@ -160,8 +154,7 @@ CREATE MATERIALIZED VIEW local_timeline AS AND "userId" IS NOT NULL AND "userHost" = 'local' AND "visibility" = 'public' - AND "score" IS NOT NULL - PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score") + PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW score_feed AS @@ -172,8 +165,8 @@ CREATE MATERIALIZED VIEW score_feed AS AND "userHost" IS NOT NULL AND "score" IS NOT NULL AND "visibility" = 'public' - AND "score" > 0 - PRIMARY KEY ("createdAtDate", "score", "userHost", "createdAt", "userId", "visibility") + AND "score" IS NOT NULL + PRIMARY KEY ("createdAtDate", "score", "createdAt", "userHost", "userId", "visibility") WITH CLUSTERING ORDER BY ("score" DESC, "createdAt" DESC); CREATE TABLE home_timeline ( diff --git a/packages/backend/src/db/cql.ts b/packages/backend/src/db/cql.ts index 18d49698a0..6d98c6c5e3 100644 --- a/packages/backend/src/db/cql.ts +++ b/packages/backend/src/db/cql.ts @@ -144,7 +144,7 @@ export const scyllaQueries = { }, }, scoreFeed: { - select: `SELECT * FROM score_feed WHERE "createdAtDate" = ?`, + select: `SELECT * FROM score_feed WHERE "createdAtDate" = ? AND "score" > 0`, }, reaction: { insert: `INSERT INTO reaction diff --git a/packages/backend/src/server/api/endpoints/notes/featured.ts b/packages/backend/src/server/api/endpoints/notes/featured.ts index f2191cdd5d..ec26bc3acd 100644 --- a/packages/backend/src/server/api/endpoints/notes/featured.ts +++ b/packages/backend/src/server/api/endpoints/notes/featured.ts @@ -92,15 +92,39 @@ export default define(meta, paramDef, async (ps, user) => { .execute(prepared.scoreFeed.select, [targetDay], { prepare: true }) .then((result) => result.rows.map(parseScyllaNote)); + switch (ps.origin) { + case "local": + notes = notes.filter((note) => !note.userHost); + break; + case "remote": + notes = notes.filter((note) => !!note.userHost); + break; + } + if (user) { - notes = await filterMutedUser(notes, user, mutedUserIds, mutedInstances); + notes = await filterMutedUser( + notes, + user, + mutedUserIds, + mutedInstances, + ); notes = await filterMutedNote(notes, user, mutedWords); - notes = await filterBlockUser(notes, user, [...blockerIds, ...blockingIds]); + notes = await filterBlockUser(notes, user, [ + ...blockerIds, + ...blockingIds, + ]); } foundNotes.push(...notes); targetDay = new Date(targetDay.getTime() - ONEDAY); } + + return Notes.packMany( + foundNotes + .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()) + .slice(ps.offset, ps.offset + ps.limit), + user, + ); } const query = Notes.createQueryBuilder("note")