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.

233 lines
6.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? || 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. process_tags
  21. end
  22. end
  23. return if @account.nil?
  24. after_protocol_change! if protocol_changed?
  25. after_key_change! if key_changed?
  26. check_featured_collection! if @account.featured_collection_url.present?
  27. @account
  28. rescue Oj::ParseError
  29. nil
  30. end
  31. private
  32. def create_account
  33. @account = Account.new
  34. @account.protocol = :activitypub
  35. @account.username = @username
  36. @account.domain = @domain
  37. @account.uri = @uri
  38. @account.suspended = true if auto_suspend?
  39. @account.silenced = true if auto_silence?
  40. @account.private_key = nil
  41. end
  42. def update_account
  43. @account.last_webfingered_at = Time.now.utc
  44. @account.protocol = :activitypub
  45. set_immediate_attributes!
  46. set_fetchable_attributes!
  47. @account.save_with_optional_media!
  48. end
  49. def set_immediate_attributes!
  50. @account.inbox_url = @json['inbox'] || ''
  51. @account.outbox_url = @json['outbox'] || ''
  52. @account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
  53. @account.followers_url = @json['followers'] || ''
  54. @account.featured_collection_url = @json['featured'] || ''
  55. @account.url = url || @uri
  56. @account.display_name = @json['name'] || ''
  57. @account.note = @json['summary'] || ''
  58. @account.locked = @json['manuallyApprovesFollowers'] || false
  59. @account.fields = property_values || {}
  60. @account.actor_type = actor_type
  61. end
  62. def set_fetchable_attributes!
  63. @account.avatar_remote_url = image_url('icon') unless skip_download?
  64. @account.header_remote_url = image_url('image') unless skip_download?
  65. @account.public_key = public_key || ''
  66. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  67. @account.following_count = following_total_items if following_total_items.present?
  68. @account.followers_count = followers_total_items if followers_total_items.present?
  69. @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
  70. end
  71. def after_protocol_change!
  72. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  73. end
  74. def after_key_change!
  75. RefollowWorker.perform_async(@account.id)
  76. end
  77. def check_featured_collection!
  78. ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
  79. end
  80. def actor_type
  81. if @json['type'].is_a?(Array)
  82. @json['type'].find { |type| ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES.include?(type) }
  83. else
  84. @json['type']
  85. end
  86. end
  87. def image_url(key)
  88. value = first_of_value(@json[key])
  89. return if value.nil?
  90. return value['url'] if value.is_a?(Hash)
  91. image = fetch_resource_without_id_validation(value)
  92. image['url'] if image
  93. end
  94. def public_key
  95. value = first_of_value(@json['publicKey'])
  96. return if value.nil?
  97. return value['publicKeyPem'] if value.is_a?(Hash)
  98. key = fetch_resource_without_id_validation(value)
  99. key['publicKeyPem'] if key
  100. end
  101. def url
  102. return if @json['url'].blank?
  103. url_candidate = url_to_href(@json['url'], 'text/html')
  104. if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
  105. nil
  106. else
  107. url_candidate
  108. end
  109. end
  110. def property_values
  111. return unless @json['attachment'].is_a?(Array)
  112. @json['attachment'].select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
  113. end
  114. def mismatching_origin?(url)
  115. needle = Addressable::URI.parse(url).host
  116. haystack = Addressable::URI.parse(@uri).host
  117. !haystack.casecmp(needle).zero?
  118. end
  119. def outbox_total_items
  120. collection_total_items('outbox')
  121. end
  122. def following_total_items
  123. collection_total_items('following')
  124. end
  125. def followers_total_items
  126. collection_total_items('followers')
  127. end
  128. def collection_total_items(type)
  129. return if @json[type].blank?
  130. return @collections[type] if @collections.key?(type)
  131. collection = fetch_resource_without_id_validation(@json[type])
  132. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  133. rescue HTTP::Error, OpenSSL::SSL::SSLError
  134. @collections[type] = nil
  135. end
  136. def moved_account
  137. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  138. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true)
  139. account
  140. end
  141. def skip_download?
  142. @account.suspended? || domain_block&.reject_media?
  143. end
  144. def auto_suspend?
  145. domain_block&.suspend?
  146. end
  147. def auto_silence?
  148. domain_block&.silence?
  149. end
  150. def domain_block
  151. return @domain_block if defined?(@domain_block)
  152. @domain_block = DomainBlock.find_by(domain: @domain)
  153. end
  154. def key_changed?
  155. !@old_public_key.nil? && @old_public_key != @account.public_key
  156. end
  157. def protocol_changed?
  158. !@old_protocol.nil? && @old_protocol != @account.protocol
  159. end
  160. def lock_options
  161. { redis: Redis.current, key: "process_account:#{@uri}" }
  162. end
  163. def process_tags
  164. return if @json['tag'].blank?
  165. as_array(@json['tag']).each do |tag|
  166. process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
  167. end
  168. end
  169. def process_emoji(tag)
  170. return if skip_download?
  171. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  172. shortcode = tag['name'].delete(':')
  173. image_url = tag['icon']['url']
  174. uri = tag['id']
  175. updated = tag['updated']
  176. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  177. return unless emoji.nil? || emoji.updated_at >= updated
  178. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  179. emoji.image_remote_url = image_url
  180. emoji.save
  181. end
  182. end