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.
Files
outline/server/presenters/document.js
Tom Moor 642c11ff7d Document Archive (#921)
* WIP: Archive

* WIP

* Finishing up archive endpoints

* WIP

* Update docs

* Flow

* Stash

* Add toast message confirmations

* Redirect handling, fixed publishhing info for archived docs

* Redirect to collection instead of home, remove unused pub info

* Account for deleted parent

* Trash -> Archive
Allow reading of archived docs

* Dont overload deletedAt

* Fixes

* 💚

* ParentDocumentId wipe for unarchived sub docs

* Fix: CMD+S exits editing
Fix: Duplicate user name on published but unedited docs

* Improve jank on paginated lists

* Prevent editing when archived

* 💚
Separate lint / flow steps
2019-04-06 16:20:27 -07:00

68 lines
1.7 KiB
JavaScript

// @flow
import { takeRight } from 'lodash';
import { User, Document } from '../models';
import presentUser from './user';
import presentCollection from './collection';
type Options = {
isPublic?: boolean,
};
async function present(ctx: Object, document: Document, options: ?Options) {
options = {
isPublic: false,
...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.url,
urlId: document.urlId,
title: document.title,
text: document.text,
emoji: document.emoji,
createdAt: document.createdAt,
createdBy: undefined,
updatedAt: document.updatedAt,
updatedBy: undefined,
publishedAt: document.publishedAt,
archivedAt: document.archivedAt,
deletedAt: document.deletedAt,
team: document.teamId,
collaborators: [],
starred: !!(document.starred && document.starred.length),
revision: document.revisionCount,
pinned: undefined,
collectionId: undefined,
collection: undefined,
};
if (!options.isPublic) {
data.pinned = !!document.pinnedById;
data.collectionId = document.collectionId;
data.createdBy = presentUser(ctx, document.createdBy);
data.updatedBy = presentUser(ctx, document.updatedBy);
if (document.collection) {
data.collection = await presentCollection(ctx, document.collection);
}
// This could be further optimized by using ctx.cache
data.collaborators = await User.findAll({
where: {
id: takeRight(document.collaboratorIds, 10) || [],
},
}).map(user => presentUser(ctx, user));
}
return data;
}
export default present;