]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-follow.ts
Add follow tests
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
CommitLineData
54141398 1import { ActivityFollow } from '../../../../shared/models/activitypub/activity'
892211e8 2import { retryTransactionWrapper } from '../../../helpers'
54141398
C
3import { database as db } from '../../../initializers'
4import { AccountInstance } from '../../../models/account/account-interface'
5import { logger } from '../../../helpers/logger'
6import { sendAccept } from '../send/send-accept'
0f91ae62 7import { getOrCreateAccountAndServer } from '../account'
7a7724e6
C
8
9async 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
18export {
19 processFollowActivity
20}
21
22// ---------------------------------------------------------------------------
23
ce548a10
C
24function processFollow (account: AccountInstance, targetAccountURL: string) {
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
33async function follow (account: AccountInstance, targetAccountURL: string) {
34 await db.sequelize.transaction(async t => {
35 const targetAccount = await db.Account.loadByUrl(targetAccountURL, t)
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
54141398 40 const [ accountFollow ] = await db.AccountFollow.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 })
54141398
C
52 accountFollow.AccountFollower = account
53 accountFollow.AccountFollowing = targetAccount
ce548a10
C
54
55 // Target sends to account he accepted the follow request
54141398 56 return sendAccept(accountFollow, t)
7a7724e6 57 })
ce548a10
C
58
59 logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL)
7a7724e6 60}