]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/search.ts
Add ability for users to block an account/instance on server side
[github/Chocobozzz/PeerTube.git] / server / controllers / api / search.ts
index 7a7504b7d5c0e58987eeee64d7bd6d54cd2c26bf..534305ba633b9eb68c457e1640dc135b29362c63 100644 (file)
@@ -1,18 +1,24 @@
 import * as express from 'express'
-import { buildNSFWFilter } from '../../helpers/express-utils'
-import { getFormattedObjects } from '../../helpers/utils'
+import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
+import { getFormattedObjects, getServerActor } from '../../helpers/utils'
 import { VideoModel } from '../../models/video/video'
 import {
   asyncMiddleware,
   commonVideosFiltersValidator,
   optionalAuthenticate,
   paginationValidator,
-  searchValidator,
   setDefaultPagination,
   setDefaultSearchSort,
-  videosSearchSortValidator
+  videoChannelsSearchSortValidator,
+  videoChannelsSearchValidator,
+  videosSearchSortValidator,
+  videosSearchValidator
 } from '../../middlewares'
-import { VideosSearchQuery } from '../../../shared/models/search'
+import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
+import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
+import { logger } from '../../helpers/logger'
+import { VideoChannelModel } from '../../models/video/video-channel'
+import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
 
 const searchRouter = express.Router()
 
@@ -23,24 +29,129 @@ searchRouter.get('/videos',
   setDefaultSearchSort,
   optionalAuthenticate,
   commonVideosFiltersValidator,
-  searchValidator,
+  videosSearchValidator,
   asyncMiddleware(searchVideos)
 )
 
+searchRouter.get('/video-channels',
+  paginationValidator,
+  setDefaultPagination,
+  videoChannelsSearchSortValidator,
+  setDefaultSearchSort,
+  optionalAuthenticate,
+  videoChannelsSearchValidator,
+  asyncMiddleware(searchVideoChannels)
+)
+
 // ---------------------------------------------------------------------------
 
 export { searchRouter }
 
 // ---------------------------------------------------------------------------
 
-async function searchVideos (req: express.Request, res: express.Response) {
+function searchVideoChannels (req: express.Request, res: express.Response) {
+  const query: VideoChannelsSearchQuery = req.query
+  const search = query.search
+
+  const isURISearch = search.startsWith('http://') || search.startsWith('https://')
+
+  const parts = search.split('@')
+
+  // 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)
+
+  if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
+
+  return searchVideoChannelsDB(query, res)
+}
+
+async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
+  const serverActor = await getServerActor()
+
+  const options = {
+    actorId: serverActor.id,
+    search: query.search,
+    start: query.start,
+    count: query.count,
+    sort: query.sort
+  }
+  const resultList = await VideoChannelModel.searchForApi(options)
+
+  return res.json(getFormattedObjects(resultList.data, resultList.total))
+}
+
+async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
+  let videoChannel: VideoChannelModel
+  let uri = search
+
+  if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
+
+  if (isUserAbleToSearchRemoteURI(res)) {
+    try {
+      const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
+      videoChannel = actor.VideoChannel
+    } catch (err) {
+      logger.info('Cannot search remote video channel %s.', uri, { err })
+    }
+  } else {
+    videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
+  }
+
+  return res.json({
+    total: videoChannel ? 1 : 0,
+    data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
+  })
+}
+
+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)
+  }
 
+  return searchVideosDB(query, res)
+}
+
+async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
   const options = Object.assign(query, {
     includeLocalVideos: true,
-    nsfw: buildNSFWFilter(res, query.nsfw)
+    nsfw: buildNSFWFilter(res, query.nsfw),
+    filter: query.filter,
+    user: res.locals.oauth ? res.locals.oauth.token.User : undefined
   })
   const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
+
+async function searchVideoURI (url: string, res: express.Response) {
+  let video: VideoModel
+
+  // Check if we can fetch a remote video with the URL
+  if (isUserAbleToSearchRemoteURI(res)) {
+    try {
+      const syncParam = {
+        likes: false,
+        dislikes: false,
+        shares: false,
+        comments: false,
+        thumbnail: true,
+        refreshVideo: false
+      }
+
+      const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
+      video = result ? result.video : undefined
+    } catch (err) {
+      logger.info('Cannot search remote video %s.', url, { err })
+    }
+  } else {
+    video = await VideoModel.loadByUrlAndPopulateAccount(url)
+  }
+
+  return res.json({
+    total: video ? 1 : 0,
+    data: video ? [ video.toFormattedJSON() ] : []
+  })
+}