1 import * as express from 'express'
2 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
3 import { getFormattedObjects, getServerActor } 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, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
19 import { logger } from '../../helpers/logger'
20 import { VideoChannelModel } from '../../models/video/video-channel'
21 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
23 const searchRouter = express.Router()
25 searchRouter.get('/videos',
28 videosSearchSortValidator,
31 commonVideosFiltersValidator,
32 videosSearchValidator,
33 asyncMiddleware(searchVideos)
36 searchRouter.get('/video-channels',
39 videoChannelsSearchSortValidator,
42 videoChannelsSearchValidator,
43 asyncMiddleware(searchVideoChannels)
46 // ---------------------------------------------------------------------------
48 export { searchRouter }
50 // ---------------------------------------------------------------------------
52 function searchVideoChannels (req: express.Request, res: express.Response) {
53 const query: VideoChannelsSearchQuery = req.query
54 const search = query.search
56 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
58 const parts = search.split('@')
60 // Handle strings like @toto@example.com
61 if (parts.length === 3 && parts[0].length === 0) parts.shift()
62 const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
64 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
66 return searchVideoChannelsDB(query, res)
69 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
70 const serverActor = await getServerActor()
73 actorId: serverActor.id,
79 const resultList = await VideoChannelModel.searchForApi(options)
81 return res.json(getFormattedObjects(resultList.data, resultList.total))
84 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
85 let videoChannel: VideoChannelModel
88 if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
90 if (isUserAbleToSearchRemoteURI(res)) {
92 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
93 videoChannel = actor.VideoChannel
95 logger.info('Cannot search remote video channel %s.', uri, { err })
98 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
102 total: videoChannel ? 1 : 0,
103 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
107 function searchVideos (req: express.Request, res: express.Response) {
108 const query: VideosSearchQuery = req.query
109 const search = query.search
110 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
111 return searchVideoURI(search, res)
114 return searchVideosDB(query, res)
117 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
118 const options = Object.assign(query, {
119 includeLocalVideos: true,
120 nsfw: buildNSFWFilter(res, query.nsfw),
121 filter: query.filter,
122 userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
124 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
126 return res.json(getFormattedObjects(resultList.data, resultList.total))
129 async function searchVideoURI (url: string, res: express.Response) {
130 let video: VideoModel
132 // Check if we can fetch a remote video with the URL
133 if (isUserAbleToSearchRemoteURI(res)) {
144 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
145 video = result ? result.video : undefined
147 logger.info('Cannot search remote video %s.', url, { err })
150 video = await VideoModel.loadByUrlAndPopulateAccount(url)
154 total: video ? 1 : 0,
155 data: video ? [ video.toFormattedJSON() ] : []