135 lines
3.6 KiB
JavaScript
135 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
|
|
const argv = require('./argv');
|
|
const knex = require('./knex');
|
|
const whereOr = require('./utils/where-or');
|
|
|
|
function curateEntity(entity, includeParameters = false) {
|
|
if (!entity) {
|
|
return null;
|
|
}
|
|
|
|
const curatedEntity = entity.id ? {
|
|
id: entity.id,
|
|
name: entity.name,
|
|
url: entity.url,
|
|
description: entity.description,
|
|
slug: entity.slug,
|
|
type: entity.type,
|
|
parameters: includeParameters ? entity.parameters : null,
|
|
parent: curateEntity(entity.parent),
|
|
} : {};
|
|
|
|
if (entity.children) {
|
|
curatedEntity.children = entity.children.map(child => curateEntity({
|
|
...child,
|
|
parent: curatedEntity.id ? curatedEntity : null,
|
|
}, includeParameters));
|
|
}
|
|
|
|
return curatedEntity;
|
|
}
|
|
|
|
async function curateEntities(entities, includeParameters) {
|
|
return Promise.all(entities.map(async entity => curateEntity(entity, includeParameters)));
|
|
}
|
|
|
|
async function fetchIncludedEntities() {
|
|
const include = {
|
|
includeAll: !argv.networks && !argv.channels && !config.include?.networks && !config.include?.channels,
|
|
includedNetworks: argv.networks || (!argv.channels && config.include?.networks) || [],
|
|
includedChannels: argv.channels || (!argv.networks && config.include?.channels) || [],
|
|
excludedNetworks: argv.excludeNetworks || config.exclude?.networks || [],
|
|
excludedChannels: argv.excludeChannels || config.exclude?.channels || [],
|
|
};
|
|
|
|
const rawNetworks = await knex.raw(`
|
|
WITH RECURSIVE channels AS (
|
|
/* select configured channels and networks */
|
|
SELECT
|
|
entities.*
|
|
FROM
|
|
entities
|
|
WHERE
|
|
CASE WHEN :includeAll
|
|
THEN
|
|
/* select all top level networks and independent channels */
|
|
entities.parent_id IS NULL
|
|
ELSE
|
|
((entities.slug = ANY(:includedNetworks)
|
|
AND entities.type = 'network')
|
|
OR (entities.slug = ANY(:includedChannels)
|
|
AND entities.type = 'channel'))
|
|
END
|
|
AND NOT (
|
|
(entities.slug = ANY(:excludedNetworks)
|
|
AND entities.type = 'network')
|
|
OR (entities.slug = ANY(:excludedChannels)
|
|
AND entities.type = 'channel'))
|
|
|
|
UNION ALL
|
|
|
|
/* select recursive children of configured networks */
|
|
SELECT
|
|
entities.*
|
|
FROM
|
|
entities
|
|
INNER JOIN
|
|
channels ON channels.id = entities.parent_id
|
|
WHERE
|
|
NOT ((entities.slug = ANY(:excludedNetworks)
|
|
AND entities.type = 'network')
|
|
OR (entities.slug = ANY(:excludedChannels)
|
|
AND entities.type = 'channel'))
|
|
)
|
|
/* select recursive channels as children of networks */
|
|
SELECT
|
|
entities.*, json_agg(channels ORDER BY channels.id) as children
|
|
FROM
|
|
channels
|
|
LEFT JOIN
|
|
entities ON entities.id = channels.parent_id
|
|
WHERE
|
|
channels.type = 'channel'
|
|
GROUP BY
|
|
entities.id
|
|
`, include);
|
|
|
|
const curatedNetworks = rawNetworks.rows.map(entity => curateEntity(entity, true));
|
|
|
|
return curatedNetworks;
|
|
}
|
|
|
|
async function fetchChannels(queryObject) {
|
|
const sites = await knex('sites')
|
|
.where(builder => whereOr(queryObject, 'sites', builder))
|
|
.select(
|
|
'sites.*',
|
|
'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', 'networks.description as network_description', 'networks.parameters as network_parameters',
|
|
)
|
|
.leftJoin('networks', 'sites.network_id', 'networks.id')
|
|
.limit(100);
|
|
|
|
return curateEntities(sites);
|
|
}
|
|
|
|
async function fetchChannelsFromReleases() {
|
|
const sites = await knex('releases')
|
|
.select('site_id', '')
|
|
.leftJoin('sites', 'sites.id', 'releases.site_id')
|
|
.groupBy('sites.id')
|
|
.limit(100);
|
|
|
|
return curateEntities(sites);
|
|
}
|
|
|
|
module.exports = {
|
|
curateEntity,
|
|
curateEntities,
|
|
fetchIncludedEntities,
|
|
fetchChannels,
|
|
fetchChannelsFromReleases,
|
|
};
|