Merge master

This commit is contained in:
Tom Moor
2017-07-08 22:30:20 -07:00
58 changed files with 828 additions and 524 deletions

View File

@@ -1,8 +1,9 @@
// @flow
import _ from 'lodash';
import { Document } from '../models';
import { Collection } from '../models';
import presentDocument from './document';
async function present(ctx, collection, includeRecentDocuments = false) {
async function present(ctx: Object, collection: Collection) {
ctx.cache.set(collection.id, collection);
const data = {
@@ -13,31 +14,21 @@ async function present(ctx, collection, includeRecentDocuments = false) {
type: collection.type,
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,
recentDocuments: undefined,
documents: undefined,
};
if (collection.type === 'atlas')
if (collection.type === 'atlas') {
data.documents = await collection.getDocumentsStructure();
}
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,
})
);
})
if (collection.documents) {
data.recentDocuments = await Promise.all(
collection.documents.map(
async document =>
await presentDocument(ctx, document, { includeCollaborators: true })
)
);
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
}
return data;

View File

@@ -1,26 +1,22 @@
// @flow
import { Collection, Star, User, View, Document } from '../models';
import _ from 'lodash';
import { User, Document, View } from '../models';
import presentUser from './user';
import presentCollection from './collection';
import _ from 'lodash';
type Options = {
includeCollection?: boolean,
includeCollaborators?: boolean,
includeViews?: boolean,
};
async function present(ctx: Object, document: Document, options: Options) {
async function present(ctx: Object, document: Document, options: ?Options) {
options = {
includeCollection: true,
includeCollaborators: true,
includeViews: true,
includeViews: false,
...options,
};
ctx.cache.set(document.id, document);
const userId = ctx.state.user.id;
let data = {
const data = {
id: document.id,
url: document.getUrl(),
private: document.private,
@@ -29,36 +25,23 @@ async function present(ctx: Object, document: Document, options: Options) {
html: document.html,
preview: document.preview,
createdAt: document.createdAt,
createdBy: undefined,
starred: false,
createdBy: presentUser(ctx, document.createdBy),
updatedAt: document.updatedAt,
updatedBy: undefined,
updatedBy: presentUser(ctx, document.updatedBy),
team: document.teamId,
collaborators: [],
starred: !!document.starred,
collection: undefined,
views: undefined,
};
data.starred = !!await Star.findOne({
where: { documentId: document.id, userId },
});
if (options.includeViews) {
// $FlowIssue not found in object literal?
data.views = await View.sum('count', {
where: { documentId: document.id },
});
if (document.private) {
data.collection = await presentCollection(ctx, document.collection);
}
if (options.includeCollection) {
// $FlowIssue not found in object literal?
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.includeViews) {
data.views = await View.sum('count', {
where: { documentId: document.id },
});
}
@@ -66,27 +49,13 @@ async function present(ctx: Object, document: Document, options: Options) {
// This could be further optimized by using ctx.cache
data['collaborators'] = await User.findAll({
where: {
id: {
$in: _.takeRight(document.collaboratorIds, 10) || [],
},
id: { $in: _.takeRight(document.collaboratorIds, 10) || [] },
},
}).map(user => presentUser(ctx, user));
// $FlowIssue not found in object literal?
data.collaboratorCount = document.collaboratorIds.length;
}
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;
}

View File

@@ -1,4 +1,7 @@
function present(ctx, team) {
// @flow
import { Team } from '../models';
function present(ctx: Object, team: Team) {
ctx.cache.set(team.id, team);
return {