2019-11-10 03:20:22 +00:00
'use strict' ;
2019-11-16 02:33:36 +00:00
const config = require ( 'config' ) ;
2020-02-08 01:49:39 +00:00
const logger = require ( './logger' ) ( _ _filename ) ;
2019-11-16 02:33:36 +00:00
const argv = require ( './argv' ) ;
2019-11-10 03:20:22 +00:00
const knex = require ( './knex' ) ;
2019-11-12 00:22:20 +00:00
const whereOr = require ( './utils/where-or' ) ;
2019-11-10 03:20:22 +00:00
2020-05-18 01:34:41 +00:00
function curateSite ( site , includeParameters = false ) {
2020-05-14 02:26:05 +00:00
const curatedSite = {
id : site . id ,
name : site . name ,
url : site . url ,
description : site . description ,
slug : site . slug ,
independent : ! ! site . parameters && site . parameters . independent ,
parameters : includeParameters ? site . parameters : null ,
network : {
id : site . network _id ,
name : site . network _name ,
description : site . network _description ,
slug : site . network _slug ,
url : site . network _url ,
parameters : includeParameters ? site . network _parameters : null ,
} ,
} ;
2020-05-18 01:34:41 +00:00
/ *
2020-05-14 02:26:05 +00:00
if ( includeTags ) {
curatedSite . tags = await knex ( 'sites_tags' )
. select ( 'tags.*' , 'sites_tags.inherit' )
. where ( 'site_id' , site . id )
. join ( 'tags' , 'tags.id' , 'sites_tags.tag_id' ) ;
}
2020-05-18 01:34:41 +00:00
* /
2020-05-14 02:26:05 +00:00
return curatedSite ;
2019-11-10 03:20:22 +00:00
}
2020-03-30 01:01:08 +00:00
async function curateSites ( sites , includeParameters ) {
2020-05-14 02:26:05 +00:00
return Promise . all ( sites . map ( async site => curateSite ( site , includeParameters ) ) ) ;
2019-11-10 03:20:22 +00:00
}
2020-02-01 01:26:00 +00:00
function destructConfigNetworks ( networks = [ ] ) {
2020-05-14 02:26:05 +00:00
return networks . reduce ( ( acc , network ) => {
if ( Array . isArray ( network ) ) {
// network specifies sites
return {
... acc ,
sites : [ ... acc . sites , ... network [ 1 ] ] ,
} ;
}
return {
... acc ,
networks : [ ... acc . networks , network ] ,
} ;
} , {
networks : [ ] ,
sites : [ ] ,
} ) ;
2019-11-16 02:33:36 +00:00
}
async function findSiteByUrl ( url ) {
2020-05-14 02:26:05 +00:00
const { origin , hostname , pathname } = new URL ( url ) ;
// const domain = hostname.replace(/www.|tour./, '');
const dirUrl = ` ${ origin } ${ pathname . split ( '/' ) . slice ( 0 , 2 ) . join ( '/' ) } ` ; // allow for sites on URI directory
const site = await knex ( 'sites' )
. leftJoin ( 'networks' , 'sites.network_id' , 'networks.id' )
. 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' ,
)
. where ( 'sites.url' , url )
. orWhere ( 'sites.url' , origin )
. orWhere ( 'sites.url' , origin . replace ( /www\.|tour\./ , '' ) )
. orWhere ( 'sites.url' , ` https://www. ${ hostname } ` )
. orWhere ( 'sites.url' , ` http://www. ${ hostname } ` )
. orWhere ( 'sites.url' , dirUrl )
// .orWhere('sites.url', 'like', `%${domain}`)
. first ( ) ;
if ( site ) {
const curatedSite = curateSite ( site , true , false ) ;
return curatedSite ;
}
return null ;
2019-11-16 02:33:36 +00:00
}
2019-12-04 20:58:08 +00:00
function sitesByNetwork ( sites ) {
2020-05-14 02:26:05 +00:00
const networks = sites . reduce ( ( acc , site ) => {
if ( acc [ site . network . slug ] ) {
acc [ site . network . slug ] . sites = acc [ site . network . slug ] . sites . concat ( site ) ;
2019-12-04 20:58:08 +00:00
2020-05-14 02:26:05 +00:00
return acc ;
}
2019-12-04 20:58:08 +00:00
2020-05-14 02:26:05 +00:00
acc [ site . network . slug ] = {
... site . network ,
sites : [ site ] ,
} ;
2019-12-04 20:58:08 +00:00
2020-05-14 02:26:05 +00:00
return acc ;
} , { } ) ;
2019-12-04 20:58:08 +00:00
2020-05-14 02:26:05 +00:00
return Object . values ( networks ) ;
2019-12-04 20:58:08 +00:00
}
2019-11-16 02:33:36 +00:00
async function fetchSitesFromArgv ( ) {
2020-05-14 02:26:05 +00:00
const rawSites = await knex ( 'sites' )
. 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' ,
)
. whereIn ( 'sites.slug' , argv . sites || [ ] )
. orWhereIn ( 'networks.slug' , argv . networks || [ ] )
. leftJoin ( 'networks' , 'sites.network_id' , 'networks.id' ) ;
const curatedSites = await curateSites ( rawSites , true ) ;
logger . info ( ` Found ${ curatedSites . length } sites in database ` ) ;
return sitesByNetwork ( curatedSites ) ;
2019-11-16 02:33:36 +00:00
}
async function fetchSitesFromConfig ( ) {
2020-05-14 02:26:05 +00:00
const included = destructConfigNetworks ( config . include ) ;
const excluded = destructConfigNetworks ( config . exclude ) ;
const rawSites = await knex ( 'sites' )
. 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' )
. where ( ( builder ) => {
if ( config . include ) {
builder
. whereIn ( 'sites.slug' , included . sites )
. orWhereIn ( 'networks.slug' , included . networks ) ;
}
} )
. whereNot ( ( builder ) => {
builder
. whereIn ( 'sites.slug' , excluded . sites )
. orWhereIn ( 'networks.slug' , excluded . networks ) ;
} ) ;
const curatedSites = await curateSites ( rawSites , true ) ;
logger . info ( ` Found ${ curatedSites . length } sites in database ` ) ;
return sitesByNetwork ( curatedSites ) ;
2019-11-16 02:33:36 +00:00
}
async function fetchIncludedSites ( ) {
2020-05-14 02:26:05 +00:00
if ( argv . networks || argv . sites ) {
return fetchSitesFromArgv ( ) ;
}
2019-11-16 02:33:36 +00:00
2020-05-14 02:26:05 +00:00
return fetchSitesFromConfig ( ) ;
2019-11-16 02:33:36 +00:00
}
2019-11-13 02:14:24 +00:00
async function fetchSites ( queryObject ) {
2020-05-14 02:26:05 +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 ) ;
return curateSites ( sites ) ;
2019-11-10 03:20:22 +00:00
}
async function fetchSitesFromReleases ( ) {
2020-05-14 02:26:05 +00:00
const sites = await knex ( 'releases' )
. select ( 'site_id' , '' )
. leftJoin ( 'sites' , 'sites.id' , 'releases.site_id' )
. groupBy ( 'sites.id' )
. limit ( 100 ) ;
2019-11-10 03:20:22 +00:00
2020-05-14 02:26:05 +00:00
return curateSites ( sites ) ;
2019-11-10 03:20:22 +00:00
}
module . exports = {
2020-05-14 02:26:05 +00:00
curateSite ,
curateSites ,
fetchIncludedSites ,
fetchSites ,
fetchSitesFromConfig ,
fetchSitesFromArgv ,
fetchSitesFromReleases ,
findSiteByUrl ,
2019-11-10 03:20:22 +00:00
} ;