]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Add joblog at the end of ci
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index e08e1d79fa15baea1ec64193c49e30e9e6d31cdc..7e1b7b230b9ec9381a9917e967de982350a53301 100644 (file)
@@ -6,6 +6,7 @@ import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/vi
 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'
@@ -22,13 +23,13 @@ import {
   setDefaultPagination,
   setDefaultSearchSort,
   videoChannelsSearchSortValidator,
-  videoChannelsSearchValidator,
+  videoChannelsListSearchValidator,
   videosSearchSortValidator,
   videosSearchValidator
 } from '../../middlewares'
 import { VideoModel } from '../../models/video/video'
 import { VideoChannelModel } from '../../models/video/video-channel'
-import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
+import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models'
 
 const searchRouter = express.Router()
 
@@ -49,7 +50,7 @@ searchRouter.get('/video-channels',
   videoChannelsSearchSortValidator,
   setDefaultSearchSort,
   optionalAuthenticate,
-  videoChannelsSearchValidator,
+  videoChannelsListSearchValidator,
   asyncMiddleware(searchVideoChannels)
 )
 
@@ -76,7 +77,7 @@ function searchVideoChannels (req: express.Request, res: express.Response) {
   // @username -> username to search in DB
   if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
 
-  if (isSearchIndexEnabled(query)) {
+  if (isSearchIndexSearch(query)) {
     return searchVideoChannelsIndex(query, res)
   }
 
@@ -84,8 +85,6 @@ function searchVideoChannels (req: express.Request, res: express.Response) {
 }
 
 async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
-  logger.debug('Doing channels search on search index.')
-
   const result = await buildMutedForSearchIndex(res)
 
   const body = Object.assign(query, result)
@@ -93,13 +92,15 @@ async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: e
   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 searchIndexResult = await doRequest<ResultList<VideoChannel>>({ uri: url, body, json: true })
 
     return res.json(searchIndexResult.body)
   } catch (err) {
     logger.warn('Cannot use search index to make video channels search.', { err })
 
-    return res.sendStatus(500)
+    return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
   }
 }
 
@@ -157,7 +158,7 @@ function searchVideos (req: express.Request, res: express.Response) {
     return searchVideoURI(search, res)
   }
 
-  if (isSearchIndexEnabled(query)) {
+  if (isSearchIndexSearch(query)) {
     return searchVideosIndex(query, res)
   }
 
@@ -165,22 +166,33 @@ function searchVideos (req: express.Request, res: express.Response) {
 }
 
 async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
-  logger.debug('Doing videos search on search index.')
-
   const result = await buildMutedForSearchIndex(res)
 
-  const body = Object.assign(query, result)
+  const 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'
+  }
 
   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 searchIndexResult = await doRequest<ResultList<Video>>({ uri: url, body, json: true })
 
     return res.json(searchIndexResult.body)
   } catch (err) {
     logger.warn('Cannot use search index to make video search.', { err })
 
-    return res.sendStatus(500)
+    return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
   }
 }
 
@@ -226,7 +238,7 @@ async function searchVideoURI (url: string, res: express.Response) {
   })
 }
 
-function isSearchIndexEnabled (query: SearchTargetQuery) {
+function isSearchIndexSearch (query: SearchTargetQuery) {
   if (query.searchTarget === 'search-index') return true
 
   const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX