]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { ActivityFollow } from '../../../../shared/models/activitypub' |
2 | import { logger, retryTransactionWrapper } from '../../../helpers' | |
3 | import { sequelizeTypescript } from '../../../initializers' | |
4 | import { AccountModel } from '../../../models/account/account' | |
5 | import { AccountFollowModel } from '../../../models/account/account-follow' | |
0f91ae62 | 6 | import { getOrCreateAccountAndServer } from '../account' |
3fd3ab2d | 7 | import { sendAccept } from '../send' |
7a7724e6 C |
8 | |
9 | async function processFollowActivity (activity: ActivityFollow) { | |
10 | const activityObject = activity.object | |
0f91ae62 | 11 | const account = await getOrCreateAccountAndServer(activity.actor) |
7a7724e6 C |
12 | |
13 | return processFollow(account, activityObject) | |
14 | } | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | ||
18 | export { | |
19 | processFollowActivity | |
20 | } | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
3fd3ab2d | 24 | function processFollow (account: AccountModel, targetAccountURL: string) { |
ce548a10 C |
25 | const options = { |
26 | arguments: [ account, targetAccountURL ], | |
27 | errorMessage: 'Cannot follow with many retries.' | |
28 | } | |
7a7724e6 | 29 | |
ce548a10 C |
30 | return retryTransactionWrapper(follow, options) |
31 | } | |
32 | ||
3fd3ab2d C |
33 | async function follow (account: AccountModel, targetAccountURL: string) { |
34 | await sequelizeTypescript.transaction(async t => { | |
35 | const targetAccount = await AccountModel.loadByUrl(targetAccountURL, t) | |
ce548a10 | 36 | |
54141398 | 37 | if (!targetAccount) throw new Error('Unknown account') |
ce548a10 | 38 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') |
7a7724e6 | 39 | |
3fd3ab2d | 40 | const [ accountFollow ] = await AccountFollowModel.findOrCreate({ |
350e31d6 C |
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 | }, | |
ce548a10 | 50 | transaction: t |
350e31d6 | 51 | }) |
40ff5707 C |
52 | |
53 | if (accountFollow.state !== 'accepted') { | |
54 | accountFollow.state = 'accepted' | |
55 | await accountFollow.save({ transaction: t }) | |
56 | } | |
57 | ||
54141398 C |
58 | accountFollow.AccountFollower = account |
59 | accountFollow.AccountFollowing = targetAccount | |
ce548a10 C |
60 | |
61 | // Target sends to account he accepted the follow request | |
54141398 | 62 | return sendAccept(accountFollow, t) |
7a7724e6 | 63 | }) |
ce548a10 C |
64 | |
65 | logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL) | |
7a7724e6 | 66 | } |