From fe0cb5ef1297ca85612364528cfe39ee5231cc08 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 20 Jan 2024 22:58:37 -0500 Subject: [PATCH] fix: documents.search API does not work with custom search slug --- server/routes/api/documents/documents.test.ts | 14 +++++++++++--- server/routes/api/documents/documents.ts | 7 +++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 9bf2f4c3cc..c537693aa7 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1,3 +1,4 @@ +import { faker } from "@faker-js/faker"; import { addMinutes, subDays } from "date-fns"; import { CollectionPermission, DocumentPermission } from "@shared/types"; import { @@ -1311,28 +1312,35 @@ describe("#documents.search", () => { }); it("should return results using shareId", async () => { + const subdomain = faker.internet.domainWord(); + const team = await buildTeam({ subdomain }); const findableDocument = await buildDocument({ title: "search term", text: "random text", + teamId: team.id, }); await buildDocument({ title: "search term", text: "should not be found", userId: findableDocument.createdById, - teamId: findableDocument.teamId, + teamId: team.id, }); const share = await buildShare({ includeChildDocuments: true, documentId: findableDocument.id, - teamId: findableDocument.teamId, + teamId: team.id, + urlId: "abc123", }); const res = await server.post("/api/documents.search", { body: { query: "search term", - shareId: share.id, + shareId: "abc123", + }, + headers: { + host: `${subdomain}.outline.dev`, }, }); diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index cda70a4d86..8ea3a39a7b 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -791,15 +791,18 @@ router.post( let teamId; let response; + let share; if (shareId) { const teamFromCtx = await getTeamFromContext(ctx); - const { share, document } = await documentLoader({ + const { document, ...loaded } = await documentLoader({ teamId: teamFromCtx?.id, shareId, user, }); + share = loaded.share; + if (!share?.includeChildDocuments) { throw InvalidRequestError("Child documents cannot be searched"); } @@ -868,7 +871,7 @@ router.post( await SearchQuery.create({ userId: user?.id, teamId, - shareId, + shareId: share?.id, source: ctx.state.auth.type || "app", // we'll consider anything that isn't "api" to be "app" query, results: totalCount,