]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/search.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
CommitLineData
57c36b27 1import * as express from 'express'
5fb2e288
C
2import { sanitizeUrl } from '@server/helpers/core-utils'
3import { doRequest } from '@server/helpers/requests'
4import { CONFIG } from '@server/initializers/config'
5import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos'
6import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
7import { getServerActor } from '@server/models/application/application'
8import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
2d53be02 9import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
5fb2e288
C
10import { ResultList, Video, VideoChannel } from '@shared/models'
11import { SearchTargetQuery } from '@shared/models/search/search-target-query.model'
12import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
687d638c 13import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
5fb2e288 14import { logger } from '../../helpers/logger'
8dc8a34e 15import { getFormattedObjects } from '../../helpers/utils'
5fb2e288
C
16import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
17import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
57c36b27
C
18import {
19 asyncMiddleware,
d525fc39 20 commonVideosFiltersValidator,
57c36b27
C
21 optionalAuthenticate,
22 paginationValidator,
57c36b27
C
23 setDefaultPagination,
24 setDefaultSearchSort,
f37dc0dd 25 videoChannelsSearchSortValidator,
7b390964 26 videoChannelsListSearchValidator,
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
C
87async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
88 logger.debug('Doing channels search on search index.')
89
90 const result = await buildMutedForSearchIndex(res)
91
92 const body = Object.assign(query, result)
93
94 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
95
96 try {
97 const searchIndexResult = await doRequest<ResultList<VideoChannel>>({ uri: url, body, json: true })
98
99 return res.json(searchIndexResult.body)
100 } catch (err) {
101 logger.warn('Cannot use search index to make video channels search.', { err })
102
2d53be02 103 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
5fb2e288
C
104 }
105}
106
f37dc0dd
C
107async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
108 const serverActor = await getServerActor()
109
110 const options = {
111 actorId: serverActor.id,
112 search: query.search,
113 start: query.start,
114 count: query.count,
115 sort: query.sort
116 }
117 const resultList = await VideoChannelModel.searchForApi(options)
118
119 return res.json(getFormattedObjects(resultList.data, resultList.total))
120}
121
f5b0af50 122async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
453e83ea 123 let videoChannel: MChannelAccountDefault
f5b0af50 124 let uri = search
f37dc0dd 125
cce1b3df
C
126 if (isWebfingerSearch) {
127 try {
128 uri = await loadActorUrlOrGetFromWebfinger(search)
129 } catch (err) {
130 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
131
132 return res.json({ total: 0, data: [] })
133 }
134 }
f37dc0dd 135
f5b0af50
C
136 if (isUserAbleToSearchRemoteURI(res)) {
137 try {
e587e0ec 138 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
f5b0af50
C
139 videoChannel = actor.VideoChannel
140 } catch (err) {
141 logger.info('Cannot search remote video channel %s.', uri, { err })
142 }
f37dc0dd 143 } else {
f5b0af50 144 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
f37dc0dd
C
145 }
146
147 return res.json({
148 total: videoChannel ? 1 : 0,
149 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
150 })
151}
152
f6eebcb3 153function searchVideos (req: express.Request, res: express.Response) {
d525fc39 154 const query: VideosSearchQuery = req.query
240085d0 155 const search = query.search
5fb2e288 156
240085d0 157 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
f37dc0dd 158 return searchVideoURI(search, res)
f6eebcb3 159 }
d525fc39 160
3521ab8f 161 if (isSearchIndexSearch(query)) {
5fb2e288
C
162 return searchVideosIndex(query, res)
163 }
164
f6eebcb3
C
165 return searchVideosDB(query, res)
166}
167
5fb2e288
C
168async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
169 logger.debug('Doing videos search on search index.')
170
171 const result = await buildMutedForSearchIndex(res)
172
1a40132c
C
173 const body: VideosSearchQuery = Object.assign(query, result)
174
175 // Use the default instance NSFW policy if not specified
176 if (!body.nsfw) {
ba114024
C
177 const nsfwPolicy = res.locals.oauth
178 ? res.locals.oauth.token.User.nsfwPolicy
179 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
180
181 body.nsfw = nsfwPolicy === 'do_not_list'
1a40132c
C
182 ? 'false'
183 : 'both'
184 }
5fb2e288
C
185
186 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
187
188 try {
189 const searchIndexResult = await doRequest<ResultList<Video>>({ uri: url, body, json: true })
190
191 return res.json(searchIndexResult.body)
192 } catch (err) {
193 logger.warn('Cannot use search index to make video search.', { err })
194
2d53be02 195 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
5fb2e288
C
196 }
197}
198
f6eebcb3 199async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
06a05d5f
C
200 const options = Object.assign(query, {
201 includeLocalVideos: true,
6e46de09 202 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 203 filter: query.filter,
7ad9b984 204 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
06a05d5f 205 })
d525fc39 206 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
57c36b27
C
207
208 return res.json(getFormattedObjects(resultList.data, resultList.total))
209}
f6eebcb3 210
f37dc0dd 211async function searchVideoURI (url: string, res: express.Response) {
0283eaac 212 let video: MVideoAccountLightBlacklistAllFiles
f6eebcb3 213
1297eb5d 214 // Check if we can fetch a remote video with the URL
f37dc0dd 215 if (isUserAbleToSearchRemoteURI(res)) {
1297eb5d
C
216 try {
217 const syncParam = {
218 likes: false,
219 dislikes: false,
220 shares: false,
221 comments: false,
222 thumbnail: true,
223 refreshVideo: false
224 }
f6eebcb3 225
4157cdb1 226 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
f37dc0dd 227 video = result ? result.video : undefined
1297eb5d 228 } catch (err) {
f5b0af50 229 logger.info('Cannot search remote video %s.', url, { err })
1297eb5d
C
230 }
231 } else {
232 video = await VideoModel.loadByUrlAndPopulateAccount(url)
f6eebcb3
C
233 }
234
235 return res.json({
236 total: video ? 1 : 0,
237 data: video ? [ video.toFormattedJSON() ] : []
238 })
239}
5fb2e288 240
3521ab8f 241function isSearchIndexSearch (query: SearchTargetQuery) {
5fb2e288
C
242 if (query.searchTarget === 'search-index') return true
243
244 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
245
246 if (searchIndexConfig.ENABLED !== true) return false
247
248 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
249 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
250
251 return false
252}
253
254async function buildMutedForSearchIndex (res: express.Response) {
255 const serverActor = await getServerActor()
256 const accountIds = [ serverActor.Account.id ]
257
258 if (res.locals.oauth) {
259 accountIds.push(res.locals.oauth.token.User.Account.id)
260 }
261
262 const [ blockedHosts, blockedAccounts ] = await Promise.all([
263 ServerBlocklistModel.listHostsBlockedBy(accountIds),
264 AccountBlocklistModel.listHandlesBlockedBy(accountIds)
265 ])
266
267 return {
268 blockedHosts,
269 blockedAccounts
270 }
271}