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