2022-12-20 15:18:43 +01:00
|
|
|
import { ResultList } from '@peertube/peertube-types'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { CONFIG } from '../../initializers/constants'
|
|
|
|
import { CommonSearch } from '../../types/search-query/common-search.model'
|
2024-10-30 15:50:30 +01:00
|
|
|
import { listFilteredInstanceHostsMemoized } from '../requests/instances-index'
|
2021-06-24 15:18:54 +02:00
|
|
|
|
|
|
|
export class Searcher <T extends CommonSearch, R, F> {
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
private readonly queryFn: (query: T) => Promise<ResultList<R>>,
|
|
|
|
private readonly formatFn: (data: R, fromHost: string) => F
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async getResult (queryArg: T): Promise<ResultList<F>> {
|
|
|
|
const query = { ...queryArg }
|
|
|
|
|
|
|
|
if (!Array.isArray(query.blockedHosts)) {
|
|
|
|
query.blockedHosts = []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CONFIG.API.BLACKLIST.ENABLED && Array.isArray(CONFIG.API.BLACKLIST.HOSTS)) {
|
|
|
|
query.blockedHosts = query.blockedHosts.concat(CONFIG.API.BLACKLIST.HOSTS)
|
|
|
|
}
|
|
|
|
|
2024-10-30 15:50:30 +01:00
|
|
|
if (queryArg.filterTag) {
|
|
|
|
if (!Array.isArray(query.hosts)) query.hosts = []
|
|
|
|
|
|
|
|
query.hosts = query.hosts.concat(await listFilteredInstanceHostsMemoized(query.filterTag))
|
|
|
|
|
|
|
|
if (query.hosts.length === 0) {
|
|
|
|
return {
|
|
|
|
total: 0,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:18:54 +02:00
|
|
|
const resultList = await this.queryFn(query)
|
|
|
|
|
|
|
|
return {
|
|
|
|
total: resultList.total,
|
|
|
|
data: resultList.data.map(d => this.formatFn(d, query.fromHost))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|