This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/presenters/document.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

// @flow
2017-07-07 05:17:37 +00:00
import _ from 'lodash';
import { User, Document } from '../models';
import presentUser from './user';
import presentCollection from './collection';
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,
};
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(),
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),
firstViewedAt: undefined,
lastViewedAt: undefined,
2016-08-05 15:09:14 +00:00
team: document.teamId,
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
};
if (document.private && document.collection) {
data.collection = await presentCollection(ctx, document.collection);
2017-07-07 03:59: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;
}
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
data.collaboratorCount = document.collaboratorIds.length;
2016-08-15 12:52:07 +00:00
}
2016-08-05 15:09:14 +00:00
return data;
}
export default present;