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