]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/server/follows.ts
Remove traefik docker support
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / follows.ts
index 207a09a4cdfda1d5bb39ee42109af9ca3dc38932..80025bc5ba24decf6828cb9f8bbea193ee85f31e 100644 (file)
@@ -1,8 +1,8 @@
 import * as express from 'express'
 import { UserRight } from '../../../../shared/models/users'
 import { logger } from '../../../helpers/logger'
-import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
-import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers'
+import { getFormattedObjects } from '../../../helpers/utils'
+import { SERVER_ACTOR_NAME } from '../../../initializers/constants'
 import { sendAccept, sendReject, sendUndoFollow } from '../../../lib/activitypub/send'
 import {
   asyncMiddleware,
@@ -19,14 +19,20 @@ import {
   followingSortValidator,
   followValidator,
   getFollowerValidator,
-  removeFollowingValidator
+  removeFollowingValidator,
+  listFollowsValidator
 } from '../../../middlewares/validators'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { JobQueue } from '../../../lib/job-queue'
-import { removeRedundancyOf } from '../../../lib/redundancy'
+import { removeRedundanciesOfServer } from '../../../lib/redundancy'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { autoFollowBackIfNeeded } from '../../../lib/activitypub/follow'
+import { getServerActor } from '@server/models/application/application'
+import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
 
 const serverFollowsRouter = express.Router()
 serverFollowsRouter.get('/following',
+  listFollowsValidator,
   paginationValidator,
   followingSortValidator,
   setDefaultSort,
@@ -50,6 +56,7 @@ serverFollowsRouter.delete('/following/:host',
 )
 
 serverFollowsRouter.get('/followers',
+  listFollowsValidator,
   paginationValidator,
   followersSortValidator,
   setDefaultSort,
@@ -90,26 +97,30 @@ export {
 
 async function listFollowing (req: express.Request, res: express.Response) {
   const serverActor = await getServerActor()
-  const resultList = await ActorFollowModel.listFollowingForApi(
-    serverActor.id,
-    req.query.start,
-    req.query.count,
-    req.query.sort,
-    req.query.search
-  )
+  const resultList = await ActorFollowModel.listFollowingForApi({
+    id: serverActor.id,
+    start: req.query.start,
+    count: req.query.count,
+    sort: req.query.sort,
+    search: req.query.search,
+    actorType: req.query.actorType,
+    state: req.query.state
+  })
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
 async function listFollowers (req: express.Request, res: express.Response) {
   const serverActor = await getServerActor()
-  const resultList = await ActorFollowModel.listFollowersForApi(
-    serverActor.id,
-    req.query.start,
-    req.query.count,
-    req.query.sort,
-    req.query.search
-  )
+  const resultList = await ActorFollowModel.listFollowersForApi({
+    actorId: serverActor.id,
+    start: req.query.start,
+    count: req.query.count,
+    sort: req.query.sort,
+    search: req.query.search,
+    actorType: req.query.actorType,
+    state: req.query.state
+  })
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
@@ -126,10 +137,9 @@ async function followInstance (req: express.Request, res: express.Response) {
     }
 
     JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
-      .catch(err => logger.error('Cannot create follow job for %s.', host, err))
   }
 
-  return res.status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function removeFollowing (req: express.Request, res: express.Response) {
@@ -144,23 +154,23 @@ async function removeFollowing (req: express.Request, res: express.Response) {
     await server.save({ transaction: t })
 
     // Async, could be long
-    removeRedundancyOf(server.id)
+    removeRedundanciesOfServer(server.id)
       .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))
 
     await follow.destroy({ transaction: t })
   })
 
-  return res.status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function removeOrRejectFollower (req: express.Request, res: express.Response) {
   const follow = res.locals.follow
 
-  await sendReject(follow.ActorFollower, follow.ActorFollowing)
+  await sendReject(follow.url, follow.ActorFollower, follow.ActorFollowing)
 
   await follow.destroy()
 
-  return res.status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function acceptFollower (req: express.Request, res: express.Response) {
@@ -171,5 +181,7 @@ async function acceptFollower (req: express.Request, res: express.Response) {
   follow.state = 'accepted'
   await follow.save()
 
-  return res.status(204).end()
+  await autoFollowBackIfNeeded(follow)
+
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }