diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-13 18:48:28 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | ce548a10db3822c415b30ea0edb59e02a460734a (patch) | |
tree | b39127c62101b5bf47e0a8a3c30bc70d6449824d /server/lib/activitypub/process-follow.ts | |
parent | 7a7724e66e4533523083e7336cd0d0c747c4a33b (diff) | |
download | PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.tar.gz PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.tar.zst PeerTube-ce548a10db3822c415b30ea0edb59e02a460734a.zip |
Send follow/accept
Diffstat (limited to 'server/lib/activitypub/process-follow.ts')
-rw-r--r-- | server/lib/activitypub/process-follow.ts | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/server/lib/activitypub/process-follow.ts b/server/lib/activitypub/process-follow.ts index a04fc7994..ee5d97a0b 100644 --- a/server/lib/activitypub/process-follow.ts +++ b/server/lib/activitypub/process-follow.ts | |||
@@ -1,7 +1,9 @@ | |||
1 | import { ActivityFollow } from '../../../shared/models/activitypub/activity' | 1 | import { ActivityFollow } from '../../../shared/models/activitypub/activity' |
2 | import { getOrCreateAccount } from '../../helpers' | 2 | import { getOrCreateAccount, retryTransactionWrapper } from '../../helpers' |
3 | import { database as db } from '../../initializers' | 3 | import { database as db } from '../../initializers' |
4 | import { AccountInstance } from '../../models/account/account-interface' | 4 | import { AccountInstance } from '../../models/account/account-interface' |
5 | import { sendAccept } from './send-request' | ||
6 | import { logger } from '../../helpers/logger' | ||
5 | 7 | ||
6 | async function processFollowActivity (activity: ActivityFollow) { | 8 | async function processFollowActivity (activity: ActivityFollow) { |
7 | const activityObject = activity.object | 9 | const activityObject = activity.object |
@@ -18,15 +20,34 @@ export { | |||
18 | 20 | ||
19 | // --------------------------------------------------------------------------- | 21 | // --------------------------------------------------------------------------- |
20 | 22 | ||
21 | async function processFollow (account: AccountInstance, targetAccountURL: string) { | 23 | function processFollow (account: AccountInstance, targetAccountURL: string) { |
22 | const targetAccount = await db.Account.loadByUrl(targetAccountURL) | 24 | const options = { |
25 | arguments: [ account, targetAccountURL ], | ||
26 | errorMessage: 'Cannot follow with many retries.' | ||
27 | } | ||
23 | 28 | ||
24 | if (targetAccount === undefined) throw new Error('Unknown account') | 29 | return retryTransactionWrapper(follow, options) |
25 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') | 30 | } |
31 | |||
32 | async function follow (account: AccountInstance, targetAccountURL: string) { | ||
33 | await db.sequelize.transaction(async t => { | ||
34 | const targetAccount = await db.Account.loadByUrl(targetAccountURL, t) | ||
35 | |||
36 | if (targetAccount === undefined) throw new Error('Unknown account') | ||
37 | if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') | ||
26 | 38 | ||
27 | return db.AccountFollow.create({ | 39 | const sequelizeOptions = { |
28 | accountId: account.id, | 40 | transaction: t |
29 | targetAccountId: targetAccount.id, | 41 | } |
30 | state: 'accepted' | 42 | await db.AccountFollow.create({ |
43 | accountId: account.id, | ||
44 | targetAccountId: targetAccount.id, | ||
45 | state: 'accepted' | ||
46 | }, sequelizeOptions) | ||
47 | |||
48 | // Target sends to account he accepted the follow request | ||
49 | return sendAccept(targetAccount, account, t) | ||
31 | }) | 50 | }) |
51 | |||
52 | logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL) | ||
32 | } | 53 | } |