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.

136 lines
3.8 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. old_public_key = @account.public_key
  17. update_account
  18. RefollowWorker.perform_async(@account.id) if old_public_key != @account.public_key
  19. @account
  20. rescue Oj::ParseError
  21. nil
  22. end
  23. private
  24. def create_account
  25. @account = Account.new
  26. @account.protocol = :activitypub
  27. @account.username = @username
  28. @account.domain = @domain
  29. @account.uri = @uri
  30. @account.suspended = true if auto_suspend?
  31. @account.silenced = true if auto_silence?
  32. @account.private_key = nil
  33. @account.save!
  34. end
  35. def update_account
  36. @account.last_webfingered_at = Time.now.utc
  37. @account.protocol = :activitypub
  38. @account.inbox_url = @json['inbox'] || ''
  39. @account.outbox_url = @json['outbox'] || ''
  40. @account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
  41. @account.followers_url = @json['followers'] || ''
  42. @account.url = url || @uri
  43. @account.display_name = @json['name'] || ''
  44. @account.note = @json['summary'] || ''
  45. @account.avatar_remote_url = image_url('icon') unless skip_download?
  46. @account.header_remote_url = image_url('image') unless skip_download?
  47. @account.public_key = public_key || ''
  48. @account.locked = @json['manuallyApprovesFollowers'] || false
  49. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  50. @account.following_count = following_total_items if following_total_items.present?
  51. @account.followers_count = followers_total_items if followers_total_items.present?
  52. @account.save_with_optional_media!
  53. end
  54. def upgrade_account
  55. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  56. end
  57. def image_url(key)
  58. value = first_of_value(@json[key])
  59. return if value.nil?
  60. return value['url'] if value.is_a?(Hash)
  61. image = fetch_resource(value)
  62. image['url'] if image
  63. end
  64. def public_key
  65. value = first_of_value(@json['publicKey'])
  66. return if value.nil?
  67. return value['publicKeyPem'] if value.is_a?(Hash)
  68. key = fetch_resource(value)
  69. key['publicKeyPem'] if key
  70. end
  71. def url
  72. return if @json['url'].blank?
  73. value = first_of_value(@json['url'])
  74. return value if value.is_a?(String)
  75. value['href']
  76. end
  77. def outbox_total_items
  78. collection_total_items('outbox')
  79. end
  80. def following_total_items
  81. collection_total_items('following')
  82. end
  83. def followers_total_items
  84. collection_total_items('followers')
  85. end
  86. def collection_total_items(type)
  87. return if @json[type].blank?
  88. return @collections[type] if @collections.key?(type)
  89. collection = fetch_resource(@json[type])
  90. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  91. rescue HTTP::Error, OpenSSL::SSL::SSLError
  92. @collections[type] = nil
  93. end
  94. def skip_download?
  95. @account.suspended? || domain_block&.reject_media?
  96. end
  97. def auto_suspend?
  98. domain_block && domain_block.suspend?
  99. end
  100. def auto_silence?
  101. domain_block && domain_block.silence?
  102. end
  103. def domain_block
  104. return @domain_block if defined?(@domain_block)
  105. @domain_block = DomainBlock.find_by(domain: @domain)
  106. end
  107. end