]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Optimize videos feed endpoint
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index 7fef7c1732709bc3eef8fce8058fe2464a1c3cfe..f0cdf3a8904205c9215f60131d8dee4854c966e7 100644 (file)
@@ -1,7 +1,21 @@
 import * as express from 'express'
+import { sanitizeUrl } from '@server/helpers/core-utils'
+import { doJSONRequest } from '@server/helpers/requests'
+import { CONFIG } from '@server/initializers/config'
+import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos'
+import { Hooks } from '@server/lib/plugins/hooks'
+import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
+import { getServerActor } from '@server/models/application/application'
+import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
+import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
+import { ResultList, Video, VideoChannel } from '@shared/models'
+import { SearchTargetQuery } from '@shared/models/search/search-target-query.model'
+import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
-import { getFormattedObjects, getServerActor } from '../../helpers/utils'
-import { VideoModel } from '../../models/video/video'
+import { logger } from '../../helpers/logger'
+import { getFormattedObjects } from '../../helpers/utils'
+import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
+import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
 import {
   asyncMiddleware,
   commonVideosFiltersValidator,
@@ -9,17 +23,14 @@ import {
   paginationValidator,
   setDefaultPagination,
   setDefaultSearchSort,
+  videoChannelsListSearchValidator,
   videoChannelsSearchSortValidator,
-  videoChannelsSearchValidator,
   videosSearchSortValidator,
   videosSearchValidator
 } from '../../middlewares'
-import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
-import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
-import { logger } from '../../helpers/logger'
+import { VideoModel } from '../../models/video/video'
 import { VideoChannelModel } from '../../models/video/video-channel'
-import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
-import { MChannelAccountDefault, MVideoAccountAllFiles } from '../../typings/models'
+import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models'
 
 const searchRouter = express.Router()
 
@@ -40,7 +51,7 @@ searchRouter.get('/video-channels',
   videoChannelsSearchSortValidator,
   setDefaultSearchSort,
   optionalAuthenticate,
-  videoChannelsSearchValidator,
+  videoChannelsListSearchValidator,
   asyncMiddleware(searchVideoChannels)
 )
 
@@ -60,26 +71,57 @@ function searchVideoChannels (req: express.Request, res: express.Response) {
 
   // Handle strings like @toto@example.com
   if (parts.length === 3 && parts[0].length === 0) parts.shift()
-  const isWebfingerSearch = parts.length === 2 && parts.every(p => p && p.indexOf(' ') === -1)
+  const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
 
   if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
 
   // @username -> username to search in DB
   if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
+
+  if (isSearchIndexSearch(query)) {
+    return searchVideoChannelsIndex(query, res)
+  }
+
   return searchVideoChannelsDB(query, res)
 }
 
+async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
+  const result = await buildMutedForSearchIndex(res)
+
+  const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params')
+
+  const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
+
+  try {
+    logger.debug('Doing video channels search index request on %s.', url, { body })
+
+    const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body })
+    const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result')
+
+    return res.json(jsonResult)
+  } catch (err) {
+    logger.warn('Cannot use search index to make video channels search.', { err })
+
+    return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
+  }
+}
+
 async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
   const serverActor = await getServerActor()
 
-  const options = {
+  const apiOptions = await Hooks.wrapObject({
     actorId: serverActor.id,
     search: query.search,
     start: query.start,
     count: query.count,
     sort: query.sort
-  }
-  const resultList = await VideoChannelModel.searchForApi(options)
+  }, 'filter:api.search.video-channels.local.list.params')
+
+  const resultList = await Hooks.wrapPromiseFun(
+    VideoChannelModel.searchForApi,
+    apiOptions,
+    'filter:api.search.video-channels.local.list.result'
+  )
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
@@ -118,27 +160,71 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean
 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)
   }
 
+  if (isSearchIndexSearch(query)) {
+    return searchVideosIndex(query, res)
+  }
+
   return searchVideosDB(query, res)
 }
 
+async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
+  const result = await buildMutedForSearchIndex(res)
+
+  let body: VideosSearchQuery = Object.assign(query, result)
+
+  // Use the default instance NSFW policy if not specified
+  if (!body.nsfw) {
+    const nsfwPolicy = res.locals.oauth
+      ? res.locals.oauth.token.User.nsfwPolicy
+      : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
+
+    body.nsfw = nsfwPolicy === 'do_not_list'
+      ? 'false'
+      : 'both'
+  }
+
+  body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
+
+  const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
+
+  try {
+    logger.debug('Doing videos search index request on %s.', url, { body })
+
+    const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
+    const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
+
+    return res.json(jsonResult)
+  } catch (err) {
+    logger.warn('Cannot use search index to make video search.', { err })
+
+    return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
+  }
+}
+
 async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
-  const options = Object.assign(query, {
+  const apiOptions = await Hooks.wrapObject(Object.assign(query, {
     includeLocalVideos: true,
     nsfw: buildNSFWFilter(res, query.nsfw),
     filter: query.filter,
     user: res.locals.oauth ? res.locals.oauth.token.User : undefined
-  })
-  const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
+  }), 'filter:api.search.videos.local.list.params')
+
+  const resultList = await Hooks.wrapPromiseFun(
+    VideoModel.searchAndPopulateAccountAndServer,
+    apiOptions,
+    'filter:api.search.videos.local.list.result'
+  )
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
 async function searchVideoURI (url: string, res: express.Response) {
-  let video: MVideoAccountAllFiles
+  let video: MVideoAccountLightBlacklistAllFiles
 
   // Check if we can fetch a remote video with the URL
   if (isUserAbleToSearchRemoteURI(res)) {
@@ -166,3 +252,35 @@ async function searchVideoURI (url: string, res: express.Response) {
     data: video ? [ video.toFormattedJSON() ] : []
   })
 }
+
+function isSearchIndexSearch (query: SearchTargetQuery) {
+  if (query.searchTarget === 'search-index') return true
+
+  const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
+
+  if (searchIndexConfig.ENABLED !== true) return false
+
+  if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
+  if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
+
+  return false
+}
+
+async function buildMutedForSearchIndex (res: express.Response) {
+  const serverActor = await getServerActor()
+  const accountIds = [ serverActor.Account.id ]
+
+  if (res.locals.oauth) {
+    accountIds.push(res.locals.oauth.token.User.Account.id)
+  }
+
+  const [ blockedHosts, blockedAccounts ] = await Promise.all([
+    ServerBlocklistModel.listHostsBlockedBy(accountIds),
+    AccountBlocklistModel.listHandlesBlockedBy(accountIds)
+  ])
+
+  return {
+    blockedHosts,
+    blockedAccounts
+  }
+}