+
+async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
+ let videoChannel: VideoChannelModel
+ let uri = search
+
+ if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
+
+ if (isUserAbleToSearchRemoteURI(res)) {
+ try {
+ const actor = await getOrCreateActorAndServerAndModel(uri, true, true)
+ videoChannel = actor.VideoChannel
+ } catch (err) {
+ logger.info('Cannot search remote video channel %s.', uri, { err })
+ }
+ } else {
+ videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
+ }
+
+ return res.json({
+ total: videoChannel ? 1 : 0,
+ data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
+ })
+}
+
+function searchVideos (req: express.Request, res: express.Response) {
+ const query: VideosSearchQuery = req.query
+ const search = query.search
+ if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
+ return searchVideoURI(search, res)
+ }
+
+ return searchVideosDB(query, res)
+}
+
+async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
+ const options = Object.assign(query, {
+ includeLocalVideos: true,
+ nsfw: buildNSFWFilter(res, query.nsfw)
+ })
+ const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
+
+ return res.json(getFormattedObjects(resultList.data, resultList.total))
+}
+
+async function searchVideoURI (url: string, res: express.Response) {
+ let video: VideoModel
+
+ // Check if we can fetch a remote video with the URL
+ if (isUserAbleToSearchRemoteURI(res)) {
+ try {
+ const syncParam = {
+ likes: false,
+ dislikes: false,
+ shares: false,
+ comments: false,
+ thumbnail: true,
+ refreshVideo: false
+ }
+
+ const result = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
+ video = result ? result.video : undefined
+ } catch (err) {
+ logger.info('Cannot search remote video %s.', url, { err })
+ }
+ } else {
+ video = await VideoModel.loadByUrlAndPopulateAccount(url)
+ }
+
+ return res.json({
+ total: video ? 1 : 0,
+ data: video ? [ video.toFormattedJSON() ] : []
+ })
+}