]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-follow.ts
Fix ownership change
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
index ed16ba172688d5f11d1d3aeade1439706b766f94..bc5660395ae92a78df35544e50fb0b4b7b0394bf 100644 (file)
@@ -9,8 +9,11 @@ import { Notifier } from '../../notifier'
 import { getAPId } from '../../../helpers/activitypub'
 import { getServerActor } from '../../../helpers/utils'
 import { CONFIG } from '../../../initializers/config'
+import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
+import { MAccount, MActorFollowActors, MActorFollowFull, MActorSignature } from '../../../typings/models'
 
-async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
+async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
+  const { activity, byActor } = options
   const activityObject = getAPId(activity.object)
 
   return retryTransactionWrapper(processFollow, byActor, activityObject)
@@ -24,7 +27,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processFollow (actor: ActorModel, targetActorURL: string) {
+async function processFollow (byActor: MActorSignature, targetActorURL: string) {
   const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
     const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
 
@@ -37,18 +40,18 @@ async function processFollow (actor: ActorModel, targetActorURL: string) {
     if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
       logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
 
-      await sendReject(actor, targetActor)
+      await sendReject(byActor, targetActor)
 
-      return { actorFollow: undefined }
+      return { actorFollow: undefined as MActorFollowActors }
     }
 
-    const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
+    const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
       where: {
-        actorId: actor.id,
+        actorId: byActor.id,
         targetActorId: targetActor.id
       },
       defaults: {
-        actorId: actor.id,
+        actorId: byActor.id,
         targetActorId: targetActor.id,
         state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted'
       },
@@ -60,7 +63,7 @@ async function processFollow (actor: ActorModel, targetActorURL: string) {
       await actorFollow.save({ transaction: t })
     }
 
-    actorFollow.ActorFollower = actor
+    actorFollow.ActorFollower = byActor
     actorFollow.ActorFollowing = targetActor
 
     // Target sends to actor he accepted the follow request
@@ -73,9 +76,15 @@ async function processFollow (actor: ActorModel, targetActorURL: string) {
   if (!actorFollow) return
 
   if (created) {
-    if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
-    else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
+    if (isFollowingInstance) {
+      Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
+    } else {
+      const actorFollowFull = actorFollow as MActorFollowFull
+      actorFollowFull.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as MAccount
+
+      Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
+    }
   }
 
-  logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
+  logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url)
 }