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

49 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-02-14 16:14:45 +01:00
import * as express from 'express'
import { paginationValidator } from '../../middlewares/validators/pagination'
import { setDefaultPagination } from '../../middlewares/pagination'
import { asyncMiddleware } from '../../middlewares/async'
2020-02-18 15:33:21 +01:00
import { formatVideoForAPI, queryVideos } from '../../lib/elastic-search-videos'
2020-02-14 16:14:45 +01:00
import { videosSearchSortValidator } from '../../middlewares/validators/sort'
import { commonVideosFiltersValidator, videosSearchValidator, commonFiltersValidators } from '../../middlewares/validators/search'
2020-02-14 16:14:45 +01:00
import { setDefaultSearchSort } from '../../middlewares/sort'
import { VideosSearchQuery } from 'server/types/video-search.model'
2020-06-11 11:04:00 +02:00
import { CONFIG } from '../../initializers/constants'
2020-02-14 16:14:45 +01:00
2020-02-19 15:39:35 +01:00
const searchVideosRouter = express.Router()
2020-02-14 16:14:45 +01:00
2020-02-19 15:39:35 +01:00
searchVideosRouter.get('/search/videos',
2020-02-14 16:14:45 +01:00
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
commonFiltersValidators,
2020-02-14 16:14:45 +01:00
commonVideosFiltersValidator,
videosSearchValidator,
asyncMiddleware(searchVideos)
)
// ---------------------------------------------------------------------------
2020-02-19 15:39:35 +01:00
export { searchVideosRouter }
2020-02-14 16:14:45 +01:00
// ---------------------------------------------------------------------------
async function searchVideos (req: express.Request, res: express.Response) {
2020-05-29 16:16:55 +02:00
const query = Object.assign(req.query || {}, req.body || {}) as VideosSearchQuery
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)
}
const resultList = await queryVideos(query)
2020-02-14 16:14:45 +01:00
2020-02-18 15:33:21 +01:00
return res.json({
total: resultList.total,
data: resultList.data.map(v => formatVideoForAPI(v, query.fromHost))
2020-02-18 15:33:21 +01:00
})
2020-02-14 16:14:45 +01:00
}