]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Add refresh video on search
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index 2ff340b597df0ef566035d83918be05f499679de..d95e7cac9dd8ef6a0d44a25b6dc99eb6074548f2 100644 (file)
@@ -1,9 +1,10 @@
 import * as express from 'express'
-import { isNSFWHidden } from '../../helpers/express-utils'
+import { buildNSFWFilter } from '../../helpers/express-utils'
 import { getFormattedObjects } from '../../helpers/utils'
 import { VideoModel } from '../../models/video/video'
 import {
   asyncMiddleware,
+  commonVideosFiltersValidator,
   optionalAuthenticate,
   paginationValidator,
   searchValidator,
@@ -11,6 +12,11 @@ import {
   setDefaultSearchSort,
   videosSearchSortValidator
 } from '../../middlewares'
+import { VideosSearchQuery } from '../../../shared/models/search'
+import { getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
+import { logger } from '../../helpers/logger'
+import { User } from '../../../shared/models/users'
+import { CONFIG } from '../../initializers/constants'
 
 const searchRouter = express.Router()
 
@@ -20,6 +26,7 @@ searchRouter.get('/videos',
   videosSearchSortValidator,
   setDefaultSearchSort,
   optionalAuthenticate,
+  commonVideosFiltersValidator,
   searchValidator,
   asyncMiddleware(searchVideos)
 )
@@ -30,14 +37,55 @@ export { searchRouter }
 
 // ---------------------------------------------------------------------------
 
-async function searchVideos (req: express.Request, res: express.Response) {
-  const resultList = await VideoModel.searchAndPopulateAccountAndServer(
-    req.query.search as string,
-    req.query.start as number,
-    req.query.count as number,
-    req.query.sort as string,
-    isNSFWHidden(res)
-  )
+function searchVideos (req: express.Request, res: express.Response) {
+  const query: VideosSearchQuery = req.query
+  if (query.search.startsWith('http://') || query.search.startsWith('https://')) {
+    return searchVideoUrl(query.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 searchVideoUrl (url: string, res: express.Response) {
+  let video: VideoModel
+  const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
+
+  // Check if we can fetch a remote video with the URL
+  if (
+    CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
+    (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
+  ) {
+    try {
+      const syncParam = {
+        likes: false,
+        dislikes: false,
+        shares: false,
+        comments: false,
+        thumbnail: true,
+        refreshVideo: false
+      }
+
+      const res = await getOrCreateVideoAndAccountAndChannel(url, syncParam)
+      video = res ? res.video : undefined
+    } catch (err) {
+      logger.info('Cannot search remote video %s.', url)
+    }
+  } else {
+    video = await VideoModel.loadByUrlAndPopulateAccount(url)
+  }
+
+  return res.json({
+    total: video ? 1 : 0,
+    data: video ? [ video.toFormattedJSON() ] : []
+  })
+}