X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Factivitypub-follow.ts;h=2ee98171cfd98e96066186b7a9d3ee1baddce055;hb=7630e1c893f9848dce9d94135ce9b9a21ab80788;hp=286e343f2b4736a83f8c73c0b1d7d7e5fa97f2a9;hpb=90d4bb8125e80c8060416d4d135ddeaf0a622ede;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 286e343f2..2ee98171c 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -1,33 +1,41 @@ -import * as kue from 'kue' -import { logger } from '../../../helpers/logger' -import { getServerActor } from '../../../helpers/utils' -import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers' -import { sendFollow } from '../../activitypub/send' +import { Job } from 'bull' +import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url' +import { ActivitypubFollowPayload } from '@shared/models' import { sanitizeHost } from '../../../helpers/core-utils' -import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger' -import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor' import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { ActorFollowModel } from '../../../models/activitypub/actor-follow' -import { ActorModel } from '../../../models/activitypub/actor' - -export type ActivitypubFollowPayload = { - host: string -} +import { logger } from '../../../helpers/logger' +import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants' +import { sequelizeTypescript } from '../../../initializers/database' +import { ActorModel } from '../../../models/actor/actor' +import { ActorFollowModel } from '../../../models/actor/actor-follow' +import { MActor, MActorFull } from '../../../types/models' +import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../activitypub/actors' +import { sendFollow } from '../../activitypub/send' +import { Notifier } from '../../notifier' -async function processActivityPubFollow (job: kue.Job) { +async function processActivityPubFollow (job: 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) + let targetActor: MActorFull + if (!host || host === 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 getOrCreateAPActor(actorUrl, 'all') + } - const actorUrl = await loadActorUrlOrGetFromWebfinger(SERVER_ACTOR_NAME, sanitizedHost) - const targetActor = await getOrCreateActorAndServerAndModel(actorUrl) + if (payload.assertIsChannel && !targetActor.VideoChannel) { + logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host) + return + } - const fromActor = await getServerActor() + const fromActor = await ActorModel.load(payload.followerActorId) - return retryTransactionWrapper(follow, fromActor, targetActor) + return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow) } // --------------------------------------------------------------------------- @@ -37,28 +45,38 @@ export { // --------------------------------------------------------------------------- -function follow (fromActor: ActorModel, targetActor: ActorModel) { +async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) { if (fromActor.id === targetActor.id) { - throw new Error('Follower is the same than target actor.') + throw new Error('Follower is the same as target actor.') } - return sequelizeTypescript.transaction(async t => { - const [ actorFollow ] = await ActorFollowModel.findOrCreate({ - where: { - actorId: fromActor.id, - targetActorId: targetActor.id - }, - defaults: { - state: 'pending', - actorId: fromActor.id, - targetActorId: targetActor.id - }, + // Same server, direct accept + const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending' + + const actorFollow = await sequelizeTypescript.transaction(async t => { + const [ actorFollow ] = await ActorFollowModel.findOrCreateCustom({ + byActor: fromActor, + state, + targetActor, + activityId: getLocalActorFollowActivityPubUrl(fromActor, targetActor), transaction: t }) - 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') sendFollow(actorFollow, t) + + return actorFollow }) + + const followerFull = await ActorModel.loadFull(fromActor.id) + + const actorFollowFull = Object.assign(actorFollow, { + ActorFollowing: targetActor, + ActorFollower: followerFull + }) + + if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) + if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull) + + return actorFollow }