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'
19 commonVideosFiltersValidator,
24 videoChannelsSearchSortValidator,
25 videoChannelsSearchValidator,
26 videosSearchSortValidator,
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'
33 const searchRouter = express.Router()
35 searchRouter.get('/videos',
38 videosSearchSortValidator,
41 commonVideosFiltersValidator,
42 videosSearchValidator,
43 asyncMiddleware(searchVideos)
46 searchRouter.get('/video-channels',
49 videoChannelsSearchSortValidator,
52 videoChannelsSearchValidator,
53 asyncMiddleware(searchVideoChannels)
56 // ---------------------------------------------------------------------------
58 export { searchRouter }
60 // ---------------------------------------------------------------------------
62 function searchVideoChannels (req: express.Request, res: express.Response) {
63 const query: VideoChannelsSearchQuery = req.query
64 const search = query.search
66 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
68 const parts = search.split('@')
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(' '))
74 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
76 // @username -> username to search in DB
77 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
79 if (isSearchIndexSearch(query)) {
80 return searchVideoChannelsIndex(query, res)
83 return searchVideoChannelsDB(query, res)
86 async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
87 logger.debug('Doing channels search on search index.')
89 const result = await buildMutedForSearchIndex(res)
91 const body = Object.assign(query, result)
93 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
96 const searchIndexResult = await doRequest<ResultList<VideoChannel>>({ uri: url, body, json: true })
98 return res.json(searchIndexResult.body)
100 logger.warn('Cannot use search index to make video channels search.', { err })
102 return res.sendStatus(500)
106 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
107 const serverActor = await getServerActor()
110 actorId: serverActor.id,
111 search: query.search,
116 const resultList = await VideoChannelModel.searchForApi(options)
118 return res.json(getFormattedObjects(resultList.data, resultList.total))
121 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
122 let videoChannel: MChannelAccountDefault
125 if (isWebfingerSearch) {
127 uri = await loadActorUrlOrGetFromWebfinger(search)
129 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
131 return res.json({ total: 0, data: [] })
135 if (isUserAbleToSearchRemoteURI(res)) {
137 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
138 videoChannel = actor.VideoChannel
140 logger.info('Cannot search remote video channel %s.', uri, { err })
143 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
147 total: videoChannel ? 1 : 0,
148 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
152 function searchVideos (req: express.Request, res: express.Response) {
153 const query: VideosSearchQuery = req.query
154 const search = query.search
156 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
157 return searchVideoURI(search, res)
160 if (isSearchIndexSearch(query)) {
161 return searchVideosIndex(query, res)
164 return searchVideosDB(query, res)
167 async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
168 logger.debug('Doing videos search on search index.')
170 const result = await buildMutedForSearchIndex(res)
172 const body: VideosSearchQuery = Object.assign(query, result)
174 // Use the default instance NSFW policy if not specified
176 const nsfwPolicy = res.locals.oauth
177 ? res.locals.oauth.token.User.nsfwPolicy
178 : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
180 body.nsfw = nsfwPolicy === 'do_not_list'
185 const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
188 const searchIndexResult = await doRequest<ResultList<Video>>({ uri: url, body, json: true })
190 return res.json(searchIndexResult.body)
192 logger.warn('Cannot use search index to make video search.', { err })
194 return res.sendStatus(500)
198 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
199 const options = Object.assign(query, {
200 includeLocalVideos: true,
201 nsfw: buildNSFWFilter(res, query.nsfw),
202 filter: query.filter,
203 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
205 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
207 return res.json(getFormattedObjects(resultList.data, resultList.total))
210 async function searchVideoURI (url: string, res: express.Response) {
211 let video: MVideoAccountLightBlacklistAllFiles
213 // Check if we can fetch a remote video with the URL
214 if (isUserAbleToSearchRemoteURI(res)) {
225 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
226 video = result ? result.video : undefined
228 logger.info('Cannot search remote video %s.', url, { err })
231 video = await VideoModel.loadByUrlAndPopulateAccount(url)
235 total: video ? 1 : 0,
236 data: video ? [ video.toFormattedJSON() ] : []
240 function isSearchIndexSearch (query: SearchTargetQuery) {
241 if (query.searchTarget === 'search-index') return true
243 const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
245 if (searchIndexConfig.ENABLED !== true) return false
247 if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
248 if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
253 async function buildMutedForSearchIndex (res: express.Response) {
254 const serverActor = await getServerActor()
255 const accountIds = [ serverActor.Account.id ]
257 if (res.locals.oauth) {
258 accountIds.push(res.locals.oauth.token.User.Account.id)
261 const [ blockedHosts, blockedAccounts ] = await Promise.all([
262 ServerBlocklistModel.listHostsBlockedBy(accountIds),
263 AccountBlocklistModel.listHandlesBlockedBy(accountIds)