]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityFollow } from '../../../../shared/models/activitypub/activity' | |
2 | import { retryTransactionWrapper } from '../../../helpers' | |
3 | import { database as db } from '../../../initializers' | |
4 | import { AccountInstance } from '../../../models/account/account-interface' | |
5 | import { logger } from '../../../helpers/logger' | |
6 | import { sendAccept } from '../send/send-accept' | |
7 | import { getOrCreateAccountAndServer } from '../account' | |
8 | ||
9 | async function processFollowActivity (activity: ActivityFollow) { | |
10 | const activityObject = activity.object | |
11 | const account = await getOrCreateAccountAndServer(activity.actor) | |
12 | ||
13 | return processFollow(account, activityObject) | |
14 | } | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | ||
18 | export { | |
19 | processFollowActivity | |
20 | } | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
24 | function processFollow (account: AccountInstance, targetAccountURL: string) { | |
25 | const options = { | |
26 | arguments: [ account, targetAccountURL ], | |
27 | errorMessage: 'Cannot follow with many retries.' | |
28 | } | |
29 | ||
30 | return retryTransactionWrapper(follow, options) | |
31 | } | |
32 | ||
33 | async function follow (account: AccountInstance, targetAccountURL: string) { | |
34 | await db.sequelize.transaction(async t => { | |
35 | const targetAccount = await db.Account.loadByUrl(targetAccountURL, t) | |
36 | ||
37 | if (!targetAccount) throw new Error('Unknown account') | |
38 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') | |
39 | ||
40 | const [ accountFollow ] = await db.AccountFollow.findOrCreate({ | |
41 | where: { | |
42 | accountId: account.id, | |
43 | targetAccountId: targetAccount.id | |
44 | }, | |
45 | defaults: { | |
46 | accountId: account.id, | |
47 | targetAccountId: targetAccount.id, | |
48 | state: 'accepted' | |
49 | }, | |
50 | transaction: t | |
51 | }) | |
52 | ||
53 | if (accountFollow.state !== 'accepted') { | |
54 | accountFollow.state = 'accepted' | |
55 | await accountFollow.save({ transaction: t }) | |
56 | } | |
57 | ||
58 | accountFollow.AccountFollower = account | |
59 | accountFollow.AccountFollowing = targetAccount | |
60 | ||
61 | // Target sends to account he accepted the follow request | |
62 | return sendAccept(accountFollow, t) | |
63 | }) | |
64 | ||
65 | logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL) | |
66 | } |