sepia-search-motore-di-rice.../server/lib/controllers/searcher.ts

31 lines
923 B
TypeScript
Raw Normal View History

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'
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)
}
const resultList = await this.queryFn(query)
return {
total: resultList.total,
data: resultList.data.map(d => this.formatFn(d, query.fromHost))
}
}
}