mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
perf: Avoid loading collection structure when fetching document shares (#14611)
* perf: Avoid loading collection structure when fetching document shares * fix: Do not return inherited shares for archived documents
This commit is contained in:
1 parent
e463e5565b
commit
bb7372ab05
3 files changed
+174
-26
No files matched your search
@@ -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 });
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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";'
|
||||
);
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user