diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/lib/activitypub/process-follow.ts | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/lib/activitypub/process-follow.ts')
-rw-r--r-- | server/lib/activitypub/process-follow.ts | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/server/lib/activitypub/process-follow.ts b/server/lib/activitypub/process-follow.ts deleted file mode 100644 index a805c0757..000000000 --- a/server/lib/activitypub/process-follow.ts +++ /dev/null | |||
@@ -1,57 +0,0 @@ | |||
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' | ||
7 | |||
8 | async 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 | |||
17 | export { | ||
18 | processFollowActivity | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | function processFollow (account: AccountInstance, targetAccountURL: string) { | ||
24 | const options = { | ||
25 | arguments: [ account, targetAccountURL ], | ||
26 | errorMessage: 'Cannot follow with many retries.' | ||
27 | } | ||
28 | |||
29 | return retryTransactionWrapper(follow, options) | ||
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.') | ||
38 | |||
39 | await db.AccountFollow.findOrCreate({ | ||
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 | }, | ||
49 | transaction: t | ||
50 | }) | ||
51 | |||
52 | // Target sends to account he accepted the follow request | ||
53 | return sendAccept(targetAccount, account, t) | ||
54 | }) | ||
55 | |||
56 | logger.info('Account uuid %s is followed by account %s.', account.url, targetAccountURL) | ||
57 | } | ||