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 { User } from '../../../shared/models/users'
21 import { CONFIG } from '../../initializers/constants'
22 import { VideoChannelModel } from '../../models/video/video-channel'
23 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
25 const searchRouter = express.Router()
27 searchRouter.get('/videos',
30 videosSearchSortValidator,
33 commonVideosFiltersValidator,
34 videosSearchValidator,
35 asyncMiddleware(searchVideos)
38 searchRouter.get('/video-channels',
41 videoChannelsSearchSortValidator,
44 videoChannelsSearchValidator,
45 asyncMiddleware(searchVideoChannels)
48 // ---------------------------------------------------------------------------
50 export { searchRouter }
52 // ---------------------------------------------------------------------------
54 function searchVideoChannels (req: express.Request, res: express.Response) {
55 const query: VideoChannelsSearchQuery = req.query
56 const search = query.search
58 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
60 const parts = search.split('@')
61 const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
63 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
65 return searchVideoChannelsDB(query, res)
68 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
69 const serverActor = await getServerActor()
72 actorId: serverActor.id,
78 const resultList = await VideoChannelModel.searchForApi(options)
80 return res.json(getFormattedObjects(resultList.data, resultList.total))
83 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
84 let videoChannel: VideoChannelModel
87 if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
89 if (isUserAbleToSearchRemoteURI(res)) {
91 const actor = await getOrCreateActorAndServerAndModel(uri, true, true)
92 videoChannel = actor.VideoChannel
94 logger.info('Cannot search remote video channel %s.', uri, { err })
97 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
101 total: videoChannel ? 1 : 0,
102 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
106 function searchVideos (req: express.Request, res: express.Response) {
107 const query: VideosSearchQuery = req.query
108 const search = query.search
109 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
110 return searchVideoURI(search, res)
113 return searchVideosDB(query, res)
116 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
117 const options = Object.assign(query, {
118 includeLocalVideos: true,
119 nsfw: buildNSFWFilter(res, query.nsfw)
121 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
123 return res.json(getFormattedObjects(resultList.data, resultList.total))
126 async function searchVideoURI (url: string, res: express.Response) {
127 let video: VideoModel
129 // Check if we can fetch a remote video with the URL
130 if (isUserAbleToSearchRemoteURI(res)) {
141 const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
142 video = result ? result.video : undefined
144 logger.info('Cannot search remote video %s.', url, { err })
147 video = await VideoModel.loadByUrlAndPopulateAccount(url)
151 total: video ? 1 : 0,
152 data: video ? [ video.toFormattedJSON() ] : []