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.

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