]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/search.ts
Add refresh video on search
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { buildNSFWFilter } from '../../helpers/express-utils'
3import { getFormattedObjects } from '../../helpers/utils'
4import { VideoModel } from '../../models/video/video'
5import {
6 asyncMiddleware,
7 commonVideosFiltersValidator,
8 optionalAuthenticate,
9 paginationValidator,
10 searchValidator,
11 setDefaultPagination,
12 setDefaultSearchSort,
13 videosSearchSortValidator
14} from '../../middlewares'
15import { VideosSearchQuery } from '../../../shared/models/search'
16import { getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
17import { logger } from '../../helpers/logger'
18import { User } from '../../../shared/models/users'
19import { CONFIG } from '../../initializers/constants'
20
21const searchRouter = express.Router()
22
23searchRouter.get('/videos',
24 paginationValidator,
25 setDefaultPagination,
26 videosSearchSortValidator,
27 setDefaultSearchSort,
28 optionalAuthenticate,
29 commonVideosFiltersValidator,
30 searchValidator,
31 asyncMiddleware(searchVideos)
32)
33
34// ---------------------------------------------------------------------------
35
36export { searchRouter }
37
38// ---------------------------------------------------------------------------
39
40function 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
49async 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
59async 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}