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'
18 const podsRouter = express.Router()
20 podsRouter.get('/following',
22 followingSortValidator,
25 asyncMiddleware(listFollowing)
28 podsRouter.post('/follow',
30 ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW),
33 asyncMiddleware(follow)
36 podsRouter.get('/followers',
38 followersSortValidator,
41 asyncMiddleware(listFollowers)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
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)
56 return res.json(getFormattedObjects(resultList.data, resultList.total))
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)
63 return res.json(getFormattedObjects(resultList.data, resultList.total))
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()
70 const tasks: Bluebird<any>[] = []
71 for (const host of hosts) {
72 const url = REMOTE_SCHEME.HTTP + '://' + host
73 const targetAccount = await getOrCreateAccount(url)
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,
84 .then(() => sendFollow(fromAccount, targetAccount, t))
90 await Promise.all(tasks)
92 return res.status(204).end()