Websocket Support (#937)

* Atom / RSS meta link

* Spike

* Feeling good about this spike now

* Remove document.collection

* Remove koa.ctx from all presenters to make them portable outside requests

* Remove full serialized model from events
Move events.add to controllers for now, will eventually be in commands

* collections.create event
parentDocument -> parentDocumentId

* Fix up deprecated tests

* Fixed: Doc creation

* documents.move

* Handle collection deleted

* 💚

* Authorize room join requests

* Move starred data structure
Account for documents with no context on sockets

* Add socket.io-redis

* Add WEBSOCKETS_ENABLED env variable to disable websockets entirely for self hosted
New installations will default to true, existing installations to false

* 💚 No need for promise response here

* Reload notice
This commit is contained in:
Tom Moor
2019-04-17 19:11:23 -07:00
committed by GitHub
parent 4a571a088e
commit 07a941a65d
93 changed files with 2441 additions and 744 deletions

View File

@@ -23,7 +23,7 @@ router.post('apiKeys.create', auth(), async ctx => {
});
ctx.body = {
data: presentApiKey(ctx, key),
data: presentApiKey(key),
};
});
@@ -38,11 +38,9 @@ router.post('apiKeys.list', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
const data = keys.map(key => presentApiKey(ctx, key));
ctx.body = {
pagination: ctx.state.pagination,
data,
data: keys.map(presentApiKey),
};
});

View File

@@ -12,8 +12,8 @@ router.post('auth.info', auth(), async ctx => {
ctx.body = {
data: {
user: await presentUser(ctx, user, { includeDetails: true }),
team: await presentTeam(ctx, team),
user: presentUser(user, { includeDetails: true }),
team: presentTeam(team),
},
};
});

View File

