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'
22 import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
24 const searchRouter = express.Router()
26 searchRouter.get('/videos',
29 videosSearchSortValidator,
32 commonVideosFiltersValidator,
33 videosSearchValidator,
34 asyncMiddleware(searchVideos)
37 searchRouter.get('/video-channels',
40 videoChannelsSearchSortValidator,
43 videoChannelsSearchValidator,
44 asyncMiddleware(searchVideoChannels)
47 // ---------------------------------------------------------------------------
49 export { searchRouter }
51 // ---------------------------------------------------------------------------
53 function searchVideoChannels (req: express.Request, res: express.Response) {
54 const query: VideoChannelsSearchQuery = req.query
55 const search = query.search
57 const isURISearch = search.startsWith('http://') || search.startsWith('https://')
59 const parts = search.split('@')
61 // Handle strings like @toto@example.com
62 if (parts.length === 3 && parts[0].length === 0) parts.shift()
63 const isWebfingerSearch = parts.length === 2 && parts.every(p => p && p.indexOf(' ') === -1)
65 if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
67 // @username -> username to search in DB
68 if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
69 return searchVideoChannelsDB(query, res)
72 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
73 const serverActor = await getServerActor()
76 actorId: serverActor.id,
82 const resultList = await VideoChannelModel.searchForApi(options)
84 return res.json(getFormattedObjects(resultList.data, resultList.total))
87 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
88 let videoChannel: MChannelAccountDefault
91 if (isWebfingerSearch) {
93 uri = await loadActorUrlOrGetFromWebfinger(search)
95 logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
97 return res.json({ total: 0, data: [] })
101 if (isUserAbleToSearchRemoteURI(res)) {
103 const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
104 videoChannel = actor.VideoChannel
106 logger.info('Cannot search remote video channel %s.', uri, { err })
109 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
113 total: videoChannel ? 1 : 0,
114 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
118 function searchVideos (req: express.Request, res: express.Response) {
119 const query: VideosSearchQuery = req.query
120 const search = query.search
121 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
122 return searchVideoURI(search, res)
125 return searchVideosDB(query, res)
128 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
129 const options = Object.assign(query, {
130 includeLocalVideos: true,
131 nsfw: buildNSFWFilter(res, query.nsfw),
132 filter: query.filter,
133 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
135 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
137 return res.json(getFormattedObjects(resultList.data, resultList.total))
140 async function searchVideoURI (url: string, res: express.Response) {
141 let video: MVideoAccountLightBlacklistAllFiles
143 // Check if we can fetch a remote video with the URL
144 if (isUserAbleToSearchRemoteURI(res)) {
155 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
156 video = result ? result.video : undefined
158 logger.info('Cannot search remote video %s.', url, { err })
161 video = await VideoModel.loadByUrlAndPopulateAccount(url)
165 total: video ? 1 : 0,
166 data: video ? [ video.toFormattedJSON() ] : []