]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/search.ts
d95e7cac9dd8ef6a0d44a25b6dc99eb6074548f2
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
1 import * as express from 'express'
2 import { buildNSFWFilter } from '../../helpers/express-utils'
3 import { getFormattedObjects } from '../../helpers/utils'
4 import { VideoModel } from '../../models/video/video'
5 import {
6 asyncMiddleware,
7 commonVideosFiltersValidator,
8 optionalAuthenticate,
9 paginationValidator,
10 searchValidator,
11 setDefaultPagination,
12 setDefaultSearchSort,
13 videosSearchSortValidator
14 } from '../../middlewares'
15 import { VideosSearchQuery } from '../../../shared/models/search'
16 import { getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
17 import { logger } from '../../helpers/logger'
18 import { User } from '../../../shared/models/users'
19 import { CONFIG } from '../../initializers/constants'
20
21 const searchRouter = express.Router()
22
23 searchRouter.get('/videos',
24 paginationValidator,
25 setDefaultPagination,
26 videosSearchSortValidator,
27 setDefaultSearchSort,
28 optionalAuthenticate,
29 commonVideosFiltersValidator,
30 searchValidator,
31 asyncMiddleware(searchVideos)
32 )
33
34 // ---------------------------------------------------------------------------
35
36 export { searchRouter }
37
38 // ---------------------------------------------------------------------------
39
40 function searchVideos (req: express.Request, res: express.Response) {
41 const query: VideosSearchQuery = req.query
42 if (query.search.startsWith('http://') || query.search.startsWith('https://')) {
43 return searchVideoUrl(query.search, res)
44 }
45
46 return searchVideosDB(query, res)
47 }
48
49 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
50 const options = Object.assign(query, {
51 includeLocalVideos: true,
52 nsfw: buildNSFWFilter(res, query.nsfw)
53 })
54 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
55
56 return res.json(getFormattedObjects(resultList.data, resultList.total))
57 }
58
59 async function searchVideoUrl (url: string, res: express.Response) {
60 let video: VideoModel
61 const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
62
63 // Check if we can fetch a remote video with the URL
64 if (
65 CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
66 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
67 ) {
68 try {
69 const syncParam = {
70 likes: false,
71 dislikes: false,
72 shares: false,
73 comments: false,
74 thumbnail: true,
75 refreshVideo: false
76 }
77
78 const res = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
79 video = res ? res.video : undefined
80 } catch (err) {
81 logger.info('Cannot search remote video %s.', url)
82 }
83 } else {
84 video = await VideoModel.loadByUrlAndPopulateAccount(url)
85 }
86
87 return res.json({
88 total: video ? 1 : 0,
89 data: video ? [ video.toFormattedJSON() ] : []
90 })
91 }