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,17 +1,19 @@
exports[`test presents a user 1`] = `
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`presents a user 1`] = `
Object {
"avatarUrl": "http://example.com/avatar.png",
"id": "123",
"name": "Test User",
"username": "testuser"
"username": "testuser",
}
`;
exports[`test presents a user without slack data 1`] = `
exports[`presents a user without slack data 1`] = `
Object {
"avatarUrl": null,
"id": "123",
"name": "Test User",
"username": "testuser"
"username": "testuser",
}
`;

View File

@@ -0,0 +1,9 @@
function present(ctx, key) {
return {
id: key.id,
name: key.name,
secret: key.secret,
};
}
export default present;

View File

@@ -0,0 +1,46 @@
import _ from 'lodash';
import { Document } from '../models';
import presentDocument from './document';
async function present(ctx, collection, includeRecentDocuments = false) {
ctx.cache.set(collection.id, collection);
const data = {
id: collection.id,
url: collection.getUrl(),
name: collection.name,
description: collection.description,
type: collection.type,
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,
};
if (collection.type === 'atlas')
data.navigationTree = collection.navigationTree;
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: collection.id,
},
limit: 10,
order: [['updatedAt', 'DESC']],
});
const recentDocuments = [];
await Promise.all(
documents.map(async document => {
recentDocuments.push(
await presentDocument(ctx, document, {
includeCollaborators: true,
})
);
})
);
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
}
return data;
}
export default present;

View File

@@ -0,0 +1,80 @@
import { Collection, Star, User, View } from '../models';
import presentUser from './user';
import presentCollection from './collection';
async function present(ctx, document, options) {
options = {
includeCollection: true,
includeCollaborators: true,
includeViews: true,
...options,
};
ctx.cache.set(document.id, document);
const userId = ctx.state.user.id;
const data = {
id: document.id,
url: document.getUrl(),
private: document.private,
title: document.title,
text: document.text,
html: document.html,
preview: document.preview,
createdAt: document.createdAt,
createdBy: undefined,
updatedAt: document.updatedAt,
updatedBy: undefined,
team: document.teamId,
collaborators: [],
};
data.starred = !!await Star.findOne({
where: { documentId: document.id, userId },
});
if (options.includeViews) {
data.views = await View.sum('count', {
where: { documentId: document.id },
});
}
if (options.includeCollection) {
data.collection = await ctx.cache.get(document.atlasId, async () => {
const collection =
options.collection ||
(await Collection.findOne({
where: {
id: document.atlasId,
},
}));
return presentCollection(ctx, collection);
});
}
if (options.includeCollaborators) {
// This could be further optimized by using ctx.cache
data.collaborators = await User.findAll({
where: {
id: {
$in: document.collaboratorIds || [],
},
},
}).map(user => presentUser(ctx, user));
}
const createdBy = await ctx.cache.get(
document.createdById,
async () => await User.findById(document.createdById)
);
data.createdBy = await presentUser(ctx, createdBy);
const updatedBy = await ctx.cache.get(
document.lastModifiedById,
async () => await User.findById(document.lastModifiedById)
);
data.updatedBy = await presentUser(ctx, updatedBy);
return data;
}
export default present;

View File

@@ -0,0 +1,16 @@
// @flow
import presentUser from './user';
import presentView from './view';
import presentDocument from './document';
import presentCollection from './collection';
import presentApiKey from './apiKey';
import presentTeam from './team';
export {
presentUser,
presentView,
presentDocument,
presentCollection,
presentApiKey,
presentTeam,
};

10
server/presenters/team.js Normal file
View File

@@ -0,0 +1,10 @@
function present(ctx, team) {
ctx.cache.set(team.id, team);
return {
id: team.id,
name: team.name,
};
}
export default present;

View File

@@ -1,7 +1,7 @@
// @flow
import User from '../models/User';
async function presentUser(ctx: Object, user: User) {
function present(ctx: Object, user: User) {
ctx.cache.set(user.id, user);
return {
@@ -12,4 +12,4 @@ async function presentUser(ctx: Object, user: User) {
};
}
export default presentUser;
export default present;

18
server/presenters/view.js Normal file
View File

@@ -0,0 +1,18 @@
// @flow
import { View, User } from '../models';
import { presentUser } from '../presenters';
async function present(ctx: Object, view: View) {
let data = {
count: view.count,
user: undefined,
};
const user = await ctx.cache.get(
view.userId,
async () => await User.findById(view.userId)
);
data.user = await presentUser(ctx, user);
return data;
}
export default present;