1
0
Fork 0
peertube/server/controllers/api/search.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-07-19 10:17:54 -04:00
import * as express from 'express'
2018-07-20 08:35:18 -04:00
import { buildNSFWFilter } from '../../helpers/express-utils'
2018-07-19 10:17:54 -04:00
import { getFormattedObjects } from '../../helpers/utils'
import { VideoModel } from '../../models/video/video'
import {
asyncMiddleware,
2018-07-20 08:35:18 -04:00
commonVideosFiltersValidator,
2018-07-19 10:17:54 -04:00
optionalAuthenticate,
paginationValidator,
searchValidator,
setDefaultPagination,
setDefaultSearchSort,
videosSearchSortValidator
} from '../../middlewares'
2018-07-20 08:35:18 -04:00
import { VideosSearchQuery } from '../../../shared/models/search'
2018-07-19 10:17:54 -04:00
const searchRouter = express.Router()
searchRouter.get('/videos',
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
optionalAuthenticate,
2018-07-20 08:35:18 -04:00
commonVideosFiltersValidator,
2018-07-19 10:17:54 -04:00
searchValidator,
asyncMiddleware(searchVideos)
)
// ---------------------------------------------------------------------------
export { searchRouter }
// ---------------------------------------------------------------------------
async function searchVideos (req: express.Request, res: express.Response) {
2018-07-20 08:35:18 -04:00
const query: VideosSearchQuery = req.query
const options = Object.assign(query, { nsfw: buildNSFWFilter(res, query.nsfw) })
const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
2018-07-19 10:17:54 -04:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}