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.

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