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/collection.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

// @flow
import { Collection } from '../models';
import presentDocument from './document';
import naturalSort from '../../shared/utils/naturalSort';
type Document = {
children: Document[],
id: string,
title: string,
url: string,
};
const sortDocuments = (documents: Document[]) => {
const orderedDocs = naturalSort(documents, 'title');
return orderedDocs.map(document => ({
...document,
children: sortDocuments(document.children),
}));
};
async function present(ctx: Object, collection: Collection) {
ctx.cache.set(collection.id, collection);
const data = {
id: collection.id,
url: collection.getUrl(),
name: collection.name,
description: collection.description,
2017-10-30 06:22:46 +00:00
color: collection.color || '#4E5C6E',
type: collection.type,
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,
recentDocuments: undefined,
documents: undefined,
};
if (collection.type === 'atlas') {
// Force alphabetical sorting
data.documents = sortDocuments(collection.documentStructure);
}
if (collection.documents) {
data.recentDocuments = await Promise.all(
collection.documents.map(
2018-05-24 06:59:00 +00:00
async document => await presentDocument(ctx, document)
)
);
}
return data;
}
export default present;