闭社主体 forked from https://github.com/tootsuite/mastodon
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.

241 lines
7.0 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. 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.actor_type = actor_type
  65. end
  66. def set_fetchable_attributes!
  67. @account.avatar_remote_url = image_url('icon') unless skip_download?
  68. @account.header_remote_url = image_url('image') unless skip_download?
  69. @account.public_key = public_key || ''
  70. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  71. @account.following_count = following_total_items if following_total_items.present?
  72. @account.followers_count = followers_total_items if followers_total_items.present?
  73. @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
  74. end
  75. def after_protocol_change!
  76. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  77. end
  78. def after_key_change!
  79. RefollowWorker.perform_async(@account.id)
  80. end
  81. def check_featured_collection!
  82. ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
  83. end
  84. def check_links!
  85. VerifyAccountLinksWorker.perform_async(@account.id)
  86. end
  87. def actor_type
  88. if @json['type'].is_a?(Array)
  89. @json['type'].find { |type| ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES.include?(type) }
  90. else
  91. @json['type']
  92. end
  93. end
  94. def image_url(key)
  95. value = first_of_value(@json[key])
  96. return if value.nil?
  97. return value['url'] if value.is_a?(Hash)
  98. image = fetch_resource_without_id_validation(value)
  99. image['url'] if image
  100. end
  101. def public_key
  102. value = first_of_value(@json['publicKey'])
  103. return if value.nil?
  104. return value['publicKeyPem'] if value.is_a?(Hash)
  105. key = fetch_resource_without_id_validation(value)
  106. key['publicKeyPem'] if key
  107. end
  108. def url
  109. return if @json['url'].blank?
  110. url_candidate = url_to_href(@json['url'], 'text/html')
  111. if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
  112. nil
  113. else
  114. url_candidate
  115. end
  116. end
  117. def property_values
  118. return unless @json['attachment'].is_a?(Array)
  119. @json['attachment'].select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
  120. end
  121. def mismatching_origin?(url)
  122. needle = Addressable::URI.parse(url).host
  123. haystack = Addressable::URI.parse(@uri).host
  124. !haystack.casecmp(needle).zero?
  125. end
  126. def outbox_total_items
  127. collection_total_items('outbox')
  128. end
  129. def following_total_items
  130. collection_total_items('following')
  131. end
  132. def followers_total_items
  133. collection_total_items('followers')
  134. end
  135. def collection_total_items(type)
  136. return if @json[type].blank?
  137. return @collections[type] if @collections.key?(type)
  138. collection = fetch_resource_without_id_validation(@json[type])
  139. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  140. rescue HTTP::Error, OpenSSL::SSL::SSLError
  141. @collections[type] = nil
  142. end
  143. def moved_account
  144. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  145. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true, break_on_redirect: true)
  146. account
  147. end
  148. def skip_download?
  149. @account.suspended? || domain_block&.reject_media?
  150. end
  151. def auto_suspend?
  152. domain_block&.suspend?
  153. end
  154. def auto_silence?
  155. domain_block&.silence?
  156. end
  157. def domain_block
  158. return @domain_block if defined?(@domain_block)
  159. @domain_block = DomainBlock.find_by(domain: @domain)
  160. end
  161. def key_changed?
  162. !@old_public_key.nil? && @old_public_key != @account.public_key
  163. end
  164. def protocol_changed?
  165. !@old_protocol.nil? && @old_protocol != @account.protocol
  166. end
  167. def lock_options
  168. { redis: Redis.current, key: "process_account:#{@uri}" }
  169. end
  170. def process_tags
  171. return if @json['tag'].blank?
  172. as_array(@json['tag']).each do |tag|
  173. process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
  174. end
  175. end
  176. def process_emoji(tag)
  177. return if skip_download?
  178. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  179. shortcode = tag['name'].delete(':')
  180. image_url = tag['icon']['url']
  181. uri = tag['id']
  182. updated = tag['updated']
  183. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  184. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
  185. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  186. emoji.image_remote_url = image_url
  187. emoji.save
  188. end
  189. end