X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fjob-queue%2Fhandlers%2Factivitypub-follow.ts;h=b4d381062564d5ba4b59178cedb26d8ad670f301;hb=7ccddd7b5250bd25a917a6e77e58b87b9484a2a4;hp=6764a403710c62137439ce1f082e433dacc0f7af;hpb=5350fd8e5b2b2d017b16d97828893a8a4a40bd89;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index 6764a4037..b4d381062 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -1,7 +1,6 @@ -import * as kue from 'kue' +import * as Bull from 'bull' import { logger } from '../../../helpers/logger' -import { getServerActor } from '../../../helpers/utils' -import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers' +import { CONFIG, REMOTE_SCHEME, sequelizeTypescript } from '../../../initializers' import { sendFollow } from '../../activitypub/send' import { sanitizeHost } from '../../../helpers/core-utils' import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger' @@ -9,29 +8,32 @@ import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor' import { retryTransactionWrapper } from '../../../helpers/database-utils' import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { ActorModel } from '../../../models/activitypub/actor' +import { Notifier } from '../../notifier' export type ActivitypubFollowPayload = { + followerActorId: number + name: string host: string } -async function processActivityPubFollow (job: kue.Job) { +async function processActivityPubFollow (job: Bull.Job) { const payload = job.data as ActivitypubFollowPayload const host = payload.host logger.info('Processing ActivityPub follow in job %d.', job.id) - const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) - - const actorUrl = await loadActorUrlOrGetFromWebfinger(SERVER_ACTOR_NAME, sanitizedHost) - const targetActor = await getOrCreateActorAndServerAndModel(actorUrl) - - const fromActor = await getServerActor() - const options = { - arguments: [ fromActor, targetActor ], - errorMessage: 'Cannot follow with many retries.' + let targetActor: ActorModel + if (!host || host === CONFIG.WEBSERVER.HOST) { + targetActor = await ActorModel.loadLocalByName(payload.name) + } else { + const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) + const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) + targetActor = await getOrCreateActorAndServerAndModel(actorUrl) } - return retryTransactionWrapper(follow, options) + const fromActor = await ActorModel.load(payload.followerActorId) + + return retryTransactionWrapper(follow, fromActor, targetActor) } // --------------------------------------------------------------------------- @@ -41,19 +43,22 @@ export { // --------------------------------------------------------------------------- -function follow (fromActor: ActorModel, targetActor: ActorModel) { +async function follow (fromActor: ActorModel, targetActor: ActorModel) { if (fromActor.id === targetActor.id) { throw new Error('Follower is the same than target actor.') } - return sequelizeTypescript.transaction(async t => { + // Same server, direct accept + const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending' + + const actorFollow = await sequelizeTypescript.transaction(async t => { const [ actorFollow ] = await ActorFollowModel.findOrCreate({ where: { actorId: fromActor.id, targetActorId: targetActor.id }, defaults: { - state: 'pending', + state, actorId: fromActor.id, targetActorId: targetActor.id }, @@ -62,7 +67,11 @@ function follow (fromActor: ActorModel, targetActor: ActorModel) { actorFollow.ActorFollowing = targetActor actorFollow.ActorFollower = fromActor - // Send a notification to remote server - await sendFollow(actorFollow) + // Send a notification to remote server if our follow is not already accepted + if (actorFollow.state !== 'accepted') await sendFollow(actorFollow) + + return actorFollow }) + + if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewFollow(actorFollow) }