From 3319473b2c493ea422bd6b68b3d08adb8b5acfac Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Wed, 5 Oct 2016 13:50:21 +0200
Subject: [PATCH] Move PubSubHubbub pinging to a background worker It can take
 as much as 0.5s if not longer to complete

---
 app/services/favourite_service.rb   | 2 +-
 app/services/follow_service.rb      | 2 +-
 app/services/post_status_service.rb | 2 +-
 app/services/reblog_service.rb      | 2 +-
 app/workers/hub_ping_worker.rb      | 9 +++++++++
 5 files changed, 13 insertions(+), 4 deletions(-)
 create mode 100644 app/workers/hub_ping_worker.rb

diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb
index 98f08d32b8..ab7f8aea12 100644
--- a/app/services/favourite_service.rb
+++ b/app/services/favourite_service.rb
@@ -5,7 +5,7 @@ class FavouriteService < BaseService
   # @return [Favourite]
   def call(account, status)
     favourite = Favourite.create!(account: account, status: status)
-    account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url])
+    HubPingWorker.perform_async(account.id)
 
     if status.local?
       NotificationMailer.favourite(status, account).deliver_later unless status.account.blocking?(account)
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index f44d533980..4caf55078e 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -17,7 +17,7 @@ class FollowService < BaseService
     end
 
     merge_into_timeline(target_account, source_account)
-    source_account.ping!(account_url(source_account, format: 'atom'), [Rails.configuration.x.hub_url])
+    HubPingWorker.perform_async(source_account.id)
     follow
   end
 
diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb
index 6e53c7e526..5cac6b70aa 100644
--- a/app/services/post_status_service.rb
+++ b/app/services/post_status_service.rb
@@ -10,7 +10,7 @@ class PostStatusService < BaseService
     attach_media(status, media_ids)
     process_mentions_service.call(status)
     DistributionWorker.perform_async(status.id)
-    account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url])
+    HubPingWorker.perform_async(account.id)
     status
   end
 
diff --git a/app/services/reblog_service.rb b/app/services/reblog_service.rb
index 56c59cb18e..627d4e0e81 100644
--- a/app/services/reblog_service.rb
+++ b/app/services/reblog_service.rb
@@ -6,7 +6,7 @@ class ReblogService < BaseService
   def call(account, reblogged_status)
     reblog = account.statuses.create!(reblog: reblogged_status, text: '')
     DistributionWorker.perform_async(reblog.id)
-    account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url])
+    HubPingWorker.perform_async(account.id)
 
     if reblogged_status.local?
       NotificationMailer.reblog(reblogged_status, account).deliver_later unless reblogged_status.account.blocking?(account)
diff --git a/app/workers/hub_ping_worker.rb b/app/workers/hub_ping_worker.rb
new file mode 100644
index 0000000000..57d716c97f
--- /dev/null
+++ b/app/workers/hub_ping_worker.rb
@@ -0,0 +1,9 @@
+class HubPingWorker
+  include Sidekiq::Worker
+  include RoutingHelper
+
+  def perform(account_id)
+    account = Account.find(account_id)
+    account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url])
+  end
+end