mirror of
https://github.com/outline/outline.git
synced 2026-02-20 19:39:24 -06:00
* Use `shareLoader` for loading public docs in `documents.info` endpoint * cleanup sharedCache in documents store * remove shareId path param for authenticated routes * require auth in document loader --------- Co-authored-by: Tom Moor <tom@getoutline.com>
43 lines
854 B
TypeScript
43 lines
854 B
TypeScript
import { NotFoundError, PaymentRequiredError } from "@server/errors";
|
|
import { Document, User } from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
|
|
type Props = {
|
|
id: string;
|
|
user: User;
|
|
includeState?: boolean;
|
|
};
|
|
|
|
export default async function loadDocument({
|
|
id,
|
|
user,
|
|
includeState,
|
|
}: Props): Promise<Document> {
|
|
const document = await Document.findByPk(id, {
|
|
userId: user ? user.id : undefined,
|
|
paranoid: false,
|
|
includeState,
|
|
});
|
|
|
|
if (!document) {
|
|
throw NotFoundError();
|
|
}
|
|
|
|
if (document.deletedAt) {
|
|
// don't send data if user cannot restore deleted doc
|
|
if (user) {
|
|
authorize(user, "restore", document);
|
|
}
|
|
} else {
|
|
if (user) {
|
|
authorize(user, "read", document);
|
|
}
|
|
}
|
|
|
|
if (document.isTrialImport) {
|
|
throw PaymentRequiredError();
|
|
}
|
|
|
|
return document;
|
|
}
|