From 284ce18001020cd1aabb6cc311aef71c688f5263 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Sat, 30 Dec 2023 19:07:41 +0530 Subject: [PATCH] fix: include in documents.search_titles --- server/models/helpers/SearchHelper.ts | 53 ++++++++----- server/routes/api/documents/documents.test.ts | 79 +++++++++++++++++++ 2 files changed, 114 insertions(+), 18 deletions(-) diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index 276d0290b9..f0b4d2f656 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -188,28 +188,37 @@ export default class SearchHelper { // Ensure we're filtering by the users accessible collections. If // collectionId is passed as an option it is assumed that the authorization // has already been done in the router + let collectionIds; + if (options.collectionId) { - where[Op.and].push({ - collectionId: options.collectionId, - }); + collectionIds = [options.collectionId]; } else { - where[Op.and].push({ - [Op.or]: [ - { - collectionId: { - [Op.in]: await user.collectionIds(), - }, - }, - { - collectionId: { - [Op.is]: null, - }, - createdById: user.id, - }, - ], - }); + collectionIds = await user.collectionIds(); } + const documentIds = await user.documentIds(); + + where[Op.and].push({ + [Op.or]: [ + { + collectionId: { + [Op.in]: collectionIds, + }, + }, + { + collectionId: { + [Op.is]: null, + }, + createdById: user.id, + }, + { + id: { + [Op.in]: documentIds, + }, + }, + ], + }); + if (options.dateFilter) { where[Op.and].push({ updatedAt: { @@ -239,6 +248,11 @@ export default class SearchHelper { { createdById: user.id, }, + { + id: { + [Op.in]: documentIds, + }, + }, ], }); } else { @@ -266,6 +280,9 @@ export default class SearchHelper { { method: ["withCollectionPermissions", user.id], }, + { + method: ["withMembership", user.id], + }, ]).findAll({ where, order: [["updatedAt", "DESC"]], diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 3ef8a3137e..94307d600a 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1067,6 +1067,85 @@ describe("#documents.drafts", () => { }); describe("#documents.search_titles", () => { + it("should include individually shared drafts with a user in search results", async () => { + const user = await buildUser(); + // create a private collection + const collection = await buildCollection({ + createdById: user.id, + teamId: user.teamId, + permission: null, + }); + // create a draft in collection + const document = await buildDocument({ + collectionId: collection.id, + teamId: user.teamId, + createdById: user.id, + title: "Some title", + }); + document.publishedAt = null; + await document.save(); + const member = await buildUser({ + teamId: user.teamId, + }); + // add member to the document + await server.post("/api/documents.add_user", { + body: { + token: user.getJwtToken(), + id: document.id, + userId: member.id, + }, + }); + const res = await server.post("/api/documents.search_titles", { + body: { + token: member.getJwtToken(), + query: "title", + includeDrafts: true, + }, + }); + const body = await res.json(); + expect(body.data).toHaveLength(1); + expect(body.data[0].id).toEqual(document.id); + expect(res.status).toEqual(200); + }); + + it("should include individually shared docs with a user in search results", async () => { + const user = await buildUser(); + // create a private collection + const collection = await buildCollection({ + createdById: user.id, + teamId: user.teamId, + permission: null, + }); + // create document in that private collection + const document = await buildDocument({ + collectionId: collection.id, + teamId: user.teamId, + createdById: user.id, + title: "Some title", + }); + const member = await buildUser({ + teamId: user.teamId, + }); + // add member to the document + await server.post("/api/documents.add_user", { + body: { + token: user.getJwtToken(), + id: document.id, + userId: member.id, + }, + }); + const res = await server.post("/api/documents.search_titles", { + body: { + token: member.getJwtToken(), + query: "title", + }, + }); + const body = await res.json(); + expect(body.data).toHaveLength(1); + expect(body.data[0].id).toEqual(document.id); + expect(res.status).toEqual(200); + }); + it("should fail without query", async () => { const user = await buildUser(); const res = await server.post("/api/documents.search_titles", {