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

264 lines
6.8 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-21 22:45:48 +00:00
import isUUID from 'validator/lib/isUUID';
2016-05-20 03:46:34 +00:00
2016-08-18 21:42:53 +00:00
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
2016-08-15 19:41:51 +00:00
2016-08-27 17:48:56 +00:00
import auth from './middlewares/authentication';
2016-05-20 03:46:34 +00:00
import { presentDocument } from '../presenters';
2017-05-27 18:08:52 +00:00
import { Document, Collection } from '../models';
2016-05-20 03:46:34 +00:00
const router = new Router();
2017-04-27 04:47:03 +00:00
const getDocumentForId = async id => {
2016-08-21 22:45:48 +00:00
try {
let document;
if (isUUID(id)) {
2016-08-18 21:42:53 +00:00
document = await Document.findOne({
where: {
id,
},
});
2016-08-21 22:45:48 +00:00
} else if (id.match(URL_REGEX)) {
document = await Document.findOne({
where: {
urlId: id.match(URL_REGEX)[1],
},
});
} else {
2016-08-18 21:42:53 +00:00
throw httpErrors.NotFound();
}
2016-08-21 22:45:48 +00:00
return document;
} catch (e) {
// Invalid UUID
throw httpErrors.NotFound();
2016-08-15 19:41:51 +00:00
}
};
2016-05-30 19:37:07 +00:00
// FIXME: This really needs specs :/
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');
2016-08-15 19:41:51 +00:00
const document = await getDocumentForId(id);
2016-05-20 03:46:34 +00:00
2016-08-03 12:36:50 +00:00
if (!document) throw httpErrors.NotFound();
// Don't expose private documents outside the team
if (document.private) {
if (!ctx.state.user) throw httpErrors.NotFound();
2016-05-20 03:46:34 +00:00
2016-06-20 07:18:03 +00:00
const user = await ctx.state.user;
if (document.teamId !== user.teamId) {
2016-05-31 05:56:49 +00:00
throw httpErrors.NotFound();
}
ctx.body = {
2016-08-15 12:52:07 +00:00
data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}),
};
} else {
ctx.body = {
2016-08-15 12:52:07 +00:00
data: await presentDocument(ctx, document, {
includeCollaborators: true,
}),
};
}
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
2016-08-12 13:36:48 +00:00
const data = [];
2017-04-27 04:47:03 +00:00
await Promise.all(
documents.map(async document => {
data.push(
await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
})
);
})
);
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
};
});
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,
});
2017-06-05 07:36:50 +00:00
if (ownerCollection.type === 'atlas') {
await ownerCollection.addDocumentToStructure(newDocument, index);
}
2016-08-21 22:45:48 +00:00
ctx.body = {
data: await presentDocument(ctx, newDocument, {
2016-08-21 22:45:48 +00:00
includeCollection: true,
includeCollaborators: true,
2017-06-05 07:36:50 +00:00
collection: ownerCollection,
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 } = 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;
2016-08-15 19:41:51 +00:00
const document = await getDocumentForId(id);
2016-05-26 04:26:06 +00:00
2017-06-05 05:12:36 +00:00
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
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();
2017-05-27 18:08:52 +00:00
const collection = await Collection.findById(document.atlasId);
2016-08-05 15:09:14 +00:00
if (collection.type === 'atlas') {
await collection.updateDocument(document);
2016-07-01 06:47:49 +00:00
}
2017-06-06 06:50:32 +00:00
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
collection: collection,
}),
};
});
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 user = ctx.state.user;
const document = await getDocumentForId(id);
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
// Set parent document
if (parentDocument) {
const parent = await getDocumentForId(parentDocument);
if (parent.atlasId !== document.atlasId)
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();
const collection = await Collection.findById(document.atlasId);
if (collection.type === 'atlas') {
await collection.deleteDocument(document);
await collection.addDocumentToStructure(document, index);
}
2016-05-26 04:26:06 +00:00
ctx.body = {
2016-08-15 12:52:07 +00:00
data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
2017-06-05 05:12:36 +00:00
collection: collection,
2016-08-15 12:52:07 +00:00
}),
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 user = ctx.state.user;
2016-08-15 19:41:51 +00:00
const document = await getDocumentForId(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-04-27 04:47:03 +00:00
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
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
2016-07-11 00:02:45 +00:00
// Delete all chilren
try {
2016-08-05 15:09:14 +00:00
await collection.deleteDocument(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;