From 5bed95e3577f0c65361c99aa274a8f4de0fcbd1c Mon Sep 17 00:00:00 2001 From: Natty Date: Sun, 31 Dec 2023 02:05:07 +0100 Subject: [PATCH] Reverted user note querying and removed the user join --- .../src/server/api/endpoints/users/notes.ts | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts index 2475d0865f..e3c28a5d2d 100644 --- a/packages/backend/src/server/api/endpoints/users/notes.ts +++ b/packages/backend/src/server/api/endpoints/users/notes.ts @@ -3,11 +3,10 @@ import {Notes} from "@/models/index.js"; import define from "../../define.js"; import {ApiError} from "../../error.js"; import {getUser} from "../../common/getters.js"; +import {makePaginationQuery} from "../../common/make-pagination-query.js"; import {generateVisibilityQuery} from "../../common/generate-visibility-query.js"; import {generateMutedUserQuery} from "../../common/generate-muted-user-query.js"; import {generateBlockedUserQuery} from "../../common/generate-block-query.js"; -import {makePaginationQuery} from "@/server/api/common/make-pagination-query.js"; -import {Note} from "@/models/entities/note.js"; export const meta = { tags: ["users", "notes"], @@ -67,31 +66,7 @@ export default define(meta, paramDef, async (ps, me) => { throw e; }); - const cte = - Notes.createQueryBuilder("n") - .select(`"n"."id"`, "id") - .andWhere("n.userId = :userId", { userId: user.id }); - - if (ps.withFiles || ps.fileType != null) { - cte.andWhere("n.fileIds != '{}'"); - } - if (!ps.includeReplies) { - cte.andWhere("n.replyId IS NULL"); - } - - if (ps.includeMyRenotes === false) { - cte.andWhere( - new Brackets((qb) => { - qb.orWhere("n.renoteId IS NULL"); - qb.orWhere("n.text IS NOT NULL"); - qb.orWhere("n.fileIds != '{}'"); - qb.orWhere( - '0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = n.id)', - ); - }), - ); - } - + //#region Construct query const query = makePaginationQuery( Notes.createQueryBuilder("note"), ps.sinceId, @@ -99,14 +74,20 @@ export default define(meta, paramDef, async (ps, me) => { ps.sinceDate, ps.untilDate, ) - .addCommonTableExpression(cte, "noteCte"); + .andWhere("note.userId = :userId", { userId: user.id }); - query.andWhere(`"note"."id" IN` + query.subQuery() - .select(`"noteCte"."id"`) - .from("noteCte", "noteCte") - .getQuery()); + generateVisibilityQuery(query, me); + if (me) { + generateMutedUserQuery(query, me, user); + generateBlockedUserQuery(query, me); + } + + if (ps.withFiles) { + query.andWhere("note.fileIds != '{}'"); + } if (ps.fileType != null) { + query.andWhere("note.fileIds != '{}'"); query.andWhere( new Brackets((qb) => { for (const type of ps.fileType!) { @@ -126,10 +107,22 @@ export default define(meta, paramDef, async (ps, me) => { } } - generateVisibilityQuery(query, me); - if (me) { - generateMutedUserQuery(query, me, user); - generateBlockedUserQuery(query, me); + if (!ps.includeReplies) { + query.andWhere("note.replyId IS NULL"); + } + + if (ps.includeMyRenotes === false) { + query.andWhere( + new Brackets((qb) => { + qb.orWhere("note.userId != :userId", { userId: user.id }); + qb.orWhere("note.renoteId IS NULL"); + qb.orWhere("note.text IS NOT NULL"); + qb.orWhere("note.fileIds != '{}'"); + qb.orWhere( + '0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)', + ); + }), + ); } //#endregion