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
Tom Moor 18b0338736
Pinned documents (#608)
* Migrations and API for pinned documents

* Documentation

* Add pin icon

* Fin.

* v0.2.0

* Remove pin from DocumentPreview, add general menu
Add Pinned documents header

* Tidy

* Fixed: Drafts appearing on collection home
2018-02-28 23:28:36 -08:00

75 lines
2.0 KiB
JavaScript

// @flow
import _ from 'lodash';
import { Op } from 'sequelize';
import { User, Document } from '../models';
import presentUser from './user';
import presentCollection from './collection';
type Options = {
includeCollaborators?: boolean,
};
async function present(ctx: Object, document: Document, options: ?Options) {
options = {
includeCollaborators: true,
...options,
};
ctx.cache.set(document.id, document);
// For empty document content, return the title
if (!document.text.trim()) {
document.text = `# ${document.title}`;
}
const data = {
id: document.id,
url: document.getUrl(),
urlId: document.urlId,
private: document.private,
title: document.title,
text: document.text,
emoji: document.emoji,
createdAt: document.createdAt,
createdBy: presentUser(ctx, document.createdBy),
updatedAt: document.updatedAt,
updatedBy: presentUser(ctx, document.updatedBy),
publishedAt: document.publishedAt,
firstViewedAt: undefined,
lastViewedAt: undefined,
team: document.teamId,
collaborators: [],
starred: !!(document.starred && document.starred.length),
pinned: !!document.pinnedById,
revision: document.revisionCount,
collectionId: document.atlasId,
collaboratorCount: undefined,
collection: undefined,
views: undefined,
};
if (document.private && document.collection) {
data.collection = await presentCollection(ctx, document.collection);
}
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;
}
if (options.includeCollaborators) {
// This could be further optimized by using ctx.cache
data.collaborators = await User.findAll({
where: {
id: { [Op.in]: _.takeRight(document.collaboratorIds, 10) || [] },
},
}).map(user => presentUser(ctx, user));
data.collaboratorCount = document.collaboratorIds.length;
}
return data;
}
export default present;