From 0f91ae62df8a37194fea84ce1efa9e733d9c1fd8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 21 Nov 2017 13:43:29 +0100 Subject: Add follow tests --- server/lib/activitypub/account.ts | 69 +++++++++++++++------- server/lib/activitypub/process/process-add.ts | 4 +- server/lib/activitypub/process/process-announce.ts | 4 +- server/lib/activitypub/process/process-create.ts | 4 +- server/lib/activitypub/process/process-delete.ts | 4 +- server/lib/activitypub/process/process-follow.ts | 4 +- server/lib/activitypub/process/process-update.ts | 4 +- 7 files changed, 60 insertions(+), 33 deletions(-) (limited to 'server/lib/activitypub') 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 @@ +import * as Bluebird from 'bluebird' import * as url from 'url' import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor' import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account' +import { retryTransactionWrapper } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' import { doRequest } from '../../helpers/requests' import { ACTIVITY_PUB } from '../../initializers/constants' import { database as db } from '../../initializers/database' +import { AccountInstance } from '../../models/account/account-interface' +import { Transaction } from 'sequelize' -async function getOrCreateAccount (accountUrl: string) { +async function getOrCreateAccountAndServer (accountUrl: string) { let account = await db.Account.loadByUrl(accountUrl) // We don't have this account in our database, fetch it on remote if (!account) { - const res = await fetchRemoteAccountAndCreateServer(accountUrl) - if (res === undefined) throw new Error('Cannot fetch remote account.') + account = await fetchRemoteAccount(accountUrl) + if (account === undefined) throw new Error('Cannot fetch remote account.') - // Save our new account in database - account = await res.account.save() + const options = { + arguments: [ account ], + errorMessage: 'Cannot save account and server with many retries.' + } + account = await retryTransactionWrapper(saveAccountAndServerIfNotExist, options) } return account } -async function fetchRemoteAccountAndCreateServer (accountUrl: string) { +function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird | Promise { + if (t !== undefined) { + return save(t) + } else { + return db.sequelize.transaction(t => { + return save(t) + }) + } + + async function save (t: Transaction) { + const accountHost = url.parse(account.url).host + + const serverOptions = { + where: { + host: accountHost + }, + defaults: { + host: accountHost + }, + transaction: t + } + const [ server ] = await db.Server.findOrCreate(serverOptions) + + // Save our new account in database + account.set('serverId', server.id) + account = await account.save({ transaction: t }) + + return account + } +} + +async function fetchRemoteAccount (accountUrl: string) { const options = { uri: accountUrl, method: 'GET', @@ -64,24 +102,13 @@ async function fetchRemoteAccountAndCreateServer (accountUrl: string) { followingUrl: accountJSON.following }) - const accountHost = url.parse(account.url).host - const serverOptions = { - where: { - host: accountHost - }, - defaults: { - host: accountHost - } - } - const [ server ] = await db.Server.findOrCreate(serverOptions) - account.set('serverId', server.id) - - return { account, server } + return account } export { - getOrCreateAccount, - fetchRemoteAccountAndCreateServer + getOrCreateAccountAndServer, + fetchRemoteAccount, + saveAccountAndServerIfNotExist } // --------------------------------------------------------------------------- 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' import { database as db } from '../../../initializers' import { AccountInstance } from '../../../models/account/account-interface' import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' import { getOrCreateVideoChannel } from '../video-channels' import { generateThumbnailFromUrl } from '../videos' import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' @@ -14,7 +14,7 @@ import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } async function processAddActivity (activity: ActivityAdd) { const activityObject = activity.object const activityType = activityObject.type - const account = await getOrCreateAccount(activity.actor) + const account = await getOrCreateAccountAndServer(activity.actor) if (activityType === 'Video') { 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' import { VideoChannelInstance } from '../../../models/video/video-channel-interface' import { processAddActivity } from './process-add' import { processCreateActivity } from './process-create' -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' async function processAnnounceActivity (activity: ActivityAnnounce) { const announcedActivity = activity.object - const accountAnnouncer = await getOrCreateAccount(activity.actor) + const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { // 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/ import { logger, retryTransactionWrapper } from '../../../helpers' import { database as db } from '../../../initializers' import { AccountInstance } from '../../../models/account/account-interface' -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' import { getVideoChannelActivityPubUrl } from '../url' import { videoChannelActivityObjectToDBAttributes } from './misc' async function processCreateActivity (activity: ActivityCreate) { const activityObject = activity.object const activityType = activityObject.type - const account = await getOrCreateAccount(activity.actor) + const account = await getOrCreateAccountAndServer(activity.actor) if (activityType === 'VideoChannel') { 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' import { AccountInstance } from '../../../models/account/account-interface' import { VideoChannelInstance } from '../../../models/video/video-channel-interface' import { VideoInstance } from '../../../models/video/video-interface' -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' async function processDeleteActivity (activity: ActivityDelete) { - const account = await getOrCreateAccount(activity.actor) + const account = await getOrCreateAccountAndServer(activity.actor) if (account.url === activity.id) { 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' import { AccountInstance } from '../../../models/account/account-interface' import { logger } from '../../../helpers/logger' import { sendAccept } from '../send/send-accept' -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' async function processFollowActivity (activity: ActivityFollow) { const activityObject = activity.object - const account = await getOrCreateAccount(activity.actor) + const account = await getOrCreateAccountAndServer(activity.actor) return processFollow(account, activityObject) } 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' import { VideoInstance } from '../../../models/video/video-interface' import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' import Bluebird = require('bluebird') -import { getOrCreateAccount } from '../account' +import { getOrCreateAccountAndServer } from '../account' async function processUpdateActivity (activity: ActivityUpdate) { - const account = await getOrCreateAccount(activity.actor) + const account = await getOrCreateAccountAndServer(activity.actor) if (activity.object.type === 'Video') { return processUpdateVideo(account, activity.object) -- cgit v1.2.3