2017-07-03 17:29:20 +00:00
|
|
|
// @flow
|
2019-01-05 21:37:33 +00:00
|
|
|
import { takeRight } from 'lodash';
|
2020-02-13 03:40:44 +00:00
|
|
|
import { User, Document, Attachment } from '../models';
|
|
|
|
import { getSignedImageUrl } from '../utils/s3';
|
2017-06-26 00:21:33 +00:00
|
|
|
import presentUser from './user';
|
2016-08-15 10:51:26 +00:00
|
|
|
|
2017-07-03 17:29:20 +00:00
|
|
|
type Options = {
|
2018-05-24 06:59:00 +00:00
|
|
|
isPublic?: boolean,
|
2017-07-03 17:29:20 +00:00
|
|
|
};
|
|
|
|
|
2020-02-13 03:40:44 +00:00
|
|
|
const attachmentRegex = /!\[.*\]\(\/api\/attachments\.redirect\?id=(?<id>.*)\)/gi;
|
|
|
|
|
|
|
|
// replaces attachments.redirect urls with signed/authenticated url equivalents
|
|
|
|
async function replaceImageAttachments(text) {
|
|
|
|
const attachmentIds = [...text.matchAll(attachmentRegex)].map(
|
|
|
|
match => match.groups && match.groups.id
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const id of attachmentIds) {
|
|
|
|
const attachment = await Attachment.findByPk(id);
|
|
|
|
const accessUrl = await getSignedImageUrl(attachment.key);
|
|
|
|
text = text.replace(attachment.redirectUrl, accessUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
export default async function present(document: Document, options: ?Options) {
|
2016-08-15 12:52:07 +00:00
|
|
|
options = {
|
2018-05-24 06:59:00 +00:00
|
|
|
isPublic: false,
|
2016-08-15 12:52:07 +00:00
|
|
|
...options,
|
|
|
|
};
|
2017-10-23 05:20:53 +00:00
|
|
|
|
2020-02-13 03:40:44 +00:00
|
|
|
const text = options.isPublic
|
|
|
|
? await replaceImageAttachments(document.text)
|
|
|
|
: document.text;
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
const data = {
|
|
|
|
id: document.id,
|
2018-11-04 07:59:52 +00:00
|
|
|
url: document.url,
|
2017-09-13 02:55:01 +00:00
|
|
|
urlId: document.urlId,
|
2016-08-05 15:09:14 +00:00
|
|
|
title: document.title,
|
2020-02-13 03:40:44 +00:00
|
|
|
text,
|
2017-07-29 22:06:17 +00:00
|
|
|
emoji: document.emoji,
|
2016-08-05 15:09:14 +00:00
|
|
|
createdAt: document.createdAt,
|
2018-05-24 06:59:00 +00:00
|
|
|
createdBy: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
updatedAt: document.updatedAt,
|
2018-05-24 06:59:00 +00:00
|
|
|
updatedBy: undefined,
|
2018-02-28 06:41:12 +00:00
|
|
|
publishedAt: document.publishedAt,
|
2019-04-06 23:20:27 +00:00
|
|
|
archivedAt: document.archivedAt,
|
|
|
|
deletedAt: document.deletedAt,
|
2019-04-20 18:40:14 +00:00
|
|
|
teamId: document.teamId,
|
2016-08-15 10:51:26 +00:00
|
|
|
collaborators: [],
|
2019-04-18 02:11:23 +00:00
|
|
|
starred: document.starred ? !!document.starred.length : undefined,
|
2017-11-25 21:04:54 +00:00
|
|
|
revision: document.revisionCount,
|
2018-05-24 06:59:00 +00:00
|
|
|
pinned: undefined,
|
|
|
|
collectionId: undefined,
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocumentId: undefined,
|
2016-08-05 15:09:14 +00:00
|
|
|
};
|
|
|
|
|
2018-05-24 06:59:00 +00:00
|
|
|
if (!options.isPublic) {
|
|
|
|
data.pinned = !!document.pinnedById;
|
2018-08-08 06:23:26 +00:00
|
|
|
data.collectionId = document.collectionId;
|
2019-04-18 02:11:23 +00:00
|
|
|
data.parentDocumentId = document.parentDocumentId;
|
|
|
|
data.createdBy = presentUser(document.createdBy);
|
|
|
|
data.updatedBy = presentUser(document.updatedBy);
|
2017-06-26 00:21:33 +00:00
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
// TODO: This could be further optimized
|
2017-07-09 05:36:59 +00:00
|
|
|
data.collaborators = await User.findAll({
|
2016-08-15 12:52:07 +00:00
|
|
|
where: {
|
2019-01-05 21:37:33 +00:00
|
|
|
id: takeRight(document.collaboratorIds, 10) || [],
|
2016-08-15 12:52:07 +00:00
|
|
|
},
|
2019-04-18 02:11:23 +00:00
|
|
|
}).map(presentUser);
|
2016-08-15 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 15:09:14 +00:00
|
|
|
return data;
|
|
|
|
}
|