You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::ProcessAccountService < BaseService
  3. include JsonLdHelper
  4. # Should be called with confirmed valid JSON
  5. # and WebFinger-resolved username and domain
  6. def call(username, domain, json)
  7. return if json['inbox'].blank?
  8. @json = json
  9. @uri = @json['id']
  10. @username = username
  11. @domain = domain
  12. @account = Account.find_by(uri: @uri)
  13. @collections = {}
  14. create_account if @account.nil?
  15. upgrade_account if @account.ostatus?
  16. update_account
  17. @account
  18. rescue Oj::ParseError
  19. nil
  20. end
  21. private
  22. def create_account
  23. @account = Account.new
  24. @account.protocol = :activitypub
  25. @account.username = @username
  26. @account.domain = @domain
  27. @account.uri = @uri
  28. @account.suspended = true if auto_suspend?
  29. @account.silenced = true if auto_silence?
  30. @account.private_key = nil
  31. @account.save!
  32. end
  33. def update_account
  34. @account.last_webfingered_at = Time.now.utc
  35. @account.protocol = :activitypub
  36. @account.inbox_url = @json['inbox'] || ''
  37. @account.outbox_url = @json['outbox'] || ''
  38. @account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
  39. @account.followers_url = @json['followers'] || ''
  40. @account.url = url || @uri
  41. @account.display_name = @json['name'] || ''
  42. @account.note = @json['summary'] || ''
  43. @account.avatar_remote_url = image_url('icon')
  44. @account.header_remote_url = image_url('image')
  45. @account.public_key = public_key || ''
  46. @account.locked = @json['manuallyApprovesFollowers'] || false
  47. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  48. @account.following_count = following_total_items if following_total_items.present?
  49. @account.followers_count = followers_total_items if followers_total_items.present?
  50. @account.save!
  51. end
  52. def upgrade_account
  53. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  54. end
  55. def image_url(key)
  56. value = first_of_value(@json[key])
  57. return if value.nil?
  58. return value['url'] if value.is_a?(Hash)
  59. image = fetch_resource(value)
  60. image['url'] if image
  61. end
  62. def public_key
  63. value = first_of_value(@json['publicKey'])
  64. return if value.nil?
  65. return value['publicKeyPem'] if value.is_a?(Hash)
  66. key = fetch_resource(value)
  67. key['publicKeyPem'] if key
  68. end
  69. def url
  70. return if @json['url'].blank?
  71. value = first_of_value(@json['url'])
  72. return value if value.is_a?(String)
  73. value['href']
  74. end
  75. def outbox_total_items
  76. collection_total_items('outbox')
  77. end
  78. def following_total_items
  79. collection_total_items('following')
  80. end
  81. def followers_total_items
  82. collection_total_items('followers')
  83. end
  84. def collection_total_items(type)
  85. return if @json[type].blank?
  86. return @collections[type] if @collections.key?(type)
  87. collection = fetch_resource(@json[type])
  88. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  89. rescue HTTP::Error, OpenSSL::SSL::SSLError
  90. @collections[type] = nil
  91. end
  92. def auto_suspend?
  93. domain_block && domain_block.suspend?
  94. end
  95. def auto_silence?
  96. domain_block && domain_block.silence?
  97. end
  98. def domain_block
  99. return @domain_block if defined?(@domain_block)
  100. @domain_block = DomainBlock.find_by(domain: @domain)
  101. end
  102. end