]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Update OpenAPI documentation to include basic playlists and new comment sorting
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index 534305ba633b9eb68c457e1640dc135b29362c63..349650aca65e290c53b33794cb895a550dd60da3 100644 (file)
@@ -19,6 +19,7 @@ import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel
 import { logger } from '../../helpers/logger'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
+import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../typings/models'
 
 const searchRouter = express.Router()
 
@@ -59,10 +60,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.indexOf(' ') === -1)
 
   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 +85,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 {
@@ -127,7 +138,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)) {