2020-02-19 15:39:35 +01:00
|
|
|
import * as express from 'express'
|
2020-05-28 11:37:47 +02:00
|
|
|
import { ChannelsSearchQuery } from 'server/types/channel-search.model'
|
2021-03-10 09:09:04 +01:00
|
|
|
import { CONFIG } from '../../initializers/constants'
|
2020-05-28 11:37:47 +02:00
|
|
|
import { formatChannelForAPI, queryChannels } from '../../lib/elastic-search-channels'
|
2020-02-19 15:39:35 +01:00
|
|
|
import { asyncMiddleware } from '../../middlewares/async'
|
2020-05-28 11:37:47 +02:00
|
|
|
import { setDefaultPagination } from '../../middlewares/pagination'
|
2020-02-19 15:39:35 +01:00
|
|
|
import { setDefaultSearchSort } from '../../middlewares/sort'
|
2021-03-10 09:09:04 +01:00
|
|
|
import { methodsValidator } from '../../middlewares/validators/method'
|
2020-05-28 11:37:47 +02:00
|
|
|
import { paginationValidator } from '../../middlewares/validators/pagination'
|
2021-03-10 09:09:04 +01:00
|
|
|
import { commonFiltersValidators, videoChannelsSearchValidator } from '../../middlewares/validators/search'
|
2020-05-28 11:37:47 +02:00
|
|
|
import { channelsSearchSortValidator } from '../../middlewares/validators/sort'
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
const searchChannelsRouter = express.Router()
|
|
|
|
|
2021-03-10 09:09:04 +01:00
|
|
|
searchChannelsRouter.all('/search/video-channels',
|
|
|
|
methodsValidator([ 'POST', 'GET' ]),
|
2020-02-19 15:39:35 +01:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
channelsSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
2020-05-28 16:32:49 +02:00
|
|
|
commonFiltersValidators,
|
2020-02-19 15:39:35 +01:00
|
|
|
videoChannelsSearchValidator,
|
|
|
|
asyncMiddleware(searchChannels)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { searchChannelsRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function searchChannels (req: express.Request, res: express.Response) {
|
2020-05-29 16:16:55 +02:00
|
|
|
const query = Object.assign(req.query || {}, req.body || {}) as ChannelsSearchQuery
|
2020-06-11 11:04:00 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:37:47 +02:00
|
|
|
const resultList = await queryChannels(query)
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: resultList.total,
|
2020-05-28 11:37:47 +02:00
|
|
|
data: resultList.data.map(v => formatChannelForAPI(v, query.fromHost))
|
2020-02-19 15:39:35 +01:00
|
|
|
})
|
|
|
|
}
|