mirror of
https://github.com/outline/outline.git
synced 2026-12-03 18:10:36 +03:00
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
This commit is contained in:
1 parent
d3eb3db7ba
commit
dedc684229
4 files changed
+111
-110
No files matched your search
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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<Share> = {
|
||||
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 [];
|
||||
|
||||
+104
-1
@@ -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<Share> = {
|
||||
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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user