@@ -7,6 +7,7 @@ import { Collection, CollectionUser, Team, User } from '../models';
import { ValidationError, InvalidRequestError } from '../errors';
import { exportCollection, exportCollections } from '../logistics';
import policy from '../policies';
import events from '../events';
const { authorize } = policy;
const router = new Router();
@@ -32,8 +33,15 @@ router.post('collections.create', auth(), async ctx => {
private: isPrivate,
});
events.add({
name: 'collections.create',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
ctx.body = {
data: await presentCollection(ctx, collection),
data: await presentCollection(collection),
};
});
@@ -45,7 +53,7 @@ router.post('collections.info', auth(), async ctx => {
authorize(ctx.state.user, 'read', collection);
ctx.body = {
data: await presentCollection(ctx, collection),
data: await presentCollection(collection),
};
});
@@ -71,6 +79,14 @@ router.post('collections.add_user', auth(), async ctx => {
createdById: ctx.state.user.id,
});
events.add({
name: 'collections.add_user',
modelId: userId,
collectionId: collection.id,
teamId: collection.teamId,
actorId: ctx.state.user.id,
});
ctx.body = {
success: true,
};
@@ -93,6 +109,14 @@ router.post('collections.remove_user', auth(), async ctx => {
await collection.removeUser(user);
events.add({
name: 'collections.remove_user',
modelId: userId,
collectionId: collection.id,
teamId: collection.teamId,
actorId: ctx.state.user.id,
});
ctx.body = {
success: true,
};
@@ -107,12 +131,8 @@ router.post('collections.users', auth(), async ctx => {
const users = await collection.getUsers();
const data = await Promise.all(
users.map(async user => await presentUser(ctx, user))
);
ctx.body = {
data,
data: users.map(presentUser),
};
});
@@ -176,8 +196,15 @@ router.post('collections.update', auth(), async ctx => {
collection.private = isPrivate;
await collection.save();
events.add({
name: 'collections.update',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
ctx.body = {
data: await presentCollection(ctx, collection),
data: presentCollection(collection),
};
});
@@ -196,9 +223,7 @@ router.post('collections.list', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
collections.map(
async collection => await presentCollection(ctx, collection)
)
collections.map(async collection => await presentCollection(collection))
);
ctx.body = {
@@ -209,16 +234,24 @@ router.post('collections.list', auth(), pagination(), async ctx => {
router.post('collections.delete', auth(), async ctx => {
const { id } = ctx.body;
const user = ctx.state.user;
ctx.assertUuid(id, 'id is required');
const collection = await Collection.findById(id);
authorize(ctx.state.user, 'delete', collection);
authorize(user, 'delete', collection);
const total = await Collection.count();
if (total === 1) throw new ValidationError('Cannot delete last collection');
await collection.destroy();
events.add({
name: 'collections.delete',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
ctx.body = {
success: true,
};

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { flushdb, seed } from '../test/support';
import { buildUser, buildCollection } from '../test/factories';
import { Collection } from '../models';

View File

@@ -60,7 +60,7 @@ router.post('documents.list', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
documents.map(document => presentDocument(document))
);
ctx.body = {
@@ -96,7 +96,7 @@ router.post('documents.pinned', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
documents.map(document => presentDocument(document))
);
ctx.body = {
@@ -128,7 +128,7 @@ router.post('documents.archived', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
documents.map(document => presentDocument(document))
);
ctx.body = {
@@ -169,7 +169,7 @@ router.post('documents.viewed', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
views.map(view => presentDocument(ctx, view.document))
views.map(view => presentDocument(view.document))
);
ctx.body = {
@@ -212,7 +212,7 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
stars.map(star => presentDocument(ctx, star.document))
stars.map(star => presentDocument(star.document))
);
ctx.body = {
@@ -241,7 +241,7 @@ router.post('documents.drafts', auth(), pagination(), async ctx => {
});
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
documents.map(document => presentDocument(document))
);
ctx.body = {
@@ -284,7 +284,7 @@ router.post('documents.info', auth({ required: false }), async ctx => {
const isPublic = cannot(user, 'read', document);
ctx.body = {
data: await presentDocument(ctx, document, { isPublic }),
data: await presentDocument(document, { isPublic }),
};
});
@@ -305,7 +305,7 @@ router.post('documents.revision', auth(), async ctx => {
ctx.body = {
pagination: ctx.state.pagination,
data: presentRevision(ctx, revision),
data: presentRevision(revision),
};
});
@@ -324,9 +324,7 @@ router.post('documents.revisions', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
revisions.map((revision, index) => presentRevision(ctx, revision))
);
const data = await Promise.all(revisions.map(presentRevision));
ctx.body = {
pagination: ctx.state.pagination,
@@ -347,8 +345,15 @@ router.post('documents.restore', auth(), async ctx => {
// restore a previously archived document
await document.unarchive(user.id);
// restore a document to a specific revision
events.add({
name: 'documents.unarchive',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
} else if (revisionId) {
// restore a document to a specific revision
authorize(user, 'update', document);
const revision = await Revision.findById(revisionId);
@@ -357,12 +362,20 @@ router.post('documents.restore', auth(), async ctx => {
document.text = revision.text;
document.title = revision.title;
await document.save();
events.add({
name: 'documents.restore',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
} else {
ctx.assertPresent(revisionId, 'revisionId is required');
}
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
@@ -380,7 +393,7 @@ router.post('documents.search', auth(), pagination(), async ctx => {
const data = await Promise.all(
results.map(async result => {
const document = await presentDocument(ctx, result.document);
const document = await presentDocument(result.document);
return { ...result, document };
})
);
@@ -402,8 +415,16 @@ router.post('documents.pin', auth(), async ctx => {
document.pinnedById = user.id;
await document.save();
events.add({
name: 'documents.pin',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
@@ -418,8 +439,16 @@ router.post('documents.unpin', auth(), async ctx => {
document.pinnedById = null;
await document.save();
events.add({
name: 'documents.unpin',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
@@ -434,6 +463,14 @@ router.post('documents.star', auth(), async ctx => {
await Star.findOrCreate({
where: { documentId: document.id, userId: user.id },
});
events.add({
name: 'documents.star',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
});
router.post('documents.unstar', auth(), async ctx => {
@@ -447,16 +484,32 @@ router.post('documents.unstar', auth(), async ctx => {
await Star.destroy({
where: { documentId: document.id, userId: user.id },
});
events.add({
name: 'documents.unstar',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
});
router.post('documents.create', auth(), async ctx => {
const { title, text, publish, parentDocument, index } = ctx.body;
const collectionId = ctx.body.collection;
ctx.assertUuid(collectionId, 'collection must be an uuid');
const {
title,
text,
publish,
collectionId,
parentDocumentId,
index,
} = ctx.body;
ctx.assertUuid(collectionId, 'collectionId must be an uuid');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
if (parentDocument)
ctx.assertUuid(parentDocument, 'parentDocument must be an uuid');
if (parentDocumentId) {
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be an uuid');
}
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
const user = ctx.state.user;
@@ -470,19 +523,19 @@ router.post('documents.create', auth(), async ctx => {
});
authorize(user, 'publish', collection);
let parentDocumentObj = {};
if (parentDocument && collection.type === 'atlas') {
parentDocumentObj = await Document.findOne({
let parentDocument;
if (parentDocumentId && collection.type === 'atlas') {
parentDocument = await Document.findOne({
where: {
id: parentDocument,
id: parentDocumentId,
collectionId: collection.id,
},
});
authorize(user, 'read', parentDocumentObj);
authorize(user, 'read', parentDocument);
}
let document = await Document.create({
parentDocumentId: parentDocumentObj.id,
parentDocumentId,
collectionId: collection.id,
teamId: user.teamId,
userId: user.id,
@@ -492,8 +545,24 @@ router.post('documents.create', auth(), async ctx => {
text,
});
events.add({
name: 'documents.create',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
if (publish) {
await document.publish();
events.add({
name: 'documents.publish',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
}
// reload to get all of the data needed to present (user, collection etc)
@@ -504,12 +573,12 @@ router.post('documents.create', auth(), async ctx => {
});
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
router.post('documents.update', auth(), async ctx => {
const { id, title, text, publish, autosave, done, lastRevision } = ctx.body;
const { id, title, text, publish, autosave, lastRevision } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(title || text, 'title or text is required');
@@ -529,16 +598,28 @@ router.post('documents.update', auth(), async ctx => {
if (publish) {
await document.publish();
events.add({
name: 'documents.publish',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
} else {
await document.save({ autosave });
if (document.publishedAt && done) {
events.add({ name: 'documents.update', model: document });
}
events.add({
name: 'documents.update',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
}
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
@@ -587,10 +668,10 @@ router.post('documents.move', auth(), async ctx => {
ctx.body = {
data: {
documents: await Promise.all(
documents.map(document => presentDocument(ctx, document))
documents.map(document => presentDocument(document))
),
collections: await Promise.all(
collections.map(collection => presentCollection(ctx, collection))
collections.map(collection => presentCollection(collection))
),
},
};
@@ -606,8 +687,16 @@ router.post('documents.archive', auth(), async ctx => {
await document.archive(user.id);
events.add({
name: 'documents.archive',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
ctx.body = {
data: await presentDocument(ctx, document),
data: await presentDocument(document),
};
});
@@ -621,6 +710,14 @@ router.post('documents.delete', auth(), async ctx => {
await document.delete();
events.add({
name: 'documents.delete',
modelId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
});
ctx.body = {
success: true,
};

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { Document, View, Star, Revision } from '../models';
import { flushdb, seed } from '../test/support';
import {
@@ -79,7 +79,6 @@ describe('#documents.info', async () => {
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(document.id);
expect(body.data.collection).toEqual(undefined);
expect(body.data.createdBy).toEqual(undefined);
expect(body.data.updatedBy).toEqual(undefined);
});
@@ -113,7 +112,7 @@ describe('#documents.info', async () => {
});
it('should return document from shareId with token', async () => {
const { user, document, collection } = await seed();
const { user, document } = await seed();
const share = await buildShare({
documentId: document.id,
teamId: document.teamId,
@@ -126,7 +125,6 @@ describe('#documents.info', async () => {
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(document.id);
expect(body.data.collection.id).toEqual(collection.id);
expect(body.data.createdBy.id).toEqual(user.id);
expect(body.data.updatedBy.id).toEqual(user.id);
});
@@ -892,7 +890,7 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
title: 'new document',
text: 'hello',
publish: true,
@@ -910,7 +908,7 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
title: ' ',
text: ' ',
},
@@ -926,7 +924,7 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
title:
'This is a really long title that is not acceptable to Outline because it is so ridiculously long that we need to have a limit somewhere',
text: ' ',
@@ -940,10 +938,10 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
parentDocumentId: document.id,
title: 'new document',
text: 'hello',
parentDocument: document.id,
publish: true,
},
});
@@ -951,8 +949,6 @@ describe('#documents.create', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('new document');
expect(body.data.collection.documents.length).toBe(2);
expect(body.data.collection.documents[0].children[0].id).toBe(body.data.id);
});
it('should error with invalid parentDocument', async () => {
@@ -960,10 +956,10 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
parentDocumentId: 'd7a4eb73-fac1-4028-af45-d7e34d54db8e',
title: 'new document',
text: 'hello',
parentDocument: 'd7a4eb73-fac1-4028-af45-d7e34d54db8e',
},
});
const body = await res.json();
@@ -977,17 +973,16 @@ describe('#documents.create', async () => {
const res = await server.post('/api/documents.create', {
body: {
token: user.getJwtToken(),
collection: collection.id,
collectionId: collection.id,
parentDocumentId: document.id,
title: 'new document',
text: 'hello',
parentDocument: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.title).toBe('new document');
expect(body.data.collection.documents.length).toBe(2);
});
});
@@ -1009,7 +1004,6 @@ describe('#documents.update', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('Updated title');
expect(body.data.text).toBe('Updated text');
expect(body.data.collection.documents[0].title).toBe('Updated title');
});
it('should not edit archived document', async () => {
@@ -1070,7 +1064,6 @@ describe('#documents.update', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('Untitled document');
expect(body.data.text).toBe('# Untitled document');
expect(body.data.collection.documents[0].title).toBe('Untitled document');
});
it('should fail if document lastRevision does not match', async () => {
@@ -1121,9 +1114,6 @@ describe('#documents.update', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('Updated title');
expect(body.data.collection.documents[0].children[1].title).toBe(
'Updated title'
);
});
it('should require authentication', async () => {

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { Authentication } from '../models';
import { flushdb, seed } from '../test/support';
import { buildDocument, buildUser } from '../test/factories';

View File

@@ -5,6 +5,7 @@ import pagination from './middlewares/pagination';
import auth from '../middlewares/authentication';
import { presentIntegration } from '../presenters';
import policy from '../policies';
import events from '../events';
const { authorize } = policy;
const router = new Router();
@@ -21,9 +22,7 @@ router.post('integrations.list', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
integrations.map(integration => presentIntegration(ctx, integration))
);
const data = await Promise.all(integrations.map(presentIntegration));
ctx.body = {
pagination: ctx.state.pagination,
@@ -35,11 +34,19 @@ router.post('integrations.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertUuid(id, 'id is required');
const user = ctx.state.user;
const integration = await Integration.findById(id);
authorize(ctx.state.user, 'delete', integration);
authorize(user, 'delete', integration);
await integration.destroy();
events.add({
name: 'integrations.delete',
modelId: integration.id,
teamId: integration.teamId,
actorId: user.id,
});
ctx.body = {
success: true,
};

View File

@@ -25,7 +25,7 @@ router.post('notificationSettings.create', auth(), async ctx => {
});
ctx.body = {
data: presentNotificationSetting(ctx, setting),
data: presentNotificationSetting(setting),
};
});
@@ -38,7 +38,7 @@ router.post('notificationSettings.list', auth(), async ctx => {
});
ctx.body = {
data: settings.map(setting => presentNotificationSetting(ctx, setting)),
data: settings.map(presentNotificationSetting),
};
});

View File

@@ -48,10 +48,8 @@ router.post('shares.list', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(shares.map(share => presentShare(ctx, share)));
ctx.body = {
data,
data: shares.map(presentShare),
};
});
@@ -78,7 +76,7 @@ router.post('shares.create', auth(), async ctx => {
share.document = document;
ctx.body = {
data: presentShare(ctx, share),
data: presentShare(share),
};
});

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { flushdb, seed } from '../test/support';
import { buildUser, buildShare } from '../test/factories';

View File

@@ -30,7 +30,9 @@ router.post('team.update', auth(), async ctx => {
}
await team.save();
ctx.body = { data: await presentTeam(ctx, team) };
ctx.body = {
data: presentTeam(team),
};
});
export default router;

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { flushdb, seed } from '../test/support';

View File

@@ -27,13 +27,13 @@ router.post('users.list', auth(), pagination(), async ctx => {
ctx.body = {
pagination: ctx.state.pagination,
data: users.map(listUser =>
presentUser(ctx, listUser, { includeDetails: user.isAdmin })
presentUser(listUser, { includeDetails: user.isAdmin })
),
};
});
router.post('users.info', auth(), async ctx => {
ctx.body = { data: await presentUser(ctx, ctx.state.user) };
ctx.body = { data: await presentUser(ctx.state.user) };
});
router.post('users.update', auth(), async ctx => {
@@ -48,7 +48,7 @@ router.post('users.update', auth(), async ctx => {
await user.save();
ctx.body = { data: await presentUser(ctx, user, { includeDetails: true }) };
ctx.body = { data: await presentUser(user, { includeDetails: true }) };
});
router.post('users.s3Upload', auth(), async ctx => {
@@ -112,7 +112,7 @@ router.post('users.promote', auth(), async ctx => {
await team.addAdmin(user);
ctx.body = {
data: presentUser(ctx, user, { includeDetails: true }),
data: presentUser(user, { includeDetails: true }),
};
});
@@ -132,7 +132,7 @@ router.post('users.demote', auth(), async ctx => {
}
ctx.body = {
data: presentUser(ctx, user, { includeDetails: true }),
data: presentUser(user, { includeDetails: true }),
};
});
@@ -158,7 +158,7 @@ router.post('users.suspend', auth(), async ctx => {
}
ctx.body = {
data: presentUser(ctx, user, { includeDetails: true }),
data: presentUser(user, { includeDetails: true }),
};
});
@@ -181,7 +181,7 @@ router.post('users.activate', auth(), async ctx => {
await team.activateUser(user, admin);
ctx.body = {
data: presentUser(ctx, user, { includeDetails: true }),
data: presentUser(user, { includeDetails: true }),
};
});

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { flushdb, seed } from '../test/support';
import { buildUser } from '../test/factories';

View File

@@ -27,10 +27,8 @@ router.post('views.list', auth(), async ctx => {
],
});
const data = views.map(view => presentView(ctx, view));
ctx.body = {
data,
data: views.map(presentView),
};
});

View File

@@ -1,6 +1,6 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';
import app from '../app';
import { View } from '../models';
import { flushdb, seed } from '../test/support';
import { buildUser } from '../test/factories';