1 import { ActivityFollow } from '../../../../shared/models/activitypub/activity'
2 import { getOrCreateAccount, 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'
8 async function processFollowActivity (activity: ActivityFollow) {
9 const activityObject = activity.object
10 const account = await getOrCreateAccount(activity.actor)
12 return processFollow(account, activityObject)
15 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
23 function processFollow (account: AccountInstance, targetAccountURL: string) {
25 arguments: [ account, targetAccountURL ],
26 errorMessage: 'Cannot follow with many retries.'
29 return retryTransactionWrapper(follow, options)
32 async function follow (account: AccountInstance, targetAccountURL: string) {
33 await db.sequelize.transaction(async t => {
34 const targetAccount = await db.Account.loadByUrl(targetAccountURL, t)
36 if (!targetAccount) throw new Error('Unknown account')
37 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
39 const [ accountFollow ] = await db.AccountFollow.findOrCreate({
41 accountId: account.id,
42 targetAccountId: targetAccount.id
45 accountId: account.id,
46 targetAccountId: targetAccount.id,
51 accountFollow.AccountFollower = account
52 accountFollow.AccountFollowing = targetAccount
54 // Target sends to account he accepted the follow request
55 return sendAccept(accountFollow, t)
58 logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)