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