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

131 lines
3.1 KiB
JavaScript
Raw Normal View History

import _ from 'lodash';
2016-09-11 20:38:52 +00:00
import { Document, Atlas, User } from './models';
2016-09-11 20:38:52 +00:00
import presentUser from './presenters/user';
2016-05-20 03:46:34 +00:00
2016-09-11 20:38:52 +00:00
export {
presentUser,
};
2016-04-29 05:25:37 +00:00
export function presentTeam(ctx, team) {
ctx.cache.set(team.id, team);
2016-08-05 15:09:14 +00:00
return new Promise(async (resolve, _reject) => {
2016-05-20 06:53:07 +00:00
resolve({
id: team.id,
name: team.name,
});
});
2016-04-29 05:25:37 +00:00
}
2016-05-07 18:52:08 +00:00
2016-08-15 12:52:07 +00:00
export async function presentDocument(ctx, document, options) {
options = {
2016-08-15 12:59:53 +00:00
includeCollection: true,
2016-08-15 12:52:07 +00:00
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(),
2016-08-05 15:09:14 +00:00
private: document.private,
title: document.title,
text: document.text,
html: document.html,
preview: document.preview,
createdAt: document.createdAt,
createdBy: undefined,
2016-08-05 15:09:14 +00:00
updatedAt: document.updatedAt,
updatedBy: undefined,
2016-08-05 15:09:14 +00:00
team: document.teamId,
collaborators: [],
2016-08-05 15:09:14 +00:00
};
2016-08-15 12:52:07 +00:00
if (options.includeCollection) {
data.collection = await ctx.cache.get(
2016-08-15 12:52:07 +00:00
document.atlasId,
async () => {
const collection = await Atlas.findOne({ where: {
id: document.atlasId,
} });
return await presentCollection(ctx, collection);
}
);
2016-08-05 15:09:14 +00:00
}
2016-08-15 12:52:07 +00:00
if (options.includeCollaborators) {
// This could be further optimized by using ctx.cache
data.collaborators = await User.findAll({
where: {
id: {
$in: document.collaboratorIds || [],
},
},
})
.map(user => presentUser(ctx, user));
}
const createdBy = await ctx.cache.get(
document.createdById,
async () => await User.findById(document.createdById)
);
data.createdBy = await presentUser(ctx, createdBy);
const updatedBy = await ctx.cache.get(
2016-08-15 12:52:07 +00:00
document.lastModifiedById,
async () => await User.findById(document.lastModifiedById)
);
2016-08-15 12:52:07 +00:00
data.updatedBy = await presentUser(ctx, updatedBy);
2016-08-05 15:09:14 +00:00
return data;
}
export function presentCollection(ctx, collection, includeRecentDocuments=false) {
ctx.cache.set(collection.id, collection);
2016-08-05 15:09:14 +00:00
return new Promise(async (resolve, _reject) => {
2016-05-20 06:53:07 +00:00
const data = {
2016-08-05 15:09:14 +00:00
id: collection.id,
2016-08-15 19:41:51 +00:00
url: collection.getUrl(),
2016-08-05 15:09:14 +00:00
name: collection.name,
description: collection.description,
type: collection.type,
2016-08-22 06:40:57 +00:00
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,
2016-08-05 15:09:14 +00:00
};
2016-05-20 06:53:07 +00:00
if (collection.type === 'atlas') data.navigationTree = collection.navigationTree;
2016-06-21 06:12:56 +00:00
2016-05-20 06:53:07 +00:00
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
2016-08-05 15:09:14 +00:00
atlasId: collection.id,
2016-05-20 06:53:07 +00:00
},
limit: 10,
2016-05-20 07:33:52 +00:00
order: [
['updatedAt', 'DESC'],
],
2016-05-20 06:53:07 +00:00
});
2016-08-05 15:09:14 +00:00
const recentDocuments = [];
2016-05-20 06:53:07 +00:00
await Promise.all(documents.map(async (document) => {
2016-08-15 12:52:07 +00:00
recentDocuments.push(await presentDocument(ctx, document, {
includeCollaborators: true,
}));
2016-08-05 15:09:14 +00:00
}));
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
2016-05-20 06:53:07 +00:00
}
resolve(data);
});
2016-05-20 03:46:34 +00:00
}
2016-08-24 07:37:54 +00:00
export function presentApiKey(ctx, key) {
return {
id: key.id,
name: key.name,
secret: key.secret,
};
}