]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search/search-videos.ts
Also fill avatar width for channels
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search / search-videos.ts
index 0e6fd9c721b1923ddac2308017c70d553e191f9e..68428d7660a547ff88e4ee6e02bc7bb82206ae2a 100644 (file)
@@ -1,14 +1,16 @@
-import * as express from 'express'
+import express from 'express'
 import { sanitizeUrl } from '@server/helpers/core-utils'
-import { doJSONRequest } from '@server/helpers/requests'
+import { pickSearchVideoQuery } from '@server/helpers/query'
+import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests'
 import { CONFIG } from '@server/initializers/config'
 import { WEBSERVER } from '@server/initializers/constants'
 import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
 import { Hooks } from '@server/lib/plugins/hooks'
 import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search'
-import { HttpStatusCode } from '@shared/models'
-import { ResultList, Video } from '@shared/models'
-import { VideosSearchQuery } from '../../../../shared/models/search'
+import { getServerActor } from '@server/models/application/application'
+import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
+import { HttpStatusCode, ResultList, Video } from '@shared/models'
+import { VideosSearchQueryAfterSanitize } from '../../../../shared/models/search'
 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils'
 import { logger } from '../../../helpers/logger'
 import { getFormattedObjects } from '../../../helpers/utils'
@@ -25,6 +27,7 @@ import {
 } from '../../../middlewares'
 import { VideoModel } from '../../../models/video/video'
 import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
+import { searchLocalUrl } from './shared'
 
 const searchVideosRouter = express.Router()
 
@@ -47,7 +50,7 @@ export { searchVideosRouter }
 // ---------------------------------------------------------------------------
 
 function searchVideos (req: express.Request, res: express.Response) {
-  const query: VideosSearchQuery = req.query
+  const query = pickSearchVideoQuery(req.query)
   const search = query.search
 
   if (isURISearch(search)) {
@@ -61,10 +64,10 @@ function searchVideos (req: express.Request, res: express.Response) {
   return searchVideosDB(query, res)
 }
 
-async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
+async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) {
   const result = await buildMutedForSearchIndex(res)
 
-  let body: VideosSearchQuery = Object.assign(query, result)
+  let body = { ...query, ...result }
 
   // Use the default instance NSFW policy if not specified
   if (!body.nsfw) {
@@ -98,13 +101,22 @@ async function searchVideosIndex (query: VideosSearchQuery, res: express.Respons
   }
 }
 
-async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
-  const apiOptions = await Hooks.wrapObject(Object.assign(query, {
-    includeLocalVideos: true,
+async function searchVideosDB (query: VideosSearchQueryAfterSanitize, res: express.Response) {
+  const serverActor = await getServerActor()
+
+  const apiOptions = await Hooks.wrapObject({
+    ...query,
+
+    displayOnlyForFollower: {
+      actorId: serverActor.id,
+      orLocalVideos: true
+    },
+
     nsfw: buildNSFWFilter(res, query.nsfw),
-    filter: query.filter,
-    user: res.locals.oauth ? res.locals.oauth.token.User : undefined
-  }), 'filter:api.search.videos.local.list.params')
+    user: res.locals.oauth
+      ? res.locals.oauth.token.User
+      : undefined
+  }, 'filter:api.search.videos.local.list.params')
 
   const resultList = await Hooks.wrapPromiseFun(
     VideoModel.searchAndPopulateAccountAndServer,
@@ -112,7 +124,7 @@ async function searchVideosDB (query: VideosSearchQuery, res: express.Response)
     'filter:api.search.videos.local.list.result'
   )
 
-  return res.json(getFormattedObjects(resultList.data, resultList.total))
+  return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
 }
 
 async function searchVideoURI (url: string, res: express.Response) {
@@ -130,13 +142,16 @@ async function searchVideoURI (url: string, res: express.Response) {
         refreshVideo: false
       }
 
-      const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
+      const result = await getOrCreateAPVideo({
+        videoObject: await findLatestRedirection(url, { activityPub: true }),
+        syncParam
+      })
       video = result ? result.video : undefined
     } catch (err) {
       logger.info('Cannot search remote video %s.', url, { err })
     }
   } else {
-    video = await VideoModel.loadByUrlAndPopulateAccount(sanitizeLocalUrl(url))
+    video = await searchLocalUrl(sanitizeLocalUrl(url), url => VideoModel.loadByUrlAndPopulateAccount(url))
   }
 
   return res.json({