This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/documents.js

498 lines
13 KiB
JavaScript
Raw Normal View History

// @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';
import auth from '../middlewares/authentication';
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';
import { InvalidRequestError } from '../errors';
import events from '../events';
import policy from '../policies';
2017-10-17 05:36:44 +00:00
2018-05-05 23:16:08 +00:00
const Op = Sequelize.Op;
const { authorize, cannot } = policy;
2016-05-20 03:46:34 +00:00
const router = new Router();
2017-10-17 05:36:44 +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;
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 };
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,
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,
};
});
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,
pinnedById: {
2018-05-05 23:16:08 +00:00
// $FlowFixMe
[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,
};
});
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]],
include: [
{
model: Document,
required: true,
include: [
{
model: Star,
as: 'starred',
where: { userId: user.id },
required: false,
},
],
},
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
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 } }],
},
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
views.map(view => presentDocument(ctx, view.document))
);
ctx.body = {
pagination: ctx.state.pagination,
data,
};
});
2016-08-15 19:41:51 +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
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');
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) {
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);
authorize(user, 'read', document);
2018-05-13 20:26:06 +00:00
}
2017-10-17 05:36:44 +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
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
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(
revisions.map((revision, index) => presentRevision(ctx, revision))
2017-10-17 05:36:44 +00:00
);
ctx.body = {
2017-10-17 05:36:44 +00:00
pagination: ctx.state.pagination,
data,
};
2016-05-20 03:46:34 +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');
const user = ctx.state.user;
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
const data = await Promise.all(
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
};
});
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),
};
});
router.post('documents.star', 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, 'read', document);
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');
const user = ctx.state.user;
const document = await Document.findById(id);
authorize(user, 'read', document);
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 => {
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');
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
const user = ctx.state.user;
authorize(user, 'create', Document);
const collection = await Collection.findOne({
2016-05-20 03:46:34 +00:00
where: {
id: collectionId,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-20 03:46:34 +00:00
},
});
authorize(user, 'publish', collection);
2016-05-20 03:46:34 +00:00
let parentDocumentObj = {};
if (parentDocument && collection.type === 'atlas') {
parentDocumentObj = await Document.findOne({
where: {
id: parentDocument,
2018-08-08 06:23:26 +00:00
collectionId: collection.id,
},
2016-08-21 18:12:24 +00:00
});
2018-02-25 04:44:13 +00:00
authorize(user, 'read', parentDocumentObj);
}
let document = await Document.create({
parentDocumentId: parentDocumentObj.id,
2018-08-08 06:23:26 +00:00
collectionId: collection.id,
teamId: user.teamId,
userId: user.id,
lastModifiedById: user.id,
createdById: user.id,
title,
text,
});
if (publish) {
await document.publish();
}
2016-08-21 22:45:48 +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({
where: { id: document.id, publishedAt: document.publishedAt },
});
2016-08-21 22:45:48 +00:00
ctx.body = {
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);
authorize(ctx.state.user, 'update', document);
2016-05-26 04:26:06 +00:00
if (lastRevision && lastRevision !== document.revisionCount) {
2018-02-25 04:44:13 +00:00
throw new InvalidRequestError('Document has changed since last revision');
}
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
if (publish) {
await document.publish();
} else {
2018-05-07 05:13:52 +00:00
await document.save({ autosave });
if (document.publishedAt && done) {
events.add({ name: 'documents.update', model: document });
}
}
2017-06-06 06:50:32 +00:00
ctx.body = {
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;
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
const collection = document.collection;
if (collection.type !== 'atlas')
2018-02-25 04:44:13 +00:00
throw new InvalidRequestError('This document cant be moved');
2017-06-06 06:50:32 +00:00
// Set parent document
if (parentDocument) {
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();
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 = {
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');
const document = await Document.findById(id);
authorize(ctx.state.user, 'delete', document);
2016-05-30 18:15:35 +00:00
const collection = document.collection;
if (collection && collection.type === 'atlas') {
// Delete document and all of its children
2018-02-25 04:44:13 +00:00
await collection.removeDocument(document);
}
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;