2020-06-08 01:41:12 +00:00
'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 ) {
2020-08-13 21:59:54 +00:00
if ( ! entity ) {
return null ;
}
const curatedEntity = entity . id ? {
2020-06-08 01:41:12 +00:00
id : entity . id ,
name : entity . name ,
url : entity . url ,
description : entity . description ,
slug : entity . slug ,
2020-06-15 01:58:35 +00:00
type : entity . type ,
2020-06-08 01:41:12 +00:00
parameters : includeParameters ? entity . parameters : null ,
2020-08-13 21:59:54 +00:00
parent : curateEntity ( entity . parent ) ,
} : { } ;
if ( entity . children ) {
curatedEntity . children = entity . children . map ( child => curateEntity ( {
2020-06-17 02:07:24 +00:00
... child ,
2020-08-13 21:59:54 +00:00
parent : curatedEntity . id ? curatedEntity : null ,
} , includeParameters ) ) ;
}
2020-06-08 01:41:12 +00:00
return curatedEntity ;
}
async function curateEntities ( entities , includeParameters ) {
return Promise . all ( entities . map ( async entity => curateEntity ( entity , includeParameters ) ) ) ;
}
2020-08-13 22:32:59 +00:00
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 || [ ] ,
} ;
2020-06-08 01:41:12 +00:00
2020-07-09 00:00:54 +00:00
const rawNetworks = await knex . raw ( `
2020-08-13 14:10:58 +00:00
WITH RECURSIVE channels AS (
2020-08-13 21:59:54 +00:00
/* select configured channels and networks */
2020-08-13 14:10:58 +00:00
SELECT
2020-08-13 21:59:54 +00:00
entities . *
2020-08-13 14:10:58 +00:00
FROM
entities
WHERE
2020-08-13 21:59:54 +00:00
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' ) )
2020-07-09 00:00:54 +00:00
2020-08-13 14:10:58 +00:00
UNION ALL
/* select recursive children of configured networks */
SELECT
2020-08-13 21:59:54 +00:00
entities . *
2020-08-13 14:10:58 +00:00
FROM
entities
INNER JOIN
channels ON channels . id = entities . parent _id
WHERE
2020-08-13 21:59:54 +00:00
NOT ( ( entities . slug = ANY ( : excludedNetworks )
AND entities . type = 'network' )
OR ( entities . slug = ANY ( : excludedChannels )
AND entities . type = 'channel' ) )
2020-08-13 14:10:58 +00:00
)
/* select recursive channels as children of networks */
SELECT
2020-08-22 02:22:56 +00:00
entities . * , json _agg ( channels ORDER BY channels . id ) as children
2020-08-13 14:10:58 +00:00
FROM
channels
LEFT JOIN
entities ON entities . id = channels . parent _id
WHERE
channels . type = 'channel'
GROUP BY
entities . id
2020-08-13 22:32:59 +00:00
` , include);
2020-08-13 14:10:58 +00:00
2020-08-13 21:59:54 +00:00
const curatedNetworks = rawNetworks . rows . map ( entity => curateEntity ( entity , true ) ) ;
2020-06-08 01:41:12 +00:00
2020-08-13 21:59:54 +00:00
return curatedNetworks ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
async function fetchChannels ( queryObject ) {
2020-06-08 01:41:12 +00:00
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 ) ;
2020-06-15 01:58:35 +00:00
return curateEntities ( sites ) ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
async function fetchChannelsFromReleases ( ) {
2020-06-08 01:41:12 +00:00
const sites = await knex ( 'releases' )
. select ( 'site_id' , '' )
. leftJoin ( 'sites' , 'sites.id' , 'releases.site_id' )
. groupBy ( 'sites.id' )
. limit ( 100 ) ;
2020-06-15 01:58:35 +00:00
return curateEntities ( sites ) ;
2020-06-08 01:41:12 +00:00
}
module . exports = {
curateEntity ,
curateEntities ,
fetchIncludedEntities ,
2020-06-27 00:57:30 +00:00
fetchChannels ,
fetchChannelsFromReleases ,
2020-06-08 01:41:12 +00:00
} ;