2017-06-04 21:40:27 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import Router from "koa-router";
|
|
|
|
import Sequelize from "sequelize";
|
2020-11-09 06:33:52 +00:00
|
|
|
import { subtractDate } from "../../shared/utils/date";
|
2020-12-17 05:39:37 +00:00
|
|
|
import documentCreator from "../commands/documentCreator";
|
2020-09-21 05:27:11 +00:00
|
|
|
import documentImporter from "../commands/documentImporter";
|
2020-06-20 20:59:15 +00:00
|
|
|
import documentMover from "../commands/documentMover";
|
2020-12-01 07:39:23 +00:00
|
|
|
import {
|
|
|
|
NotFoundError,
|
|
|
|
InvalidRequestError,
|
|
|
|
AuthorizationError,
|
|
|
|
} from "../errors";
|
2020-08-09 05:53:59 +00:00
|
|
|
import auth from "../middlewares/authentication";
|
2019-07-08 02:25:45 +00:00
|
|
|
import {
|
2020-09-01 03:03:05 +00:00
|
|
|
Backlink,
|
2019-07-08 02:25:45 +00:00
|
|
|
Collection,
|
2019-08-06 03:38:31 +00:00
|
|
|
Document,
|
|
|
|
Event,
|
2020-09-01 03:03:05 +00:00
|
|
|
Revision,
|
2020-09-22 06:05:42 +00:00
|
|
|
SearchQuery,
|
2019-07-08 02:25:45 +00:00
|
|
|
Share,
|
|
|
|
Star,
|
2019-07-13 19:40:25 +00:00
|
|
|
User,
|
2020-09-01 03:03:05 +00:00
|
|
|
View,
|
2020-12-01 07:39:23 +00:00
|
|
|
Team,
|
2020-06-20 20:59:15 +00:00
|
|
|
} from "../models";
|
|
|
|
import policy from "../policies";
|
2020-08-09 05:53:59 +00:00
|
|
|
import {
|
|
|
|
presentCollection,
|
2020-09-01 03:03:05 +00:00
|
|
|
presentDocument,
|
2020-08-09 05:53:59 +00:00
|
|
|
presentPolicies,
|
|
|
|
} from "../presenters";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { sequelize } from "../sequelize";
|
2020-08-09 05:53:59 +00:00
|
|
|
import pagination from "./middlewares/pagination";
|
2017-10-17 05:36:44 +00:00
|
|
|
|
2018-05-05 23:16:08 +00:00
|
|
|
const Op = Sequelize.Op;
|
2018-05-25 05:15:36 +00:00
|
|
|
const { authorize, cannot } = policy;
|
2016-05-20 03:46:34 +00:00
|
|
|
const router = new Router();
|
2017-10-17 05:36:44 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.list", auth(), pagination(), async (ctx) => {
|
2021-01-31 20:37:27 +00:00
|
|
|
let {
|
2020-08-08 22:18:37 +00:00
|
|
|
sort = "updatedAt",
|
|
|
|
template,
|
|
|
|
backlinkDocumentId,
|
|
|
|
parentDocumentId,
|
|
|
|
} = ctx.body;
|
2020-06-17 03:56:17 +00:00
|
|
|
|
2020-07-18 16:33:27 +00:00
|
|
|
// collection and user are here for backwards compatibility
|
2020-06-17 03:56:17 +00:00
|
|
|
const collectionId = ctx.body.collectionId || ctx.body.collection;
|
|
|
|
const createdById = ctx.body.userId || ctx.body.user;
|
2019-01-05 21:37:33 +00:00
|
|
|
let direction = ctx.body.direction;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2019-01-05 21:37:33 +00:00
|
|
|
// always filter by the current team
|
|
|
|
const user = ctx.state.user;
|
2020-10-14 03:38:28 +00:00
|
|
|
let where = {
|
|
|
|
teamId: user.teamId,
|
|
|
|
archivedAt: {
|
|
|
|
[Op.eq]: null,
|
|
|
|
},
|
|
|
|
};
|
2019-01-05 21:37:33 +00:00
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
if (template) {
|
|
|
|
where = { ...where, template: true };
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:37:33 +00:00
|
|
|
// if a specific user is passed then add to filters. If the user doesn't
|
|
|
|
// exist in the team then nothing will be returned, so no need to check auth
|
|
|
|
if (createdById) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(createdById, "user must be a UUID");
|
2019-01-05 21:37:33 +00:00
|
|
|
where = { ...where, createdById };
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
let documentIds = [];
|
2019-01-05 21:37:33 +00:00
|
|
|
// if a specific collection is passed then we need to check auth to view it
|
|
|
|
if (collectionId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(collectionId, "collection must be a UUID");
|
2019-01-05 21:37:33 +00:00
|
|
|
|
|
|
|
where = { ...where, collectionId };
|
2019-10-06 01:42:03 +00:00
|
|
|
const collection = await Collection.scope({
|
2020-06-20 20:59:15 +00:00
|
|
|
method: ["withMembership", user.id],
|
2019-10-06 01:42:03 +00:00
|
|
|
}).findByPk(collectionId);
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", collection);
|
2017-11-20 05:32:18 +00:00
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
// index sort is special because it uses the order of the documents in the
|
|
|
|
// collection.documentStructure rather than a database column
|
|
|
|
if (sort === "index") {
|
|
|
|
documentIds = collection.documentStructure
|
|
|
|
.map((node) => node.id)
|
|
|
|
.slice(ctx.state.pagination.offset, ctx.state.pagination.limit);
|
|
|
|
where = { ...where, id: documentIds };
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:37:33 +00:00
|
|
|
// otherwise, filter by all collections the user has access to
|
|
|
|
} else {
|
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
where = { ...where, collectionId: collectionIds };
|
|
|
|
}
|
|
|
|
|
2019-12-23 01:06:29 +00:00
|
|
|
if (parentDocumentId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(parentDocumentId, "parentDocumentId must be a UUID");
|
2019-12-23 01:06:29 +00:00
|
|
|
where = { ...where, parentDocumentId };
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
// Explicitly passing 'null' as the parentDocumentId allows listing documents
|
|
|
|
// that have no parent document (aka they are at the root of the collection)
|
|
|
|
if (parentDocumentId === null) {
|
|
|
|
where = { ...where, parentDocumentId: { [Op.eq]: null } };
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
if (backlinkDocumentId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(backlinkDocumentId, "backlinkDocumentId must be a UUID");
|
2019-12-23 01:06:29 +00:00
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
const backlinks = await Backlink.findAll({
|
2020-06-20 20:59:15 +00:00
|
|
|
attributes: ["reverseDocumentId"],
|
2019-07-08 02:25:45 +00:00
|
|
|
where: {
|
|
|
|
documentId: backlinkDocumentId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
where = {
|
|
|
|
...where,
|
2020-08-09 01:53:11 +00:00
|
|
|
id: backlinks.map((backlink) => backlink.reverseDocumentId),
|
2019-07-08 02:25:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
if (sort === "index") {
|
|
|
|
sort = "updatedAt";
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:37:33 +00:00
|
|
|
// add the users starred state to the response by default
|
2020-06-20 20:59:15 +00:00
|
|
|
const starredScope = { method: ["withStarred", user.id] };
|
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2020-09-21 05:32:28 +00:00
|
|
|
const viewScope = { method: ["withViews", user.id] };
|
2019-10-06 01:42:03 +00:00
|
|
|
const documents = await Document.scope(
|
2020-06-20 20:59:15 +00:00
|
|
|
"defaultScope",
|
2019-10-06 01:42:03 +00:00
|
|
|
starredScope,
|
2020-09-21 05:32:28 +00:00
|
|
|
collectionScope,
|
|
|
|
viewScope
|
2019-10-06 01:42:03 +00:00
|
|
|
).findAll({
|
2017-11-20 05:32:18 +00:00
|
|
|
where,
|
2017-06-26 00:21:33 +00:00
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
// index sort is special because it uses the order of the documents in the
|
|
|
|
// collection.documentStructure rather than a database column
|
|
|
|
if (documentIds.length) {
|
|
|
|
documents.sort(
|
|
|
|
(a, b) => documentIds.indexOf(a.id) - documentIds.indexOf(b.id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2017-07-07 05:02:55 +00:00
|
|
|
);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2017-06-26 00:21:33 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.pinned", auth(), pagination(), async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
const { collectionId, sort = "updatedAt" } = ctx.body;
|
2019-01-05 21:37:33 +00:00
|
|
|
let direction = ctx.body.direction;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (direction !== "ASC") direction = "DESC";
|
|
|
|
ctx.assertUuid(collectionId, "collectionId is required");
|
2018-03-01 07:28:36 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-10-06 01:42:03 +00:00
|
|
|
const collection = await Collection.scope({
|
2020-06-20 20:59:15 +00:00
|
|
|
method: ["withMembership", user.id],
|
2019-10-06 01:42:03 +00:00
|
|
|
}).findByPk(collectionId);
|
2020-03-15 03:48:32 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", collection);
|
2019-01-05 21:37:33 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const starredScope = { method: ["withStarred", user.id] };
|
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2020-09-21 05:32:28 +00:00
|
|
|
const viewScope = { method: ["withViews", user.id] };
|
2019-10-06 01:42:03 +00:00
|
|
|
const documents = await Document.scope(
|
2020-06-20 20:59:15 +00:00
|
|
|
"defaultScope",
|
2019-10-06 01:42:03 +00:00
|
|
|
starredScope,
|
2020-09-21 05:32:28 +00:00
|
|
|
collectionScope,
|
|
|
|
viewScope
|
2019-10-06 01:42:03 +00:00
|
|
|
).findAll({
|
2018-03-01 07:28:36 +00:00
|
|
|
where: {
|
|
|
|
teamId: user.teamId,
|
2019-01-05 21:37:33 +00:00
|
|
|
collectionId,
|
2018-03-01 07:28:36 +00:00
|
|
|
pinnedById: {
|
|
|
|
[Op.ne]: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2018-03-01 07:28:36 +00:00
|
|
|
);
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2018-03-01 07:28:36 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.archived", auth(), pagination(), async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
const { sort = "updatedAt" } = ctx.body;
|
2019-04-06 23:20:27 +00:00
|
|
|
let direction = ctx.body.direction;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2019-04-06 23:20:27 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2020-09-21 05:32:28 +00:00
|
|
|
const viewScope = { method: ["withViews", user.id] };
|
2019-10-12 20:24:48 +00:00
|
|
|
const documents = await Document.scope(
|
2020-06-20 20:59:15 +00:00
|
|
|
"defaultScope",
|
2020-09-21 05:32:28 +00:00
|
|
|
collectionScope,
|
|
|
|
viewScope
|
2019-10-12 20:24:48 +00:00
|
|
|
).findAll({
|
2019-04-06 23:20:27 +00:00
|
|
|
where: {
|
|
|
|
teamId: user.teamId,
|
|
|
|
collectionId: collectionIds,
|
|
|
|
archivedAt: {
|
|
|
|
[Op.ne]: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2019-04-06 23:20:27 +00:00
|
|
|
);
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2019-04-06 23:20:27 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.deleted", auth(), pagination(), async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
const { sort = "deletedAt" } = ctx.body;
|
2019-11-19 02:51:32 +00:00
|
|
|
let direction = ctx.body.direction;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2019-11-19 02:51:32 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2020-09-07 18:51:09 +00:00
|
|
|
const collectionIds = await user.collectionIds({ paranoid: false });
|
2019-11-19 02:51:32 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2020-09-21 05:32:28 +00:00
|
|
|
const viewScope = { method: ["withViews", user.id] };
|
|
|
|
const documents = await Document.scope(collectionScope, viewScope).findAll({
|
2019-11-19 02:51:32 +00:00
|
|
|
where: {
|
|
|
|
teamId: user.teamId,
|
|
|
|
collectionId: collectionIds,
|
|
|
|
deletedAt: {
|
|
|
|
[Op.ne]: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: [
|
2020-06-20 20:59:15 +00:00
|
|
|
{ model: User, as: "createdBy", paranoid: false },
|
|
|
|
{ model: User, as: "updatedBy", paranoid: false },
|
2019-11-19 02:51:32 +00:00
|
|
|
],
|
|
|
|
paranoid: false,
|
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2019-11-19 02:51:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
policies,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.viewed", auth(), pagination(), async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
let { sort = "updatedAt", direction } = ctx.body;
|
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-01-05 21:37:33 +00:00
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
const views = await View.findAll({
|
|
|
|
where: { userId: user.id },
|
|
|
|
order: [[sort, direction]],
|
2017-08-03 03:45:09 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Document,
|
2017-09-14 06:04:58 +00:00
|
|
|
required: true,
|
2019-01-05 21:37:33 +00:00
|
|
|
where: {
|
|
|
|
collectionId: collectionIds,
|
|
|
|
},
|
2017-08-03 03:45:09 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Star,
|
2020-06-20 20:59:15 +00:00
|
|
|
as: "starred",
|
2017-08-03 03:45:09 +00:00
|
|
|
where: { userId: user.id },
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2020-09-21 06:37:09 +00:00
|
|
|
const documents = views.map((view) => {
|
|
|
|
const document = view.document;
|
|
|
|
document.views = [view];
|
|
|
|
return document;
|
|
|
|
});
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2017-06-26 00:21:33 +00:00
|
|
|
);
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2017-06-26 00:21:33 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.starred", auth(), pagination(), async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
let { sort = "updatedAt", direction } = ctx.body;
|
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-01-05 21:37:33 +00:00
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
2019-01-09 06:49:20 +00:00
|
|
|
const stars = await Star.findAll({
|
2019-01-05 21:37:33 +00:00
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
2017-06-26 00:21:33 +00:00
|
|
|
order: [[sort, direction]],
|
2017-07-04 04:35:17 +00:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: Document,
|
2019-01-05 21:37:33 +00:00
|
|
|
where: {
|
|
|
|
collectionId: collectionIds,
|
|
|
|
},
|
|
|
|
include: [
|
2020-05-21 04:42:26 +00:00
|
|
|
{
|
|
|
|
model: Collection,
|
2020-06-20 20:59:15 +00:00
|
|
|
as: "collection",
|
2020-05-21 04:42:26 +00:00
|
|
|
},
|
2019-01-05 21:37:33 +00:00
|
|
|
{
|
|
|
|
model: Star,
|
2020-06-20 20:59:15 +00:00
|
|
|
as: "starred",
|
2019-01-05 21:37:33 +00:00
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2017-07-04 04:35:17 +00:00
|
|
|
},
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
const documents = stars.map((star) => star.document);
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2017-06-26 00:21:33 +00:00
|
|
|
);
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2017-06-26 00:21:33 +00:00
|
|
|
};
|
|
|
|
});
|
2016-08-15 19:41:51 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.drafts", auth(), pagination(), async (ctx) => {
|
2020-11-09 06:33:52 +00:00
|
|
|
let { collectionId, dateFilter, sort = "updatedAt", direction } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2018-02-28 06:41:12 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2020-11-09 06:33:52 +00:00
|
|
|
|
|
|
|
if (collectionId) {
|
|
|
|
ctx.assertUuid(collectionId, "collectionId must be a UUID");
|
|
|
|
|
|
|
|
const collection = await Collection.scope({
|
|
|
|
method: ["withMembership", user.id],
|
|
|
|
}).findByPk(collectionId);
|
|
|
|
authorize(user, "read", collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
const collectionIds = !!collectionId
|
|
|
|
? [collectionId]
|
|
|
|
: await user.collectionIds();
|
|
|
|
|
|
|
|
const whereConditions = {
|
|
|
|
userId: user.id,
|
|
|
|
collectionId: collectionIds,
|
|
|
|
publishedAt: { [Op.eq]: null },
|
|
|
|
updatedAt: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dateFilter) {
|
|
|
|
ctx.assertIn(
|
|
|
|
dateFilter,
|
|
|
|
["day", "week", "month", "year"],
|
|
|
|
"dateFilter must be one of day,week,month,year"
|
|
|
|
);
|
|
|
|
|
|
|
|
whereConditions.updatedAt = {
|
|
|
|
[Op.gte]: subtractDate(new Date(), dateFilter),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
delete whereConditions.updatedAt;
|
|
|
|
}
|
2019-01-05 21:37:33 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2019-10-06 01:42:03 +00:00
|
|
|
const documents = await Document.scope(
|
2020-06-20 20:59:15 +00:00
|
|
|
"defaultScope",
|
2020-11-09 06:33:52 +00:00
|
|
|
collectionScope
|
2019-10-06 01:42:03 +00:00
|
|
|
).findAll({
|
2020-11-09 06:33:52 +00:00
|
|
|
where: whereConditions,
|
2018-02-28 06:41:12 +00:00
|
|
|
order: [[sort, direction]],
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2018-02-28 06:41:12 +00:00
|
|
|
);
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2018-02-28 06:41:12 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-07-14 01:23:15 +00:00
|
|
|
async function loadDocument({ id, shareId, user }) {
|
2018-05-13 20:26:06 +00:00
|
|
|
let document;
|
2018-05-24 06:59:00 +00:00
|
|
|
|
2018-05-13 20:26:06 +00:00
|
|
|
if (shareId) {
|
2019-06-23 22:49:45 +00:00
|
|
|
const share = await Share.findOne({
|
2018-06-16 19:36:25 +00:00
|
|
|
where: {
|
|
|
|
revokedAt: { [Op.eq]: null },
|
|
|
|
id: shareId,
|
|
|
|
},
|
2018-05-13 20:26:06 +00:00
|
|
|
include: [
|
|
|
|
{
|
2019-07-13 19:40:25 +00:00
|
|
|
// unscoping here allows us to return unpublished documents
|
|
|
|
model: Document.unscoped(),
|
|
|
|
include: [
|
2020-06-20 20:59:15 +00:00
|
|
|
{ model: User, as: "createdBy", paranoid: false },
|
|
|
|
{ model: User, as: "updatedBy", paranoid: false },
|
2019-07-13 19:40:25 +00:00
|
|
|
],
|
2018-05-13 20:26:06 +00:00
|
|
|
required: true,
|
2020-06-20 20:59:15 +00:00
|
|
|
as: "document",
|
2018-05-13 20:26:06 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2019-04-06 23:20:27 +00:00
|
|
|
if (!share || share.document.archivedAt) {
|
2020-06-20 20:59:15 +00:00
|
|
|
throw new InvalidRequestError("Document could not be found for shareId");
|
2018-05-26 18:23:21 +00:00
|
|
|
}
|
2020-07-29 02:14:32 +00:00
|
|
|
|
2018-05-13 20:26:06 +00:00
|
|
|
document = share.document;
|
2020-07-29 02:14:32 +00:00
|
|
|
|
|
|
|
if (!share.published) {
|
|
|
|
authorize(user, "read", document);
|
|
|
|
}
|
2020-12-01 07:39:23 +00:00
|
|
|
|
2021-02-10 03:04:03 +00:00
|
|
|
const collection = await Collection.findByPk(document.collectionId);
|
|
|
|
if (!collection.sharing) {
|
|
|
|
throw new AuthorizationError();
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:39:23 +00:00
|
|
|
const team = await Team.findByPk(document.teamId);
|
|
|
|
if (!team.sharing) {
|
|
|
|
throw new AuthorizationError();
|
|
|
|
}
|
2018-05-13 20:26:06 +00:00
|
|
|
} else {
|
2020-09-08 02:05:10 +00:00
|
|
|
document = await Document.findByPk(id, {
|
|
|
|
userId: user ? user.id : undefined,
|
|
|
|
paranoid: false,
|
|
|
|
});
|
2020-09-08 14:29:06 +00:00
|
|
|
if (!document) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
|
2020-09-08 02:05:10 +00:00
|
|
|
if (document.deletedAt) {
|
|
|
|
authorize(user, "restore", document);
|
|
|
|
} else {
|
|
|
|
authorize(user, "read", document);
|
|
|
|
}
|
2018-05-13 20:26:06 +00:00
|
|
|
}
|
2017-10-17 05:36:44 +00:00
|
|
|
|
2020-07-14 01:23:15 +00:00
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.info", auth({ required: false }), async (ctx) => {
|
2020-07-14 01:23:15 +00:00
|
|
|
const { id, shareId } = ctx.body;
|
|
|
|
ctx.assertPresent(id || shareId, "id or shareId is required");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await loadDocument({ id, shareId, user });
|
2020-06-20 20:59:15 +00:00
|
|
|
const isPublic = cannot(user, "read", document);
|
2018-05-25 05:15:36 +00:00
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document, { isPublic }),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: isPublic ? undefined : presentPolicies(user, [document]),
|
2017-10-17 05:36:44 +00:00
|
|
|
};
|
|
|
|
});
|
2016-08-03 12:36:50 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.export", auth({ required: false }), async (ctx) => {
|
2020-07-14 01:23:15 +00:00
|
|
|
const { id, shareId } = ctx.body;
|
|
|
|
ctx.assertPresent(id || shareId, "id or shareId is required");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await loadDocument({ id, shareId, user });
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
data: document.toMarkdown(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.restore", auth(), async (ctx) => {
|
2020-09-07 18:51:09 +00:00
|
|
|
const { id, collectionId, revisionId } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2018-09-30 04:24:07 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-11-19 02:51:32 +00:00
|
|
|
const document = await Document.findByPk(id, {
|
|
|
|
userId: user.id,
|
|
|
|
paranoid: false,
|
|
|
|
});
|
2020-09-08 14:29:06 +00:00
|
|
|
if (!document) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
2019-11-19 02:51:32 +00:00
|
|
|
|
2021-01-07 16:09:19 +00:00
|
|
|
// Passing collectionId allows restoring to a different collection than the
|
|
|
|
// document was originally within
|
2020-09-07 18:51:09 +00:00
|
|
|
if (collectionId) {
|
|
|
|
ctx.assertUuid(collectionId, "collectionId must be a uuid");
|
|
|
|
document.collectionId = collectionId;
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:09:19 +00:00
|
|
|
const collection = await Collection.scope({
|
|
|
|
method: ["withMembership", user.id],
|
|
|
|
}).findByPk(document.collectionId);
|
|
|
|
|
|
|
|
// if the collectionId was provided in the request and isn't valid then it will
|
|
|
|
// be caught as a 403 on the authorize call below. Otherwise we're checking here
|
|
|
|
// that the original collection still exists and advising to pass collectionId
|
|
|
|
// if not.
|
|
|
|
if (!collectionId) {
|
|
|
|
ctx.assertPresent(collection, "collectionId is required");
|
|
|
|
}
|
|
|
|
|
|
|
|
authorize(user, "update", collection);
|
|
|
|
|
2019-11-19 02:51:32 +00:00
|
|
|
if (document.deletedAt) {
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "restore", document);
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2019-11-19 02:51:32 +00:00
|
|
|
// restore a previously deleted document
|
|
|
|
await document.unarchive(user.id);
|
|
|
|
|
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.restore",
|
2019-11-19 02:51:32 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
} else if (document.archivedAt) {
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "unarchive", document);
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
// restore a previously archived document
|
|
|
|
await document.unarchive(user.id);
|
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.unarchive",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
2019-04-06 23:20:27 +00:00
|
|
|
} else if (revisionId) {
|
2019-04-18 02:11:23 +00:00
|
|
|
// restore a document to a specific revision
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "update", document);
|
2019-04-06 23:20:27 +00:00
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
const revision = await Revision.findByPk(revisionId);
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(document, "restore", revision);
|
2019-04-06 23:20:27 +00:00
|
|
|
|
|
|
|
document.text = revision.text;
|
|
|
|
document.title = revision.title;
|
|
|
|
await document.save();
|
2019-04-18 02:11:23 +00:00
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.restore",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
2019-04-06 23:20:27 +00:00
|
|
|
} else {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(revisionId, "revisionId is required");
|
2019-04-06 23:20:27 +00:00
|
|
|
}
|
2018-09-30 04:24:07 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: presentPolicies(user, [document]),
|
2018-09-30 04:24:07 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-09-26 18:07:49 +00:00
|
|
|
router.post("documents.search_titles", auth(), pagination(), async (ctx) => {
|
|
|
|
const { query } = ctx.body;
|
|
|
|
const { offset, limit } = ctx.state.pagination;
|
|
|
|
const user = ctx.state.user;
|
|
|
|
ctx.assertPresent(query, "query is required");
|
|
|
|
|
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
|
|
|
const documents = await Document.scope(
|
|
|
|
{
|
|
|
|
method: ["withViews", user.id],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: ["withCollection", user.id],
|
|
|
|
}
|
|
|
|
).findAll({
|
|
|
|
where: {
|
|
|
|
title: {
|
|
|
|
[Op.iLike]: `%${query}%`,
|
|
|
|
},
|
|
|
|
collectionId: collectionIds,
|
|
|
|
archivedAt: {
|
|
|
|
[Op.eq]: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
order: [["updatedAt", "DESC"]],
|
|
|
|
include: [
|
|
|
|
{ model: User, as: "createdBy", paranoid: false },
|
|
|
|
{ model: User, as: "updatedBy", paranoid: false },
|
|
|
|
],
|
|
|
|
offset,
|
|
|
|
limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
const data = await Promise.all(
|
|
|
|
documents.map((document) => presentDocument(document))
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
policies,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.search", auth(), pagination(), async (ctx) => {
|
2020-01-06 01:24:57 +00:00
|
|
|
const {
|
|
|
|
query,
|
|
|
|
includeArchived,
|
|
|
|
includeDrafts,
|
|
|
|
collectionId,
|
|
|
|
userId,
|
|
|
|
dateFilter,
|
|
|
|
} = ctx.body;
|
2017-12-04 00:50:50 +00:00
|
|
|
const { offset, limit } = ctx.state.pagination;
|
2019-04-23 14:31:20 +00:00
|
|
|
const user = ctx.state.user;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(query, "query is required");
|
2016-07-13 06:43:41 +00:00
|
|
|
|
2019-04-23 14:31:20 +00:00
|
|
|
if (collectionId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(collectionId, "collectionId must be a UUID");
|
2019-04-23 14:31:20 +00:00
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
const collection = await Collection.scope({
|
2020-06-20 20:59:15 +00:00
|
|
|
method: ["withMembership", user.id],
|
2019-10-06 01:42:03 +00:00
|
|
|
}).findByPk(collectionId);
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", collection);
|
2019-04-23 14:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let collaboratorIds = undefined;
|
|
|
|
if (userId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(userId, "userId must be a UUID");
|
2019-04-23 14:31:20 +00:00
|
|
|
collaboratorIds = [userId];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateFilter) {
|
|
|
|
ctx.assertIn(
|
|
|
|
dateFilter,
|
2020-06-20 20:59:15 +00:00
|
|
|
["day", "week", "month", "year"],
|
|
|
|
"dateFilter must be one of day,week,month,year"
|
2019-04-23 14:31:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:05:42 +00:00
|
|
|
const { results, totalCount } = await Document.searchForUser(user, query, {
|
2020-06-20 20:59:15 +00:00
|
|
|
includeArchived: includeArchived === "true",
|
|
|
|
includeDrafts: includeDrafts === "true",
|
2019-04-23 14:31:20 +00:00
|
|
|
collaboratorIds,
|
|
|
|
collectionId,
|
|
|
|
dateFilter,
|
2017-12-04 00:50:50 +00:00
|
|
|
offset,
|
|
|
|
limit,
|
|
|
|
});
|
2016-07-13 06:43:41 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
const documents = results.map((result) => result.document);
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
results.map(async (result) => {
|
2019-04-18 02:11:23 +00:00
|
|
|
const document = await presentDocument(result.document);
|
2018-08-05 01:32:56 +00:00
|
|
|
return { ...result, document };
|
|
|
|
})
|
2017-04-27 04:47:03 +00:00
|
|
|
);
|
2016-07-13 06:43:41 +00:00
|
|
|
|
2020-09-22 06:31:10 +00:00
|
|
|
// When requesting subsequent pages of search results we don't want to record
|
|
|
|
// duplicate search query records
|
|
|
|
if (offset === 0) {
|
|
|
|
SearchQuery.create({
|
|
|
|
userId: user.id,
|
|
|
|
teamId: user.teamId,
|
|
|
|
source: ctx.state.authType,
|
|
|
|
query,
|
|
|
|
results: totalCount,
|
|
|
|
});
|
|
|
|
}
|
2020-09-22 06:05:42 +00:00
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
2016-07-13 06:43:41 +00:00
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
2016-08-01 07:12:55 +00:00
|
|
|
data,
|
2019-08-22 04:41:37 +00:00
|
|
|
policies,
|
2016-07-13 06:43:41 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.pin", auth(), async (ctx) => {
|
2018-03-01 07:28:36 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2018-03-01 07:28:36 +00:00
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "pin", document);
|
2018-03-01 07:28:36 +00:00
|
|
|
|
|
|
|
document.pinnedById = user.id;
|
|
|
|
await document.save();
|
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.pin",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: presentPolicies(user, [document]),
|
2018-03-01 07:28:36 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.unpin", auth(), async (ctx) => {
|
2018-03-01 07:28:36 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2018-03-01 07:28:36 +00:00
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "unpin", document);
|
2018-03-01 07:28:36 +00:00
|
|
|
|
|
|
|
document.pinnedById = null;
|
|
|
|
await document.save();
|
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.unpin",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: presentPolicies(user, [document]),
|
2018-03-01 07:28:36 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.star", auth(), async (ctx) => {
|
2017-06-26 00:21:33 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
await Star.findOrCreate({
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
});
|
2019-04-18 02:11:23 +00:00
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.star",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
2020-06-17 03:56:17 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
success: true,
|
|
|
|
};
|
2017-06-26 00:21:33 +00:00
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.unstar", auth(), async (ctx) => {
|
2017-06-26 00:21:33 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
await Star.destroy({
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
});
|
2019-04-18 02:11:23 +00:00
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.unstar",
|
2019-10-06 01:42:03 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
2020-06-17 03:56:17 +00:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
success: true,
|
|
|
|
};
|
2017-06-26 00:21:33 +00:00
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.templatize", auth(), async (ctx) => {
|
2020-08-08 22:18:37 +00:00
|
|
|
const { id } = ctx.body;
|
|
|
|
ctx.assertPresent(id, "id is required");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const original = await Document.findByPk(id, { userId: user.id });
|
|
|
|
authorize(user, "update", original);
|
|
|
|
|
|
|
|
let document = await Document.create({
|
|
|
|
editorVersion: original.editorVersion,
|
|
|
|
collectionId: original.collectionId,
|
|
|
|
teamId: original.teamId,
|
|
|
|
userId: user.id,
|
|
|
|
publishedAt: new Date(),
|
|
|
|
lastModifiedById: user.id,
|
|
|
|
createdById: user.id,
|
|
|
|
template: true,
|
|
|
|
title: original.title,
|
|
|
|
text: original.text,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Event.create({
|
|
|
|
name: "documents.create",
|
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: { title: document.title, template: true },
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
|
|
|
|
// reload to get all of the data needed to present (user, collection etc)
|
|
|
|
document = await Document.findByPk(document.id, { userId: user.id });
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
data: await presentDocument(document),
|
|
|
|
policies: presentPolicies(user, [document]),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.update", auth(), async (ctx) => {
|
2019-06-14 06:36:24 +00:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
text,
|
|
|
|
publish,
|
|
|
|
autosave,
|
|
|
|
done,
|
|
|
|
lastRevision,
|
2020-08-08 22:18:37 +00:00
|
|
|
templateId,
|
2019-06-14 06:36:24 +00:00
|
|
|
append,
|
|
|
|
} = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
const editorVersion = ctx.headers["x-editor-version"];
|
2020-05-16 21:05:51 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
|
|
|
ctx.assertPresent(title || text, "title or text is required");
|
|
|
|
if (append) ctx.assertPresent(text, "Text is required while appending");
|
2016-05-26 04:26:06 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-10-06 01:42:03 +00:00
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "update", document);
|
2016-05-26 04:26:06 +00:00
|
|
|
|
2017-11-25 21:04:54 +00:00
|
|
|
if (lastRevision && lastRevision !== document.revisionCount) {
|
2020-06-20 20:59:15 +00:00
|
|
|
throw new InvalidRequestError("Document has changed since last revision");
|
2017-11-25 21:04:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 18:26:39 +00:00
|
|
|
const previousTitle = document.title;
|
|
|
|
|
2016-07-01 06:47:49 +00:00
|
|
|
// Update document
|
2017-06-05 05:12:36 +00:00
|
|
|
if (title) document.title = title;
|
2020-03-16 15:30:23 +00:00
|
|
|
if (editorVersion) document.editorVersion = editorVersion;
|
2020-08-08 22:18:37 +00:00
|
|
|
if (templateId) document.templateId = templateId;
|
2019-07-08 02:25:45 +00:00
|
|
|
|
2019-06-14 06:36:24 +00:00
|
|
|
if (append) {
|
|
|
|
document.text += text;
|
2020-05-08 03:52:02 +00:00
|
|
|
} else if (text !== undefined) {
|
2019-06-14 06:36:24 +00:00
|
|
|
document.text = text;
|
|
|
|
}
|
2016-06-26 18:23:03 +00:00
|
|
|
document.lastModifiedById = user.id;
|
2019-10-06 01:42:03 +00:00
|
|
|
const { collection } = document;
|
2016-05-26 04:26:06 +00:00
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
let transaction;
|
|
|
|
try {
|
|
|
|
transaction = await sequelize.transaction();
|
|
|
|
|
|
|
|
if (publish) {
|
2021-01-31 02:31:08 +00:00
|
|
|
await document.publish(user.id, { transaction });
|
2019-07-08 02:25:45 +00:00
|
|
|
} else {
|
|
|
|
await document.save({ autosave, transaction });
|
|
|
|
}
|
2019-08-06 03:38:31 +00:00
|
|
|
await transaction.commit();
|
2019-07-08 02:25:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (transaction) {
|
|
|
|
await transaction.rollback();
|
|
|
|
}
|
|
|
|
throw err;
|
2017-10-09 07:05:15 +00:00
|
|
|
}
|
2017-07-10 03:27:06 +00:00
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
if (publish) {
|
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.publish",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.update",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: {
|
|
|
|
autosave,
|
|
|
|
done,
|
|
|
|
title: document.title,
|
|
|
|
},
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:26:39 +00:00
|
|
|
if (document.title !== previousTitle) {
|
|
|
|
Event.add({
|
|
|
|
name: "documents.title_change",
|
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: {
|
|
|
|
previousTitle,
|
|
|
|
title: document.title,
|
|
|
|
},
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:18:38 +00:00
|
|
|
document.updatedBy = user;
|
2019-10-06 01:42:03 +00:00
|
|
|
document.collection = collection;
|
|
|
|
|
2017-06-06 06:50:32 +00:00
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: presentPolicies(user, [document]),
|
2017-06-06 06:50:32 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.move", auth(), async (ctx) => {
|
2019-04-09 04:25:13 +00:00
|
|
|
const { id, collectionId, parentDocumentId, index } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(id, "id must be a uuid");
|
|
|
|
ctx.assertUuid(collectionId, "collectionId must be a uuid");
|
2019-04-09 04:25:13 +00:00
|
|
|
|
|
|
|
if (parentDocumentId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(parentDocumentId, "parentDocumentId must be a uuid");
|
2019-04-09 04:25:13 +00:00
|
|
|
}
|
|
|
|
if (index) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPositiveInteger(index, "index must be a positive integer");
|
2019-04-09 04:25:13 +00:00
|
|
|
}
|
|
|
|
if (parentDocumentId === id) {
|
|
|
|
throw new InvalidRequestError(
|
2020-06-20 20:59:15 +00:00
|
|
|
"Infinite loop detected, cannot nest a document inside itself"
|
2019-04-09 04:25:13 +00:00
|
|
|
);
|
|
|
|
}
|
2017-06-06 06:50:32 +00:00
|
|
|
|
2018-02-25 04:44:13 +00:00
|
|
|
const user = ctx.state.user;
|
2019-10-06 01:42:03 +00:00
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "move", document);
|
2017-10-17 05:36:44 +00:00
|
|
|
|
2020-09-07 19:17:04 +00:00
|
|
|
const collection = await Collection.scope({
|
|
|
|
method: ["withMembership", user.id],
|
|
|
|
}).findByPk(collectionId);
|
2020-09-07 18:51:09 +00:00
|
|
|
authorize(user, "update", collection);
|
|
|
|
|
2019-04-09 04:25:13 +00:00
|
|
|
if (parentDocumentId) {
|
2019-10-16 15:45:21 +00:00
|
|
|
const parent = await Document.findByPk(parentDocumentId, {
|
|
|
|
userId: user.id,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "update", parent);
|
2019-04-09 04:25:13 +00:00
|
|
|
}
|
2017-06-06 06:50:32 +00:00
|
|
|
|
2019-04-09 04:25:13 +00:00
|
|
|
const { documents, collections } = await documentMover({
|
2019-08-06 03:38:31 +00:00
|
|
|
user,
|
2019-04-09 04:25:13 +00:00
|
|
|
document,
|
|
|
|
collectionId,
|
|
|
|
parentDocumentId,
|
|
|
|
index,
|
2019-08-06 03:38:31 +00:00
|
|
|
ip: ctx.request.ip,
|
2019-04-09 04:25:13 +00:00
|
|
|
});
|
2017-06-06 06:50:32 +00:00
|
|
|
|
2016-05-26 04:26:06 +00:00
|
|
|
ctx.body = {
|
2019-04-09 04:25:13 +00:00
|
|
|
data: {
|
|
|
|
documents: await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
documents.map((document) => presentDocument(document))
|
2019-04-09 04:25:13 +00:00
|
|
|
),
|
|
|
|
collections: await Promise.all(
|
2020-08-09 01:53:11 +00:00
|
|
|
collections.map((collection) => presentCollection(collection))
|
2019-04-09 04:25:13 +00:00
|
|
|
),
|
|
|
|
},
|
2020-06-17 03:56:17 +00:00
|
|
|
policies: presentPolicies(user, documents),
|
2016-05-26 04:26:06 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.archive", auth(), async (ctx) => {
|
2017-04-27 04:47:03 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2016-05-30 18:15:35 +00:00
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
const user = ctx.state.user;
|
2019-10-06 01:42:03 +00:00
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "archive", document);
|
2016-05-30 18:15:35 +00:00
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
await document.archive(user.id);
|
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.archive",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
ctx.body = {
|
2019-04-18 02:11:23 +00:00
|
|
|
data: await presentDocument(document),
|
2019-08-22 04:41:37 +00:00
|
|
|
policies: presentPolicies(user, [document]),
|
2019-04-06 23:20:27 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.post("documents.delete", auth(), async (ctx) => {
|
2019-04-06 23:20:27 +00:00
|
|
|
const { id } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertPresent(id, "id is required");
|
2019-04-06 23:20:27 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-10-06 01:42:03 +00:00
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "delete", document);
|
2016-08-14 08:50:42 +00:00
|
|
|
|
2020-08-25 15:51:12 +00:00
|
|
|
await document.delete(user.id);
|
2016-07-07 04:36:50 +00:00
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.delete",
|
2019-08-06 03:38:31 +00:00
|
|
|
documentId: document.id,
|
2019-04-18 02:11:23 +00:00
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
2019-08-06 03:38:31 +00:00
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
2019-04-18 02:11:23 +00:00
|
|
|
});
|
|
|
|
|
2016-05-30 18:15:35 +00:00
|
|
|
ctx.body = {
|
2016-08-27 05:04:28 +00:00
|
|
|
success: true,
|
2016-05-30 18:15:35 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-09-01 03:03:05 +00:00
|
|
|
router.post("documents.unpublish", auth(), async (ctx) => {
|
|
|
|
const { id } = ctx.body;
|
|
|
|
ctx.assertPresent(id, "id is required");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
const document = await Document.findByPk(id, { userId: user.id });
|
|
|
|
|
|
|
|
authorize(user, "unpublish", document);
|
|
|
|
|
2021-01-31 02:31:08 +00:00
|
|
|
await document.unpublish(user.id);
|
2020-09-01 03:03:05 +00:00
|
|
|
|
|
|
|
await Event.create({
|
|
|
|
name: "documents.unpublish",
|
|
|
|
documentId: document.id,
|
|
|
|
collectionId: document.collectionId,
|
|
|
|
teamId: document.teamId,
|
|
|
|
actorId: user.id,
|
|
|
|
data: { title: document.title },
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
data: await presentDocument(document),
|
|
|
|
policies: presentPolicies(user, [document]),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-12-17 05:39:37 +00:00
|
|
|
router.post("documents.import", auth(), async (ctx) => {
|
2020-12-18 05:19:31 +00:00
|
|
|
const { publish, collectionId, parentDocumentId, index } = ctx.body;
|
2020-12-17 05:39:37 +00:00
|
|
|
|
|
|
|
if (!ctx.is("multipart/form-data")) {
|
|
|
|
throw new InvalidRequestError("Request type must be multipart/form-data");
|
|
|
|
}
|
|
|
|
|
|
|
|
const file: any = Object.values(ctx.request.files)[0];
|
|
|
|
ctx.assertPresent(file, "file is required");
|
|
|
|
|
|
|
|
ctx.assertUuid(collectionId, "collectionId must be an uuid");
|
|
|
|
if (parentDocumentId) {
|
|
|
|
ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index) ctx.assertPositiveInteger(index, "index must be an integer (>=0)");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
authorize(user, "create", Document);
|
|
|
|
|
|
|
|
const collection = await Collection.scope({
|
|
|
|
method: ["withMembership", user.id],
|
|
|
|
}).findOne({
|
|
|
|
where: {
|
|
|
|
id: collectionId,
|
|
|
|
teamId: user.teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
authorize(user, "publish", collection);
|
|
|
|
|
|
|
|
let parentDocument;
|
|
|
|
if (parentDocumentId) {
|
|
|
|
parentDocument = await Document.findOne({
|
|
|
|
where: {
|
|
|
|
id: parentDocumentId,
|
|
|
|
collectionId: collection.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
authorize(user, "read", parentDocument, { collection });
|
|
|
|
}
|
|
|
|
|
|
|
|
const { text, title } = await documentImporter({
|
|
|
|
user,
|
|
|
|
file,
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
|
|
|
|
const document = await documentCreator({
|
2020-12-29 02:02:58 +00:00
|
|
|
source: "import",
|
2020-12-17 05:39:37 +00:00
|
|
|
title,
|
|
|
|
text,
|
|
|
|
publish,
|
|
|
|
collectionId,
|
|
|
|
parentDocumentId,
|
|
|
|
index,
|
|
|
|
user,
|
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (ctx.body = {
|
|
|
|
data: await presentDocument(document),
|
|
|
|
policies: presentPolicies(user, [document]),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post("documents.create", auth(), async (ctx) => {
|
2020-09-21 05:27:11 +00:00
|
|
|
const {
|
|
|
|
title = "",
|
|
|
|
text = "",
|
|
|
|
publish,
|
|
|
|
collectionId,
|
|
|
|
parentDocumentId,
|
|
|
|
templateId,
|
|
|
|
template,
|
|
|
|
index,
|
|
|
|
} = ctx.body;
|
|
|
|
const editorVersion = ctx.headers["x-editor-version"];
|
|
|
|
|
|
|
|
ctx.assertUuid(collectionId, "collectionId must be an uuid");
|
|
|
|
if (parentDocumentId) {
|
|
|
|
ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index) ctx.assertPositiveInteger(index, "index must be an integer (>=0)");
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
authorize(user, "create", Document);
|
|
|
|
|
|
|
|
const collection = await Collection.scope({
|
|
|
|
method: ["withMembership", user.id],
|
|
|
|
}).findOne({
|
|
|
|
where: {
|
|
|
|
id: collectionId,
|
|
|
|
teamId: user.teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
authorize(user, "publish", collection);
|
|
|
|
|
|
|
|
let parentDocument;
|
|
|
|
if (parentDocumentId) {
|
|
|
|
parentDocument = await Document.findOne({
|
|
|
|
where: {
|
|
|
|
id: parentDocumentId,
|
|
|
|
collectionId: collection.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
authorize(user, "read", parentDocument, { collection });
|
|
|
|
}
|
|
|
|
|
|
|
|
let templateDocument;
|
|
|
|
if (templateId) {
|
|
|
|
templateDocument = await Document.findByPk(templateId, { userId: user.id });
|
|
|
|
authorize(user, "read", templateDocument);
|
|
|
|
}
|
|
|
|
|
2020-12-17 05:39:37 +00:00
|
|
|
const document = await documentCreator({
|
|
|
|
title,
|
|
|
|
text,
|
|
|
|
publish,
|
|
|
|
collectionId,
|
2020-09-21 05:27:11 +00:00
|
|
|
parentDocumentId,
|
2020-12-17 05:39:37 +00:00
|
|
|
templateDocument,
|
2020-09-21 05:27:11 +00:00
|
|
|
template,
|
2020-12-17 05:39:37 +00:00
|
|
|
index,
|
|
|
|
user,
|
|
|
|
editorVersion,
|
2020-09-21 05:27:11 +00:00
|
|
|
ip: ctx.request.ip,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (ctx.body = {
|
|
|
|
data: await presentDocument(document),
|
|
|
|
policies: presentPolicies(user, [document]),
|
|
|
|
});
|
2020-12-17 05:39:37 +00:00
|
|
|
});
|
2020-09-21 05:27:11 +00:00
|
|
|
|
2016-07-01 06:47:49 +00:00
|
|
|
export default router;
|