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

354 lines
9.4 KiB
JavaScript
Raw Normal View History

// @flow
2016-05-20 03:46:34 +00:00
import Router from 'koa-router';
import httpErrors from 'http-errors';
2016-08-15 19:41:51 +00:00
2016-08-27 17:48:56 +00:00
import auth from './middlewares/authentication';
import pagination from './middlewares/pagination';
2017-10-17 05:36:44 +00:00
import { presentDocument, presentRevision } from '../presenters';
import { Document, Collection, Star, View, Revision } from '../models';
const authDocumentForUser = (ctx, document) => {
const user = ctx.state.user;
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
};
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 => {
let { sort = 'updatedAt', direction, collection } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
const user = ctx.state.user;
2017-11-20 05:32:18 +00:00
let where = { teamId: user.teamId };
if (collection) where = { ...where, atlasId: collection };
2017-11-20 05:32:18 +00:00
2017-07-15 23:08:12 +00:00
const userId = user.id;
const starredScope = { method: ['withStarred', userId] };
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.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
2017-04-27 04:47:03 +00:00
router.post('documents.info', auth(), async ctx => {
2016-08-12 13:36:48 +00:00
const { id } = ctx.body;
2016-05-20 03:46:34 +00:00
ctx.assertPresent(id, 'id is required');
const document = await Document.findById(id);
2016-05-20 03:46:34 +00:00
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, document);
ctx.body = {
data: await presentDocument(ctx, document),
};
});
2016-08-03 12:36:50 +00:00
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
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, document);
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 => presentRevision(ctx, revision))
);
ctx.body = {
2017-10-17 05:36:44 +00:00
pagination: ctx.state.pagination,
data,
};
2016-05-20 03:46:34 +00:00
});
2017-04-27 04:47:03 +00:00
router.post('documents.search', auth(), async ctx => {
2016-08-01 07:12:55 +00:00
const { query } = ctx.body;
2016-07-13 06:43:41 +00:00
ctx.assertPresent(query, 'query is required');
const user = await ctx.state.user;
2016-08-23 06:37:01 +00:00
const documents = await Document.searchForUser(user, query);
2016-07-13 06:43:41 +00:00
const data = await Promise.all(
documents.map(async document => await presentDocument(ctx, 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.star', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = await ctx.state.user;
const document = await Document.findById(id);
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, 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 = await ctx.state.user;
const document = await Document.findById(id);
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, 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 { collection, title, text, parentDocument, index } = ctx.body;
2016-08-05 15:09:14 +00:00
ctx.assertPresent(collection, 'collection is required');
ctx.assertUuid(collection, '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;
2017-05-27 18:08:52 +00:00
const ownerCollection = await Collection.findOne({
2016-05-20 03:46:34 +00:00
where: {
2016-08-05 15:09:14 +00:00
id: collection,
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
2016-05-20 03:46:34 +00:00
},
});
2016-08-12 13:36:48 +00:00
if (!ownerCollection) throw httpErrors.BadRequest();
2016-05-20 03:46:34 +00:00
let parentDocumentObj = {};
if (parentDocument && ownerCollection.type === 'atlas') {
parentDocumentObj = await Document.findOne({
where: {
id: parentDocument,
atlasId: ownerCollection.id,
},
2016-08-21 18:12:24 +00:00
});
}
const newDocument = await Document.create({
parentDocumentId: parentDocumentObj.id,
atlasId: ownerCollection.id,
teamId: user.teamId,
userId: user.id,
lastModifiedById: user.id,
createdById: user.id,
title,
text,
});
// reload to get all of the data needed to present (user, collection etc)
const document = await Document.findById(newDocument.id);
2017-06-05 07:36:50 +00:00
if (ownerCollection.type === 'atlas') {
await ownerCollection.addDocumentToStructure(document, index);
}
2016-08-21 22:45:48 +00:00
document.collection = ownerCollection;
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 => {
const { id, title, text, 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);
const collection = document.collection;
2016-05-26 04:26:06 +00:00
if (lastRevision && lastRevision !== document.revisionCount) {
throw httpErrors.BadRequest('Document has changed since last revision');
}
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, document);
2016-05-26 04:26:06 +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
await document.save();
if (collection.type === 'atlas') {
await collection.updateDocument(document);
}
document.collection = collection;
2016-07-01 06:47:49 +00:00
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)
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
const document = await Document.findById(id);
const collection = await Collection.findById(document.atlasId);
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, document);
if (collection.type !== 'atlas')
throw httpErrors.BadRequest("This document can't be moved");
2017-06-06 06:50:32 +00:00
// Set parent document
if (parentDocument) {
const parent = await Document.findById(parentDocument);
2017-10-02 06:42:17 +00:00
if (!parent || parent.atlasId !== document.atlasId)
2017-06-06 06:50:32 +00:00
throw httpErrors.BadRequest(
'Invalid parentDocument (must be same collection)'
);
}
if (parentDocument === id)
throw httpErrors.BadRequest('Infinite loop detected and prevented!');
// 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);
2017-05-27 18:08:52 +00:00
const collection = await Collection.findById(document.atlasId);
2016-05-30 18:15:35 +00:00
2017-10-17 05:36:44 +00:00
authDocumentForUser(ctx, document);
2016-05-30 18:15:35 +00:00
2016-08-05 15:09:14 +00:00
if (collection.type === 'atlas') {
2016-07-11 00:02:45 +00:00
// Don't allow deletion of root docs
2017-06-05 07:36:50 +00:00
if (collection.documentStructure.length === 1) {
throw httpErrors.BadRequest(
"Unable to delete collection's only document"
);
2016-07-11 00:02:45 +00:00
}
2016-06-23 07:19:45 +00:00
// Delete document and all of its children
2016-07-11 00:02:45 +00:00
try {
await collection.removeDocument(document);
2016-07-11 00:02:45 +00:00
} catch (e) {
throw httpErrors.BadRequest('Error while deleting');
2016-08-01 07:11:28 +00:00
}
}
// Delete the actual document
try {
await document.destroy();
} catch (e) {
throw httpErrors.BadRequest('Error while deleting document');
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;