process_account_service.rb 8.3 KB

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