]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search.ts
Refactor AP actors
[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'
5fb2e288 17import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
136d7efd 18import { getOrCreateAPActor } from '../../lib/activitypub/actors'
57c36b27
C
19import {
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 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',
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
48searchRouter.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
60export { searchRouter }
61
62// ---------------------------------------------------------------------------
63
f37dc0dd
C
64function 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 88async 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
76148b27
RK
105 return res.fail({
106 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
107 message: 'Cannot use search index to make video channels search'
108 })
5fb2e288
C
109 }
110}
111
f37dc0dd
C
112async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
113 const serverActor = await getServerActor()
114
74a4d531 115 const apiOptions = await Hooks.wrapObject({
f37dc0dd
C
116 actorId: serverActor.id,
117 search: query.search,
118 start: query.start,
119 count: query.count,
120 sort: query.sort
74a4d531
C
121 }, 'filter:api.search.video-channels.local.list.params')
122
123 const resultList = await Hooks.wrapPromiseFun(
124 VideoChannelModel.searchForApi,
125 apiOptions,
126 'filter:api.search.video-channels.local.list.result'
127 )
f37dc0dd
C
128
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
130}
131
f5b0af50 132async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
453e83ea 133 let videoChannel: MChannelAccountDefault
f5b0af50 134 let uri = search
f37dc0dd 135
cce1b3df
C
136 if (isWebfingerSearch) {
137 try {
138 uri = await loadActorUrlOrGetFromWebfinger(search)
139 } catch (err) {
140 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
141
142 return res.json({ total: 0, data: [] })
143 }
144 }
f37dc0dd 145
f5b0af50
C
146 if (isUserAbleToSearchRemoteURI(res)) {
147 try {
136d7efd 148 const actor = await getOrCreateAPActor(uri, 'all', true, true)
f5b0af50
C
149 videoChannel = actor.VideoChannel
150 } catch (err) {
151 logger.info('Cannot search remote video channel %s.', uri, { err })
152 }
f37dc0dd 153 } else {
f5b0af50 154 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
f37dc0dd
C
155 }
156
157 return res.json({
158 total: videoChannel ? 1 : 0,
159 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
160 })
161}
162
f6eebcb3 163function searchVideos (req: express.Request, res: express.Response) {
d525fc39 164 const query: VideosSearchQuery = req.query
240085d0 165 const search = query.search
5fb2e288 166
240085d0 167 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
f37dc0dd 168 return searchVideoURI(search, res)
f6eebcb3 169 }
d525fc39 170
3521ab8f 171 if (isSearchIndexSearch(query)) {
5fb2e288
C
172 return searchVideosIndex(query, res)
173 }
174
f6eebcb3
C
175 return searchVideosDB(query, res)
176}
177
5fb2e288 178async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
5fb2e288
C
179 const result = await buildMutedForSearchIndex(res)
180
74a4d531 181 let body: VideosSearchQuery = Object.assign(query, result)
1a40132c
C
182
183 // Use the default instance NSFW policy if not specified
184 if (!body.nsfw) {
ba114024
C
185 const nsfwPolicy = res.locals.oauth
186 ? res.locals.oauth.token.User.nsfwPolicy
187 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
188
189 body.nsfw = nsfwPolicy === 'do_not_list'
1a40132c
C
190 ? 'false'
191 : 'both'
192 }
5fb2e288 193
74a4d531
C
194 body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
195
5fb2e288
C
196 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
197
198 try {
34caef7f
C
199 logger.debug('Doing videos search index request on %s.', url, { body })
200
e5abb482 201 const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
74a4d531 202 const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
5fb2e288 203
74a4d531 204 return res.json(jsonResult)
5fb2e288
C
205 } catch (err) {
206 logger.warn('Cannot use search index to make video search.', { err })
207
76148b27
RK
208 return res.fail({
209 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
210 message: 'Cannot use search index to make video search'
211 })
5fb2e288
C
212 }
213}
214
f6eebcb3 215async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
74a4d531 216 const apiOptions = await Hooks.wrapObject(Object.assign(query, {
06a05d5f 217 includeLocalVideos: true,
6e46de09 218 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 219 filter: query.filter,
7ad9b984 220 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
74a4d531
C
221 }), 'filter:api.search.videos.local.list.params')
222
223 const resultList = await Hooks.wrapPromiseFun(
224 VideoModel.searchAndPopulateAccountAndServer,
225 apiOptions,
226 'filter:api.search.videos.local.list.result'
227 )
57c36b27
C
228
229 return res.json(getFormattedObjects(resultList.data, resultList.total))
230}
f6eebcb3 231
f37dc0dd 232async function searchVideoURI (url: string, res: express.Response) {
0283eaac 233 let video: MVideoAccountLightBlacklistAllFiles
f6eebcb3 234
1297eb5d 235 // Check if we can fetch a remote video with the URL
f37dc0dd 236 if (isUserAbleToSearchRemoteURI(res)) {
1297eb5d
C
237 try {
238 const syncParam = {
239 likes: false,
240 dislikes: false,
241 shares: false,
242 comments: false,
243 thumbnail: true,
244 refreshVideo: false
245 }
f6eebcb3 246
304a84d5 247 const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
f37dc0dd 248 video = result ? result.video : undefined
1297eb5d 249 } catch (err) {
f5b0af50 250 logger.info('Cannot search remote video %s.', url, { err })
1297eb5d
C
251 }
252 } else {
253 video = await VideoModel.loadByUrlAndPopulateAccount(url)
f6eebcb3
C
254 }
255
256 return res.json({
257 total: video ? 1 : 0,
258 data: video ? [ video.toFormattedJSON() ] : []
259 })
260}
5fb2e288 261
3521ab8f 262function isSearchIndexSearch (query: SearchTargetQuery) {
5fb2e288
C
263 if (query.searchTarget === 'search-index') return true
264
265 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
266
267 if (searchIndexConfig.ENABLED !== true) return false
268
269 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
270 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
271
272 return false
273}
274
275async function buildMutedForSearchIndex (res: express.Response) {
276 const serverActor = await getServerActor()
277 const accountIds = [ serverActor.Account.id ]
278
279 if (res.locals.oauth) {
280 accountIds.push(res.locals.oauth.token.User.Account.id)
281 }
282
283 const [ blockedHosts, blockedAccounts ] = await Promise.all([
284 ServerBlocklistModel.listHostsBlockedBy(accountIds),
285 AccountBlocklistModel.listHandlesBlockedBy(accountIds)
286 ])
287
288 return {
289 blockedHosts,
290 blockedAccounts
291 }
292}