diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-21 13:43:29 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 0f91ae62df8a37194fea84ce1efa9e733d9c1fd8 (patch) | |
tree | 1cb0fccadebb629d02632dc8e21fae7ba521c464 /server/lib | |
parent | 81de19482b89342c3dbc098a9f512ef7f1056e45 (diff) | |
download | PeerTube-0f91ae62df8a37194fea84ce1efa9e733d9c1fd8.tar.gz PeerTube-0f91ae62df8a37194fea84ce1efa9e733d9c1fd8.tar.zst PeerTube-0f91ae62df8a37194fea84ce1efa9e733d9c1fd8.zip |
Add follow tests
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/activitypub/account.ts | 69 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-add.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-announce.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-delete.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-follow.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 4 |
7 files changed, 60 insertions, 33 deletions
diff --git a/server/lib/activitypub/account.ts b/server/lib/activitypub/account.ts index 704a92e13..906c8ff29 100644 --- a/server/lib/activitypub/account.ts +++ b/server/lib/activitypub/account.ts | |||
@@ -1,27 +1,65 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
1 | import * as url from 'url' | 2 | import * as url from 'url' |
2 | import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor' | 3 | import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor' |
3 | import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account' | 4 | import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account' |
5 | import { retryTransactionWrapper } from '../../helpers/database-utils' | ||
4 | import { logger } from '../../helpers/logger' | 6 | import { logger } from '../../helpers/logger' |
5 | import { doRequest } from '../../helpers/requests' | 7 | import { doRequest } from '../../helpers/requests' |
6 | import { ACTIVITY_PUB } from '../../initializers/constants' | 8 | import { ACTIVITY_PUB } from '../../initializers/constants' |
7 | import { database as db } from '../../initializers/database' | 9 | import { database as db } from '../../initializers/database' |
10 | import { AccountInstance } from '../../models/account/account-interface' | ||
11 | import { Transaction } from 'sequelize' | ||
8 | 12 | ||
9 | async function getOrCreateAccount (accountUrl: string) { | 13 | async function getOrCreateAccountAndServer (accountUrl: string) { |
10 | let account = await db.Account.loadByUrl(accountUrl) | 14 | let account = await db.Account.loadByUrl(accountUrl) |
11 | 15 | ||
12 | // We don't have this account in our database, fetch it on remote | 16 | // We don't have this account in our database, fetch it on remote |
13 | if (!account) { | 17 | if (!account) { |
14 | const res = await fetchRemoteAccountAndCreateServer(accountUrl) | 18 | account = await fetchRemoteAccount(accountUrl) |
15 | if (res === undefined) throw new Error('Cannot fetch remote account.') | 19 | if (account === undefined) throw new Error('Cannot fetch remote account.') |
16 | 20 | ||
17 | // Save our new account in database | 21 | const options = { |
18 | account = await res.account.save() | 22 | arguments: [ account ], |
23 | errorMessage: 'Cannot save account and server with many retries.' | ||
24 | } | ||
25 | account = await retryTransactionWrapper(saveAccountAndServerIfNotExist, options) | ||
19 | } | 26 | } |
20 | 27 | ||
21 | return account | 28 | return account |
22 | } | 29 | } |
23 | 30 | ||
24 | async function fetchRemoteAccountAndCreateServer (accountUrl: string) { | 31 | function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird<AccountInstance> | Promise<AccountInstance> { |
32 | if (t !== undefined) { | ||
33 | return save(t) | ||
34 | } else { | ||
35 | return db.sequelize.transaction(t => { | ||
36 | return save(t) | ||
37 | }) | ||
38 | } | ||
39 | |||
40 | async function save (t: Transaction) { | ||
41 | const accountHost = url.parse(account.url).host | ||
42 | |||
43 | const serverOptions = { | ||
44 | where: { | ||
45 | host: accountHost | ||
46 | }, | ||
47 | defaults: { | ||
48 | host: accountHost | ||
49 | }, | ||
50 | transaction: t | ||
51 | } | ||
52 | const [ server ] = await db.Server.findOrCreate(serverOptions) | ||
53 | |||
54 | // Save our new account in database | ||
55 | account.set('serverId', server.id) | ||
56 | account = await account.save({ transaction: t }) | ||
57 | |||
58 | return account | ||
59 | } | ||
60 | } | ||
61 | |||
62 | async function fetchRemoteAccount (accountUrl: string) { | ||
25 | const options = { | 63 | const options = { |
26 | uri: accountUrl, | 64 | uri: accountUrl, |
27 | method: 'GET', | 65 | method: 'GET', |
@@ -64,24 +102,13 @@ async function fetchRemoteAccountAndCreateServer (accountUrl: string) { | |||
64 | followingUrl: accountJSON.following | 102 | followingUrl: accountJSON.following |
65 | }) | 103 | }) |
66 | 104 | ||
67 | const accountHost = url.parse(account.url).host | 105 | return account |
68 | const serverOptions = { | ||
69 | where: { | ||
70 | host: accountHost | ||
71 | }, | ||
72 | defaults: { | ||
73 | host: accountHost | ||
74 | } | ||
75 | } | ||
76 | const [ server ] = await db.Server.findOrCreate(serverOptions) | ||
77 | account.set('serverId', server.id) | ||
78 | |||
79 | return { account, server } | ||
80 | } | 106 | } |
81 | 107 | ||
82 | export { | 108 | export { |
83 | getOrCreateAccount, | 109 | getOrCreateAccountAndServer, |
84 | fetchRemoteAccountAndCreateServer | 110 | fetchRemoteAccount, |
111 | saveAccountAndServerIfNotExist | ||
85 | } | 112 | } |
86 | 113 | ||
87 | // --------------------------------------------------------------------------- | 114 | // --------------------------------------------------------------------------- |
diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts index 281036228..edc90dee5 100644 --- a/server/lib/activitypub/process/process-add.ts +++ b/server/lib/activitypub/process/process-add.ts | |||
@@ -6,7 +6,7 @@ import { logger } from '../../../helpers/logger' | |||
6 | import { database as db } from '../../../initializers' | 6 | import { database as db } from '../../../initializers' |
7 | import { AccountInstance } from '../../../models/account/account-interface' | 7 | import { AccountInstance } from '../../../models/account/account-interface' |
8 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 8 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' |
9 | import { getOrCreateAccount } from '../account' | 9 | import { getOrCreateAccountAndServer } from '../account' |
10 | import { getOrCreateVideoChannel } from '../video-channels' | 10 | import { getOrCreateVideoChannel } from '../video-channels' |
11 | import { generateThumbnailFromUrl } from '../videos' | 11 | import { generateThumbnailFromUrl } from '../videos' |
12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 12 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
@@ -14,7 +14,7 @@ import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } | |||
14 | async function processAddActivity (activity: ActivityAdd) { | 14 | async function processAddActivity (activity: ActivityAdd) { |
15 | const activityObject = activity.object | 15 | const activityObject = activity.object |
16 | const activityType = activityObject.type | 16 | const activityType = activityObject.type |
17 | const account = await getOrCreateAccount(activity.actor) | 17 | const account = await getOrCreateAccountAndServer(activity.actor) |
18 | 18 | ||
19 | if (activityType === 'Video') { | 19 | if (activityType === 'Video') { |
20 | const videoChannelUrl = activity.target | 20 | const videoChannelUrl = activity.target |
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 40712ef03..d8532d3a1 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts | |||
@@ -5,11 +5,11 @@ import { VideoInstance } from '../../../models/index' | |||
5 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 5 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' |
6 | import { processAddActivity } from './process-add' | 6 | import { processAddActivity } from './process-add' |
7 | import { processCreateActivity } from './process-create' | 7 | import { processCreateActivity } from './process-create' |
8 | import { getOrCreateAccount } from '../account' | 8 | import { getOrCreateAccountAndServer } from '../account' |
9 | 9 | ||
10 | async function processAnnounceActivity (activity: ActivityAnnounce) { | 10 | async function processAnnounceActivity (activity: ActivityAnnounce) { |
11 | const announcedActivity = activity.object | 11 | const announcedActivity = activity.object |
12 | const accountAnnouncer = await getOrCreateAccount(activity.actor) | 12 | const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) |
13 | 13 | ||
14 | if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { | 14 | if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { |
15 | // Add share entry | 15 | // Add share entry |
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index fc635eb1f..ddf7c74f6 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -3,14 +3,14 @@ import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/ | |||
3 | import { logger, retryTransactionWrapper } from '../../../helpers' | 3 | import { logger, retryTransactionWrapper } from '../../../helpers' |
4 | import { database as db } from '../../../initializers' | 4 | import { database as db } from '../../../initializers' |
5 | import { AccountInstance } from '../../../models/account/account-interface' | 5 | import { AccountInstance } from '../../../models/account/account-interface' |
6 | import { getOrCreateAccount } from '../account' | 6 | import { getOrCreateAccountAndServer } from '../account' |
7 | import { getVideoChannelActivityPubUrl } from '../url' | 7 | import { getVideoChannelActivityPubUrl } from '../url' |
8 | import { videoChannelActivityObjectToDBAttributes } from './misc' | 8 | import { videoChannelActivityObjectToDBAttributes } from './misc' |
9 | 9 | ||
10 | async function processCreateActivity (activity: ActivityCreate) { | 10 | async function processCreateActivity (activity: ActivityCreate) { |
11 | const activityObject = activity.object | 11 | const activityObject = activity.object |
12 | const activityType = activityObject.type | 12 | const activityType = activityObject.type |
13 | const account = await getOrCreateAccount(activity.actor) | 13 | const account = await getOrCreateAccountAndServer(activity.actor) |
14 | 14 | ||
15 | if (activityType === 'VideoChannel') { | 15 | if (activityType === 'VideoChannel') { |
16 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) | 16 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) |
diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 0328d1a7d..41cdc236d 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts | |||
@@ -5,10 +5,10 @@ import { database as db } from '../../../initializers' | |||
5 | import { AccountInstance } from '../../../models/account/account-interface' | 5 | import { AccountInstance } from '../../../models/account/account-interface' |
6 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 6 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' |
7 | import { VideoInstance } from '../../../models/video/video-interface' | 7 | import { VideoInstance } from '../../../models/video/video-interface' |
8 | import { getOrCreateAccount } from '../account' | 8 | import { getOrCreateAccountAndServer } from '../account' |
9 | 9 | ||
10 | async function processDeleteActivity (activity: ActivityDelete) { | 10 | async function processDeleteActivity (activity: ActivityDelete) { |
11 | const account = await getOrCreateAccount(activity.actor) | 11 | const account = await getOrCreateAccountAndServer(activity.actor) |
12 | 12 | ||
13 | if (account.url === activity.id) { | 13 | if (account.url === activity.id) { |
14 | return processDeleteAccount(account) | 14 | return processDeleteAccount(account) |
diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 41b38828c..248004226 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts | |||
@@ -4,11 +4,11 @@ import { database as db } from '../../../initializers' | |||
4 | import { AccountInstance } from '../../../models/account/account-interface' | 4 | import { AccountInstance } from '../../../models/account/account-interface' |
5 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
6 | import { sendAccept } from '../send/send-accept' | 6 | import { sendAccept } from '../send/send-accept' |
7 | import { getOrCreateAccount } from '../account' | 7 | import { getOrCreateAccountAndServer } from '../account' |
8 | 8 | ||
9 | async function processFollowActivity (activity: ActivityFollow) { | 9 | async function processFollowActivity (activity: ActivityFollow) { |
10 | const activityObject = activity.object | 10 | const activityObject = activity.object |
11 | const account = await getOrCreateAccount(activity.actor) | 11 | const account = await getOrCreateAccountAndServer(activity.actor) |
12 | 12 | ||
13 | return processFollow(account, activityObject) | 13 | return processFollow(account, activityObject) |
14 | } | 14 | } |
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 4876735b8..7caf2ca78 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -8,10 +8,10 @@ import { AccountInstance } from '../../../models/account/account-interface' | |||
8 | import { VideoInstance } from '../../../models/video/video-interface' | 8 | import { VideoInstance } from '../../../models/video/video-interface' |
9 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 9 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
10 | import Bluebird = require('bluebird') | 10 | import Bluebird = require('bluebird') |
11 | import { getOrCreateAccount } from '../account' | 11 | import { getOrCreateAccountAndServer } from '../account' |
12 | 12 | ||
13 | async function processUpdateActivity (activity: ActivityUpdate) { | 13 | async function processUpdateActivity (activity: ActivityUpdate) { |
14 | const account = await getOrCreateAccount(activity.actor) | 14 | const account = await getOrCreateAccountAndServer(activity.actor) |
15 | 15 | ||
16 | if (activity.object.type === 'Video') { | 16 | if (activity.object.type === 'Video') { |
17 | return processUpdateVideo(account, activity.object) | 17 | return processUpdateVideo(account, activity.object) |