PeerTube/server/controllers/api/search.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-07-19 09:17:54 -05:00
import * as express from 'express'
2018-07-20 07:35:18 -05:00
import { buildNSFWFilter } from '../../helpers/express-utils'
2018-07-19 09:17:54 -05:00
import { getFormattedObjects } from '../../helpers/utils'
import { VideoModel } from '../../models/video/video'
import {
asyncMiddleware,
2018-07-20 07:35:18 -05:00
commonVideosFiltersValidator,
2018-07-19 09:17:54 -05:00
optionalAuthenticate,
paginationValidator,
searchValidator,
setDefaultPagination,
setDefaultSearchSort,
videosSearchSortValidator
} from '../../middlewares'
2018-07-20 07:35:18 -05:00
import { VideosSearchQuery } from '../../../shared/models/search'
2018-07-19 09:17:54 -05:00
const searchRouter = express.Router()
searchRouter.get('/videos',
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
optionalAuthenticate,
2018-07-20 07:35:18 -05:00
commonVideosFiltersValidator,
2018-07-19 09:17:54 -05:00
searchValidator,
asyncMiddleware(searchVideos)
)
// ---------------------------------------------------------------------------
export { searchRouter }
// ---------------------------------------------------------------------------
async function searchVideos (req: express.Request, res: express.Response) {
2018-07-20 07:35:18 -05: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 09:17:54 -05:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}