process_account_service.rb 7.1 KB

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