]>
Commit | Line | Data |
---|---|---|
ce548a10 | 1 | import * as Bluebird from 'bluebird' |
4d4e5cd4 | 2 | import * as express from 'express' |
571389d4 | 3 | import { getFormattedObjects } from '../../helpers' |
ce548a10 | 4 | import { getOrCreateAccount } from '../../helpers/activitypub' |
7a7724e6 | 5 | import { getApplicationAccount } from '../../helpers/utils' |
ce548a10 | 6 | import { REMOTE_SCHEME } from '../../initializers/constants' |
e02643f3 | 7 | import { database as db } from '../../initializers/database' |
7a7724e6 | 8 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' |
ce548a10 | 9 | import { setBodyHostsPort } from '../../middlewares/pods' |
7a7724e6 | 10 | import { setFollowingSort } from '../../middlewares/sort' |
ce548a10 | 11 | import { followValidator } from '../../middlewares/validators/pods' |
7a7724e6 | 12 | import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' |
ce548a10 | 13 | import { sendFollow } from '../../lib/activitypub/send-request' |
8e696487 C |
14 | import { authenticate } from '../../middlewares/oauth' |
15 | import { ensureUserHasRight } from '../../middlewares/user-right' | |
16 | import { UserRight } from '../../../shared/models/users/user-right.enum' | |
65fcc311 C |
17 | |
18 | const podsRouter = express.Router() | |
19 | ||
7a7724e6 | 20 | podsRouter.get('/following', |
8a02bd04 | 21 | paginationValidator, |
7a7724e6 C |
22 | followingSortValidator, |
23 | setFollowingSort, | |
8a02bd04 | 24 | setPagination, |
7a7724e6 C |
25 | asyncMiddleware(listFollowing) |
26 | ) | |
27 | ||
ce548a10 | 28 | podsRouter.post('/follow', |
8e696487 C |
29 | authenticate, |
30 | ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW), | |
ce548a10 C |
31 | followValidator, |
32 | setBodyHostsPort, | |
33 | asyncMiddleware(follow) | |
34 | ) | |
35 | ||
7a7724e6 C |
36 | podsRouter.get('/followers', |
37 | paginationValidator, | |
38 | followersSortValidator, | |
39 | setFollowersSort, | |
40 | setPagination, | |
41 | asyncMiddleware(listFollowers) | |
65fcc311 | 42 | ) |
65fcc311 C |
43 | |
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | export { | |
47 | podsRouter | |
48 | } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
7a7724e6 C |
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) | |
55 | ||
56 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
57 | } | |
58 | ||
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) | |
eb080476 C |
62 | |
63 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
65fcc311 | 64 | } |
ce548a10 C |
65 | |
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() | |
69 | ||
70 | const tasks: Bluebird<any>[] = [] | |
71 | for (const host of hosts) { | |
72 | const url = REMOTE_SCHEME.HTTP + '://' + host | |
73 | const targetAccount = await getOrCreateAccount(url) | |
74 | ||
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, | |
82 | state: 'pending' | |
83 | }) | |
84 | .then(() => sendFollow(fromAccount, targetAccount, t)) | |
85 | }) | |
86 | ||
87 | tasks.push(p) | |
88 | } | |
89 | ||
90 | await Promise.all(tasks) | |
91 | ||
92 | return res.status(204).end() | |
93 | } |