From dedc68422966da045e36c6cfc9b60632910c9e4b Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Sun, 3 Aug 2025 17:23:44 +0000 Subject: [PATCH] Refactor loadShareWithParent to Share.findWithParent static method - Move loadShareWithParent function from shareLoader.ts to Share model as static method findWithParent - Update imports and usage in routes/api/shares/shares.ts - Update tests to use new static method - Remove old function and clean up unused imports - Maintains exact same functionality and behavior Fixes #9802 --- server/commands/shareLoader.test.ts | 7 +- server/commands/shareLoader.ts | 102 +-------------------------- server/models/Share.ts | 105 +++++++++++++++++++++++++++- server/routes/api/shares/shares.ts | 7 +- 4 files changed, 111 insertions(+), 110 deletions(-) diff --git a/server/commands/shareLoader.test.ts b/server/commands/shareLoader.test.ts index 1c483a51d3..bf51b5b58c 100644 --- a/server/commands/shareLoader.test.ts +++ b/server/commands/shareLoader.test.ts @@ -5,7 +5,8 @@ import { buildTeam, buildUser, } from "@server/test/factories"; -import { loadPublicShare, loadShareWithParent } from "./shareLoader"; +import { Share } from "@server/models"; +import { loadPublicShare } from "./shareLoader"; describe("shareLoader", () => { describe("collection share", () => { @@ -58,7 +59,7 @@ describe("shareLoader", () => { collectionId: collection.id, }); - const result = await loadShareWithParent({ + const result = await Share.findWithParent({ collectionId: collection.id, user, }); @@ -192,7 +193,7 @@ describe("shareLoader", () => { }), ]); - const result = await loadShareWithParent({ + const result = await Share.findWithParent({ documentId: childDocument.id, user, }); diff --git a/server/commands/shareLoader.ts b/server/commands/shareLoader.ts index bb9b59e1f6..bca5184bb4 100644 --- a/server/commands/shareLoader.ts +++ b/server/commands/shareLoader.ts @@ -3,8 +3,7 @@ import isUUID from "validator/lib/isUUID"; import { NavigationNode } from "@shared/types"; import { UrlHelper } from "@shared/utils/UrlHelper"; import { AuthorizationError, NotFoundError } from "@server/errors"; -import { Collection, Document, Share, User } from "@server/models"; -import { authorize, can } from "@server/policies"; +import { Collection, Document, Share } from "@server/models"; type LoadPublicShareProps = { id: string; @@ -122,105 +121,6 @@ export async function loadPublicShare({ }; } -type LoadShareWithParentProps = { - collectionId?: string; - documentId?: string; - user: User; -}; - -export async function loadShareWithParent({ - collectionId, - documentId, - user, -}: LoadShareWithParentProps) { - const where: WhereOptions = { - revokedAt: { - [Op.is]: null, - }, - teamId: user.teamId, - }; - - if (collectionId) { - where.collectionId = collectionId; - } else if (documentId) { - where.documentId = documentId; - } - - const share = await Share.scope({ - method: ["withCollectionPermissions", user.id], - }).findOne({ where }); - - if (!share) { - throw NotFoundError(); - } - - authorize(user, "read", share); - - if (collectionId) { - authorize(user, "read", share.collection); - } - - let parentShare: Share | null = null; - - // Load the parent shares and return one (needed for share toggle in UI). - // Parent share is needed for documents only since collections don't have parents. - if (documentId) { - authorize(user, "read", share.document); - - const docCollectionId = share.document.collectionId; - - if (!docCollectionId) { - throw NotFoundError("Collection not found for the shared document"); - } - - const docCollection = await Collection.findByPk(docCollectionId, { - userId: user.id, - includeDocumentStructure: true, - rejectOnEmpty: true, - }); - - const collectionShare = await Share.scope({ - method: ["withCollectionPermissions", user.id], - }).findOne({ - where: { - revokedAt: { - [Op.is]: null, - }, - published: true, - teamId: user.teamId, - collectionId: docCollectionId, - }, - }); - - // prefer collection share if it exists and user has read access. - if (collectionShare && can(user, "read", collectionShare)) { - parentShare = collectionShare; - } else { - const parentDocIds = docCollection.getDocumentParents(documentId); - - const allParentShares = parentDocIds - ? await Share.scope({ - method: ["withCollectionPermissions", user.id], - }).findAll({ - where: { - revokedAt: { - [Op.is]: null, - }, - published: true, - teamId: user.teamId, - includeChildDocuments: true, - documentId: parentDocIds, - }, - }) - : null; - - parentShare = allParentShares?.find((s) => can(user, "read", s)) ?? null; - } - } - - return { share, parentShare }; -} - function getAllIdsInSharedTree(sharedTree: NavigationNode | null): string[] { if (!sharedTree) { return []; diff --git a/server/models/Share.ts b/server/models/Share.ts index 03219ac8c3..6833dc555d 100644 --- a/server/models/Share.ts +++ b/server/models/Share.ts @@ -2,6 +2,8 @@ import { InferAttributes, InferCreationAttributes, type SaveOptions, + Op, + WhereOptions, } from "sequelize"; import { ForeignKey, @@ -19,8 +21,9 @@ import { } from "sequelize-typescript"; import { UrlHelper } from "@shared/utils/UrlHelper"; import env from "@server/env"; -import { ValidationError } from "@server/errors"; +import { NotFoundError, ValidationError } from "@server/errors"; import { APIContext } from "@server/types"; +import { authorize, can } from "@server/policies"; import Collection from "./Collection"; import Document from "./Document"; import Team from "./Team"; @@ -149,6 +152,106 @@ class Share extends IdModel< @Column showLastUpdated: boolean; + // static methods + + static async findWithParent({ + collectionId, + documentId, + user, + }: { + collectionId?: string; + documentId?: string; + user: User; + }) { + const where: WhereOptions = { + revokedAt: { + [Op.is]: null, + }, + teamId: user.teamId, + }; + + if (collectionId) { + where.collectionId = collectionId; + } else if (documentId) { + where.documentId = documentId; + } + + const share = await Share.scope({ + method: ["withCollectionPermissions", user.id], + }).findOne({ where }); + + if (!share) { + throw NotFoundError(); + } + + authorize(user, "read", share); + + if (collectionId) { + authorize(user, "read", share.collection); + } + + let parentShare: Share | null = null; + + // Load the parent shares and return one (needed for share toggle in UI). + // Parent share is needed for documents only since collections don't have parents. + if (documentId) { + authorize(user, "read", share.document); + + const docCollectionId = share.document.collectionId; + + if (!docCollectionId) { + throw NotFoundError("Collection not found for the shared document"); + } + + const docCollection = await Collection.findByPk(docCollectionId, { + userId: user.id, + includeDocumentStructure: true, + rejectOnEmpty: true, + }); + + const collectionShare = await Share.scope({ + method: ["withCollectionPermissions", user.id], + }).findOne({ + where: { + revokedAt: { + [Op.is]: null, + }, + published: true, + teamId: user.teamId, + collectionId: docCollectionId, + }, + }); + + // prefer collection share if it exists and user has read access. + if (collectionShare && can(user, "read", collectionShare)) { + parentShare = collectionShare; + } else { + const parentDocIds = docCollection.getDocumentParents(documentId); + + const allParentShares = parentDocIds + ? await Share.scope({ + method: ["withCollectionPermissions", user.id], + }).findAll({ + where: { + revokedAt: { + [Op.is]: null, + }, + published: true, + teamId: user.teamId, + includeChildDocuments: true, + documentId: parentDocIds, + }, + }) + : null; + + parentShare = + allParentShares?.find((s) => can(user, "read", s)) ?? null; + } + } + + return { share, parentShare }; + } + // hooks @BeforeUpdate diff --git a/server/routes/api/shares/shares.ts b/server/routes/api/shares/shares.ts index 9eef9d2759..ae46fdd90a 100644 --- a/server/routes/api/shares/shares.ts +++ b/server/routes/api/shares/shares.ts @@ -22,10 +22,7 @@ import { getTeamFromContext } from "@server/utils/passport"; import { navigationNodeToSitemap } from "@server/utils/sitemap"; import pagination from "../middlewares/pagination"; import * as T from "./schema"; -import { - loadPublicShare, - loadShareWithParent, -} from "@server/commands/shareLoader"; +import { loadPublicShare } from "@server/commands/shareLoader"; const router = new Router(); @@ -96,7 +93,7 @@ router.post( // load share with parent for displaying in the share popovers. - const { share, parentShare } = await loadShareWithParent({ + const { share, parentShare } = await Share.findWithParent({ collectionId, documentId, user,