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.

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