diff --git a/server/models/Document.ts b/server/models/Document.ts index 719c528df8..d90c9cb511 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -106,20 +106,6 @@ type AdditionalFindOptions = { }, })) @Scopes(() => ({ - withCollectionPermissions: (userId: string, paranoid = true) => ({ - include: [ - { - attributes: ["id", "permission", "sharing", "teamId", "deletedAt"], - model: userId - ? Collection.scope({ - method: ["withMembership", userId], - }) - : Collection, - as: "collection", - paranoid, - }, - ], - }), withoutState: { attributes: { exclude: ["state"], @@ -169,13 +155,23 @@ type AdditionalFindOptions = { ], }; }, - withMembership: (userId: string) => { + withMembership: (userId: string, paranoid = true) => { if (!userId) { return {}; } return { include: [ + { + attributes: ["id", "permission", "sharing", "teamId", "deletedAt"], + model: userId + ? Collection.scope({ + method: ["withMembership", userId], + }) + : Collection, + as: "collection", + paranoid, + }, { association: "memberships", where: { @@ -638,21 +634,13 @@ class Document extends ArchivableModel< } static defaultScopeWithUser(userId: string) { - const collectionScope: Readonly = { - method: ["withCollectionPermissions", userId], - }; const viewScope: Readonly = { method: ["withViews", userId], }; const membershipScope: Readonly = { method: ["withMembership", userId], }; - return this.scope([ - "defaultScope", - collectionScope, - viewScope, - membershipScope, - ]); + return this.scope(["defaultScope", viewScope, membershipScope]); } /** @@ -685,14 +673,12 @@ class Document extends ArchivableModel< // almost every endpoint needs the collection membership to determine policy permissions. const scope = this.scope([ "withDrafts", - { - method: ["withCollectionPermissions", userId, rest.paranoid], - }, + options.includeState ? "withState" : "withoutState", { method: ["withViews", userId], }, { - method: ["withMembership", userId], + method: ["withMembership", userId, rest.paranoid], }, ]); @@ -750,9 +736,6 @@ class Document extends ArchivableModel< const user = userId ? await User.findByPk(userId) : null; const documents = await this.scope([ "withDrafts", - { - method: ["withCollectionPermissions", userId, rest.paranoid], - }, { method: ["withViews", userId], }, diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index e8a85d83b0..24aca16a8d 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -182,18 +182,7 @@ export default class SearchHelper { }, ]; - return Document.scope([ - "withDrafts", - { - method: ["withViews", user.id], - }, - { - method: ["withCollectionPermissions", user.id], - }, - { - method: ["withMembership", user.id], - }, - ]).findAll({ + return Document.defaultScopeWithUser(user.id).findAll({ where, subQuery: false, order: [["updatedAt", "DESC"]], @@ -273,18 +262,7 @@ export default class SearchHelper { // Final query to get associated document data const [documents, count] = await Promise.all([ - Document.scope([ - "withDrafts", - { - method: ["withViews", user.id], - }, - { - method: ["withCollectionPermissions", user.id], - }, - { - method: ["withMembership", user.id], - }, - ]).findAll({ + Document.defaultScopeWithUser(user.id).findAll({ where: { teamId: user.teamId, id: map(results, "id"), diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 084e766e0e..2919f1d376 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -397,15 +397,11 @@ router.post( const membershipScope: Readonly = { method: ["withMembership", user.id], }; - const collectionScope: Readonly = { - method: ["withCollectionPermissions", user.id], - }; const viewScope: Readonly = { method: ["withViews", user.id], }; const documents = await Document.scope([ membershipScope, - collectionScope, viewScope, "withDrafts", ]).findAll({ @@ -2033,13 +2029,7 @@ router.post( const collectionIds = await user.collectionIds({ paranoid: false, }); - const collectionScope: Readonly = { - method: ["withCollectionPermissions", user.id], - }; - const documents = await Document.scope([ - collectionScope, - "withDrafts", - ]).findAll({ + const documents = await Document.defaultScopeWithUser(user.id).findAll({ attributes: ["id"], where: { deletedAt: { diff --git a/server/routes/api/groupMemberships/groupMemberships.ts b/server/routes/api/groupMemberships/groupMemberships.ts index 4fdd3f239c..fa544dc0f6 100644 --- a/server/routes/api/groupMemberships/groupMemberships.ts +++ b/server/routes/api/groupMemberships/groupMemberships.ts @@ -24,6 +24,7 @@ router.post( async (ctx: APIContext) => { const { groupId } = ctx.input.body; const { user } = ctx.state.auth; + const userId = user.id; const memberships = await GroupMembership.findAll({ where: { @@ -44,7 +45,7 @@ router.post( association: "groupUsers", required: true, where: { - userId: user.id, + userId, }, }, ], @@ -57,11 +58,7 @@ router.post( const documentIds = memberships .map((p) => p.documentId) .filter(Boolean) as string[]; - const documents = await Document.scope([ - "withDrafts", - { method: ["withMembership", user.id] }, - { method: ["withCollectionPermissions", user.id] }, - ]).findAll({ + const documents = await Document.defaultScopeWithUser(userId).findAll({ where: { id: documentIds, }, diff --git a/server/routes/api/userMemberships/userMemberships.ts b/server/routes/api/userMemberships/userMemberships.ts index f2ea70c647..962b8a2e33 100644 --- a/server/routes/api/userMemberships/userMemberships.ts +++ b/server/routes/api/userMemberships/userMemberships.ts @@ -1,5 +1,4 @@ import Router from "koa-router"; - import { Op, Sequelize } from "sequelize"; import auth from "@server/middlewares/authentication"; import { transaction } from "@server/middlewares/transaction"; @@ -46,14 +45,8 @@ router.post( const documentIds = memberships .map((p) => p.documentId) .filter(Boolean) as string[]; - const documents = await Document.scope([ - "withDrafts", - { method: ["withMembership", user.id] }, - { method: ["withCollectionPermissions", user.id] }, - ]).findAll({ - where: { - id: documentIds, - }, + const documents = await Document.findByIds(documentIds, { + userId: user.id, }); const policies = presentPolicies(user, [...documents, ...memberships]); diff --git a/server/utils/permissions.ts b/server/utils/permissions.ts index 2737f99add..397af7a041 100644 --- a/server/utils/permissions.ts +++ b/server/utils/permissions.ts @@ -99,10 +99,7 @@ export const getDocumentPermission = async ({ documentId: string; skipMembershipId?: string; }): Promise => { - const document = await Document.scope({ - method: ["withCollectionPermissions", userId], - }).findOne({ where: { id: documentId } }); - + const document = await Document.findByPk(documentId, { userId }); const permissions: DocumentPermission[] = []; const collection = document?.collection;