aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process-follow.ts
blob: a805c075798fabe50691cde9cccc4f6b7d0a90bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { ActivityFollow } from '../../../shared/models/activitypub/activity'
import { getOrCreateAccount, retryTransactionWrapper } from '../../helpers'
import { database as db } from '../../initializers'
import { AccountInstance } from '../../models/account/account-interface'
import { sendAccept } from './send-request'
import { logger } from '../../helpers/logger'

async function processFollowActivity (activity: ActivityFollow) {
  const activityObject = activity.object
  const account = await getOrCreateAccount(activity.actor)

  return processFollow(account, activityObject)
}

// ---------------------------------------------------------------------------

export {
  processFollowActivity
}

// ---------------------------------------------------------------------------

function processFollow (account: AccountInstance, targetAccountURL: string) {
  const options = {
    arguments: [ account, targetAccountURL ],
    errorMessage: 'Cannot follow with many retries.'
  }

  return retryTransactionWrapper(follow, options)
}

async function follow (account: AccountInstance, targetAccountURL: string) {
  await db.sequelize.transaction(async t => {
    const targetAccount = await db.Account.loadByUrl(targetAccountURL, t)

    if (targetAccount === undefined) throw new Error('Unknown account')
    if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')

    await db.AccountFollow.findOrCreate({
      where: {
        accountId: account.id,
        targetAccountId: targetAccount.id
      },
      defaults: {
        accountId: account.id,
        targetAccountId: targetAccount.id,
        state: 'accepted'
      },
      transaction: t
    })

    // Target sends to account he accepted the follow request
    return sendAccept(targetAccount, account, t)
  })

  logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)
}