]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/server/follows.ts
Refractor activity pub lib/helpers
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / follows.ts
index ac8ea87f93cbf1947bf287171eb9eccf218b11b9..8fc70f34f793de64eee0266f8c2a0ccd5ee32f7f 100644 (file)
@@ -2,22 +2,24 @@ import * as express from 'express'
 import { UserRight } from '../../../../shared/models/users/user-right.enum'
 import { getFormattedObjects } from '../../../helpers'
 import { logger } from '../../../helpers/logger'
-import { getApplicationAccount } from '../../../helpers/utils'
+import { getServerAccount } from '../../../helpers/utils'
 import { getAccountFromWebfinger } from '../../../helpers/webfinger'
 import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants'
 import { database as db } from '../../../initializers/database'
-import { sendFollow } from '../../../lib/activitypub/send-request'
-import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../../middlewares'
+import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares'
 import { authenticate } from '../../../middlewares/oauth'
 import { setBodyHostsPort } from '../../../middlewares/servers'
 import { setFollowingSort } from '../../../middlewares/sort'
 import { ensureUserHasRight } from '../../../middlewares/user-right'
-import { followValidator } from '../../../middlewares/validators/servers'
+import { followValidator } from '../../../middlewares/validators/follows'
 import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort'
+import { AccountFollowInstance } from '../../../models/index'
+import { sendFollow } from '../../../lib/index'
+import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo'
 
-const applicationFollowsRouter = express.Router()
+const serverFollowsRouter = express.Router()
 
-applicationFollowsRouter.get('/following',
+serverFollowsRouter.get('/following',
   paginationValidator,
   followingSortValidator,
   setFollowingSort,
@@ -25,15 +27,22 @@ applicationFollowsRouter.get('/following',
   asyncMiddleware(listFollowing)
 )
 
-applicationFollowsRouter.post('/follow',
+serverFollowsRouter.post('/following',
   authenticate,
-  ensureUserHasRight(UserRight.MANAGE_APPLICATION_FOLLOW),
+  ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
   followValidator,
   setBodyHostsPort,
   asyncMiddleware(follow)
 )
 
-applicationFollowsRouter.get('/followers',
+serverFollowsRouter.delete('/following/:accountId',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
+  removeFollowingValidator,
+  asyncMiddleware(removeFollow)
+)
+
+serverFollowsRouter.get('/followers',
   paginationValidator,
   followersSortValidator,
   setFollowersSort,
@@ -44,28 +53,28 @@ applicationFollowsRouter.get('/followers',
 // ---------------------------------------------------------------------------
 
 export {
-  applicationFollowsRouter
+  serverFollowsRouter
 }
 
 // ---------------------------------------------------------------------------
 
 async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const applicationAccount = await getApplicationAccount()
-  const resultList = await db.AccountFollow.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
+  const serverAccount = await getServerAccount()
+  const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
 async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const applicationAccount = await getApplicationAccount()
-  const resultList = await db.AccountFollow.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
+  const serverAccount = await getServerAccount()
+  const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
 async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
   const hosts = req.body.hosts as string[]
-  const fromAccount = await getApplicationAccount()
+  const fromAccount = await getServerAccount()
 
   const tasks: Promise<any>[] = []
   const accountName = SERVER_ACCOUNT_NAME
@@ -96,10 +105,12 @@ async function follow (req: express.Request, res: express.Response, next: expres
             },
             transaction: t
           })
+          accountFollow.AccountFollowing = targetAccount
+          accountFollow.AccountFollower = fromAccount
 
           // Send a notification to remote server
           if (accountFollow.state === 'pending') {
-            await sendFollow(fromAccount, targetAccount, t)
+            await sendFollow(accountFollow, t)
           }
         })
       })
@@ -117,6 +128,17 @@ async function follow (req: express.Request, res: express.Response, next: expres
   return res.status(204).end()
 }
 
+async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const following: AccountFollowInstance = res.locals.following
+
+  await db.sequelize.transaction(async t => {
+    await sendUndoFollow(following, t)
+    await following.destroy({ transaction: t })
+  })
+
+  return res.status(204).end()
+}
+
 async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
   let loadedFromDB = true
   let account = await db.Account.loadByNameAndHost(name, host)