'use strict';
const R = require('ramda');
const hl = require('highland');
const co = require('co');
let getAuthors = require('./authors');
let getTags = require('./tag');
let getGalleries = require('./gallery');
let getImages = require("./image");
let getHtmls = require("./html");
module.exports = R.curry((db, config, articleInstance) => {
return hl(co(function* () {
const articleId = articleInstance.get('id');
const groupedArticleRelations = groupByArticleRelations(R.concat(articleInstance.article_relations || [], articleInstance.article_contents || []));
const contentTypeInstances = yield [
getGalleries(db, groupedArticleRelations.gallery),
getImages(db, groupedArticleRelations.image),
getHtmls(db, groupedArticleRelations.html),
getTags(db, articleId),
getAuthors(config, articleInstance.article_authors)
];
const articleMeta = R.objOf('type', articleInstance.get('type'));
return R.concat([[articleMeta, articleInstance]], R.unnest(contentTypeInstances));
}));
});
function groupByArticleRelations(articleContents) {
const memo = {
image: [],
video: [],
gallery: [],
html: []
};
return R.reduce((grouped, articleContent) => {
if (R.isNil(articleContent)) {
return grouped;
} else if (!R.isNil(R.path(['content_item', 'dataValues', 'image_id'], articleContent))) {
grouped.image.push(articleContent.content_item);
return grouped;
} else if (!R.isNil(R.path(['content_item', 'dataValues', 'video_id'], articleContent))) {
grouped.video.push(articleContent.content_item);
return grouped;
} else if (!R.isNil(R.path(['content_item', 'dataValues', 'gallery_id'], articleContent))) {
grouped.gallery.push(articleContent.content_item);
return grouped;
} else if (!R.isNil(R.path(['content_item', 'dataValues', 'html_id'], articleContent))) {
grouped.html.push(articleContent.content_item);
return grouped;
}
return grouped;
}, memo, articleContents);
} |