]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Support logout and add id and pass tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index 4be2b5ef7898a14ead15677888a756351429153f..35d94d747e4a2c3164cb2e558b50f0d2077fe6e7 100644 (file)
@@ -1,6 +1,6 @@
 import * as express from 'express'
 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
-import { getFormattedObjects, getServerActor } from '../../helpers/utils'
+import { getFormattedObjects } from '../../helpers/utils'
 import { VideoModel } from '../../models/video/video'
 import {
   asyncMiddleware,
@@ -15,10 +15,13 @@ import {
   videosSearchValidator
 } from '../../middlewares'
 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
-import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
+import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub/actor'
 import { logger } from '../../helpers/logger'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
+import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
+import { getServerActor } from '@server/models/application/application'
+import { getOrCreateVideoAndAccountAndChannel } from '@server/lib/activitypub/videos'
 
 const searchRouter = express.Router()
 
@@ -59,10 +62,12 @@ 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.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(/^@/, '')
   return searchVideoChannelsDB(query, res)
 }
 
@@ -82,10 +87,18 @@ async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: expr
 }
 
 async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
-  let videoChannel: VideoChannelModel
+  let videoChannel: MChannelAccountDefault
   let uri = search
 
-  if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
+  if (isWebfingerSearch) {
+    try {
+      uri = await loadActorUrlOrGetFromWebfinger(search)
+    } catch (err) {
+      logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
+
+      return res.json({ total: 0, data: [] })
+    }
+  }
 
   if (isUserAbleToSearchRemoteURI(res)) {
     try {
@@ -118,7 +131,8 @@ async function searchVideosDB (query: VideosSearchQuery, res: express.Response)
   const options = Object.assign(query, {
     includeLocalVideos: true,
     nsfw: buildNSFWFilter(res, query.nsfw),
-    userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined
+    filter: query.filter,
+    user: res.locals.oauth ? res.locals.oauth.token.User : undefined
   })
   const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
 
@@ -126,7 +140,7 @@ async function searchVideosDB (query: VideosSearchQuery, res: express.Response)
 }
 
 async function searchVideoURI (url: string, res: express.Response) {
-  let video: VideoModel
+  let video: MVideoAccountLightBlacklistAllFiles
 
   // Check if we can fetch a remote video with the URL
   if (isUserAbleToSearchRemoteURI(res)) {