]>
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' |
65fcc311 C |
14 | |
15 | const podsRouter = express.Router() | |
16 | ||
7a7724e6 | 17 | podsRouter.get('/following', |
8a02bd04 | 18 | paginationValidator, |
7a7724e6 C |
19 | followingSortValidator, |
20 | setFollowingSort, | |
8a02bd04 | 21 | setPagination, |
7a7724e6 C |
22 | asyncMiddleware(listFollowing) |
23 | ) | |
24 | ||
ce548a10 C |
25 | podsRouter.post('/follow', |
26 | followValidator, | |
27 | setBodyHostsPort, | |
28 | asyncMiddleware(follow) | |
29 | ) | |
30 | ||
7a7724e6 C |
31 | podsRouter.get('/followers', |
32 | paginationValidator, | |
33 | followersSortValidator, | |
34 | setFollowersSort, | |
35 | setPagination, | |
36 | asyncMiddleware(listFollowers) | |
65fcc311 | 37 | ) |
65fcc311 C |
38 | |
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | export { | |
42 | podsRouter | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
7a7724e6 C |
47 | async 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 | ||
54 | async 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 | |
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 | } |