Document Viewers (#79)

* Recording document views

* Add 'views' to document response

* Basic displaying of document views, probably want it more sublte than this? But hey, lets get it in there

* Bigly improves. RESTful > RPC

* Display of who's viewed doc

* Add Popover, add Scrollable, move views store

* Working server tests 💁

* Document Stars (#81)

* Added: Starred documents

* UI is dumb but functionality works

* Star now displayed inline in title

* Optimistic rendering

* Documents Endpoints (#85)

* More seeds, documents.list endpoint

* Upgrade deprecated middleware

* document.viewers, specs

* Add documents.starred
Add request specs for star / unstar endpoints

* Basic /starred page

* Remove comment

* Fixed double layout
This commit is contained in:
Tom Moor
2017-06-25 17:21:33 -07:00
committed by GitHub
parent 1fa473b271
commit 52765d9d1d
66 changed files with 1629 additions and 460 deletions

View File

@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#auth.login should login with email 1`] = `
Object {
"avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
"name": "User 1",
"username": "user1"
"username": "user1",
}
`;
@@ -12,7 +14,7 @@ Object {
"avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
"name": "User 1",
"username": "user1"
"username": "user1",
}
`;
@@ -21,7 +23,7 @@ Object {
"error": "validation_error",
"message": "username/email is required",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -30,7 +32,7 @@ Object {
"error": "validation_error",
"message": "username/email is required",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -39,7 +41,7 @@ Object {
"error": "validation_error",
"message": "username/email is required",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -48,7 +50,7 @@ Object {
"error": "validation_error",
"message": "name is required",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -57,7 +59,7 @@ Object {
"error": "user_exists_with_email",
"message": "User already exists with this email",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -66,7 +68,7 @@ Object {
"error": "user_exists_with_username",
"message": "User already exists with this username",
"ok": false,
"status": 400
"status": 400,
}
`;
@@ -75,6 +77,6 @@ Object {
"error": "validation_error",
"message": "email is invalid",
"ok": false,
"status": 400
"status": 400,
}
`;

View File

@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#documents.list should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;
exports[`#documents.star should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;
exports[`#documents.starred should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;
exports[`#documents.unstar should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;
exports[`#documents.viewed should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401,
}
`;

View File

@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#user.info should require authentication 1`] = `
Object {
"error": "authentication_required",
"message": "Authentication required",
"ok": false,
"status": 401
"status": 401,
}
`;
@@ -13,9 +15,9 @@ Object {
"avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
"name": "User 1",
"username": "user1"
"username": "user1",
},
"ok": true,
"status": 200
"status": 200,
}
`;

View File

@@ -1,12 +1,11 @@
import TestServer from 'fetch-test-server';
import app from '..';
import { flushdb, sequelize, seed } from '../test/support';
import { flushdb, seed } from '../test/support';
const server = new TestServer(app.callback());
beforeEach(flushdb);
afterAll(() => server.close());
afterAll(() => sequelize.close());
describe('#auth.signup', async () => {
it('should signup a new user', async () => {

View File

@@ -1,46 +1,84 @@
// @flow
import Router from 'koa-router';
import httpErrors from 'http-errors';
import isUUID from 'validator/lib/isUUID';
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
import auth from './middlewares/authentication';
import pagination from './middlewares/pagination';
import { presentDocument } from '../presenters';
import { Document, Collection } from '../models';
import { Document, Collection, Star, View } from '../models';
const router = new Router();
const getDocumentForId = async id => {
try {
let document;
if (isUUID(id)) {
document = await Document.findOne({
where: {
id,
},
});
} else if (id.match(URL_REGEX)) {
document = await Document.findOne({
where: {
urlId: id.match(URL_REGEX)[1],
},
});
} else {
throw httpErrors.NotFound();
}
return document;
} catch (e) {
// Invalid UUID
throw httpErrors.NotFound();
}
};
router.post('documents.list', 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({
where: { teamId: user.teamId },
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
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 }],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
let 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]],
include: [{ model: Document }],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
let data = await Promise.all(
views.map(view => presentDocument(ctx, view.document))
);
ctx.body = {
pagination: ctx.state.pagination,
data,
};
});
// FIXME: This really needs specs :/
router.post('documents.info', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const document = await getDocumentForId(id);
const document = await Document.findById(id);
if (!document) throw httpErrors.NotFound();
@@ -52,20 +90,14 @@ router.post('documents.info', auth(), async ctx => {
if (document.teamId !== user.teamId) {
throw httpErrors.NotFound();
}
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}),
};
} else {
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollaborators: true,
}),
};
}
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollection: document.private,
includeCollaborators: true,
}),
};
});
router.post('documents.search', auth(), async ctx => {
@@ -94,6 +126,34 @@ router.post('documents.search', auth(), async ctx => {
};
});
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);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
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);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
await Star.destroy({
where: { documentId: document.id, userId: user.id },
});
});
router.post('documents.create', auth(), async ctx => {
const { collection, title, text, parentDocument, index } = ctx.body;
ctx.assertPresent(collection, 'collection is required');
@@ -154,7 +214,7 @@ router.post('documents.update', auth(), async ctx => {
ctx.assertPresent(title || text, 'title or text is required');
const user = ctx.state.user;
const document = await getDocumentForId(id);
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
@@ -186,13 +246,13 @@ router.post('documents.move', auth(), async ctx => {
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
const user = ctx.state.user;
const document = await getDocumentForId(id);
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
// Set parent document
if (parentDocument) {
const parent = await getDocumentForId(parentDocument);
const parent = await Document.findById(parentDocument);
if (parent.atlasId !== document.atlasId)
throw httpErrors.BadRequest(
'Invalid parentDocument (must be same collection)'
@@ -226,7 +286,7 @@ router.post('documents.delete', auth(), async ctx => {
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await getDocumentForId(id);
const document = await Document.findById(id);
const collection = await Collection.findById(document.atlasId);
if (!document || document.teamId !== user.teamId)
@@ -240,7 +300,7 @@ router.post('documents.delete', auth(), async ctx => {
);
}
// Delete all chilren
// Delete all children
try {
await collection.deleteDocument(document);
} catch (e) {

View File

@@ -0,0 +1,158 @@
import TestServer from 'fetch-test-server';
import app from '..';
import { View, Star } from '../models';
import { flushdb, seed } from '../test/support';
const server = new TestServer(app.callback());
beforeEach(flushdb);
afterAll(() => server.close());
describe('#documents.list', async () => {
it('should return documents', async () => {
const { user, document } = await seed();
const res = await server.post('/api/documents.list', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(2);
expect(body.data[0].id).toEqual(document.id);
});
it('should allow changing sort direction', async () => {
const { user, document } = await seed();
const res = await server.post('/api/documents.list', {
body: { token: user.getJwtToken(), direction: 'ASC' },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data[1].id).toEqual(document.id);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.list');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});
describe('#documents.viewed', async () => {
it('should return empty result if no views', async () => {
const { user } = await seed();
const res = await server.post('/api/documents.viewed', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it('should return recently viewed documents', async () => {
const { user, document } = await seed();
await View.increment({ documentId: document.id, userId: user.id });
const res = await server.post('/api/documents.viewed', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(document.id);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.viewed');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});
describe('#documents.starred', async () => {
it('should return empty result if no stars', async () => {
const { user } = await seed();
const res = await server.post('/api/documents.starred', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it('should return starred documents', async () => {
const { user, document } = await seed();
await Star.create({ documentId: document.id, userId: user.id });
const res = await server.post('/api/documents.starred', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(document.id);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.starred');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});
describe('#documents.star', async () => {
it('should star the document', async () => {
const { user, document } = await seed();
const res = await server.post('/api/documents.star', {
body: { token: user.getJwtToken(), id: document.id },
});
const stars = await Star.findAll();
expect(res.status).toEqual(200);
expect(stars.length).toEqual(1);
expect(stars[0].documentId).toEqual(document.id);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.star');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});
describe('#documents.unstar', async () => {
it('should unstar the document', async () => {
const { user, document } = await seed();
await Star.create({ documentId: document.id, userId: user.id });
const res = await server.post('/api/documents.unstar', {
body: { token: user.getJwtToken(), id: document.id },
});
const stars = await Star.findAll();
expect(res.status).toEqual(200);
expect(stars.length).toEqual(0);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.star');
const body = await res.json();
expect(res.status).toEqual(401);
expect(body).toMatchSnapshot();
});
});

View File

@@ -8,6 +8,7 @@ import auth from './auth';
import user from './user';
import collections from './collections';
import documents from './documents';
import views from './views';
import hooks from './hooks';
import apiKeys from './apiKeys';
@@ -59,6 +60,7 @@ router.use('/', auth.routes());
router.use('/', user.routes());
router.use('/', collections.routes());
router.use('/', documents.routes());
router.use('/', views.routes());
router.use('/', hooks.routes());
router.use('/', apiKeys.routes());

View File

@@ -9,7 +9,6 @@ const server = new TestServer(app.callback());
beforeEach(flushdb);
afterAll(() => server.close());
afterAll(() => sequelize.close());
describe('#user.info', async () => {
it('should return known user', async () => {

52
server/api/views.js Normal file
View File

@@ -0,0 +1,52 @@
// @flow
import Router from 'koa-router';
import httpErrors from 'http-errors';
import auth from './middlewares/authentication';
import { presentView } from '../presenters';
import { View, Document } from '../models';
const router = new Router();
router.post('views.list', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const views = await View.findAll({
where: {
documentId: id,
},
order: [['updatedAt', 'DESC']],
});
// Collectiones
let users = [];
let count = 0;
await Promise.all(
views.map(async view => {
count = view.count;
return users.push(await presentView(ctx, view));
})
);
ctx.body = {
data: {
users,
count,
},
};
});
router.post('views.create', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
await View.increment({ documentId: document.id, userId: user.id });
});
export default router;