1 import * as express from 'express'
2 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
3 import { getFormattedObjects } from '../../helpers/utils'
4 import { VideoModel } from '../../models/video/video'
7 commonVideosFiltersValidator,
12 videoChannelsSearchSortValidator,
13 videoChannelsSearchValidator,
14 videosSearchSortValidator,
16 } from '../../middlewares'
17 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
18 import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
19 import { logger } from '../../helpers/logger'
20 import { VideoChannelModel } from '../../models/video/video-channel'
21 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
22 import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
23 import { getServerActor } from '@server/models/application/application'
24 import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos'
26 const searchRouter = express.Router()
28 searchRouter.get('/videos',
31 videosSearchSortValidator,
34 commonVideosFiltersValidator,
35 videosSearchValidator,
36 asyncMiddleware(searchVideos)
39 searchRouter.get('/video-channels',
42 videoChannelsSearchSortValidator,
45 videoChannelsSearchValidator,
46 asyncMiddleware(searchVideoChannels)
49 // ---------------------------------------------------------------------------
51 export { searchRouter }
53 // ---------------------------------------------------------------------------
55 function searchVideoChannels (req: express.Request, res: express.Response) {
56 const query: VideoChannelsSearchQuery = req.query
57 const search = query.search
59 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
61 const parts = search.split('@')
63 // Handle strings like @toto@example.com
64 if (parts.length === 3 && parts[0].length === 0) parts.shift()
65 const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
67 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
69 // @username -> username to search in DB
70 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
71 return searchVideoChannelsDB(query, res)
74 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
75 const serverActor = await getServerActor()
78 actorId: serverActor.id,
84 const resultList = await VideoChannelModel.searchForApi(options)
86 return res.json(getFormattedObjects(resultList.data, resultList.total))
89 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
90 let videoChannel: MChannelAccountDefault
93 if (isWebfingerSearch) {
95 uri = await loadActorUrlOrGetFromWebfinger(search)
97 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
99 return res.json({ total: 0, data: [] })
103 if (isUserAbleToSearchRemoteURI(res)) {
105 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
106 videoChannel = actor.VideoChannel
108 logger.info('Cannot search remote video channel %s.', uri, { err })
111 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
115 total: videoChannel ? 1 : 0,
116 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
120 function searchVideos (req: express.Request, res: express.Response) {
121 const query: VideosSearchQuery = req.query
122 const search = query.search
123 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
124 return searchVideoURI(search, res)
127 return searchVideosDB(query, res)
130 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
131 const options = Object.assign(query, {
132 includeLocalVideos: true,
133 nsfw: buildNSFWFilter(res, query.nsfw),
134 filter: query.filter,
135 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
137 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
139 return res.json(getFormattedObjects(resultList.data, resultList.total))
142 async function searchVideoURI (url: string, res: express.Response) {
143 let video: MVideoAccountLightBlacklistAllFiles
145 // Check if we can fetch a remote video with the URL
146 if (isUserAbleToSearchRemoteURI(res)) {
157 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
158 video = result ? result.video : undefined
160 logger.info('Cannot search remote video %s.', url, { err })
163 video = await VideoModel.loadByUrlAndPopulateAccount(url)
167 total: video ? 1 : 0,
168 data: video ? [ video.toFormattedJSON() ] : []