]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/server/follows.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / follows.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
3fd3ab2d 2import { UserRight } from '../../../../shared/models/users'
da854ddd
C
3import { logger } from '../../../helpers/logger'
4import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
74dc3bca 5import { SERVER_ACTOR_NAME } from '../../../initializers/constants'
14893eb7 6import { sendAccept, sendReject, sendUndoFollow } from '../../../lib/activitypub/send'
3fd3ab2d 7import {
5350fd8e
C
8 asyncMiddleware,
9 authenticate,
10 ensureUserHasRight,
11 paginationValidator,
5350fd8e
C
12 setBodyHostsPort,
13 setDefaultPagination,
14 setDefaultSort
3fd3ab2d 15} from '../../../middlewares'
0e9c48c2 16import {
14893eb7 17 acceptOrRejectFollowerValidator,
0e9c48c2
C
18 followersSortValidator,
19 followingSortValidator,
20 followValidator,
14893eb7 21 getFollowerValidator,
b8f4167f
C
22 removeFollowingValidator,
23 listFollowsValidator
0e9c48c2 24} from '../../../middlewares/validators'
50d6de9c 25import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
5350fd8e 26import { JobQueue } from '../../../lib/job-queue'
b764380a 27import { removeRedundanciesOfServer } from '../../../lib/redundancy'
74dc3bca 28import { sequelizeTypescript } from '../../../initializers/database'
8424c402 29import { autoFollowBackIfNeeded } from '../../../lib/activitypub/follow'
51548b31 30
4610bc5b 31const serverFollowsRouter = express.Router()
4610bc5b 32serverFollowsRouter.get('/following',
b8f4167f 33 listFollowsValidator,
8a02bd04 34 paginationValidator,
7a7724e6 35 followingSortValidator,
1174a847 36 setDefaultSort,
f05a1c30 37 setDefaultPagination,
7a7724e6
C
38 asyncMiddleware(listFollowing)
39)
40
9a27cdc2 41serverFollowsRouter.post('/following',
8e696487 42 authenticate,
4610bc5b 43 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
ce548a10
C
44 followValidator,
45 setBodyHostsPort,
5350fd8e 46 asyncMiddleware(followInstance)
ce548a10
C
47)
48
50d6de9c 49serverFollowsRouter.delete('/following/:host',
54141398
C
50 authenticate,
51 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
a2431b7d 52 asyncMiddleware(removeFollowingValidator),
0e9c48c2 53 asyncMiddleware(removeFollowing)
54141398
C
54)
55
4610bc5b 56serverFollowsRouter.get('/followers',
b8f4167f 57 listFollowsValidator,
7a7724e6
C
58 paginationValidator,
59 followersSortValidator,
1174a847 60 setDefaultSort,
f05a1c30 61 setDefaultPagination,
7a7724e6 62 asyncMiddleware(listFollowers)
65fcc311 63)
65fcc311 64
0e9c48c2
C
65serverFollowsRouter.delete('/followers/:nameWithHost',
66 authenticate,
67 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
14893eb7
C
68 asyncMiddleware(getFollowerValidator),
69 asyncMiddleware(removeOrRejectFollower)
70)
71
72serverFollowsRouter.post('/followers/:nameWithHost/reject',
73 authenticate,
74 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
75 asyncMiddleware(getFollowerValidator),
76 acceptOrRejectFollowerValidator,
77 asyncMiddleware(removeOrRejectFollower)
78)
79
80serverFollowsRouter.post('/followers/:nameWithHost/accept',
81 authenticate,
82 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
83 asyncMiddleware(getFollowerValidator),
84 acceptOrRejectFollowerValidator,
85 asyncMiddleware(acceptFollower)
0e9c48c2
C
86)
87
65fcc311
C
88// ---------------------------------------------------------------------------
89
90export {
4610bc5b 91 serverFollowsRouter
65fcc311
C
92}
93
94// ---------------------------------------------------------------------------
95
dae86118 96async function listFollowing (req: express.Request, res: express.Response) {
50d6de9c 97 const serverActor = await getServerActor()
b8f4167f
C
98 const resultList = await ActorFollowModel.listFollowingForApi({
99 id: serverActor.id,
100 start: req.query.start,
101 count: req.query.count,
102 sort: req.query.sort,
103 search: req.query.search,
97ecddae 104 actorType: req.query.actorType,
b8f4167f
C
105 state: req.query.state
106 })
7a7724e6
C
107
108 return res.json(getFormattedObjects(resultList.data, resultList.total))
109}
110
dae86118 111async function listFollowers (req: express.Request, res: express.Response) {
50d6de9c 112 const serverActor = await getServerActor()
b8f4167f
C
113 const resultList = await ActorFollowModel.listFollowersForApi({
114 actorId: serverActor.id,
115 start: req.query.start,
116 count: req.query.count,
117 sort: req.query.sort,
118 search: req.query.search,
97ecddae 119 actorType: req.query.actorType,
b8f4167f
C
120 state: req.query.state
121 })
eb080476
C
122
123 return res.json(getFormattedObjects(resultList.data, resultList.total))
65fcc311 124}
ce548a10 125
dae86118 126async function followInstance (req: express.Request, res: express.Response) {
ce548a10 127 const hosts = req.body.hosts as string[]
06a05d5f 128 const follower = await getServerActor()
350e31d6 129
ce548a10 130 for (const host of hosts) {
06a05d5f
C
131 const payload = {
132 host,
133 name: SERVER_ACTOR_NAME,
134 followerActorId: follower.id
135 }
136
137 JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
ce548a10
C
138 }
139
ce548a10
C
140 return res.status(204).end()
141}
350e31d6 142
0e9c48c2 143async function removeFollowing (req: express.Request, res: express.Response) {
dae86118 144 const follow = res.locals.follow
54141398 145
3fd3ab2d 146 await sequelizeTypescript.transaction(async t => {
40ff5707
C
147 if (follow.state === 'accepted') await sendUndoFollow(follow, t)
148
c48e82b5
C
149 // Disable redundancy on unfollowed instances
150 const server = follow.ActorFollowing.Server
151 server.redundancyAllowed = false
152 await server.save({ transaction: t })
153
161b061d 154 // Async, could be long
b764380a 155 removeRedundanciesOfServer(server.id)
161b061d
C
156 .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))
157
0f91ae62 158 await follow.destroy({ transaction: t })
54141398
C
159 })
160
161 return res.status(204).end()
162}
0e9c48c2 163
14893eb7 164async function removeOrRejectFollower (req: express.Request, res: express.Response) {
0e9c48c2
C
165 const follow = res.locals.follow
166
5b9c965d 167 await sendReject(follow.ActorFollower, follow.ActorFollowing)
0e9c48c2
C
168
169 await follow.destroy()
170
171 return res.status(204).end()
172}
14893eb7
C
173
174async function acceptFollower (req: express.Request, res: express.Response) {
175 const follow = res.locals.follow
176
177 await sendAccept(follow)
178
179 follow.state = 'accepted'
180 await follow.save()
181
8424c402
C
182 await autoFollowBackIfNeeded(follow)
183
14893eb7
C
184 return res.status(204).end()
185}