diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-13 18:48:28 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | ce548a10db3822c415b30ea0edb59e02a460734a (patch) | |
tree | b39127c62101b5bf47e0a8a3c30bc70d6449824d /server/controllers/api/pods.ts | |
parent | 7a7724e66e4533523083e7336cd0d0c747c4a33b (diff) | |
download | PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.tar.gz PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.tar.zst PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.zip |
Send follow/accept
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r-- | server/controllers/api/pods.ts | 41 |
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 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
1 | import * as express from 'express' | 2 | import * as express from 'express' |
2 | import { getFormattedObjects } from '../../helpers' | 3 | import { getFormattedObjects } from '../../helpers' |
4 | import { getOrCreateAccount } from '../../helpers/activitypub' | ||
3 | import { getApplicationAccount } from '../../helpers/utils' | 5 | import { getApplicationAccount } from '../../helpers/utils' |
6 | import { REMOTE_SCHEME } from '../../initializers/constants' | ||
4 | import { database as db } from '../../initializers/database' | 7 | import { database as db } from '../../initializers/database' |
5 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' | 8 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' |
9 | import { setBodyHostsPort } from '../../middlewares/pods' | ||
6 | import { setFollowingSort } from '../../middlewares/sort' | 10 | import { setFollowingSort } from '../../middlewares/sort' |
11 | import { followValidator } from '../../middlewares/validators/pods' | ||
7 | import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' | 12 | import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' |
13 | import { sendFollow } from '../../lib/activitypub/send-request' | ||
8 | 14 | ||
9 | const podsRouter = express.Router() | 15 | const podsRouter = express.Router() |
10 | 16 | ||
@@ -16,6 +22,12 @@ podsRouter.get('/following', | |||
16 | asyncMiddleware(listFollowing) | 22 | asyncMiddleware(listFollowing) |
17 | ) | 23 | ) |
18 | 24 | ||
25 | podsRouter.post('/follow', | ||
26 | followValidator, | ||
27 | setBodyHostsPort, | ||
28 | asyncMiddleware(follow) | ||
29 | ) | ||
30 | |||
19 | podsRouter.get('/followers', | 31 | podsRouter.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 | |||
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 | } | ||