Allow viewing diff before revision is written (#5399)
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
import Router from "koa-router";
|
||||
import { Op } from "sequelize";
|
||||
import { ValidationError } from "@server/errors";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { Document, Revision } from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
import { authorize } from "@server/policies";
|
||||
import { presentRevision } from "@server/presenters";
|
||||
import { APIContext } from "@server/types";
|
||||
import slugify from "@server/utils/slugify";
|
||||
import { assertPresent, assertSort, assertUuid } from "@server/validation";
|
||||
import pagination from "./middlewares/pagination";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post("revisions.info", auth(), async (ctx: APIContext) => {
|
||||
const { id } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
const { user } = ctx.state.auth;
|
||||
const revision = await Revision.findByPk(id, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
const document = await Document.findByPk(revision.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
const before = await revision.previous();
|
||||
|
||||
ctx.body = {
|
||||
data: await presentRevision(
|
||||
revision,
|
||||
await DocumentHelper.diff(before, revision, {
|
||||
includeTitle: false,
|
||||
includeStyles: false,
|
||||
})
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
router.post("revisions.diff", auth(), async (ctx: APIContext) => {
|
||||
const { id, compareToId } = ctx.request.body;
|
||||
assertUuid(id, "id is required");
|
||||
|
||||
const { user } = ctx.state.auth;
|
||||
const revision = await Revision.findByPk(id, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
const document = await Document.findByPk(revision.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
let before;
|
||||
if (compareToId) {
|
||||
assertUuid(compareToId, "compareToId must be a UUID");
|
||||
before = await Revision.findOne({
|
||||
where: {
|
||||
id: compareToId,
|
||||
documentId: revision.documentId,
|
||||
createdAt: {
|
||||
[Op.lt]: revision.createdAt,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!before) {
|
||||
throw ValidationError(
|
||||
"Revision could not be found, compareToId must be a revision of the same document before the provided revision"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
before = await revision.previous();
|
||||
}
|
||||
|
||||
const accept = ctx.request.headers["accept"];
|
||||
const content = await DocumentHelper.diff(before, revision);
|
||||
|
||||
if (accept?.includes("text/html")) {
|
||||
ctx.set("Content-Type", "text/html");
|
||||
ctx.set(
|
||||
"Content-Disposition",
|
||||
`attachment; filename="${slugify(document.titleWithDefault)}-${
|
||||
revision.id
|
||||
}.html"`
|
||||
);
|
||||
ctx.body = content;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: content,
|
||||
};
|
||||
});
|
||||
|
||||
router.post("revisions.list", auth(), pagination(), async (ctx: APIContext) => {
|
||||
let { direction } = ctx.request.body;
|
||||
const { documentId, sort = "updatedAt" } = ctx.request.body;
|
||||
if (direction !== "ASC") {
|
||||
direction = "DESC";
|
||||
}
|
||||
assertSort(sort, Revision);
|
||||
assertPresent(documentId, "documentId is required");
|
||||
|
||||
const { user } = ctx.state.auth;
|
||||
const document = await Document.findByPk(documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
const revisions = await Revision.findAll({
|
||||
where: {
|
||||
documentId: document.id,
|
||||
},
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
const data = await Promise.all(
|
||||
revisions.map((revision) => presentRevision(revision))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
1
server/routes/api/revisions/index.ts
Normal file
1
server/routes/api/revisions/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default } from "./revisions";
|
||||
154
server/routes/api/revisions/revisions.ts
Normal file
154
server/routes/api/revisions/revisions.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import Router from "koa-router";
|
||||
import { Op } from "sequelize";
|
||||
import { RevisionHelper } from "@shared/utils/RevisionHelper";
|
||||
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 { authorize } from "@server/policies";
|
||||
import { presentRevision } from "@server/presenters";
|
||||
import { APIContext } from "@server/types";
|
||||
import slugify from "@server/utils/slugify";
|
||||
import pagination from "../middlewares/pagination";
|
||||
import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post(
|
||||
"revisions.info",
|
||||
auth(),
|
||||
validate(T.RevisionsInfoSchema),
|
||||
async (ctx: APIContext<T.RevisionsInfoReq>) => {
|
||||
const { id, documentId } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
let before: Revision | null, after: Revision;
|
||||
|
||||
if (id) {
|
||||
const revision = await Revision.findByPk(id, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
const document = await Document.findByPk(revision.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
after = revision;
|
||||
before = await revision.previous();
|
||||
} else if (documentId) {
|
||||
const document = await Document.findByPk(documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
after = Revision.buildFromDocument(document);
|
||||
after.id = RevisionHelper.latestId(document.id);
|
||||
after.user = document.updatedBy;
|
||||
|
||||
before = await Revision.findLatest(documentId);
|
||||
} else {
|
||||
throw ValidationError("Either id or documentId must be provided");
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: await presentRevision(
|
||||
after,
|
||||
await DocumentHelper.diff(before, after, {
|
||||
includeTitle: false,
|
||||
includeStyles: false,
|
||||
})
|
||||
),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"revisions.diff",
|
||||
auth(),
|
||||
validate(T.RevisionsDiffSchema),
|
||||
async (ctx: APIContext<T.RevisionsDiffReq>) => {
|
||||
const { id, compareToId } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
|
||||
const revision = await Revision.findByPk(id, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
const document = await Document.findByPk(revision.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
let before;
|
||||
if (compareToId) {
|
||||
before = await Revision.findOne({
|
||||
where: {
|
||||
id: compareToId,
|
||||
documentId: revision.documentId,
|
||||
createdAt: {
|
||||
[Op.lt]: revision.createdAt,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!before) {
|
||||
throw ValidationError(
|
||||
"Revision could not be found, compareToId must be a revision of the same document before the provided revision"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
before = await revision.previous();
|
||||
}
|
||||
|
||||
const accept = ctx.request.headers["accept"];
|
||||
const content = await DocumentHelper.diff(before, revision);
|
||||
|
||||
if (accept?.includes("text/html")) {
|
||||
ctx.set("Content-Type", "text/html");
|
||||
ctx.set(
|
||||
"Content-Disposition",
|
||||
`attachment; filename="${slugify(document.titleWithDefault)}-${
|
||||
revision.id
|
||||
}.html"`
|
||||
);
|
||||
ctx.body = content;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: content,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"revisions.list",
|
||||
auth(),
|
||||
pagination(),
|
||||
validate(T.RevisionsListSchema),
|
||||
async (ctx: APIContext<T.RevisionsListReq>) => {
|
||||
const { direction, documentId, sort } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
|
||||
const document = await Document.findByPk(documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
const revisions = await Revision.findAll({
|
||||
where: {
|
||||
documentId: document.id,
|
||||
},
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
const data = await Promise.all(
|
||||
revisions.map((revision) => presentRevision(revision))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
46
server/routes/api/revisions/schema.ts
Normal file
46
server/routes/api/revisions/schema.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { isEmpty } from "lodash";
|
||||
import { z } from "zod";
|
||||
import { Revision } from "@server/models";
|
||||
import BaseSchema from "@server/routes/api/BaseSchema";
|
||||
|
||||
export const RevisionsInfoSchema = BaseSchema.extend({
|
||||
body: z
|
||||
.object({
|
||||
id: z.string().uuid().optional(),
|
||||
documentId: z.string().uuid().optional(),
|
||||
})
|
||||
.refine((req) => !(isEmpty(req.id) && isEmpty(req.documentId)), {
|
||||
message: "id or documentId is required",
|
||||
}),
|
||||
});
|
||||
|
||||
export type RevisionsInfoReq = z.infer<typeof RevisionsInfoSchema>;
|
||||
|
||||
export const RevisionsDiffSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
id: z.string().uuid(),
|
||||
compareToId: z.string().uuid().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type RevisionsDiffReq = z.infer<typeof RevisionsDiffSchema>;
|
||||
|
||||
export const RevisionsListSchema = z.object({
|
||||
body: z.object({
|
||||
direction: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((val) => (val !== "ASC" ? "DESC" : val)),
|
||||
|
||||
sort: z
|
||||
.string()
|
||||
.refine((val) => Object.keys(Revision.getAttributes()).includes(val), {
|
||||
message: "Invalid sort parameter",
|
||||
})
|
||||
.default("createdAt"),
|
||||
|
||||
documentId: z.string().uuid(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type RevisionsListReq = z.infer<typeof RevisionsListSchema>;
|
||||
Reference in New Issue
Block a user