aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/search/search-videos.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-17 16:02:38 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-06-25 14:44:01 +0200
commit37a44fc915eef2140e22ceb96aba6b6eb2509007 (patch)
treedd4a370ecc96cf38c99b940261aadc27065da7ae /server/controllers/api/search/search-videos.ts
parent33eb19e5199cc9fa4d73c6675c97508e3e072ef9 (diff)
downloadPeerTube-37a44fc915eef2140e22ceb96aba6b6eb2509007.tar.gz
PeerTube-37a44fc915eef2140e22ceb96aba6b6eb2509007.tar.zst
PeerTube-37a44fc915eef2140e22ceb96aba6b6eb2509007.zip
Add ability to search playlists
Diffstat (limited to 'server/controllers/api/search/search-videos.ts')
-rw-r--r--server/controllers/api/search/search-videos.ts153
1 files changed, 153 insertions, 0 deletions
diff --git a/server/controllers/api/search/search-videos.ts b/server/controllers/api/search/search-videos.ts
new file mode 100644
index 000000000..b626baa28
--- /dev/null
+++ b/server/controllers/api/search/search-videos.ts
@@ -0,0 +1,153 @@
1import * as express from 'express'
2import { sanitizeUrl } from '@server/helpers/core-utils'
3import { doJSONRequest } from '@server/helpers/requests'
4import { CONFIG } from '@server/initializers/config'
5import { WEBSERVER } from '@server/initializers/constants'
6import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
7import { Hooks } from '@server/lib/plugins/hooks'
8import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search'
9import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
10import { ResultList, Video } from '@shared/models'
11import { VideosSearchQuery } from '../../../../shared/models/search'
12import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils'
13import { logger } from '../../../helpers/logger'
14import { getFormattedObjects } from '../../../helpers/utils'
15import {
16 asyncMiddleware,
17 commonVideosFiltersValidator,
18 openapiOperationDoc,
19 optionalAuthenticate,
20 paginationValidator,
21 setDefaultPagination,
22 setDefaultSearchSort,
23 videosSearchSortValidator,
24 videosSearchValidator
25} from '../../../middlewares'
26import { VideoModel } from '../../../models/video/video'
27import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
28
29const searchVideosRouter = express.Router()
30
31searchVideosRouter.get('/videos',
32 openapiOperationDoc({ operationId: 'searchVideos' }),
33 paginationValidator,
34 setDefaultPagination,
35 videosSearchSortValidator,
36 setDefaultSearchSort,
37 optionalAuthenticate,
38 commonVideosFiltersValidator,
39 videosSearchValidator,
40 asyncMiddleware(searchVideos)
41)
42
43// ---------------------------------------------------------------------------
44
45export { searchVideosRouter }
46
47// ---------------------------------------------------------------------------
48
49function searchVideos (req: express.Request, res: express.Response) {
50 const query: VideosSearchQuery = req.query
51 const search = query.search
52
53 if (isURISearch(search)) {
54 return searchVideoURI(search, res)
55 }
56
57 if (isSearchIndexSearch(query)) {
58 return searchVideosIndex(query, res)
59 }
60
61 return searchVideosDB(query, res)
62}
63
64async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
65 const result = await buildMutedForSearchIndex(res)
66
67 let body: VideosSearchQuery = Object.assign(query, result)
68
69 // Use the default instance NSFW policy if not specified
70 if (!body.nsfw) {
71 const nsfwPolicy = res.locals.oauth
72 ? res.locals.oauth.token.User.nsfwPolicy
73 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
74
75 body.nsfw = nsfwPolicy === 'do_not_list'
76 ? 'false'
77 : 'both'
78 }
79
80 body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
81
82 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
83
84 try {
85 logger.debug('Doing videos search index request on %s.', url, { body })
86
87 const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
88 const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
89
90 return res.json(jsonResult)
91 } catch (err) {
92 logger.warn('Cannot use search index to make video search.', { err })
93
94 return res.fail({
95 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
96 message: 'Cannot use search index to make video search'
97 })
98 }
99}
100
101async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
102 const apiOptions = await Hooks.wrapObject(Object.assign(query, {
103 includeLocalVideos: true,
104 nsfw: buildNSFWFilter(res, query.nsfw),
105 filter: query.filter,
106 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
107 }), 'filter:api.search.videos.local.list.params')
108
109 const resultList = await Hooks.wrapPromiseFun(
110 VideoModel.searchAndPopulateAccountAndServer,
111 apiOptions,
112 'filter:api.search.videos.local.list.result'
113 )
114
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
116}
117
118async function searchVideoURI (url: string, res: express.Response) {
119 let video: MVideoAccountLightBlacklistAllFiles
120
121 // Check if we can fetch a remote video with the URL
122 if (isUserAbleToSearchRemoteURI(res)) {
123 try {
124 const syncParam = {
125 likes: false,
126 dislikes: false,
127 shares: false,
128 comments: false,
129 thumbnail: true,
130 refreshVideo: false
131 }
132
133 const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
134 video = result ? result.video : undefined
135 } catch (err) {
136 logger.info('Cannot search remote video %s.', url, { err })
137 }
138 } else {
139 video = await VideoModel.loadByUrlAndPopulateAccount(sanitizeLocalUrl(url))
140 }
141
142 return res.json({
143 total: video ? 1 : 0,
144 data: video ? [ video.toFormattedJSON() ] : []
145 })
146}
147
148function sanitizeLocalUrl (url: string) {
149 if (!url) return ''
150
151 // Handle alternative video URLs
152 return url.replace(new RegExp('^' + WEBSERVER.URL + '/w/'), WEBSERVER.URL + '/videos/watch/')
153}