]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.ts
Send follow/accept
[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'
65fcc311
C
14
15const podsRouter = express.Router()
16
7a7724e6 17podsRouter.get('/following',
8a02bd04 18 paginationValidator,
7a7724e6
C
19 followingSortValidator,
20 setFollowingSort,
8a02bd04 21 setPagination,
7a7724e6
C
22 asyncMiddleware(listFollowing)
23)
24
ce548a10
C
25podsRouter.post('/follow',
26 followValidator,
27 setBodyHostsPort,
28 asyncMiddleware(follow)
29)
30
7a7724e6
C
31podsRouter.get('/followers',
32 paginationValidator,
33 followersSortValidator,
34 setFollowersSort,
35 setPagination,
36 asyncMiddleware(listFollowers)
65fcc311 37)
65fcc311
C
38
39// ---------------------------------------------------------------------------
40
41export {
42 podsRouter
43}
44
45// ---------------------------------------------------------------------------
46
7a7724e6
C
47async 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
54async 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)
eb080476
C
57
58 return res.json(getFormattedObjects(resultList.data, resultList.total))
65fcc311 59}
ce548a10
C
60
61async 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}