From 059ebbf48dc56971b88e26a15303a75643de8b98 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 20 Sep 2016 00:39:03 +0200 Subject: Separate PuSH subscriptions from following, add mastodon:push:refresh task, respect hub.lease_seconds (fix #46) --- app/services/follow_remote_account_service.rb | 29 +++++---------------------- app/services/follow_service.rb | 5 +++++ app/services/process_feed_service.rb | 8 ++++++-- app/services/process_interaction_service.rb | 8 ++++++-- app/services/subscribe_service.rb | 20 ++++++++++++++++++ 5 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 app/services/subscribe_service.rb (limited to 'app/services') diff --git a/app/services/follow_remote_account_service.rb b/app/services/follow_remote_account_service.rb index 00285f47a..f3a384ecc 100644 --- a/app/services/follow_remote_account_service.rb +++ b/app/services/follow_remote_account_service.rb @@ -3,22 +3,18 @@ class FollowRemoteAccountService < BaseService # When creating, look up the user's webfinger and fetch all # important information from their feed # @param [String] uri User URI in the form of username@domain - # @param [Boolean] subscribe Whether to initiate a PubSubHubbub subscription # @return [Account] - def call(uri, subscribe = true) + def call(uri) username, domain = uri.split('@') return Account.find_local(username) if domain == Rails.configuration.x.local_domain || domain.nil? account = Account.find_remote(username, domain) - if account.nil? - Rails.logger.debug "Creating new remote account for #{uri}" - account = Account.new(username: username, domain: domain) - elsif account.subscribed? - Rails.logger.debug "Already subscribed to remote account #{uri}" - return account - end + return account unless account.nil? + + Rails.logger.debug "Creating new remote account for #{uri}" + account = Account.new(username: username, domain: domain) data = Goldfinger.finger("acct:#{uri}") @@ -45,16 +41,6 @@ class FollowRemoteAccountService < BaseService get_profile(feed, account) account.save! - if subscribe - account.secret = SecureRandom.hex - account.verify_token = SecureRandom.hex - - subscription = account.subscription(api_subscription_url(account.id)) - subscription.subscribe - - account.save! - end - return account end @@ -90,8 +76,3 @@ class FollowRemoteAccountService < BaseService end end -class NoAuthorFeedError < StandardError -end - -class NoHubError < StandardError -end diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index 45f145df3..fb782e871 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -12,6 +12,7 @@ class FollowService < BaseService if target_account.local? NotificationMailer.follow(target_account, source_account).deliver_later else + subscribe_service.(target_account) NotificationWorker.perform_async(follow.stream_entry.id, target_account.id) end @@ -40,4 +41,8 @@ class FollowService < BaseService def follow_remote_account_service @follow_remote_account_service ||= FollowRemoteAccountService.new end + + def subscribe_service + @subscribe_service ||= SubscribeService.new + end end diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index 5fde19547..a69205494 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -106,7 +106,7 @@ class ProcessFeedService < BaseService end def delete_post!(status) - RemoveStatusService.new.(status) + remove_status_service.(status) end def find_original_status(_xml, id) @@ -126,7 +126,7 @@ class ProcessFeedService < BaseService account = Account.find_by(username: username, domain: domain) if account.nil? - account = follow_remote_account_service.("#{username}@#{domain}", false) + account = follow_remote_account_service.("#{username}@#{domain}") end status = Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml), created_at: published(xml), updated_at: updated(xml)) @@ -196,4 +196,8 @@ class ProcessFeedService < BaseService def update_remote_profile_service @update_remote_profile_service ||= UpdateRemoteProfileService.new end + + def remove_status_service + @remove_status_service ||= RemoveStatusService.new + end end diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb index 3266f73e3..1e3ed6b12 100644 --- a/app/services/process_interaction_service.rb +++ b/app/services/process_interaction_service.rb @@ -14,7 +14,7 @@ class ProcessInteractionService < BaseService account = Account.find_by(username: username, domain: domain) if account.nil? - account = follow_remote_account_service.("#{username}@#{domain}", false) + account = follow_remote_account_service.("#{username}@#{domain}") end if salmon.verify(envelope, account.keypair) @@ -71,7 +71,7 @@ class ProcessInteractionService < BaseService return if status.nil? if account.id == status.account_id - RemoveStatusService.new.(status) + remove_status_service.(status) end end @@ -108,4 +108,8 @@ class ProcessInteractionService < BaseService def update_remote_profile_service @update_remote_profile_service ||= UpdateRemoteProfileService.new end + + def remove_status_service + @remove_status_service ||= RemoveStatusService.new + end end diff --git a/app/services/subscribe_service.rb b/app/services/subscribe_service.rb new file mode 100644 index 000000000..7ead559d5 --- /dev/null +++ b/app/services/subscribe_service.rb @@ -0,0 +1,20 @@ +class SubscribeService < BaseService + def call(account) + account.secret = SecureRandom.hex + account.verify_token = SecureRandom.hex + + subscription = account.subscription(api_subscription_url(account.id)) + response = subscription.subscribe + + unless response.successful? + account.secret = '' + account.verify_token = '' + + Rails.logger.debug "PuSH subscription request for #{account.acct} failed: #{response.message}" + end + + account.save! + rescue HTTP::Error, OpenSSL::SSL::SSLError + Rails.logger.debug "PuSH subscription request for #{account.acct} could not be made due to HTTP or SSL error" + end +end -- cgit