import { CONFIG } from '../../initializers/constants' import { listIndexInstancesHost } from '../requests/instances-index' import { client } from '../../helpers/meilisearch' async function buildInstanceHosts () { let indexHosts = await listIndexInstancesHost() if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) { const whitelistHosts = Array.isArray(CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS) ? CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS : [] indexHosts = indexHosts.filter(h => whitelistHosts.includes(h)) } const dbHosts = await listDBInstances() const removedHosts = getRemovedHosts(dbHosts, indexHosts) return { indexHosts, removedHosts } } export { buildInstanceHosts } // ################################################## async function listDBInstances () { const setResult = new Set() const indexes = [ CONFIG.MEILISEARCH.INDEXES.VIDEOS, CONFIG.MEILISEARCH.INDEXES.CHANNELS ] for (const index of indexes) { const result = await client.index(index).searchForFacetValues({ facetName: 'host' }) for (const b of result.facetHits) { setResult.add(b.value) } } return Array.from(setResult) } function getRemovedHosts (dbHosts: string[], indexHosts: string[]) { return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false) }