Browse Source

Handle duplicate ActivityPub activities (#4639)

* Handle duplicate ActivityPub activities

Only perform side-effects when record processed for the first time

* Fast-forward repeat follow requests
Eugen Rochko 2 years ago
parent
commit
fe5b66aa08

+ 4 - 0
app/lib/activitypub/activity/announce.rb

@@ -7,6 +7,10 @@ class ActivityPub::Activity::Announce < ActivityPub::Activity
 
     return if original_status.nil? || delete_arrived_first?(@json['id'])
 
+    status = Status.find_by(account: @account, reblog: original_status)
+
+    return status unless status.nil?
+
     status = Status.create!(account: @account, reblog: original_status, uri: @json['id'])
     distribute(status)
     status

+ 1 - 1
app/lib/activitypub/activity/block.rb

@@ -4,7 +4,7 @@ class ActivityPub::Activity::Block < ActivityPub::Activity
   def perform
     target_account = account_from_uri(object_uri)
 
-    return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id'])
+    return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id']) || @account.blocking?(target_account)
 
     UnfollowService.new.call(target_account, @account) if target_account.following?(@account)
     @account.block!(target_account)

+ 7 - 1
app/lib/activitypub/activity/follow.rb

@@ -4,7 +4,13 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity
   def perform
     target_account = account_from_uri(object_uri)
 
-    return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id'])
+    return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id']) || @account.requested?(target_account)
+
+    # Fast-forward repeat follow requests
+    if @account.following?(target_account)
+      AuthorizeFollowService.new.call(@account, target_account, skip_follow_request: true)
+      return
+    end
 
     follow_request = FollowRequest.create!(account: @account, target_account: target_account)
 

+ 2 - 2
app/lib/activitypub/activity/like.rb

@@ -4,9 +4,9 @@ class ActivityPub::Activity::Like < ActivityPub::Activity
   def perform
     original_status = status_from_uri(object_uri)
 
-    return if original_status.nil? || !original_status.account.local? || delete_arrived_first?(@json['id'])
+    return if original_status.nil? || !original_status.account.local? || delete_arrived_first?(@json['id']) || @account.favourited?(original_status)
 
-    favourite = original_status.favourites.where(account: @account).first_or_create!(account: @account)
+    favourite = original_status.favourites.create!(account: @account)
     NotifyService.new.call(original_status.account, favourite)
   end
 end

+ 8 - 3
app/services/authorize_follow_service.rb

@@ -1,9 +1,14 @@
 # frozen_string_literal: true
 
 class AuthorizeFollowService < BaseService
-  def call(source_account, target_account)
-    follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
-    follow_request.authorize!
+  def call(source_account, target_account, options = {})
+    if options[:skip_follow_request]
+      follow_request = FollowRequest.new(account: source_account, target_account: target_account)
+    else
+      follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
+      follow_request.authorize!
+    end
+
     create_notification(follow_request) unless source_account.local?
     follow_request
   end