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.

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