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.

190 lines
5.3 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? || unsupported_uri_scheme?(json['id'])
  8. @json = json
  9. @uri = @json['id']
  10. @username = username
  11. @domain = domain
  12. @collections = {}
  13. RedisLock.acquire(lock_options) do |lock|
  14. if lock.acquired?
  15. @account = Account.find_remote(@username, @domain)
  16. @old_public_key = @account&.public_key
  17. @old_protocol = @account&.protocol
  18. create_account if @account.nil?
  19. update_account
  20. end
  21. end
  22. after_protocol_change! if protocol_changed?
  23. after_key_change! if key_changed?
  24. check_featured_collection! if @account.featured_collection_url.present?
  25. @account
  26. rescue Oj::ParseError
  27. nil
  28. end
  29. private
  30. def create_account
  31. @account = Account.new
  32. @account.protocol = :activitypub
  33. @account.username = @username
  34. @account.domain = @domain
  35. @account.uri = @uri
  36. @account.suspended = true if auto_suspend?
  37. @account.silenced = true if auto_silence?
  38. @account.private_key = nil
  39. end
  40. def update_account
  41. @account.last_webfingered_at = Time.now.utc
  42. @account.protocol = :activitypub
  43. set_immediate_attributes!
  44. set_fetchable_attributes!
  45. @account.save_with_optional_media!
  46. end
  47. def set_immediate_attributes!
  48. @account.inbox_url = @json['inbox'] || ''
  49. @account.outbox_url = @json['outbox'] || ''
  50. @account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
  51. @account.followers_url = @json['followers'] || ''
  52. @account.featured_collection_url = @json['featured'] || ''
  53. @account.url = url || @uri
  54. @account.display_name = @json['name'] || ''
  55. @account.note = @json['summary'] || ''
  56. @account.locked = @json['manuallyApprovesFollowers'] || false
  57. end
  58. def set_fetchable_attributes!
  59. @account.avatar_remote_url = image_url('icon') unless skip_download?
  60. @account.header_remote_url = image_url('image') unless skip_download?
  61. @account.public_key = public_key || ''
  62. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  63. @account.following_count = following_total_items if following_total_items.present?
  64. @account.followers_count = followers_total_items if followers_total_items.present?
  65. @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
  66. end
  67. def after_protocol_change!
  68. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  69. end
  70. def after_key_change!
  71. RefollowWorker.perform_async(@account.id)
  72. end
  73. def check_featured_collection!
  74. ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
  75. end
  76. def image_url(key)
  77. value = first_of_value(@json[key])
  78. return if value.nil?
  79. return value['url'] if value.is_a?(Hash)
  80. image = fetch_resource_without_id_validation(value)
  81. image['url'] if image
  82. end
  83. def public_key
  84. value = first_of_value(@json['publicKey'])
  85. return if value.nil?
  86. return value['publicKeyPem'] if value.is_a?(Hash)
  87. key = fetch_resource_without_id_validation(value)
  88. key['publicKeyPem'] if key
  89. end
  90. def url
  91. return if @json['url'].blank?
  92. url_candidate = url_to_href(@json['url'], 'text/html')
  93. if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
  94. nil
  95. else
  96. url_candidate
  97. end
  98. end
  99. def mismatching_origin?(url)
  100. needle = Addressable::URI.parse(url).host
  101. haystack = Addressable::URI.parse(@uri).host
  102. !haystack.casecmp(needle).zero?
  103. end
  104. def outbox_total_items
  105. collection_total_items('outbox')
  106. end
  107. def following_total_items
  108. collection_total_items('following')
  109. end
  110. def followers_total_items
  111. collection_total_items('followers')
  112. end
  113. def collection_total_items(type)
  114. return if @json[type].blank?
  115. return @collections[type] if @collections.key?(type)
  116. collection = fetch_resource_without_id_validation(@json[type])
  117. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  118. rescue HTTP::Error, OpenSSL::SSL::SSLError
  119. @collections[type] = nil
  120. end
  121. def moved_account
  122. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  123. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true)
  124. account
  125. end
  126. def skip_download?
  127. @account.suspended? || domain_block&.reject_media?
  128. end
  129. def auto_suspend?
  130. domain_block&.suspend?
  131. end
  132. def auto_silence?
  133. domain_block&.silence?
  134. end
  135. def domain_block
  136. return @domain_block if defined?(@domain_block)
  137. @domain_block = DomainBlock.find_by(domain: @domain)
  138. end
  139. def key_changed?
  140. !@old_public_key.nil? && @old_public_key != @account.public_key
  141. end
  142. def protocol_changed?
  143. !@old_protocol.nil? && @old_protocol != @account.protocol
  144. end
  145. def lock_options
  146. { redis: Redis.current, key: "process_account:#{@uri}" }
  147. end
  148. end