]>
Commit | Line | Data |
---|---|---|
57c36b27 | 1 | import * as express from 'express' |
5fb2e288 | 2 | import { sanitizeUrl } from '@server/helpers/core-utils' |
db4b15f2 | 3 | import { doJSONRequest } from '@server/helpers/requests' |
5fb2e288 C |
4 | import { CONFIG } from '@server/initializers/config' |
5 | import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos' | |
74a4d531 | 6 | import { Hooks } from '@server/lib/plugins/hooks' |
5fb2e288 C |
7 | import { AccountBlocklistModel } from '@server/models/account/account-blocklist' |
8 | import { getServerActor } from '@server/models/application/application' | |
9 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | |
2d53be02 | 10 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
5fb2e288 C |
11 | import { ResultList, Video, VideoChannel } from '@shared/models' |
12 | import { SearchTargetQuery } from '@shared/models/search/search-target-query.model' | |
13 | import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search' | |
687d638c | 14 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' |
5fb2e288 | 15 | import { logger } from '../../helpers/logger' |
8dc8a34e | 16 | import { getFormattedObjects } from '../../helpers/utils' |
5fb2e288 C |
17 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' |
18 | import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor' | |
57c36b27 C |
19 | import { |
20 | asyncMiddleware, | |
d525fc39 | 21 | commonVideosFiltersValidator, |
57c36b27 C |
22 | optionalAuthenticate, |
23 | paginationValidator, | |
57c36b27 C |
24 | setDefaultPagination, |
25 | setDefaultSearchSort, | |
7b390964 | 26 | videoChannelsListSearchValidator, |
74a4d531 | 27 | videoChannelsSearchSortValidator, |
f37dc0dd C |
28 | videosSearchSortValidator, |
29 | videosSearchValidator | |
57c36b27 | 30 | } from '../../middlewares' |
5fb2e288 | 31 | import { VideoModel } from '../../models/video/video' |
f37dc0dd | 32 | import { VideoChannelModel } from '../../models/video/video-channel' |
26d6bf65 | 33 | import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models' |
57c36b27 C |
34 | |
35 | const searchRouter = express.Router() | |
36 | ||
37 | searchRouter.get('/videos', | |
38 | paginationValidator, | |
39 | setDefaultPagination, | |
40 | videosSearchSortValidator, | |
41 | setDefaultSearchSort, | |
42 | optionalAuthenticate, | |
d525fc39 | 43 | commonVideosFiltersValidator, |
f37dc0dd | 44 | videosSearchValidator, |
57c36b27 C |
45 | asyncMiddleware(searchVideos) |
46 | ) | |
47 | ||
f37dc0dd C |
48 | searchRouter.get('/video-channels', |
49 | paginationValidator, | |
50 | setDefaultPagination, | |
51 | videoChannelsSearchSortValidator, | |
52 | setDefaultSearchSort, | |
53 | optionalAuthenticate, | |
7b390964 | 54 | videoChannelsListSearchValidator, |
f37dc0dd C |
55 | asyncMiddleware(searchVideoChannels) |
56 | ) | |
57 | ||
57c36b27 C |
58 | // --------------------------------------------------------------------------- |
59 | ||
60 | export { searchRouter } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
f37dc0dd C |
64 | function searchVideoChannels (req: express.Request, res: express.Response) { |
65 | const query: VideoChannelsSearchQuery = req.query | |
66 | const search = query.search | |
67 | ||
68 | const isURISearch = search.startsWith('http://') || search.startsWith('https://') | |
69 | ||
70 | const parts = search.split('@') | |
2ff83ae2 C |
71 | |
72 | // Handle strings like @toto@example.com | |
73 | if (parts.length === 3 && parts[0].length === 0) parts.shift() | |
bdd428a6 | 74 | const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' ')) |
f37dc0dd | 75 | |
f5b0af50 | 76 | if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res) |
f37dc0dd | 77 | |
cce1b3df C |
78 | // @username -> username to search in DB |
79 | if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '') | |
5fb2e288 | 80 | |
3521ab8f | 81 | if (isSearchIndexSearch(query)) { |
5fb2e288 C |
82 | return searchVideoChannelsIndex(query, res) |
83 | } | |
84 | ||
f37dc0dd C |
85 | return searchVideoChannelsDB(query, res) |
86 | } | |
87 | ||
5fb2e288 | 88 | async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) { |
5fb2e288 C |
89 | const result = await buildMutedForSearchIndex(res) |
90 | ||
74a4d531 | 91 | const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params') |
5fb2e288 C |
92 | |
93 | const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels' | |
94 | ||
95 | try { | |
34caef7f C |
96 | logger.debug('Doing video channels search index request on %s.', url, { body }) |
97 | ||
e5abb482 | 98 | const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body }) |
74a4d531 | 99 | const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result') |
5fb2e288 | 100 | |
74a4d531 | 101 | return res.json(jsonResult) |
5fb2e288 C |
102 | } catch (err) { |
103 | logger.warn('Cannot use search index to make video channels search.', { err }) | |
104 | ||
2d53be02 | 105 | return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) |
5fb2e288 C |
106 | } |
107 | } | |
108 | ||
f37dc0dd C |
109 | async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) { |
110 | const serverActor = await getServerActor() | |
111 | ||
74a4d531 | 112 | const apiOptions = await Hooks.wrapObject({ |
f37dc0dd C |
113 | actorId: serverActor.id, |
114 | search: query.search, | |
115 | start: query.start, | |
116 | count: query.count, | |
117 | sort: query.sort | |
74a4d531 C |
118 | }, 'filter:api.search.video-channels.local.list.params') |
119 | ||
120 | const resultList = await Hooks.wrapPromiseFun( | |
121 | VideoChannelModel.searchForApi, | |
122 | apiOptions, | |
123 | 'filter:api.search.video-channels.local.list.result' | |
124 | ) | |
f37dc0dd C |
125 | |
126 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
127 | } | |
128 | ||
f5b0af50 | 129 | async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { |
453e83ea | 130 | let videoChannel: MChannelAccountDefault |
f5b0af50 | 131 | let uri = search |
f37dc0dd | 132 | |
cce1b3df C |
133 | if (isWebfingerSearch) { |
134 | try { | |
135 | uri = await loadActorUrlOrGetFromWebfinger(search) | |
136 | } catch (err) { | |
137 | logger.warn('Cannot load actor URL or get from webfinger.', { search, err }) | |
138 | ||
139 | return res.json({ total: 0, data: [] }) | |
140 | } | |
141 | } | |
f37dc0dd | 142 | |
f5b0af50 C |
143 | if (isUserAbleToSearchRemoteURI(res)) { |
144 | try { | |
e587e0ec | 145 | const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true) |
f5b0af50 C |
146 | videoChannel = actor.VideoChannel |
147 | } catch (err) { | |
148 | logger.info('Cannot search remote video channel %s.', uri, { err }) | |
149 | } | |
f37dc0dd | 150 | } else { |
f5b0af50 | 151 | videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri) |
f37dc0dd C |
152 | } |
153 | ||
154 | return res.json({ | |
155 | total: videoChannel ? 1 : 0, | |
156 | data: videoChannel ? [ videoChannel.toFormattedJSON() ] : [] | |
157 | }) | |
158 | } | |
159 | ||
f6eebcb3 | 160 | function searchVideos (req: express.Request, res: express.Response) { |
d525fc39 | 161 | const query: VideosSearchQuery = req.query |
240085d0 | 162 | const search = query.search |
5fb2e288 | 163 | |
240085d0 | 164 | if (search && (search.startsWith('http://') || search.startsWith('https://'))) { |
f37dc0dd | 165 | return searchVideoURI(search, res) |
f6eebcb3 | 166 | } |
d525fc39 | 167 | |
3521ab8f | 168 | if (isSearchIndexSearch(query)) { |
5fb2e288 C |
169 | return searchVideosIndex(query, res) |
170 | } | |
171 | ||
f6eebcb3 C |
172 | return searchVideosDB(query, res) |
173 | } | |
174 | ||
5fb2e288 | 175 | async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) { |
5fb2e288 C |
176 | const result = await buildMutedForSearchIndex(res) |
177 | ||
74a4d531 | 178 | let body: VideosSearchQuery = Object.assign(query, result) |
1a40132c C |
179 | |
180 | // Use the default instance NSFW policy if not specified | |
181 | if (!body.nsfw) { | |
ba114024 C |
182 | const nsfwPolicy = res.locals.oauth |
183 | ? res.locals.oauth.token.User.nsfwPolicy | |
184 | : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY | |
185 | ||
186 | body.nsfw = nsfwPolicy === 'do_not_list' | |
1a40132c C |
187 | ? 'false' |
188 | : 'both' | |
189 | } | |
5fb2e288 | 190 | |
74a4d531 C |
191 | body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params') |
192 | ||
5fb2e288 C |
193 | const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos' |
194 | ||
195 | try { | |
34caef7f C |
196 | logger.debug('Doing videos search index request on %s.', url, { body }) |
197 | ||
e5abb482 | 198 | const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body }) |
74a4d531 | 199 | const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result') |
5fb2e288 | 200 | |
74a4d531 | 201 | return res.json(jsonResult) |
5fb2e288 C |
202 | } catch (err) { |
203 | logger.warn('Cannot use search index to make video search.', { err }) | |
204 | ||
2d53be02 | 205 | return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) |
5fb2e288 C |
206 | } |
207 | } | |
208 | ||
f6eebcb3 | 209 | async function searchVideosDB (query: VideosSearchQuery, res: express.Response) { |
74a4d531 | 210 | const apiOptions = await Hooks.wrapObject(Object.assign(query, { |
06a05d5f | 211 | includeLocalVideos: true, |
6e46de09 | 212 | nsfw: buildNSFWFilter(res, query.nsfw), |
1cd3facc | 213 | filter: query.filter, |
7ad9b984 | 214 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
74a4d531 C |
215 | }), 'filter:api.search.videos.local.list.params') |
216 | ||
217 | const resultList = await Hooks.wrapPromiseFun( | |
218 | VideoModel.searchAndPopulateAccountAndServer, | |
219 | apiOptions, | |
220 | 'filter:api.search.videos.local.list.result' | |
221 | ) | |
57c36b27 C |
222 | |
223 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
224 | } | |
f6eebcb3 | 225 | |
f37dc0dd | 226 | async function searchVideoURI (url: string, res: express.Response) { |
0283eaac | 227 | let video: MVideoAccountLightBlacklistAllFiles |
f6eebcb3 | 228 | |
1297eb5d | 229 | // Check if we can fetch a remote video with the URL |
f37dc0dd | 230 | if (isUserAbleToSearchRemoteURI(res)) { |
1297eb5d C |
231 | try { |
232 | const syncParam = { | |
233 | likes: false, | |
234 | dislikes: false, | |
235 | shares: false, | |
236 | comments: false, | |
237 | thumbnail: true, | |
238 | refreshVideo: false | |
239 | } | |
f6eebcb3 | 240 | |
4157cdb1 | 241 | const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam }) |
f37dc0dd | 242 | video = result ? result.video : undefined |
1297eb5d | 243 | } catch (err) { |
f5b0af50 | 244 | logger.info('Cannot search remote video %s.', url, { err }) |
1297eb5d C |
245 | } |
246 | } else { | |
247 | video = await VideoModel.loadByUrlAndPopulateAccount(url) | |
f6eebcb3 C |
248 | } |
249 | ||
250 | return res.json({ | |
251 | total: video ? 1 : 0, | |
252 | data: video ? [ video.toFormattedJSON() ] : [] | |
253 | }) | |
254 | } | |
5fb2e288 | 255 | |
3521ab8f | 256 | function isSearchIndexSearch (query: SearchTargetQuery) { |
5fb2e288 C |
257 | if (query.searchTarget === 'search-index') return true |
258 | ||
259 | const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX | |
260 | ||
261 | if (searchIndexConfig.ENABLED !== true) return false | |
262 | ||
263 | if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true | |
264 | if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true | |
265 | ||
266 | return false | |
267 | } | |
268 | ||
269 | async function buildMutedForSearchIndex (res: express.Response) { | |
270 | const serverActor = await getServerActor() | |
271 | const accountIds = [ serverActor.Account.id ] | |
272 | ||
273 | if (res.locals.oauth) { | |
274 | accountIds.push(res.locals.oauth.token.User.Account.id) | |
275 | } | |
276 | ||
277 | const [ blockedHosts, blockedAccounts ] = await Promise.all([ | |
278 | ServerBlocklistModel.listHostsBlockedBy(accountIds), | |
279 | AccountBlocklistModel.listHandlesBlockedBy(accountIds) | |
280 | ]) | |
281 | ||
282 | return { | |
283 | blockedHosts, | |
284 | blockedAccounts | |
285 | } | |
286 | } |