1 import * as express from 'express'
2 import { sanitizeUrl } from '@server/helpers/core-utils'
3 import { doJSONRequest } from '@server/helpers/requests'
4 import { CONFIG } from '@server/initializers/config'
5 import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
6 import { Hooks } from '@server/lib/plugins/hooks'
7 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
8 import { getServerActor } from '@server/models/application/application'
9 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
10 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
11 import { ResultList, Video, VideoChannel } from '@shared/models'
12 import { SearchTargetQuery } from '@shared/models/search/search-target-query.model'
13 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
14 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
15 import { logger } from '../../helpers/logger'
16 import { getFormattedObjects } from '../../helpers/utils'
17 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
18 import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
21 commonVideosFiltersValidator,
26 videoChannelsListSearchValidator,
27 videoChannelsSearchSortValidator,
28 videosSearchSortValidator,
30 } from '../../middlewares'
31 import { VideoModel } from '../../models/video/video'
32 import { VideoChannelModel } from '../../models/video/video-channel'
33 import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models'
35 const searchRouter = express.Router()
37 searchRouter.get('/videos',
40 videosSearchSortValidator,
43 commonVideosFiltersValidator,
44 videosSearchValidator,
45 asyncMiddleware(searchVideos)
48 searchRouter.get('/video-channels',
51 videoChannelsSearchSortValidator,
54 videoChannelsListSearchValidator,
55 asyncMiddleware(searchVideoChannels)
58 // ---------------------------------------------------------------------------
60 export { searchRouter }
62 // ---------------------------------------------------------------------------
64 function searchVideoChannels (req: express.Request, res: express.Response) {
65 const query: VideoChannelsSearchQuery = req.query
66 const search = query.search
68 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
70 const parts = search.split('@')
72 // Handle strings like @toto@example.com
73 if (parts.length === 3 && parts[0].length === 0) parts.shift()
74 const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
76 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
78 // @username -> username to search in DB
79 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
81 if (isSearchIndexSearch(query)) {
82 return searchVideoChannelsIndex(query, res)
85 return searchVideoChannelsDB(query, res)
88 async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
89 const result = await buildMutedForSearchIndex(res)
91 const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params')
93 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
96 logger.debug('Doing video channels search index request on %s.', url, { body })
98 const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body })
99 const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result')
101 return res.json(jsonResult)
103 logger.warn('Cannot use search index to make video channels search.', { err })
106 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
107 message: 'Cannot use search index to make video channels search'
112 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
113 const serverActor = await getServerActor()
115 const apiOptions = await Hooks.wrapObject({
116 actorId: serverActor.id,
117 search: query.search,
121 }, 'filter:api.search.video-channels.local.list.params')
123 const resultList = await Hooks.wrapPromiseFun(
124 VideoChannelModel.searchForApi,
126 'filter:api.search.video-channels.local.list.result'
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
132 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
133 let videoChannel: MChannelAccountDefault
136 if (isWebfingerSearch) {
138 uri = await loadActorUrlOrGetFromWebfinger(search)
140 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
142 return res.json({ total: 0, data: [] })
146 if (isUserAbleToSearchRemoteURI(res)) {
148 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
149 videoChannel = actor.VideoChannel
151 logger.info('Cannot search remote video channel %s.', uri, { err })
154 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
158 total: videoChannel ? 1 : 0,
159 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
163 function searchVideos (req: express.Request, res: express.Response) {
164 const query: VideosSearchQuery = req.query
165 const search = query.search
167 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
168 return searchVideoURI(search, res)
171 if (isSearchIndexSearch(query)) {
172 return searchVideosIndex(query, res)
175 return searchVideosDB(query, res)
178 async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
179 const result = await buildMutedForSearchIndex(res)
181 let body: VideosSearchQuery = Object.assign(query, result)
183 // Use the default instance NSFW policy if not specified
185 const nsfwPolicy = res.locals.oauth
186 ? res.locals.oauth.token.User.nsfwPolicy
187 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
189 body.nsfw = nsfwPolicy === 'do_not_list'
194 body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
196 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
199 logger.debug('Doing videos search index request on %s.', url, { body })
201 const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
202 const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
204 return res.json(jsonResult)
206 logger.warn('Cannot use search index to make video search.', { err })
209 status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
210 message: 'Cannot use search index to make video search'
215 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
216 const apiOptions = await Hooks.wrapObject(Object.assign(query, {
217 includeLocalVideos: true,
218 nsfw: buildNSFWFilter(res, query.nsfw),
219 filter: query.filter,
220 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
221 }), 'filter:api.search.videos.local.list.params')
223 const resultList = await Hooks.wrapPromiseFun(
224 VideoModel.searchAndPopulateAccountAndServer,
226 'filter:api.search.videos.local.list.result'
229 return res.json(getFormattedObjects(resultList.data, resultList.total))
232 async function searchVideoURI (url: string, res: express.Response) {
233 let video: MVideoAccountLightBlacklistAllFiles
235 // Check if we can fetch a remote video with the URL
236 if (isUserAbleToSearchRemoteURI(res)) {
247 const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
248 video = result ? result.video : undefined
250 logger.info('Cannot search remote video %s.', url, { err })
253 video = await VideoModel.loadByUrlAndPopulateAccount(url)
257 total: video ? 1 : 0,
258 data: video ? [ video.toFormattedJSON() ] : []
262 function isSearchIndexSearch (query: SearchTargetQuery) {
263 if (query.searchTarget === 'search-index') return true
265 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
267 if (searchIndexConfig.ENABLED !== true) return false
269 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
270 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
275 async function buildMutedForSearchIndex (res: express.Response) {
276 const serverActor = await getServerActor()
277 const accountIds = [ serverActor.Account.id ]
279 if (res.locals.oauth) {
280 accountIds.push(res.locals.oauth.token.User.Account.id)
283 const [ blockedHosts, blockedAccounts ] = await Promise.all([
284 ServerBlocklistModel.listHostsBlockedBy(accountIds),
285 AccountBlocklistModel.listHandlesBlockedBy(accountIds)