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:
80
server/presenters/document.js
Normal file
80
server/presenters/document.js
Normal 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;
|
Reference in New Issue
Block a user