JSON to client (#5553)

This commit is contained in:
Tom Moor
2024-05-24 08:29:00 -04:00
committed by GitHub
parent e1e8257df7
commit d51267b8bc
71 changed files with 651 additions and 378 deletions

View File

@@ -17,7 +17,7 @@ import {
User,
GroupPermission,
} from "@server/models";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import {
buildShare,
buildCollection,
@@ -3467,7 +3467,6 @@ describe("#documents.update", () => {
token: user.getJwtToken(),
id: document.id,
title: document.title,
text: document.text,
},
});
expect(res.status).toEqual(200);

View File

@@ -45,7 +45,8 @@ import {
UserMembership,
} from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import SearchHelper from "@server/models/helpers/SearchHelper";
import { authorize, cannot } from "@server/policies";
import {
@@ -63,7 +64,6 @@ import FileStorage from "@server/storage/files";
import { APIContext } from "@server/types";
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
import ZipHelper from "@server/utils/ZipHelper";
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
import { getTeamFromContext } from "@server/utils/passport";
import { assertPresent } from "@server/validation";
import pagination from "../middlewares/pagination";
@@ -191,7 +191,7 @@ router.post(
}
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
const policies = presentPolicies(user, documents);
ctx.body = {
@@ -224,7 +224,7 @@ router.post(
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
const policies = presentPolicies(user, documents);
@@ -287,7 +287,7 @@ router.post(
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
const policies = presentPolicies(user, documents);
@@ -343,7 +343,7 @@ router.post(
return document;
});
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
const policies = presentPolicies(user, documents);
@@ -399,7 +399,7 @@ router.post(
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
const policies = presentPolicies(user, documents);
@@ -416,8 +416,9 @@ router.post(
auth({ optional: true }),
validate(T.DocumentsInfoSchema),
async (ctx: APIContext<T.DocumentsInfoReq>) => {
const { id, shareId, apiVersion } = ctx.input.body;
const { id, shareId } = ctx.input.body;
const { user } = ctx.state.auth;
const apiVersion = getAPIVersion(ctx);
const teamFromCtx = await getTeamFromContext(ctx);
const { document, share, collection } = await documentLoader({
id,
@@ -426,7 +427,7 @@ router.post(
teamId: teamFromCtx?.id,
});
const isPublic = cannot(user, "read", document);
const serializedDocument = await presentDocument(document, {
const serializedDocument = await presentDocument(ctx, document, {
isPublic,
});
@@ -435,7 +436,7 @@ router.post(
// Passing apiVersion=2 has a single effect, to change the response payload to
// include top level keys for document, sharedTree, and team.
const data =
apiVersion === 2
apiVersion >= 2
? {
document: serializedDocument,
team: team?.getPreference(TeamPreference.PublicBranding)
@@ -572,7 +573,9 @@ router.post(
contentType === "text/markdown" ? "md" : mime.extension(contentType);
const fileName = slugify(document.titleWithDefault);
const attachmentIds = parseAttachmentIds(document.text);
const attachmentIds = ProsemirrorHelper.parseAttachmentIds(
DocumentHelper.toProsemirror(document)
);
const attachments = attachmentIds.length
? await Attachment.findAll({
where: {
@@ -729,7 +732,7 @@ router.post(
}
ctx.body = {
data: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document]),
};
}
@@ -769,7 +772,7 @@ router.post(
});
const policies = presentPolicies(user, documents);
const data = await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
);
ctx.body = {
@@ -895,7 +898,7 @@ router.post(
const data = await Promise.all(
results.map(async (result) => {
const document = await presentDocument(result.document);
const document = await presentDocument(ctx, result.document);
return { ...result, document };
})
);
@@ -982,7 +985,7 @@ router.post(
invariant(reloaded, "document not found");
ctx.body = {
data: await presentDocument(reloaded),
data: await presentDocument(ctx, reloaded),
policies: presentPolicies(user, [reloaded]),
};
}
@@ -995,9 +998,10 @@ router.post(
transaction(),
async (ctx: APIContext<T.DocumentsUpdateReq>) => {
const { transaction } = ctx.state;
const { id, apiVersion, insightsEnabled, publish, collectionId, ...input } =
const { id, insightsEnabled, publish, collectionId, ...input } =
ctx.input.body;
const editorVersion = ctx.headers["x-editor-version"] as string | undefined;
const { user } = ctx.state.auth;
let collection: Collection | null | undefined;
@@ -1052,15 +1056,7 @@ router.post(
document.collection = collection;
ctx.body = {
data:
apiVersion === 2
? {
document: await presentDocument(document),
collection: collection
? presentCollection(collection)
: undefined,
}
: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document, collection]),
};
}
@@ -1120,7 +1116,7 @@ router.post(
ctx.body = {
data: {
documents: await Promise.all(
response.map((document) => presentDocument(document))
response.map((document) => presentDocument(ctx, document))
),
},
policies: presentPolicies(user, response),
@@ -1173,7 +1169,7 @@ router.post(
ctx.body = {
data: {
documents: await Promise.all(
documents.map((document) => presentDocument(document))
documents.map((document) => presentDocument(ctx, document))
),
collections: await Promise.all(
collections.map((collection) => presentCollection(collection))
@@ -1211,7 +1207,7 @@ router.post(
});
ctx.body = {
data: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document]),
};
}
@@ -1276,7 +1272,7 @@ router.post(
auth(),
validate(T.DocumentsUnpublishSchema),
async (ctx: APIContext<T.DocumentsUnpublishReq>) => {
const { id, apiVersion } = ctx.input.body;
const { id } = ctx.input.body;
const { user } = ctx.state.auth;
const document = await Document.findByPk(id, {
@@ -1309,15 +1305,7 @@ router.post(
});
ctx.body = {
data:
apiVersion === 2
? {
document: await presentDocument(document),
collection: document.collection
? presentCollection(document.collection)
: undefined,
}
: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document]),
};
}
@@ -1395,7 +1383,7 @@ router.post(
});
ctx.body = {
data: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document]),
};
}
@@ -1481,7 +1469,7 @@ router.post(
document.collection = collection;
ctx.body = {
data: await presentDocument(document),
data: await presentDocument(ctx, document),
policies: presentPolicies(user, [document]),
};
}
@@ -1760,4 +1748,16 @@ router.post(
}
);
// Remove this helper once apiVersion is removed (#6175)
function getAPIVersion(ctx: APIContext) {
return Number(
ctx.headers["x-api-version"] ??
(typeof ctx.input.body === "object" &&
ctx.input.body &&
"apiVersion" in ctx.input.body &&
ctx.input.body.apiVersion) ??
0
);
}
export default router;

View File

@@ -118,7 +118,7 @@ export const DocumentsInfoSchema = BaseSchema.extend({
.refine((val) => isUUID(val) || UrlHelper.SHARE_URL_SLUG_REGEX.test(val))
.optional(),
/** Version of the API to be used */
/** @deprecated Version of the API to be used, remove in a few releases */
apiVersion: z.number().optional(),
}),
}).refine((req) => !(isEmpty(req.body.id) && isEmpty(req.body.shareId)), {
@@ -241,7 +241,7 @@ export const DocumentsUpdateSchema = BaseSchema.extend({
/** Boolean to denote if text should be appended */
append: z.boolean().optional(),
/** Version of the API to be used */
/** @deprecated Version of the API to be used, remove in a few releases */
apiVersion: z.number().optional(),
/** Whether the editing session is complete */
@@ -287,7 +287,7 @@ export type DocumentsDeleteReq = z.infer<typeof DocumentsDeleteSchema>;
export const DocumentsUnpublishSchema = BaseSchema.extend({
body: BaseIdSchema.extend({
/** Version of the API to be used */
/** @deprecated Version of the API to be used, remove in a few releases */
apiVersion: z.number().optional(),
}),
});

View File

@@ -117,7 +117,9 @@ router.post(
pagination: { ...ctx.state.pagination, total },
data: {
notifications: await Promise.all(
notifications.map(presentNotification)
notifications.map((notification) =>
presentNotification(ctx, notification)
)
),
unseen,
},
@@ -172,7 +174,7 @@ router.post(
});
ctx.body = {
data: await presentNotification(notification),
data: await presentNotification(ctx, notification),
policies: presentPolicies(user, [notification]),
};
}

View File

@@ -96,7 +96,7 @@ router.post(
data: {
pins: pins.map(presentPin),
documents: await Promise.all(
documents.map((document: Document) => presentDocument(document))
documents.map((document: Document) => presentDocument(ctx, document))
),
},
policies,

View File

@@ -105,7 +105,23 @@ describe("#revisions.diff", () => {
});
await Revision.createFromDocument(document);
await document.update({ text: "New text" });
await document.update({
content: {
type: "doc",
content: [
{
type: "paragraph",
content: [
{
content: [],
type: "text",
text: "New text",
},
],
},
],
},
});
const revision1 = await Revision.createFromDocument(document);
const res = await server.post("/api/revisions.diff", {

View File

@@ -6,7 +6,7 @@ import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import validate from "@server/middlewares/validate";
import { Document, Revision } from "@server/models";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { authorize } from "@server/policies";
import { presentRevision } from "@server/presenters";
import { APIContext } from "@server/types";

View File

@@ -112,7 +112,7 @@ router.post(
data: {
stars: stars.map(presentStar),
documents: await Promise.all(
documents.map((document: Document) => presentDocument(document))
documents.map((document: Document) => presentDocument(ctx, document))
),
},
policies,

View File

@@ -63,7 +63,7 @@ router.post(
data: {
memberships: memberships.map(presentMembership),
documents: await Promise.all(
documents.map((document: Document) => presentDocument(document))
documents.map((document: Document) => presentDocument(ctx, document))
),
},
policies,