]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/pods.ts
Add MANAGE_PEERTUBE_FOLLOW right
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import { getFormattedObjects } from '../../helpers'
4 import { getOrCreateAccount } from '../../helpers/activitypub'
5 import { getApplicationAccount } from '../../helpers/utils'
6 import { REMOTE_SCHEME } from '../../initializers/constants'
7 import { database as db } from '../../initializers/database'
8 import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
9 import { setBodyHostsPort } from '../../middlewares/pods'
10 import { setFollowingSort } from '../../middlewares/sort'
11 import { followValidator } from '../../middlewares/validators/pods'
12 import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
13 import { sendFollow } from '../../lib/activitypub/send-request'
14 import { authenticate } from '../../middlewares/oauth'
15 import { ensureUserHasRight } from '../../middlewares/user-right'
16 import { UserRight } from '../../../shared/models/users/user-right.enum'
17
18 const podsRouter = express.Router()
19
20 podsRouter.get('/following',
21 paginationValidator,
22 followingSortValidator,
23 setFollowingSort,
24 setPagination,
25 asyncMiddleware(listFollowing)
26 )
27
28 podsRouter.post('/follow',
29 authenticate,
30 ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW),
31 followValidator,
32 setBodyHostsPort,
33 asyncMiddleware(follow)
34 )
35
36 podsRouter.get('/followers',
37 paginationValidator,
38 followersSortValidator,
39 setFollowersSort,
40 setPagination,
41 asyncMiddleware(listFollowers)
42 )
43
44 // ---------------------------------------------------------------------------
45
46 export {
47 podsRouter
48 }
49
50 // ---------------------------------------------------------------------------
51
52 async 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
59 async 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)
62
63 return res.json(getFormattedObjects(resultList.data, resultList.total))
64 }
65
66 async 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 }