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 { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
10 import { ResultList, Video, VideoChannel } from '@shared/models'
11 import { SearchTargetQuery } from '@shared/models/search/search-target-query.model'
12 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
13 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
14 import { logger } from '../../helpers/logger'
15 import { getFormattedObjects } from '../../helpers/utils'
16 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
17 import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
20 commonVideosFiltersValidator,
25 videoChannelsSearchSortValidator,
26 videoChannelsListSearchValidator,
27 videosSearchSortValidator,
29 } from '../../middlewares'
30 import { VideoModel } from '../../models/video/video'
31 import { VideoChannelModel } from '../../models/video/video-channel'
32 import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models'
34 const searchRouter = express.Router()
36 searchRouter.get('/videos',
39 videosSearchSortValidator,
42 commonVideosFiltersValidator,
43 videosSearchValidator,
44 asyncMiddleware(searchVideos)
47 searchRouter.get('/video-channels',
50 videoChannelsSearchSortValidator,
53 videoChannelsListSearchValidator,
54 asyncMiddleware(searchVideoChannels)
57 // ---------------------------------------------------------------------------
59 export { searchRouter }
61 // ---------------------------------------------------------------------------
63 function searchVideoChannels (req: express.Request, res: express.Response) {
64 const query: VideoChannelsSearchQuery = req.query
65 const search = query.search
67 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
69 const parts = search.split('@')
71 // Handle strings like @toto@example.com
72 if (parts.length === 3 && parts[0].length === 0) parts.shift()
73 const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
75 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
77 // @username -> username to search in DB
78 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
80 if (isSearchIndexSearch(query)) {
81 return searchVideoChannelsIndex(query, res)
84 return searchVideoChannelsDB(query, res)
87 async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
88 const result = await buildMutedForSearchIndex(res)
90 const body = Object.assign(query, result)
92 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
95 logger.debug('Doing video channels search index request on %s.', url, { body })
97 const searchIndexResult = await doRequest<ResultList<VideoChannel>>({ uri: url, body, json: true })
99 return res.json(searchIndexResult.body)
101 logger.warn('Cannot use search index to make video channels search.', { err })
103 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
107 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
108 const serverActor = await getServerActor()
111 actorId: serverActor.id,
112 search: query.search,
117 const resultList = await VideoChannelModel.searchForApi(options)
119 return res.json(getFormattedObjects(resultList.data, resultList.total))
122 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
123 let videoChannel: MChannelAccountDefault
126 if (isWebfingerSearch) {
128 uri = await loadActorUrlOrGetFromWebfinger(search)
130 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
132 return res.json({ total: 0, data: [] })
136 if (isUserAbleToSearchRemoteURI(res)) {
138 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
139 videoChannel = actor.VideoChannel
141 logger.info('Cannot search remote video channel %s.', uri, { err })
144 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
148 total: videoChannel ? 1 : 0,
149 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
153 function searchVideos (req: express.Request, res: express.Response) {
154 const query: VideosSearchQuery = req.query
155 const search = query.search
157 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
158 return searchVideoURI(search, res)
161 if (isSearchIndexSearch(query)) {
162 return searchVideosIndex(query, res)
165 return searchVideosDB(query, res)
168 async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
169 const result = await buildMutedForSearchIndex(res)
171 const body: VideosSearchQuery = Object.assign(query, result)
173 // Use the default instance NSFW policy if not specified
175 const nsfwPolicy = res.locals.oauth
176 ? res.locals.oauth.token.User.nsfwPolicy
177 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
179 body.nsfw = nsfwPolicy === 'do_not_list'
184 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
187 logger.debug('Doing videos search index request on %s.', url, { body })
189 const searchIndexResult = await doRequest<ResultList<Video>>({ uri: url, body, json: true })
191 return res.json(searchIndexResult.body)
193 logger.warn('Cannot use search index to make video search.', { err })
195 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
199 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
200 const options = Object.assign(query, {
201 includeLocalVideos: true,
202 nsfw: buildNSFWFilter(res, query.nsfw),
203 filter: query.filter,
204 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
206 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
208 return res.json(getFormattedObjects(resultList.data, resultList.total))
211 async function searchVideoURI (url: string, res: express.Response) {
212 let video: MVideoAccountLightBlacklistAllFiles
214 // Check if we can fetch a remote video with the URL
215 if (isUserAbleToSearchRemoteURI(res)) {
226 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
227 video = result ? result.video : undefined
229 logger.info('Cannot search remote video %s.', url, { err })
232 video = await VideoModel.loadByUrlAndPopulateAccount(url)
236 total: video ? 1 : 0,
237 data: video ? [ video.toFormattedJSON() ] : []
241 function isSearchIndexSearch (query: SearchTargetQuery) {
242 if (query.searchTarget === 'search-index') return true
244 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
246 if (searchIndexConfig.ENABLED !== true) return false
248 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
249 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
254 async function buildMutedForSearchIndex (res: express.Response) {
255 const serverActor = await getServerActor()
256 const accountIds = [ serverActor.Account.id ]
258 if (res.locals.oauth) {
259 accountIds.push(res.locals.oauth.token.User.Account.id)
262 const [ blockedHosts, blockedAccounts ] = await Promise.all([
263 ServerBlocklistModel.listHostsBlockedBy(accountIds),
264 AccountBlocklistModel.listHandlesBlockedBy(accountIds)