]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search.ts
Add refresh video on search
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
CommitLineData
57c36b27 1import * as express from 'express'
d525fc39 2import { buildNSFWFilter } from '../../helpers/express-utils'
57c36b27
C
3import { getFormattedObjects } from '../../helpers/utils'
4import { VideoModel } from '../../models/video/video'
5import {
6 asyncMiddleware,
d525fc39 7 commonVideosFiltersValidator,
57c36b27
C
8 optionalAuthenticate,
9 paginationValidator,
10 searchValidator,
11 setDefaultPagination,
12 setDefaultSearchSort,
13 videosSearchSortValidator
14} from '../../middlewares'
d525fc39 15import { VideosSearchQuery } from '../../../shared/models/search'
1297eb5d 16import { getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
f6eebcb3 17import { logger } from '../../helpers/logger'
1297eb5d
C
18import { User } from '../../../shared/models/users'
19import { CONFIG } from '../../initializers/constants'
57c36b27
C
20
21const searchRouter = express.Router()
22
23searchRouter.get('/videos',
24 paginationValidator,
25 setDefaultPagination,
26 videosSearchSortValidator,
27 setDefaultSearchSort,
28 optionalAuthenticate,
d525fc39 29 commonVideosFiltersValidator,
57c36b27
C
30 searchValidator,
31 asyncMiddleware(searchVideos)
32)
33
34// ---------------------------------------------------------------------------
35
36export { searchRouter }
37
38// ---------------------------------------------------------------------------
39
f6eebcb3 40function searchVideos (req: express.Request, res: express.Response) {
d525fc39 41 const query: VideosSearchQuery = req.query
f6eebcb3
C
42 if (query.search.startsWith('http://') || query.search.startsWith('https://')) {
43 return searchVideoUrl(query.search, res)
44 }
d525fc39 45
f6eebcb3
C
46 return searchVideosDB(query, res)
47}
48
49async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
06a05d5f
C
50 const options = Object.assign(query, {
51 includeLocalVideos: true,
52 nsfw: buildNSFWFilter(res, query.nsfw)
53 })
d525fc39 54 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
57c36b27
C
55
56 return res.json(getFormattedObjects(resultList.data, resultList.total))
57}
f6eebcb3
C
58
59async function searchVideoUrl (url: string, res: express.Response) {
60 let video: VideoModel
1297eb5d 61 const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
f6eebcb3 62
1297eb5d
C
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 }
f6eebcb3 77
1297eb5d
C
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)
f6eebcb3
C
85 }
86
87 return res.json({
88 total: video ? 1 : 0,
89 data: video ? [ video.toFormattedJSON() ] : []
90 })
91}