From ea4fbdb7bb7aa26ee54808ff1b344f6869366d07 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 12 Mar 2026 22:09:31 -0400 Subject: [PATCH] fix: Filter relationships returned from list endpoint (#11738) * fix: Filter relationships returned from list endpoint * fix: BacklinksProcessor does not check teamId * Port from upstream --- app/scenes/Collection/components/Overview.tsx | 2 +- app/scenes/Document/components/Editor.tsx | 2 +- app/scenes/Document/components/References.tsx | 2 +- app/stores/DocumentsStore.ts | 41 ++++++++++--- .../processors/BacklinksProcessor.test.ts | 61 +++++++++++++++---- .../queues/processors/BacklinksProcessor.ts | 16 +++-- .../api/relationships/relationships.test.ts | 2 +- .../routes/api/relationships/relationships.ts | 14 +++-- 8 files changed, 108 insertions(+), 32 deletions(-) diff --git a/app/scenes/Collection/components/Overview.tsx b/app/scenes/Collection/components/Overview.tsx index aaa91b62cd..e26032a45f 100644 --- a/app/scenes/Collection/components/Overview.tsx +++ b/app/scenes/Collection/components/Overview.tsx @@ -61,7 +61,7 @@ function Overview({ collection, readOnly }: Props) { () => ({ padding: "0 32px", margin: "0 -32px", - paddingBottom: `calc(50vh - ${childOffsetHeight}px)`, + paddingBottom: `calc(30vh - ${childOffsetHeight}px)`, }), [childOffsetHeight] ); diff --git a/app/scenes/Document/components/Editor.tsx b/app/scenes/Document/components/Editor.tsx index 4b3d04e23b..457bdb90af 100644 --- a/app/scenes/Document/components/Editor.tsx +++ b/app/scenes/Document/components/Editor.tsx @@ -172,7 +172,7 @@ function DocumentEditor(props: Props, ref: React.RefObject) { () => ({ padding: "0 32px", margin: "0 -32px", - paddingBottom: `calc(50vh - ${childOffsetHeight}px)`, + paddingBottom: `calc(30vh - ${childOffsetHeight}px)`, }), [childOffsetHeight] ); diff --git a/app/scenes/Document/components/References.tsx b/app/scenes/Document/components/References.tsx index b1434e3ec0..58266669a2 100644 --- a/app/scenes/Document/components/References.tsx +++ b/app/scenes/Document/components/References.tsx @@ -30,7 +30,7 @@ function References({ document }: Props) { useEffect(() => { if (!isShare) { - void documents.fetchBacklinks(document.id); + void documents.fetchRelationships(document.id); } }, [isShare, documents, document.id]); diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index 2fdb0a4532..95bed1e481 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -53,6 +53,9 @@ export default class DocumentsStore extends Store { @observable backlinks: Map = new Map(); + @observable + similar: Map = new Map(); + @observable movingDocumentId: string | null | undefined; @@ -254,16 +257,27 @@ export default class DocumentsStore extends Store { } @action - fetchBacklinks = async (documentId: string): Promise => { - const documents = await this.fetchAll({ - backlinkDocumentId: documentId, - }); + fetchRelationships = async (documentId: string): Promise => { + const res = await client.post("/relationships.list", { documentId }); + invariant(res?.data, "Relationships not available"); - runInAction("DocumentsStore#fetchBacklinks", () => { - this.backlinks.set( - documentId, - documents.map((doc) => doc.id) - ); + runInAction("DocumentsStore#fetchRelationships", () => { + res.data.documents.forEach(this.add); + this.addPolicies(res.policies); + + const backlinkIds: string[] = []; + const similarIds: string[] = []; + + for (const relationship of res.data.relationships) { + if (relationship.type === "backlink") { + backlinkIds.push(relationship.reverseDocumentId); + } else if (relationship.type === "similar") { + similarIds.push(relationship.reverseDocumentId); + } + } + + this.backlinks.set(documentId, backlinkIds); + this.similar.set(documentId, similarIds); }); }; @@ -276,6 +290,15 @@ export default class DocumentsStore extends Store { ); } + getSimilarDocuments(documentId: string): Document[] { + const documentIds = this.similar.get(documentId) || []; + return orderBy( + compact(documentIds.map((id) => this.data.get(id))), + "title", + "asc" + ); + } + @action fetchChildDocuments = async (documentId: string): Promise => { const res = await client.post(`/documents.list`, { diff --git a/server/queues/processors/BacklinksProcessor.test.ts b/server/queues/processors/BacklinksProcessor.test.ts index 332d261eb6..88994d4929 100644 --- a/server/queues/processors/BacklinksProcessor.test.ts +++ b/server/queues/processors/BacklinksProcessor.test.ts @@ -1,7 +1,7 @@ import { parser } from "@server/editor"; import { Relationship } from "@server/models"; import { RelationshipType } from "@server/models/Relationship"; -import { buildDocument } from "@server/test/factories"; +import { buildDocument, buildTeam } from "@server/test/factories"; import BacklinksProcessor from "./BacklinksProcessor"; @@ -9,8 +9,10 @@ const ip = "127.0.0.1"; describe("documents.publish", () => { it("should create new backlink records", async () => { - const otherDocument = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); const document = await buildDocument({ + teamId: team.id, text: `[this is a link](${otherDocument.url})`, }); @@ -33,9 +35,11 @@ describe("documents.publish", () => { }); it("should not fail when linked document is destroyed", async () => { - const otherDocument = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); await otherDocument.destroy(); const document = await buildDocument({ + teamId: team.id, version: 0, text: `[ ] checklist item`, }); @@ -61,12 +65,41 @@ describe("documents.publish", () => { }); expect(backlinks.length).toBe(0); }); + + it("should not create backlink records for cross-team links", async () => { + const teamA = await buildTeam(); + const teamB = await buildTeam(); + const otherDocument = await buildDocument({ teamId: teamB.id }); + const document = await buildDocument({ + teamId: teamA.id, + text: `[this is a link](${otherDocument.url})`, + }); + + const processor = new BacklinksProcessor(); + await processor.perform({ + name: "documents.publish", + documentId: document.id, + collectionId: document.collectionId!, + teamId: document.teamId, + actorId: document.createdById, + ip, + }); + const backlinks = await Relationship.findAll({ + where: { + reverseDocumentId: document.id, + type: RelationshipType.Backlink, + }, + }); + expect(backlinks.length).toBe(0); + }); }); describe("documents.update", () => { it("should not fail on a document with no previous revisions", async () => { - const otherDocument = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); const document = await buildDocument({ + teamId: team.id, text: `[this is a link](${otherDocument.url})`, }); @@ -91,8 +124,10 @@ describe("documents.update", () => { }); it("should not fail when previous revision is different document version", async () => { - const otherDocument = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); const document = await buildDocument({ + teamId: team.id, version: undefined, text: `[ ] checklist item`, }); @@ -122,8 +157,9 @@ describe("documents.update", () => { }); it("should create new backlink records", async () => { - const otherDocument = await buildDocument(); - const document = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); + const document = await buildDocument({ teamId: team.id }); document.content = parser .parse(`[this is a link](${otherDocument.url})`) ?.toJSON(); @@ -150,9 +186,11 @@ describe("documents.update", () => { }); it("should destroy removed backlink records", async () => { - const otherDocument = await buildDocument(); - const yetAnotherDocument = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); + const yetAnotherDocument = await buildDocument({ teamId: team.id }); const document = await buildDocument({ + teamId: team.id, text: `[this is a link](${otherDocument.url}) [this is a another link](${yetAnotherDocument.url})`, @@ -199,8 +237,9 @@ describe("documents.update", () => { describe("documents.delete", () => { it("should destroy related backlinks", async () => { - const otherDocument = await buildDocument(); - const document = await buildDocument(); + const team = await buildTeam(); + const otherDocument = await buildDocument({ teamId: team.id }); + const document = await buildDocument({ teamId: team.id }); document.content = parser .parse(`[this is a link](${otherDocument.url})`) ?.toJSON(); diff --git a/server/queues/processors/BacklinksProcessor.ts b/server/queues/processors/BacklinksProcessor.ts index a4cde822d3..e3d8edb5fe 100644 --- a/server/queues/processors/BacklinksProcessor.ts +++ b/server/queues/processors/BacklinksProcessor.ts @@ -26,10 +26,14 @@ export default class BacklinksProcessor extends BaseProcessor { await Promise.all( linkIds.map(async (linkId) => { const linkedDocument = await Document.findByPk(linkId, { - attributes: ["id"], + attributes: ["id", "teamId"], }); - if (!linkedDocument || linkedDocument.id === event.documentId) { + if ( + !linkedDocument || + linkedDocument.id === event.documentId || + linkedDocument.teamId !== document.teamId + ) { return; } @@ -72,10 +76,14 @@ export default class BacklinksProcessor extends BaseProcessor { await Promise.all( linkIds.map(async (linkId) => { const linkedDocument = await Document.findByPk(linkId, { - attributes: ["id"], + attributes: ["id", "teamId"], }); - if (!linkedDocument || linkedDocument.id === event.documentId) { + if ( + !linkedDocument || + linkedDocument.id === event.documentId || + linkedDocument.teamId !== document.teamId + ) { return; } diff --git a/server/routes/api/relationships/relationships.test.ts b/server/routes/api/relationships/relationships.test.ts index db96327212..79018917b0 100644 --- a/server/routes/api/relationships/relationships.test.ts +++ b/server/routes/api/relationships/relationships.test.ts @@ -414,7 +414,7 @@ describe("#relationships.list", () => { const body = await res.json(); expect(res.status).toEqual(200); - expect(body.data.relationships).toHaveLength(1); + expect(body.data.relationships).toHaveLength(0); expect(body.data.documents).toHaveLength(0); }); }); diff --git a/server/routes/api/relationships/relationships.ts b/server/routes/api/relationships/relationships.ts index 534ca942f2..28c880ad9c 100644 --- a/server/routes/api/relationships/relationships.ts +++ b/server/routes/api/relationships/relationships.ts @@ -77,16 +77,22 @@ router.post( { userId: user.id } ); - const policies = presentPolicies(user, [...documents, ...relationships]); + const documentIds = new Set(documents.map((d) => d.id)); + const filteredRelationships = relationships.filter((relationship) => + documentIds.has( + where.reverseDocumentId + ? relationship.documentId + : relationship.reverseDocumentId + ) + ); ctx.body = { pagination: ctx.state.pagination, data: { - relationships: relationships.map(presentRelationship), + relationships: filteredRelationships.map(presentRelationship), documents: await presentDocuments(ctx, documents), - policies: presentPolicies(user, documents), }, - policies, + policies: presentPolicies(user, [...documents, ...filteredRelationships]), }; } );