diff --git a/server/commands/shareLoader.test.ts b/server/commands/shareLoader.test.ts index 9bfdb58194..1585218838 100644 --- a/server/commands/shareLoader.test.ts +++ b/server/commands/shareLoader.test.ts @@ -347,6 +347,134 @@ describe("shareLoader", () => { expect(result.parentShare?.id).toEqual(parentShare.id); }); + it("should return parentShare from a grandparent document", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + const childDocument = await buildDocument({ + parentDocumentId: document.id, + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + const grandchildDocument = await buildDocument({ + parentDocumentId: childDocument.id, + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + const parentShare = await buildShare({ + includeChildDocuments: true, + userId: user.id, + teamId: user.teamId, + documentId: document.id, + }); + + const result = await loadShareWithParent({ + documentId: grandchildDocument.id, + user, + }); + + expect(result.share).toBeNull(); + expect(result.parentShare?.id).toEqual(parentShare.id); + }); + + it("should not return a parent document share for a draft", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + const draft = await buildDocument({ + parentDocumentId: document.id, + collectionId: collection.id, + publishedAt: null, + userId: user.id, + teamId: user.teamId, + }); + await buildShare({ + includeChildDocuments: true, + userId: user.id, + teamId: user.teamId, + documentId: document.id, + }); + + const result = await loadShareWithParent({ + documentId: draft.id, + user, + }); + + expect(result.share).toBeNull(); + expect(result.parentShare).toBeNull(); + }); + + it("should not return a parent document share for an archived document", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + const archived = await buildDocument({ + parentDocumentId: document.id, + collectionId: collection.id, + archivedAt: new Date(), + userId: user.id, + teamId: user.teamId, + }); + await buildShare({ + includeChildDocuments: true, + userId: user.id, + teamId: user.teamId, + documentId: document.id, + }); + + const result = await loadShareWithParent({ + documentId: archived.id, + user, + }); + + expect(result.share).toBeNull(); + expect(result.parentShare).toBeNull(); + }); + + it("should return no shares for a top-level document without a share", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + collectionId: collection.id, + userId: user.id, + teamId: user.teamId, + }); + + const result = await loadShareWithParent({ + documentId: document.id, + user, + }); + + expect(result.share).toBeNull(); + expect(result.parentShare).toBeNull(); + }); + it("should not return parentShare when the user cannot read the document", async () => { const admin = await buildUser(); const member = await buildUser({ teamId: admin.teamId }); diff --git a/server/commands/shareLoader.ts b/server/commands/shareLoader.ts index 567e271a42..f7746b2c7a 100644 --- a/server/commands/shareLoader.ts +++ b/server/commands/shareLoader.ts @@ -200,12 +200,12 @@ export async function loadShareWithParent({ // Load the parent shares and return one (needed for share toggle in UI). if (docCollectionId) { - const [docCollection, collectionShare] = await Promise.all([ - Collection.findByPk(docCollectionId, { - userId: user.id, - includeDocumentStructure: true, - rejectOnEmpty: true, - }), + const [parentDocIds, collectionShare] = await Promise.all([ + // Drafts and archived documents are not in the shared tree, so they + // cannot inherit a parent share. + document.isDraft || document.archivedAt + ? [] + : document.findAllParentDocumentIds(), Share.scope([ { method: ["withCollectionPermissions", user.id] }, "notExpired", @@ -228,28 +228,24 @@ export async function loadShareWithParent({ can(user, "read", collectionShare.collection) ) { parentShare = collectionShare; - } else { - const parentDocIds = docCollection.getDocumentParents(documentId); - - const allParentShares = parentDocIds - ? await Share.scope([ - { method: ["withCollectionPermissions", user.id] }, - "notExpired", - ]).findAll({ - where: { - revokedAt: { - [Op.is]: null, - }, - published: true, - teamId: user.teamId, - includeChildDocuments: true, - documentId: parentDocIds, - }, - }) - : null; + } else if (parentDocIds.length) { + const allParentShares = await Share.scope([ + { method: ["withCollectionPermissions", user.id] }, + "notExpired", + ]).findAll({ + where: { + revokedAt: { + [Op.is]: null, + }, + published: true, + teamId: user.teamId, + includeChildDocuments: true, + documentId: parentDocIds, + }, + }); parentShare = - allParentShares?.find( + allParentShares.find( (s) => can(user, "read", s) && can(user, "read", s.document) ) ?? null; } diff --git a/server/migrations/20261116120000-add-shares-document-collection-indexes.js b/server/migrations/20261116120000-add-shares-document-collection-indexes.js new file mode 100644 index 0000000000..1916186a81 --- /dev/null +++ b/server/migrations/20261116120000-add-shares-document-collection-indexes.js @@ -0,0 +1,24 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface) { + // Covers share lookups by document and collection, which run on every + // document open. + await queryInterface.sequelize.query( + 'CREATE INDEX CONCURRENTLY IF NOT EXISTS "shares_document_id_not_revoked" ON "shares" ("documentId") WHERE "revokedAt" IS NULL;' + ); + await queryInterface.sequelize.query( + 'CREATE INDEX CONCURRENTLY IF NOT EXISTS "shares_collection_id_not_revoked" ON "shares" ("collectionId") WHERE "revokedAt" IS NULL;' + ); + }, + + async down(queryInterface) { + await queryInterface.sequelize.query( + 'DROP INDEX CONCURRENTLY IF EXISTS "shares_collection_id_not_revoked";' + ); + await queryInterface.sequelize.query( + 'DROP INDEX CONCURRENTLY IF EXISTS "shares_document_id_not_revoked";' + ); + }, +};