2017-06-04 21:40:27 +00:00
|
|
|
|
// @flow
|
2016-05-20 03:46:34 +00:00
|
|
|
|
import Router from 'koa-router';
|
2018-05-05 23:16:08 +00:00
|
|
|
|
import Sequelize from 'sequelize';
|
2018-05-28 18:36:37 +00:00
|
|
|
|
import auth from '../middlewares/authentication';
|
2017-06-26 00:21:33 +00:00
|
|
|
|
import pagination from './middlewares/pagination';
|
2017-10-17 05:36:44 +00:00
|
|
|
|
import { presentDocument, presentRevision } from '../presenters';
|
2018-05-13 20:26:06 +00:00
|
|
|
|
import { Document, Collection, Share, Star, View, Revision } from '../models';
|
2018-04-04 03:36:25 +00:00
|
|
|
|
import { InvalidRequestError } from '../errors';
|
|
|
|
|
import events from '../events';
|
2018-02-18 08:11:48 +00:00
|
|
|
|
import policy from '../policies';
|
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
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
|
router.post('documents.list', auth(), pagination(), async ctx => {
|
2018-08-10 07:11:58 +00:00
|
|
|
|
let { sort = 'updatedAt', direction, collection, user } = ctx.body;
|
2017-06-26 00:21:33 +00:00
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
2018-08-10 07:11:58 +00:00
|
|
|
|
let where = { teamId: ctx.state.user.teamId };
|
2018-08-08 06:23:26 +00:00
|
|
|
|
if (collection) where = { ...where, collectionId: collection };
|
2018-08-11 06:03:47 +00:00
|
|
|
|
if (user) where = { ...where, createdById: user };
|
2017-11-20 05:32:18 +00:00
|
|
|
|
|
2018-08-10 07:11:58 +00:00
|
|
|
|
const starredScope = { method: ['withStarred', ctx.state.user.id] };
|
2017-07-15 23:08:12 +00:00
|
|
|
|
const documents = await Document.scope('defaultScope', starredScope).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(
|
|
|
|
|
documents.map(document => presentDocument(ctx, document))
|
|
|
|
|
);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
|
router.post('documents.pinned', auth(), pagination(), async ctx => {
|
|
|
|
|
let { sort = 'updatedAt', direction, collection } = ctx.body;
|
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
ctx.assertPresent(collection, 'collection is required');
|
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const starredScope = { method: ['withStarred', user.id] };
|
|
|
|
|
const documents = await Document.scope('defaultScope', starredScope).findAll({
|
|
|
|
|
where: {
|
|
|
|
|
teamId: user.teamId,
|
2018-08-08 06:23:26 +00:00
|
|
|
|
collectionId: collection,
|
2018-03-01 07:28:36 +00:00
|
|
|
|
pinnedById: {
|
2018-05-05 23:16:08 +00:00
|
|
|
|
// $FlowFixMe
|
2018-03-01 07:28:36 +00:00
|
|
|
|
[Op.ne]: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
order: [[sort, direction]],
|
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
|
|
|
|
documents.map(document => presentDocument(ctx, document))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
|
router.post('documents.viewed', auth(), pagination(), async ctx => {
|
|
|
|
|
let { sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
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,
|
2017-08-03 03:45:09 +00:00
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Star,
|
|
|
|
|
as: 'starred',
|
|
|
|
|
where: { userId: user.id },
|
|
|
|
|
required: false,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
|
const data = await Promise.all(
|
2017-06-26 00:21:33 +00:00
|
|
|
|
views.map(view => presentDocument(ctx, view.document))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('documents.starred', auth(), pagination(), async ctx => {
|
|
|
|
|
let { sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const views = await Star.findAll({
|
|
|
|
|
where: { userId: user.id },
|
|
|
|
|
order: [[sort, direction]],
|
2017-07-04 04:35:17 +00:00
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Document,
|
|
|
|
|
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
|
|
|
|
},
|
|
|
|
|
],
|
2017-06-26 00:21:33 +00:00
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-07 05:02:55 +00:00
|
|
|
|
const data = await Promise.all(
|
2017-06-26 00:21:33 +00:00
|
|
|
|
views.map(view => presentDocument(ctx, view.document))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
});
|
2016-08-15 19:41:51 +00:00
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
|
router.post('documents.drafts', auth(), pagination(), async ctx => {
|
|
|
|
|
let { sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const documents = await Document.findAll({
|
2018-05-05 23:16:08 +00:00
|
|
|
|
// $FlowFixMe
|
2018-02-28 06:41:12 +00:00
|
|
|
|
where: { userId: user.id, publishedAt: { [Op.eq]: null } },
|
|
|
|
|
order: [[sort, direction]],
|
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
|
|
|
|
documents.map(document => presentDocument(ctx, document))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-13 20:26:06 +00:00
|
|
|
|
router.post('documents.info', auth({ required: false }), async ctx => {
|
|
|
|
|
const { id, shareId } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id || shareId, 'id or shareId is required');
|
|
|
|
|
|
2018-05-25 05:15:36 +00:00
|
|
|
|
const user = ctx.state.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) {
|
2018-06-16 19:36:25 +00:00
|
|
|
|
const share = await Share.find({
|
|
|
|
|
where: {
|
|
|
|
|
// $FlowFixMe
|
|
|
|
|
revokedAt: { [Op.eq]: null },
|
|
|
|
|
id: shareId,
|
|
|
|
|
},
|
2018-05-13 20:26:06 +00:00
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Document,
|
|
|
|
|
required: true,
|
|
|
|
|
as: 'document',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2018-05-26 18:23:21 +00:00
|
|
|
|
if (!share) {
|
|
|
|
|
throw new InvalidRequestError('Document could not be found for shareId');
|
|
|
|
|
}
|
2018-05-13 20:26:06 +00:00
|
|
|
|
document = share.document;
|
|
|
|
|
} else {
|
|
|
|
|
document = await Document.findById(id);
|
2018-05-25 05:15:36 +00:00
|
|
|
|
authorize(user, 'read', document);
|
2018-05-13 20:26:06 +00:00
|
|
|
|
}
|
2017-10-17 05:36:44 +00:00
|
|
|
|
|
2018-05-25 05:15:36 +00:00
|
|
|
|
const isPublic = cannot(user, 'read', document);
|
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
|
ctx.body = {
|
2018-05-24 06:59:00 +00:00
|
|
|
|
data: await presentDocument(ctx, document, { isPublic }),
|
2017-10-17 05:36:44 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
2016-08-03 12:36:50 +00:00
|
|
|
|
|
2018-09-30 04:24:07 +00:00
|
|
|
|
router.post('documents.revision', auth(), async ctx => {
|
|
|
|
|
let { id, revisionId } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
ctx.assertPresent(revisionId, 'revisionId is required');
|
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
authorize(ctx.state.user, 'read', document);
|
|
|
|
|
|
|
|
|
|
const revision = await Revision.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: revisionId,
|
|
|
|
|
documentId: document.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data: presentRevision(ctx, revision),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-17 05:36:44 +00:00
|
|
|
|
router.post('documents.revisions', auth(), pagination(), async ctx => {
|
|
|
|
|
let { id, sort = 'updatedAt', direction } = ctx.body;
|
|
|
|
|
if (direction !== 'ASC') direction = 'DESC';
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
const document = await Document.findById(id);
|
2016-05-20 03:46:34 +00:00
|
|
|
|
|
2018-02-18 08:11:48 +00:00
|
|
|
|
authorize(ctx.state.user, 'read', document);
|
2017-10-17 05:36:44 +00:00
|
|
|
|
|
|
|
|
|
const revisions = await Revision.findAll({
|
|
|
|
|
where: { documentId: id },
|
|
|
|
|
order: [[sort, direction]],
|
|
|
|
|
offset: ctx.state.pagination.offset,
|
|
|
|
|
limit: ctx.state.pagination.limit,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await Promise.all(
|
2018-11-23 19:12:14 +00:00
|
|
|
|
revisions.map((revision, index) => presentRevision(ctx, revision))
|
2017-10-17 05:36:44 +00:00
|
|
|
|
);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
|
|
ctx.body = {
|
2017-10-17 05:36:44 +00:00
|
|
|
|
pagination: ctx.state.pagination,
|
|
|
|
|
data,
|
2017-06-26 00:21:33 +00:00
|
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2018-09-30 04:24:07 +00:00
|
|
|
|
router.post('documents.restore', auth(), async ctx => {
|
|
|
|
|
const { id, revisionId } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
ctx.assertPresent(revisionId, 'revisionId is required');
|
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
authorize(user, 'update', document);
|
|
|
|
|
|
|
|
|
|
const revision = await Revision.findById(revisionId);
|
|
|
|
|
authorize(document, 'restore', revision);
|
|
|
|
|
|
|
|
|
|
document.text = revision.text;
|
|
|
|
|
document.title = revision.title;
|
|
|
|
|
await document.save();
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
data: await presentDocument(ctx, document),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-04 00:50:50 +00:00
|
|
|
|
router.post('documents.search', auth(), pagination(), async ctx => {
|
2016-08-01 07:12:55 +00:00
|
|
|
|
const { query } = ctx.body;
|
2017-12-04 00:50:50 +00:00
|
|
|
|
const { offset, limit } = ctx.state.pagination;
|
2016-07-13 06:43:41 +00:00
|
|
|
|
ctx.assertPresent(query, 'query is required');
|
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
|
const user = ctx.state.user;
|
2018-08-05 01:32:56 +00:00
|
|
|
|
const results = await Document.searchForUser(user, query, {
|
2017-12-04 00:50:50 +00:00
|
|
|
|
offset,
|
|
|
|
|
limit,
|
|
|
|
|
});
|
2016-07-13 06:43:41 +00:00
|
|
|
|
|
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 => {
|
|
|
|
|
const document = await presentDocument(ctx, result.document);
|
|
|
|
|
return { ...result, document };
|
|
|
|
|
})
|
2017-04-27 04:47:03 +00:00
|
|
|
|
);
|
2016-07-13 06:43:41 +00:00
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
pagination: ctx.state.pagination,
|
2016-08-01 07:12:55 +00:00
|
|
|
|
data,
|
2016-07-13 06:43:41 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
|
router.post('documents.pin', auth(), async ctx => {
|
|
|
|
|
const { id } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
|
|
|
|
|
authorize(user, 'update', document);
|
|
|
|
|
|
|
|
|
|
document.pinnedById = user.id;
|
|
|
|
|
await document.save();
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
data: await presentDocument(ctx, document),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('documents.unpin', auth(), async ctx => {
|
|
|
|
|
const { id } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
const user = ctx.state.user;
|
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
|
|
|
|
|
authorize(user, 'update', document);
|
|
|
|
|
|
|
|
|
|
document.pinnedById = null;
|
|
|
|
|
await document.save();
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
|
data: await presentDocument(ctx, document),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
|
router.post('documents.star', auth(), async ctx => {
|
|
|
|
|
const { id } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
2018-03-01 07:28:36 +00:00
|
|
|
|
const user = ctx.state.user;
|
2017-06-26 00:21:33 +00:00
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
|
authorize(user, 'read', document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
|
|
await Star.findOrCreate({
|
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('documents.unstar', auth(), async ctx => {
|
|
|
|
|
const { id } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
2018-03-01 07:28:36 +00:00
|
|
|
|
const user = ctx.state.user;
|
2017-06-26 00:21:33 +00:00
|
|
|
|
const document = await Document.findById(id);
|
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
|
authorize(user, 'read', document);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
|
|
|
|
|
await Star.destroy({
|
|
|
|
|
where: { documentId: document.id, userId: user.id },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
|
router.post('documents.create', auth(), async ctx => {
|
2018-02-28 06:41:12 +00:00
|
|
|
|
const { title, text, publish, parentDocument, index } = ctx.body;
|
|
|
|
|
const collectionId = ctx.body.collection;
|
|
|
|
|
ctx.assertPresent(collectionId, 'collection is required');
|
|
|
|
|
ctx.assertUuid(collectionId, 'collection must be an uuid');
|
2016-05-20 03:46:34 +00:00
|
|
|
|
ctx.assertPresent(title, 'title is required');
|
|
|
|
|
ctx.assertPresent(text, 'text is required');
|
2017-06-04 21:40:27 +00:00
|
|
|
|
if (parentDocument)
|
|
|
|
|
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
|
|
|
|
|
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;
|
2018-02-18 19:08:43 +00:00
|
|
|
|
authorize(user, 'create', Document);
|
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
|
const collection = await Collection.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
|
|
|
|
},
|
|
|
|
|
});
|
2018-02-28 06:41:12 +00:00
|
|
|
|
authorize(user, 'publish', collection);
|
2016-05-20 03:46:34 +00:00
|
|
|
|
|
2017-06-04 21:40:27 +00:00
|
|
|
|
let parentDocumentObj = {};
|
2018-02-28 06:41:12 +00:00
|
|
|
|
if (parentDocument && collection.type === 'atlas') {
|
2017-06-04 21:40:27 +00:00
|
|
|
|
parentDocumentObj = await Document.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: parentDocument,
|
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
|
|
|
|
});
|
2018-02-25 04:44:13 +00:00
|
|
|
|
authorize(user, 'read', parentDocumentObj);
|
2017-06-04 21:40:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
|
let document = await Document.create({
|
2017-06-04 21:40:27 +00:00
|
|
|
|
parentDocumentId: parentDocumentObj.id,
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-04 03:36:25 +00:00
|
|
|
|
if (publish) {
|
|
|
|
|
await document.publish();
|
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
|
|
|
|
|
document = await Document.find({
|
2018-04-04 03:36:25 +00:00
|
|
|
|
where: { id: document.id, publishedAt: document.publishedAt },
|
2018-02-28 06:41:12 +00:00
|
|
|
|
});
|
2017-09-12 07:10:02 +00:00
|
|
|
|
|
2016-08-21 22:45:48 +00:00
|
|
|
|
ctx.body = {
|
2017-07-15 05:15:56 +00:00
|
|
|
|
data: await presentDocument(ctx, document),
|
2016-08-21 22:45:48 +00:00
|
|
|
|
};
|
2016-05-20 03:46:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
|
router.post('documents.update', auth(), async ctx => {
|
2018-05-07 05:13:52 +00:00
|
|
|
|
const { id, title, text, publish, autosave, done, lastRevision } = ctx.body;
|
2016-05-26 04:26:06 +00:00
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
2017-06-05 05:12:36 +00:00
|
|
|
|
ctx.assertPresent(title || text, 'title or text is required');
|
2016-05-26 04:26:06 +00:00
|
|
|
|
|
|
|
|
|
const user = ctx.state.user;
|
2017-07-10 03:32:38 +00:00
|
|
|
|
const document = await Document.findById(id);
|
2018-02-18 08:11:48 +00:00
|
|
|
|
|
|
|
|
|
authorize(ctx.state.user, 'update', document);
|
2016-05-26 04:26:06 +00:00
|
|
|
|
|
2017-11-25 21:04:54 +00:00
|
|
|
|
if (lastRevision && lastRevision !== document.revisionCount) {
|
2018-02-25 04:44:13 +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;
|
|
|
|
|
if (text) document.text = text;
|
2016-06-26 18:23:03 +00:00
|
|
|
|
document.lastModifiedById = user.id;
|
2016-05-26 04:26:06 +00:00
|
|
|
|
|
2018-04-04 03:36:25 +00:00
|
|
|
|
if (publish) {
|
|
|
|
|
await document.publish();
|
|
|
|
|
} else {
|
2018-05-07 05:13:52 +00:00
|
|
|
|
await document.save({ autosave });
|
2018-04-04 03:36:25 +00:00
|
|
|
|
|
|
|
|
|
if (document.publishedAt && done) {
|
|
|
|
|
events.add({ name: 'documents.update', model: document });
|
2018-02-28 06:41:12 +00:00
|
|
|
|
}
|
2017-10-09 07:05:15 +00:00
|
|
|
|
}
|
2017-07-10 03:27:06 +00:00
|
|
|
|
|
2017-06-06 06:50:32 +00:00
|
|
|
|
ctx.body = {
|
2017-10-09 07:05:15 +00:00
|
|
|
|
data: await presentDocument(ctx, document),
|
2017-06-06 06:50:32 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('documents.move', auth(), async ctx => {
|
|
|
|
|
const { id, parentDocument, index } = ctx.body;
|
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
if (parentDocument)
|
2018-02-25 04:44:13 +00:00
|
|
|
|
ctx.assertUuid(parentDocument, 'parentDocument must be a uuid');
|
2017-06-06 06:50:32 +00:00
|
|
|
|
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
|
|
|
|
|
|
2018-02-25 04:44:13 +00:00
|
|
|
|
const user = ctx.state.user;
|
2017-06-26 00:21:33 +00:00
|
|
|
|
const document = await Document.findById(id);
|
2018-02-25 04:44:13 +00:00
|
|
|
|
authorize(user, 'update', document);
|
2017-10-17 05:36:44 +00:00
|
|
|
|
|
2018-02-18 08:11:48 +00:00
|
|
|
|
const collection = document.collection;
|
2017-10-09 07:05:15 +00:00
|
|
|
|
if (collection.type !== 'atlas')
|
2018-02-25 04:44:13 +00:00
|
|
|
|
throw new InvalidRequestError('This document can’t be moved');
|
2017-06-06 06:50:32 +00:00
|
|
|
|
|
|
|
|
|
// Set parent document
|
|
|
|
|
if (parentDocument) {
|
2017-06-26 00:21:33 +00:00
|
|
|
|
const parent = await Document.findById(parentDocument);
|
2018-02-25 04:44:13 +00:00
|
|
|
|
authorize(user, 'update', parent);
|
2017-06-06 06:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parentDocument === id)
|
2018-02-25 04:44:13 +00:00
|
|
|
|
throw new InvalidRequestError('Infinite loop detected and prevented!');
|
2017-06-06 06:50:32 +00:00
|
|
|
|
|
|
|
|
|
// If no parent document is provided, set it as null (move to root level)
|
|
|
|
|
document.parentDocumentId = parentDocument;
|
|
|
|
|
await document.save();
|
|
|
|
|
|
2017-10-09 07:05:15 +00:00
|
|
|
|
await collection.moveDocument(document, index);
|
2017-09-04 21:48:56 +00:00
|
|
|
|
// Update collection
|
|
|
|
|
document.collection = collection;
|
2017-06-06 06:50:32 +00:00
|
|
|
|
|
2016-05-26 04:26:06 +00:00
|
|
|
|
ctx.body = {
|
2017-07-07 05:02:55 +00:00
|
|
|
|
data: await presentDocument(ctx, document),
|
2016-05-26 04:26:06 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-27 04:47:03 +00:00
|
|
|
|
router.post('documents.delete', auth(), async ctx => {
|
|
|
|
|
const { id } = ctx.body;
|
2016-05-30 18:15:35 +00:00
|
|
|
|
ctx.assertPresent(id, 'id is required');
|
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
|
const document = await Document.findById(id);
|
2018-02-18 08:11:48 +00:00
|
|
|
|
authorize(ctx.state.user, 'delete', document);
|
2016-05-30 18:15:35 +00:00
|
|
|
|
|
2018-02-18 08:11:48 +00:00
|
|
|
|
const collection = document.collection;
|
2018-05-19 22:38:08 +00:00
|
|
|
|
if (collection && collection.type === 'atlas') {
|
2017-10-09 07:05:15 +00:00
|
|
|
|
// Delete document and all of its children
|
2018-02-25 04:44:13 +00:00
|
|
|
|
await collection.removeDocument(document);
|
2016-08-14 08:50:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-25 04:44:13 +00:00
|
|
|
|
await document.destroy();
|
2016-07-07 04:36:50 +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;
|