aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/pods.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r--server/controllers/api/pods.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts
index aa07b17f6..f662f1c03 100644
--- a/server/controllers/api/pods.ts
+++ b/server/controllers/api/pods.ts
@@ -1,10 +1,16 @@
1import * as Bluebird from 'bluebird'
1import * as express from 'express' 2import * as express from 'express'
2import { getFormattedObjects } from '../../helpers' 3import { getFormattedObjects } from '../../helpers'
4import { getOrCreateAccount } from '../../helpers/activitypub'
3import { getApplicationAccount } from '../../helpers/utils' 5import { getApplicationAccount } from '../../helpers/utils'
6import { REMOTE_SCHEME } from '../../initializers/constants'
4import { database as db } from '../../initializers/database' 7import { database as db } from '../../initializers/database'
5import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' 8import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
9import { setBodyHostsPort } from '../../middlewares/pods'
6import { setFollowingSort } from '../../middlewares/sort' 10import { setFollowingSort } from '../../middlewares/sort'
11import { followValidator } from '../../middlewares/validators/pods'
7import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' 12import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
13import { sendFollow } from '../../lib/activitypub/send-request'
8 14
9const podsRouter = express.Router() 15const podsRouter = express.Router()
10 16
@@ -16,6 +22,12 @@ podsRouter.get('/following',
16 asyncMiddleware(listFollowing) 22 asyncMiddleware(listFollowing)
17) 23)
18 24
25podsRouter.post('/follow',
26 followValidator,
27 setBodyHostsPort,
28 asyncMiddleware(follow)
29)
30
19podsRouter.get('/followers', 31podsRouter.get('/followers',
20 paginationValidator, 32 paginationValidator,
21 followersSortValidator, 33 followersSortValidator,
@@ -45,3 +57,32 @@ async function listFollowers (req: express.Request, res: express.Response, next:
45 57
46 return res.json(getFormattedObjects(resultList.data, resultList.total)) 58 return res.json(getFormattedObjects(resultList.data, resultList.total))
47} 59}
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}