process_account_service.rb 6.8 KB

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