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