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

50 lines
1.8 KiB
TypeScript
Raw Normal View History

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'
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'
import { methodsValidator } from '../../middlewares/validators/method'
2020-05-28 11:37:47 +02:00
import { paginationValidator } from '../../middlewares/validators/pagination'
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()
searchChannelsRouter.all('/search/video-channels',
methodsValidator([ 'POST', 'GET' ]),
2020-02-19 15:39:35 +01:00
paginationValidator,
setDefaultPagination,
channelsSearchSortValidator,
setDefaultSearchSort,
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
})
}