X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Factivitypub-follow.ts;h=a68c32ba069f1de826613c8cd0e325fb0dd1f1aa;hb=405c83f9af377a663a4c8e9ad025fd5c10496922;hp=4a7cda0a2ba74e216b3d76aa1a769833f604e884;hpb=32d7f2b754b8d20bf44ae2121c79570cbff973c3;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 4a7cda0a2..a68c32ba0 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -1,29 +1,23 @@ -import * as Bull from 'bull' +import { Job } from 'bullmq' +import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url' +import { ActivitypubFollowPayload } from '@shared/models' +import { sanitizeHost } from '../../../helpers/core-utils' +import { retryTransactionWrapper } from '../../../helpers/database-utils' 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 { 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' import { Notifier } from '../../notifier' -import { sequelizeTypescript } from '../../../initializers/database' -import { MActor, MActorFollowActors, MActorFull } from '../../../typings/models' - -export type ActivitypubFollowPayload = { - followerActorId: number - name: string - host: string - isAutoFollow?: boolean -} -async function processActivityPubFollow (job: Bull.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) + logger.info('Processing ActivityPub follow in job %s.', job.id) let targetActor: MActorFull if (!host || host === WEBSERVER.HOST) { @@ -31,7 +25,12 @@ async function processActivityPubFollow (job: Bull.Job) { } else { const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP) const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost) - targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all') + targetActor = await getOrCreateAPActor(actorUrl, 'all') + } + + 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 ActorModel.load(payload.followerActorId) @@ -55,20 +54,13 @@ async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow 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, - actorId: fromActor.id, - targetActorId: targetActor.id - }, + 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 if our follow is not already accepted if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)