sepia-search-motore-di-rice.../server/controllers/api/search-channels.ts

50 lines
1.8 KiB
TypeScript

import * as express from 'express'
import { ChannelsSearchQuery } from 'server/types/channel-search.model'
import { CONFIG } from '../../initializers/constants'
import { formatChannelForAPI, queryChannels } from '../../lib/elastic-search-channels'
import { asyncMiddleware } from '../../middlewares/async'
import { setDefaultPagination } from '../../middlewares/pagination'
import { setDefaultSearchSort } from '../../middlewares/sort'
import { methodsValidator } from '../../middlewares/validators/method'
import { paginationValidator } from '../../middlewares/validators/pagination'
import { commonFiltersValidators, videoChannelsSearchValidator } from '../../middlewares/validators/search'
import { channelsSearchSortValidator } from '../../middlewares/validators/sort'
const searchChannelsRouter = express.Router()
searchChannelsRouter.all('/search/video-channels',
methodsValidator([ 'POST', 'GET' ]),
paginationValidator,
setDefaultPagination,
channelsSearchSortValidator,
setDefaultSearchSort,
commonFiltersValidators,
videoChannelsSearchValidator,
asyncMiddleware(searchChannels)
)
// ---------------------------------------------------------------------------
export { searchChannelsRouter }
// ---------------------------------------------------------------------------
async function searchChannels (req: express.Request, res: express.Response) {
const query = Object.assign(req.query || {}, req.body || {}) as ChannelsSearchQuery
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 queryChannels(query)
return res.json({
total: resultList.total,
data: resultList.data.map(v => formatChannelForAPI(v, query.fromHost))
})
}