]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/users/me.ts
Add ability to search video channels
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / me.ts
index 2300f5dbe301003290bf64d490d5bb3c83bb9e54..000c706b5c10a592a130df386e8e7a865d60e525 100644 (file)
@@ -20,7 +20,8 @@ import {
   deleteMeValidator,
   userSubscriptionsSortValidator,
   videoImportsSortValidator,
-  videosSortValidator
+  videosSortValidator,
+  areSubscriptionsExistValidator
 } from '../../../middlewares/validators'
 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
 import { UserModel } from '../../../models/account/user'
@@ -98,7 +99,6 @@ meRouter.post('/me/avatar/pick',
 // ##### Subscriptions part #####
 
 meRouter.get('/me/subscriptions/videos',
-  authenticate,
   authenticate,
   paginationValidator,
   videosSortValidator,
@@ -108,6 +108,12 @@ meRouter.get('/me/subscriptions/videos',
   asyncMiddleware(getUserSubscriptionVideos)
 )
 
+meRouter.get('/me/subscriptions/exist',
+  authenticate,
+  areSubscriptionsExistValidator,
+  asyncMiddleware(areSubscriptionsExist)
+)
+
 meRouter.get('/me/subscriptions',
   authenticate,
   paginationValidator,
@@ -143,6 +149,37 @@ export {
 
 // ---------------------------------------------------------------------------
 
+async function areSubscriptionsExist (req: express.Request, res: express.Response) {
+  const uris = req.query.uris as string[]
+  const user = res.locals.oauth.token.User as UserModel
+
+  const handles = uris.map(u => {
+    let [ name, host ] = u.split('@')
+    if (host === CONFIG.WEBSERVER.HOST) host = null
+
+    return { name, host, uri: u }
+  })
+
+  const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles)
+
+  const existObject: { [id: string ]: boolean } = {}
+  for (const handle of handles) {
+    const obj = results.find(r => {
+      const server = r.ActorFollowing.Server
+
+      return r.ActorFollowing.preferredUsername === handle.name &&
+        (
+          (!server && !handle.host) ||
+          (server.host === handle.host)
+        )
+    })
+
+    existObject[handle.uri] = obj !== undefined
+  }
+
+  return res.json(existObject)
+}
+
 async function addUserSubscription (req: express.Request, res: express.Response) {
   const user = res.locals.oauth.token.User as UserModel
   const [ name, host ] = req.body.uri.split('@')