]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/pods.ts
Send follow/accept
[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
15 const podsRouter = express.Router()
16
17 podsRouter.get('/following',
18 paginationValidator,
19 followingSortValidator,
20 setFollowingSort,
21 setPagination,
22 asyncMiddleware(listFollowing)
23 )
24
25 podsRouter.post('/follow',
26 followValidator,
27 setBodyHostsPort,
28 asyncMiddleware(follow)
29 )
30
31 podsRouter.get('/followers',
32 paginationValidator,
33 followersSortValidator,
34 setFollowersSort,
35 setPagination,
36 asyncMiddleware(listFollowers)
37 )
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 podsRouter
43 }
44
45 // ---------------------------------------------------------------------------
46
47 async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
48 const applicationAccount = await getApplicationAccount()
49 const resultList = await db.Account.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
50
51 return res.json(getFormattedObjects(resultList.data, resultList.total))
52 }
53
54 async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const applicationAccount = await getApplicationAccount()
56 const resultList = await db.Account.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
57
58 return res.json(getFormattedObjects(resultList.data, resultList.total))
59 }
60
61 async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
62 const hosts = req.body.hosts as string[]
63 const fromAccount = await getApplicationAccount()
64
65 const tasks: Bluebird<any>[] = []
66 for (const host of hosts) {
67 const url = REMOTE_SCHEME.HTTP + '://' + host
68 const targetAccount = await getOrCreateAccount(url)
69
70 // We process each host in a specific transaction
71 // First, we add the follow request in the database
72 // Then we send the follow request to other account
73 const p = db.sequelize.transaction(async t => {
74 return db.AccountFollow.create({
75 accountId: fromAccount.id,
76 targetAccountId: targetAccount.id,
77 state: 'pending'
78 })
79 .then(() => sendFollow(fromAccount, targetAccount, t))
80 })
81
82 tasks.push(p)
83 }
84
85 await Promise.all(tasks)
86
87 return res.status(204).end()
88 }