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