From 9b18f62fd016072620105da9acab512bd34cff15 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 27 Nov 2026 09:14:19 -0500 Subject: [PATCH] fix: Respect collectionId filter in documents.list backlink requests (#14779) --- server/routes/api/documents/documents.test.ts | 45 +++++++++++++++++++ server/routes/api/documents/documents.ts | 5 +-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 28ea064eed..8f16432b1f 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1485,6 +1485,51 @@ describe("#documents.list", () => { expect(body.data[0].id).toEqual(anotherDoc.id); }); + it("should filter backlinks by collectionId", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const otherCollection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const inCollection = await buildDocument({ + userId: user.id, + teamId: user.teamId, + collectionId: collection.id, + }); + const inOtherCollection = await buildDocument({ + userId: user.id, + teamId: user.teamId, + collectionId: otherCollection.id, + }); + await Relationship.bulkCreate( + [inCollection, inOtherCollection].map((source) => ({ + reverseDocumentId: source.id, + type: RelationshipType.Backlink, + documentId: document.id, + userId: user.id, + })) + ); + const res = await server.post("/api/documents.list", user, { + body: { + backlinkDocumentId: document.id, + collectionId: collection.id, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.map((doc: { id: string }) => doc.id)).toEqual([ + inCollection.id, + ]); + }); + describe("filter DSL", () => { it("should match a simple contains leaf case-insensitively", async () => { const user = await buildUser(); diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 85c6677bc4..b1e58348b4 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -4,7 +4,7 @@ import fs from "fs-extra"; import invariant from "invariant"; import contentDisposition from "content-disposition"; import Router from "@koa/router"; -import { escapeRegExp, has, remove, uniq } from "es-toolkit/compat"; +import { escapeRegExp, uniq } from "es-toolkit/compat"; import mime from "mime-types"; import type { Order, ScopeOptions, WhereOptions } from "sequelize"; import type { Filter } from "@shared/helpers/FilterHelper"; @@ -331,9 +331,6 @@ router.post( ); where[Op.and].push({ id: sourceDocumentIds }); - - // For safety, ensure the collectionId is not set in the query. - remove(where[Op.and], (cond) => has(cond, "collectionId")); } // A filter referencing publishedAt can surface drafts (the replacement