]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-follow.ts
Fix like/dislike federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-follow.ts
index 140bbe9f1322e26fe4cf5326e2e1594810051d75..ed16ba172688d5f11d1d3aeade1439706b766f94 100644 (file)
@@ -1,13 +1,14 @@
 import { ActivityFollow } from '../../../../shared/models/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
-import { sequelizeTypescript, CONFIG } from '../../../initializers'
+import { sequelizeTypescript } from '../../../initializers'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { sendAccept, sendReject } from '../send'
 import { Notifier } from '../../notifier'
 import { getAPId } from '../../../helpers/activitypub'
 import { getServerActor } from '../../../helpers/utils'
+import { CONFIG } from '../../../initializers/config'
 
 async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) {
   const activityObject = getAPId(activity.object)
@@ -24,17 +25,21 @@ export {
 // ---------------------------------------------------------------------------
 
 async function processFollow (actor: ActorModel, targetActorURL: string) {
-  const { actorFollow, created } = await sequelizeTypescript.transaction(async t => {
+  const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => {
     const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
 
     if (!targetActor) throw new Error('Unknown actor')
     if (targetActor.isOwned() === false) throw new Error('This is not a local actor.')
 
     const serverActor = await getServerActor()
-    if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
+    const isFollowingInstance = targetActor.id === serverActor.id
+
+    if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) {
       logger.info('Rejecting %s because instance followers are disabled.', targetActor.url)
 
-      return sendReject(actor, targetActor)
+      await sendReject(actor, targetActor)
+
+      return { actorFollow: undefined }
     }
 
     const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({
@@ -50,9 +55,6 @@ async function processFollow (actor: ActorModel, targetActorURL: string) {
       transaction: t
     })
 
-    actorFollow.ActorFollower = actor
-    actorFollow.ActorFollowing = targetActor
-
     if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) {
       actorFollow.state = 'accepted'
       await actorFollow.save({ transaction: t })
@@ -64,10 +66,16 @@ async function processFollow (actor: ActorModel, targetActorURL: string) {
     // Target sends to actor he accepted the follow request
     if (actorFollow.state === 'accepted') await sendAccept(actorFollow)
 
-    return { actorFollow, created }
+    return { actorFollow, created, isFollowingInstance }
   })
 
-  if (created) Notifier.Instance.notifyOfNewFollow(actorFollow)
+  // Rejected
+  if (!actorFollow) return
+
+  if (created) {
+    if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow)
+    else Notifier.Instance.notifyOfNewUserFollow(actorFollow)
+  }
 
   logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url)
 }