1
0

unsubscribe_service.rb 909 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class UnsubscribeService < BaseService
  3. def call(account)
  4. return if account.hub_url.blank?
  5. @account = account
  6. begin
  7. @response = build_request.perform
  8. Rails.logger.debug "PuSH unsubscribe for #{@account.acct} failed: #{@response.status}" unless @response.status.success?
  9. @response.connection&.close
  10. rescue HTTP::Error, OpenSSL::SSL::SSLError => e
  11. Rails.logger.debug "PuSH unsubscribe for #{@account.acct} failed: #{e}"
  12. end
  13. @account.secret = ''
  14. @account.subscription_expires_at = nil
  15. @account.save!
  16. end
  17. private
  18. def build_request
  19. Request.new(:post, @account.hub_url, form: subscription_params)
  20. end
  21. def subscription_params
  22. {
  23. 'hub.topic': @account.remote_url,
  24. 'hub.mode': 'unsubscribe',
  25. 'hub.callback': api_subscription_url(@account.id),
  26. 'hub.verify': 'async',
  27. }
  28. end
  29. end