From e1a570abff3ebf375433e58e7362d56bd32d4cd8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Oct 2021 10:52:15 +0200 Subject: Fix user subscription follows count --- server/lib/activitypub/process/process-follow.ts | 26 +++++--------- .../lib/job-queue/handlers/activitypub-follow.ts | 18 +++------- server/models/actor/actor-follow.ts | 41 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 30 deletions(-) (limited to 'server') diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 5562f0798..e44590ffc 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -48,23 +48,15 @@ async function processFollow (byActor: MActorSignature, activityId: string, targ return { actorFollow: undefined as MActorFollowActors } } - // Don't use findOrCreate by sequelize that breaks our actor follow hooks - let created = false - let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, t) - - if (!actorFollow) { - created = true - - actorFollow = await ActorFollowModel.create({ - actorId: byActor.id, - targetActorId: targetActor.id, - url: activityId, - - state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL - ? 'pending' - : 'accepted' - }, { transaction: t }) - } + const [ actorFollow, created ] = await ActorFollowModel.findOrCreateCustom({ + byActor, + targetActor, + activityId, + state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL + ? 'pending' + : 'accepted', + transaction: t + }) // Set the follow as accepted if the remote actor follows a channel or account // Or if the instance automatically accepts followers diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index 91e3d33c6..55a15930a 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -54,21 +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, - url: getLocalActorFollowActivityPubUrl(fromActor, targetActor), - 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) diff --git a/server/models/actor/actor-follow.ts b/server/models/actor/actor-follow.ts index 95bb2df56..c522a7c05 100644 --- a/server/models/actor/actor-follow.ts +++ b/server/models/actor/actor-follow.ts @@ -20,8 +20,11 @@ import { } from 'sequelize-typescript' import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' import { afterCommitIfTransaction } from '@server/helpers/database-utils' +import { CONFIG } from '@server/initializers/config' import { getServerActor } from '@server/models/application/application' import { + MActor, + MActorFollowActors, MActorFollowActorsDefault, MActorFollowActorsDefaultSubscription, MActorFollowFollowingHost, @@ -137,6 +140,44 @@ export class ActorFollowModel extends Model { + const { byActor, targetActor, activityId, state, transaction } = options + + let created = false + let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, transaction) + + if (!actorFollow) { + created = true + + actorFollow = await ActorFollowModel.create({ + actorId: byActor.id, + targetActorId: targetActor.id, + url: activityId, + + state + }, { transaction }) + + actorFollow.ActorFollowing = targetActor + actorFollow.ActorFollower = byActor + } + + return [ actorFollow, created ] + } + static removeFollowsOf (actorId: number, t?: Transaction) { const query = { where: { -- cgit v1.2.3