2017-07-03 17:29:20 +00:00
|
|
|
// @flow
|
2017-07-07 05:17:37 +00:00
|
|
|
import _ from 'lodash';
|
2017-07-16 16:24:45 +00:00
|
|
|
import { User, Document } from '../models';
|
2017-06-26 00:21:33 +00:00
|
|
|
import presentUser from './user';
|
|
|
|
import presentCollection from './collection';
|
2016-08-15 10:51:26 +00:00
|
|
|
|
2017-07-03 17:29:20 +00:00
|
|
|
type Options = {
|
|
|
|
includeCollaborators?: boolean,
|
|
|
|
};
|
|
|
|
|
2017-07-09 05:30:20 +00:00
|
|
|
async function present(ctx: Object, document: Document, options: ?Options) {
|
2016-08-15 12:52:07 +00:00
|
|
|
options = {
|
|
|
|
includeCollaborators: true,
|
|
|
|
...options,
|
|
|
|
};
|
2016-08-15 10:51:26 +00:00
|
|
|
ctx.cache.set(document.id, document);
|
2016-08-05 15:09:14 +00:00
|
|
|
const data = {
|
|
|
|
id: document.id,
|
2016-08-15 19:41:51 +00:00
|
|
|
url: document.getUrl(),
|
2017-09-13 02:55:01 +00:00
|
|
|
urlId: document.urlId,
|
2016-08-05 15:09:14 +00:00
|
|
|
private: document.private,
|
|
|
|
title: document.title,
|
|
|
|
text: document.text,
|
|
|
|
html: document.html,
|
|
|
|
preview: document.preview,
|
2017-07-29 22:06:17 +00:00
|
|
|
emoji: document.emoji,
|
2016-08-05 15:09:14 +00:00
|
|
|
createdAt: document.createdAt,
|
2017-07-04 04:35:17 +00:00
|
|
|
createdBy: presentUser(ctx, document.createdBy),
|
2016-08-05 15:09:14 +00:00
|
|
|
updatedAt: document.updatedAt,
|
2017-07-04 04:35:17 +00:00
|
|
|
updatedBy: presentUser(ctx, document.updatedBy),
|
2017-07-16 16:24:45 +00:00
|
|
|
firstViewedAt: undefined,
|
|
|
|
lastViewedAt: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
team: document.teamId,
|
2016-08-15 10:51:26 +00:00
|
|
|
collaborators: [],
|
2017-09-03 01:44:41 +00:00
|
|
|
starred: !!(document.starred && document.starred.length),
|
2017-07-09 05:36:59 +00:00
|
|
|
collaboratorCount: undefined,
|
2017-07-07 03:59:45 +00:00
|
|
|
collection: undefined,
|
2017-07-04 04:35:17 +00:00
|
|
|
views: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 22:26:00 +00:00
|
|
|
if (document.private && document.collection) {
|
2017-07-07 05:02:55 +00:00
|
|
|
data.collection = await presentCollection(ctx, document.collection);
|
2017-07-07 03:59:45 +00:00
|
|
|
}
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2017-07-16 16:24:45 +00:00
|
|
|
if (document.views && document.views.length === 1) {
|
|
|
|
data.views = document.views[0].count;
|
|
|
|
data.firstViewedAt = document.views[0].createdAt;
|
|
|
|
data.lastViewedAt = document.views[0].updatedAt;
|
2017-06-26 00:21:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:52:07 +00:00
|
|
|
if (options.includeCollaborators) {
|
|
|
|
// This could be further optimized by using ctx.cache
|
2017-07-09 05:36:59 +00:00
|
|
|
data.collaborators = await User.findAll({
|
2016-08-15 12:52:07 +00:00
|
|
|
where: {
|
2017-07-07 05:17:37 +00:00
|
|
|
id: { $in: _.takeRight(document.collaboratorIds, 10) || [] },
|
2016-08-15 12:52:07 +00:00
|
|
|
},
|
2017-05-10 06:14:24 +00:00
|
|
|
}).map(user => presentUser(ctx, user));
|
2017-07-09 05:36:59 +00:00
|
|
|
|
2017-07-03 17:29:20 +00:00
|
|
|
data.collaboratorCount = document.collaboratorIds.length;
|
2016-08-15 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
export default present;
|