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";
|
|
|
|
import auth from "../middlewares/authentication";
|
|
|
|
import pagination from "./middlewares/pagination";
|
|
|
|
import documentMover from "../commands/documentMover";
|
2019-04-09 04:25:13 +00:00
|
|
|
import {
|
|
|
|
presentDocument,
|
|
|
|
presentCollection,
|
2019-08-22 04:41:37 +00:00
|
|
|
presentPolicies,
|
2020-06-20 20:59:15 +00:00
|
|
|
} from "../presenters";
|
2019-07-08 02:25:45 +00:00
|
|
|
import {
|
|
|
|
Collection,
|
2019-08-06 03:38:31 +00:00
|
|
|
Document,
|
|
|
|
Event,
|
2019-07-08 02:25:45 +00:00
|
|
|
Share,
|
|
|
|
Star,
|
|
|
|
View,
|
|
|
|
Revision,
|
|
|
|
Backlink,
|
2019-07-13 19:40:25 +00:00
|
|
|
User,
|
2020-06-20 20:59:15 +00:00
|
|
|
} from "../models";
|
|
|
|
import { InvalidRequestError } from "../errors";
|
|
|
|
import policy from "../policies";
|
|
|
|
import { sequelize } from "../sequelize";
|
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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.list", auth(), pagination(), async ctx => {
|
|
|
|
const { sort = "updatedAt", 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;
|
|
|
|
let where = { teamId: user.teamId };
|
|
|
|
|
|
|
|
// 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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
id: backlinks.map(backlink => backlink.reverseDocumentId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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] };
|
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,
|
|
|
|
collectionScope
|
|
|
|
).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,
|
|
|
|
});
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2019-04-18 02:11:23 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.pinned", auth(), pagination(), async ctx => {
|
|
|
|
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] };
|
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,
|
|
|
|
collectionScope
|
|
|
|
).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(
|
2019-04-18 02:11:23 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.archived", auth(), pagination(), async ctx => {
|
|
|
|
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] };
|
2019-10-12 20:24:48 +00:00
|
|
|
const documents = await Document.scope(
|
2020-06-20 20:59:15 +00:00
|
|
|
"defaultScope",
|
2019-10-12 20:24:48 +00:00
|
|
|
collectionScope
|
|
|
|
).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(
|
2019-04-18 02:11:23 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.deleted", auth(), pagination(), async ctx => {
|
|
|
|
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;
|
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const collectionScope = { method: ["withCollection", user.id] };
|
2019-11-19 02:51:32 +00:00
|
|
|
const documents = await Document.scope(collectionScope).findAll({
|
|
|
|
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(
|
|
|
|
documents.map(document => presentDocument(document))
|
|
|
|
);
|
|
|
|
|
|
|
|
const policies = presentPolicies(user, documents);
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
data,
|
|
|
|
policies,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
router.post("documents.viewed", auth(), pagination(), async ctx => {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const documents = views.map(view => view.document);
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2019-08-22 04:41:37 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.starred", auth(), pagination(), async ctx => {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const documents = stars.map(star => star.document);
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2019-08-22 04:41:37 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.drafts", auth(), pagination(), async ctx => {
|
|
|
|
let { sort = "updatedAt", direction } = ctx.body;
|
|
|
|
if (direction !== "ASC") direction = "DESC";
|
2018-02-28 06:41:12 +00:00
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2019-01-05 21:37:33 +00:00
|
|
|
const collectionIds = await user.collectionIds();
|
|
|
|
|
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",
|
2019-10-06 01:42:03 +00:00
|
|
|
collectionScope
|
|
|
|
).findAll({
|
2019-01-05 21:37:33 +00:00
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
collectionId: collectionIds,
|
|
|
|
publishedAt: { [Op.eq]: null },
|
|
|
|
},
|
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(
|
2019-04-18 02:11:23 +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
|
|
|
}
|
2018-05-13 20:26:06 +00:00
|
|
|
document = share.document;
|
|
|
|
} else {
|
2019-10-06 01:42:03 +00:00
|
|
|
document = await Document.findByPk(
|
|
|
|
id,
|
|
|
|
user ? { userId: user.id } : undefined
|
|
|
|
);
|
2020-06-20 20:59:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
router.post("documents.info", auth({ required: false }), async ctx => {
|
|
|
|
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-07-14 01:23:15 +00:00
|
|
|
router.post("documents.export", auth({ required: false }), async ctx => {
|
|
|
|
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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.restore", auth(), async ctx => {
|
2018-09-30 04:24:07 +00:00
|
|
|
const { id, 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,
|
|
|
|
});
|
|
|
|
|
|
|
|
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-06-20 20:59:15 +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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-05 01:32:56 +00:00
|
|
|
const results = 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
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
const documents = results.map(result => result.document);
|
2017-07-07 05:02:55 +00:00
|
|
|
const data = await Promise.all(
|
2018-08-05 01:32:56 +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
|
|
|
|
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-06-20 20:59:15 +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-06-20 20:59:15 +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-06-20 20:59:15 +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-06-20 20:59:15 +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-06-20 20:59:15 +00:00
|
|
|
router.post("documents.create", auth(), async ctx => {
|
2019-04-18 02:11:23 +00:00
|
|
|
const {
|
2020-06-20 20:59:15 +00:00
|
|
|
title = "",
|
|
|
|
text = "",
|
2019-04-18 02:11:23 +00:00
|
|
|
publish,
|
|
|
|
collectionId,
|
|
|
|
parentDocumentId,
|
|
|
|
index,
|
|
|
|
} = 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.assertUuid(collectionId, "collectionId must be an uuid");
|
2019-04-18 02:11:23 +00:00
|
|
|
if (parentDocumentId) {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid");
|
2019-04-18 02:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
if (index) ctx.assertPositiveInteger(index, "index must be an integer (>=0)");
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2016-05-22 21:31:21 +00:00
|
|
|
const user = ctx.state.user;
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "create", Document);
|
2018-02-18 19:08:43 +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
|
|
|
}).findOne({
|
2016-05-20 03:46:34 +00:00
|
|
|
where: {
|
2018-02-28 06:41:12 +00:00
|
|
|
id: collectionId,
|
2016-06-20 07:18:03 +00:00
|
|
|
teamId: user.teamId,
|
2016-05-20 03:46:34 +00:00
|
|
|
},
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "publish", collection);
|
2016-05-20 03:46:34 +00:00
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
let parentDocument;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (parentDocumentId && collection.type === "atlas") {
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocument = await Document.findOne({
|
2017-06-04 21:40:27 +00:00
|
|
|
where: {
|
2019-04-18 02:11:23 +00:00
|
|
|
id: parentDocumentId,
|
2018-08-08 06:23:26 +00:00
|
|
|
collectionId: collection.id,
|
2017-06-04 21:40:27 +00:00
|
|
|
},
|
2016-08-21 18:12:24 +00:00
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
authorize(user, "read", parentDocument, { collection });
|
2017-06-04 21:40:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
let document = await Document.create({
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocumentId,
|
2020-03-16 15:30:23 +00:00
|
|
|
editorVersion,
|
2018-08-08 06:23:26 +00:00
|
|
|
collectionId: collection.id,
|
2017-06-04 21:40:27 +00:00
|
|
|
teamId: user.teamId,
|
|
|
|
userId: user.id,
|
|
|
|
lastModifiedById: user.id,
|
|
|
|
createdById: user.id,
|
|
|
|
title,
|
|
|
|
text,
|
|
|
|
});
|
|
|
|
|
2019-08-06 03:38:31 +00:00
|
|
|
await Event.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
name: "documents.create",
|
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-04-04 03:36:25 +00:00
|
|
|
if (publish) {
|
|
|
|
await document.publish();
|
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.publish",
|
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
|
|
|
});
|
2017-06-04 21:40:27 +00:00
|
|
|
}
|
2016-08-21 22:45:48 +00:00
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
// reload to get all of the data needed to present (user, collection etc)
|
|
|
|
// we need to specify publishedAt to bypass default scope that only returns
|
|
|
|
// published documents
|
2019-06-23 22:49:45 +00:00
|
|
|
document = await Document.findOne({
|
2018-04-04 03:36:25 +00:00
|
|
|
where: { id: document.id, publishedAt: document.publishedAt },
|
2018-02-28 06:41:12 +00:00
|
|
|
});
|
2019-10-06 01:42:03 +00:00
|
|
|
document.collection = collection;
|
2017-09-12 07:10:02 +00:00
|
|
|
|
2016-08-21 22:45:48 +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]),
|
2016-08-21 22:45:48 +00:00
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
});
|
|
|
|
|
2020-06-20 20:59:15 +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,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
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) {
|
|
|
|
await document.publish({ transaction });
|
|
|
|
} 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-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-06-20 20:59:15 +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-05-21 04:03:53 +00:00
|
|
|
const { collection } = document;
|
2020-06-20 20:59:15 +00:00
|
|
|
if (collection.type !== "atlas" && parentDocumentId) {
|
2019-04-09 04:25:13 +00:00
|
|
|
throw new InvalidRequestError(
|
2020-06-20 20:59:15 +00:00
|
|
|
"Document cannot be nested in this collection type"
|
2019-04-09 04:25:13 +00:00
|
|
|
);
|
2017-06-06 06:50:32 +00:00
|
|
|
}
|
|
|
|
|
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(
|
2019-04-18 02:11:23 +00:00
|
|
|
documents.map(document => presentDocument(document))
|
2019-04-09 04:25:13 +00:00
|
|
|
),
|
|
|
|
collections: await Promise.all(
|
2019-04-18 02:11:23 +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-06-20 20:59:15 +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-06-20 20:59:15 +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
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
await document.delete();
|
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
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-07-01 06:47:49 +00:00
|
|
|
export default router;
|