]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
51548b31 C |
2 | import { UserRight } from '../../../../shared/models/users/user-right.enum' |
3 | import { getFormattedObjects } from '../../../helpers' | |
4 | import { logger } from '../../../helpers/logger' | |
efc32059 | 5 | import { getServerAccount } from '../../../helpers/utils' |
51548b31 C |
6 | import { getAccountFromWebfinger } from '../../../helpers/webfinger' |
7 | import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' | |
8 | import { database as db } from '../../../initializers/database' | |
54141398 | 9 | import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares' |
51548b31 | 10 | import { authenticate } from '../../../middlewares/oauth' |
60862425 | 11 | import { setBodyHostsPort } from '../../../middlewares/servers' |
51548b31 C |
12 | import { setFollowingSort } from '../../../middlewares/sort' |
13 | import { ensureUserHasRight } from '../../../middlewares/user-right' | |
54141398 | 14 | import { followValidator } from '../../../middlewares/validators/follows' |
51548b31 | 15 | import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' |
54141398 C |
16 | import { AccountFollowInstance } from '../../../models/index' |
17 | import { sendFollow } from '../../../lib/index' | |
18 | import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' | |
0f91ae62 C |
19 | import { AccountInstance } from '../../../models/account/account-interface' |
20 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
21 | import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account' | |
c986175d | 22 | import { addFetchOutboxJob } from '../../../lib/activitypub/fetch' |
51548b31 | 23 | |
4610bc5b | 24 | const serverFollowsRouter = express.Router() |
51548b31 | 25 | |
4610bc5b | 26 | serverFollowsRouter.get('/following', |
8a02bd04 | 27 | paginationValidator, |
7a7724e6 C |
28 | followingSortValidator, |
29 | setFollowingSort, | |
8a02bd04 | 30 | setPagination, |
7a7724e6 C |
31 | asyncMiddleware(listFollowing) |
32 | ) | |
33 | ||
9a27cdc2 | 34 | serverFollowsRouter.post('/following', |
8e696487 | 35 | authenticate, |
4610bc5b | 36 | ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), |
ce548a10 C |
37 | followValidator, |
38 | setBodyHostsPort, | |
0f91ae62 | 39 | asyncMiddleware(followRetry) |
ce548a10 C |
40 | ) |
41 | ||
54141398 C |
42 | serverFollowsRouter.delete('/following/:accountId', |
43 | authenticate, | |
44 | ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), | |
45 | removeFollowingValidator, | |
46 | asyncMiddleware(removeFollow) | |
47 | ) | |
48 | ||
4610bc5b | 49 | serverFollowsRouter.get('/followers', |
7a7724e6 C |
50 | paginationValidator, |
51 | followersSortValidator, | |
52 | setFollowersSort, | |
53 | setPagination, | |
54 | asyncMiddleware(listFollowers) | |
65fcc311 | 55 | ) |
65fcc311 C |
56 | |
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
4610bc5b | 60 | serverFollowsRouter |
65fcc311 C |
61 | } |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
7a7724e6 | 65 | async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { |
4610bc5b C |
66 | const serverAccount = await getServerAccount() |
67 | const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) | |
7a7724e6 C |
68 | |
69 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
70 | } | |
71 | ||
72 | async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { | |
4610bc5b C |
73 | const serverAccount = await getServerAccount() |
74 | const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) | |
eb080476 C |
75 | |
76 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
65fcc311 | 77 | } |
ce548a10 | 78 | |
0f91ae62 | 79 | async function followRetry (req: express.Request, res: express.Response, next: express.NextFunction) { |
ce548a10 | 80 | const hosts = req.body.hosts as string[] |
efc32059 | 81 | const fromAccount = await getServerAccount() |
ce548a10 | 82 | |
350e31d6 C |
83 | const tasks: Promise<any>[] = [] |
84 | const accountName = SERVER_ACCOUNT_NAME | |
85 | ||
ce548a10 | 86 | for (const host of hosts) { |
ce548a10 C |
87 | |
88 | // We process each host in a specific transaction | |
89 | // First, we add the follow request in the database | |
90 | // Then we send the follow request to other account | |
350e31d6 C |
91 | const p = loadLocalOrGetAccountFromWebfinger(accountName, host) |
92 | .then(accountResult => { | |
93 | let targetAccount = accountResult.account | |
94 | ||
0f91ae62 C |
95 | const options = { |
96 | arguments: [ fromAccount, targetAccount, accountResult.loadedFromDB ], | |
97 | errorMessage: 'Cannot follow with many retries.' | |
98 | } | |
99 | ||
100 | return retryTransactionWrapper(follow, options) | |
ce548a10 | 101 | }) |
350e31d6 | 102 | .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err)) |
ce548a10 C |
103 | |
104 | tasks.push(p) | |
105 | } | |
106 | ||
8e10cf1a C |
107 | // Don't make the client wait the tasks |
108 | Promise.all(tasks) | |
0f91ae62 | 109 | .catch(err => logger.error('Error in follow.', err)) |
ce548a10 C |
110 | |
111 | return res.status(204).end() | |
112 | } | |
350e31d6 | 113 | |
0f91ae62 C |
114 | async function follow (fromAccount: AccountInstance, targetAccount: AccountInstance, targetAlreadyInDB: boolean) { |
115 | try { | |
116 | await db.sequelize.transaction(async t => { | |
117 | if (targetAlreadyInDB === false) { | |
118 | await saveAccountAndServerIfNotExist(targetAccount, t) | |
119 | } | |
120 | ||
121 | const [ accountFollow ] = await db.AccountFollow.findOrCreate({ | |
122 | where: { | |
123 | accountId: fromAccount.id, | |
124 | targetAccountId: targetAccount.id | |
125 | }, | |
126 | defaults: { | |
127 | state: 'pending', | |
128 | accountId: fromAccount.id, | |
129 | targetAccountId: targetAccount.id | |
130 | }, | |
131 | transaction: t | |
132 | }) | |
133 | accountFollow.AccountFollowing = targetAccount | |
134 | accountFollow.AccountFollower = fromAccount | |
135 | ||
136 | // Send a notification to remote server | |
137 | if (accountFollow.state === 'pending') { | |
138 | await sendFollow(accountFollow, t) | |
139 | } | |
c986175d C |
140 | |
141 | await addFetchOutboxJob(targetAccount, t) | |
0f91ae62 C |
142 | }) |
143 | } catch (err) { | |
144 | // Reset target account | |
145 | targetAccount.isNewRecord = !targetAlreadyInDB | |
146 | throw err | |
147 | } | |
148 | } | |
149 | ||
54141398 | 150 | async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { |
0f91ae62 | 151 | const follow: AccountFollowInstance = res.locals.follow |
54141398 C |
152 | |
153 | await db.sequelize.transaction(async t => { | |
0f91ae62 C |
154 | await sendUndoFollow(follow, t) |
155 | await follow.destroy({ transaction: t }) | |
54141398 C |
156 | }) |
157 | ||
158 | return res.status(204).end() | |
159 | } | |
160 | ||
350e31d6 C |
161 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { |
162 | let loadedFromDB = true | |
163 | let account = await db.Account.loadByNameAndHost(name, host) | |
164 | ||
165 | if (!account) { | |
166 | const nameWithDomain = name + '@' + host | |
167 | account = await getAccountFromWebfinger(nameWithDomain) | |
168 | loadedFromDB = false | |
169 | } | |
170 | ||
171 | return { account, loadedFromDB } | |
172 | } |