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