]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/search.ts
2ff340b597df0ef566035d83918be05f499679de
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
1 import * as express from 'express'
2 import { isNSFWHidden } from '../../helpers/express-utils'
3 import { getFormattedObjects } from '../../helpers/utils'
4 import { VideoModel } from '../../models/video/video'
5 import {
6 asyncMiddleware,
7 optionalAuthenticate,
8 paginationValidator,
9 searchValidator,
10 setDefaultPagination,
11 setDefaultSearchSort,
12 videosSearchSortValidator
13 } from '../../middlewares'
14
15 const searchRouter = express.Router()
16
17 searchRouter.get('/videos',
18 paginationValidator,
19 setDefaultPagination,
20 videosSearchSortValidator,
21 setDefaultSearchSort,
22 optionalAuthenticate,
23 searchValidator,
24 asyncMiddleware(searchVideos)
25 )
26
27 // ---------------------------------------------------------------------------
28
29 export { searchRouter }
30
31 // ---------------------------------------------------------------------------
32
33 async function searchVideos (req: express.Request, res: express.Response) {
34 const resultList = await VideoModel.searchAndPopulateAccountAndServer(
35 req.query.search as string,
36 req.query.start as number,
37 req.query.count as number,
38 req.query.sort as string,
39 isNSFWHidden(res)
40 )
41
42 return res.json(getFormattedObjects(resultList.data, resultList.total))
43 }