2022-06-03 10:54:30 +02:00
|
|
|
import express from 'express'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { Searcher } from '../../lib/controllers/searcher'
|
2023-11-09 14:34:36 +01:00
|
|
|
import { formatChannelForAPI, queryChannels } from '../../lib/meilisearch/meilisearch-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'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { ChannelsSearchQuery } from '../../types/search-query/channel-search.model'
|
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
|
|
|
|
2021-07-29 10:57:33 +02:00
|
|
|
if (query.handles && query.fromHost) {
|
|
|
|
query.handles = query.handles.map(h => {
|
|
|
|
if (h.includes('@')) return h
|
|
|
|
|
|
|
|
return h + '@' + query.fromHost
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:18:54 +02:00
|
|
|
const searcher = new Searcher(queryChannels, formatChannelForAPI)
|
|
|
|
const result = await searcher.getResult(query)
|
2020-06-11 11:04:00 +02:00
|
|
|
|
2021-06-24 15:18:54 +02:00
|
|
|
return res.json(result)
|
2020-02-19 15:39:35 +01:00
|
|
|
}
|