1 import * as express from 'express'
2 import { buildNSFWFilter } 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 commonVideosFiltersValidator,
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('@')
62 const isHandleSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
64 if (isURISearch || isHandleSearch) return searchVideoChannelURI(search, isHandleSearch, 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, isHandleSearch: boolean, res: express.Response) {
85 let videoChannel: VideoChannelModel
87 if (isUserAbleToSearchRemoteURI(res)) {
89 if (isHandleSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
91 const actor = await getOrCreateActorAndServerAndModel(uri)
92 videoChannel = actor.VideoChannel
94 videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(search)
98 total: videoChannel ? 1 : 0,
99 data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
103 function searchVideos (req: express.Request, res: express.Response) {
104 const query: VideosSearchQuery = req.query
105 const search = query.search
106 if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
107 return searchVideoURI(search, res)
110 return searchVideosDB(query, res)
113 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
114 const options = Object.assign(query, {
115 includeLocalVideos: true,
116 nsfw: buildNSFWFilter(res, query.nsfw)
118 const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
120 return res.json(getFormattedObjects(resultList.data, resultList.total))
123 async function searchVideoURI (url: string, res: express.Response) {
124 let video: VideoModel
126 // Check if we can fetch a remote video with the URL
127 if (isUserAbleToSearchRemoteURI(res)) {
138 const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
139 video = result ? result.video : undefined
141 logger.info('Cannot search remote video %s.', url)
144 video = await VideoModel.loadByUrlAndPopulateAccount(url)
148 total: video ? 1 : 0,
149 data: video ? [ video.toFormattedJSON() ] : []
153 function isUserAbleToSearchRemoteURI (res: express.Response) {
154 const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
156 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
157 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)