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 { sendAccept } from './send-request'
6 import { logger } from '../../helpers/logger'
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 === undefined) throw new Error('Unknown account')
37 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
39 await db.AccountFollow.findOrCreate({
41 accountId: account.id,
42 targetAccountId: targetAccount.id
45 accountId: account.id,
46 targetAccountId: targetAccount.id,
52 // Target sends to account he accepted the follow request
53 return sendAccept(targetAccount, account, t)
56 logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)