]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.ts
Add MANAGE_PEERTUBE_FOLLOW right
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
CommitLineData
ce548a10 1import * as Bluebird from 'bluebird'
4d4e5cd4 2import * as express from 'express'
571389d4 3import { getFormattedObjects } from '../../helpers'
ce548a10 4import { getOrCreateAccount } from '../../helpers/activitypub'
7a7724e6 5import { getApplicationAccount } from '../../helpers/utils'
ce548a10 6import { REMOTE_SCHEME } from '../../initializers/constants'
e02643f3 7import { database as db } from '../../initializers/database'
7a7724e6 8import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
ce548a10 9import { setBodyHostsPort } from '../../middlewares/pods'
7a7724e6 10import { setFollowingSort } from '../../middlewares/sort'
ce548a10 11import { followValidator } from '../../middlewares/validators/pods'
7a7724e6 12import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
ce548a10 13import { sendFollow } from '../../lib/activitypub/send-request'
8e696487
C
14import { authenticate } from '../../middlewares/oauth'
15import { ensureUserHasRight } from '../../middlewares/user-right'
16import { UserRight } from '../../../shared/models/users/user-right.enum'
65fcc311
C
17
18const podsRouter = express.Router()
19
7a7724e6 20podsRouter.get('/following',
8a02bd04 21 paginationValidator,
7a7724e6
C
22 followingSortValidator,
23 setFollowingSort,
8a02bd04 24 setPagination,
7a7724e6
C
25 asyncMiddleware(listFollowing)
26)
27
ce548a10 28podsRouter.post('/follow',
8e696487
C
29 authenticate,
30 ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW),
ce548a10
C
31 followValidator,
32 setBodyHostsPort,
33 asyncMiddleware(follow)
34)
35
7a7724e6
C
36podsRouter.get('/followers',
37 paginationValidator,
38 followersSortValidator,
39 setFollowersSort,
40 setPagination,
41 asyncMiddleware(listFollowers)
65fcc311 42)
65fcc311
C
43
44// ---------------------------------------------------------------------------
45
46export {
47 podsRouter
48}
49
50// ---------------------------------------------------------------------------
51
7a7724e6
C
52async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
53 const applicationAccount = await getApplicationAccount()
54 const resultList = await db.Account.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
55
56 return res.json(getFormattedObjects(resultList.data, resultList.total))
57}
58
59async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
60 const applicationAccount = await getApplicationAccount()
61 const resultList = await db.Account.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
eb080476
C
62
63 return res.json(getFormattedObjects(resultList.data, resultList.total))
65fcc311 64}
ce548a10
C
65
66async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
67 const hosts = req.body.hosts as string[]
68 const fromAccount = await getApplicationAccount()
69
70 const tasks: Bluebird<any>[] = []
71 for (const host of hosts) {
72 const url = REMOTE_SCHEME.HTTP + '://' + host
73 const targetAccount = await getOrCreateAccount(url)
74
75 // We process each host in a specific transaction
76 // First, we add the follow request in the database
77 // Then we send the follow request to other account
78 const p = db.sequelize.transaction(async t => {
79 return db.AccountFollow.create({
80 accountId: fromAccount.id,
81 targetAccountId: targetAccount.id,
82 state: 'pending'
83 })
84 .then(() => sendFollow(fromAccount, targetAccount, t))
85 })
86
87 tasks.push(p)
88 }
89
90 await Promise.all(tasks)
91
92 return res.status(204).end()
93}