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.

345 lines
11 KiB

  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.remote.find_by(uri: @uri) if @options[:only_key]
  18. @account ||= Account.find_remote(@username, @domain)
  19. @old_public_key = @account&.public_key
  20. @old_protocol = @account&.protocol
  21. @suspension_changed = false
  22. create_account if @account.nil?
  23. update_account
  24. process_tags
  25. process_attachments
  26. process_duplicate_accounts! if @options[:verified_webfinger]
  27. else
  28. raise Mastodon::RaceConditionError
  29. end
  30. end
  31. return if @account.nil?
  32. after_protocol_change! if protocol_changed?
  33. after_key_change! if key_changed? && !@options[:signed_with_known_key]
  34. clear_tombstones! if key_changed?
  35. after_suspension_change! if suspension_changed?
  36. unless @options[:only_key] || @account.suspended?
  37. check_featured_collection! if @account.featured_collection_url.present?
  38. check_links! unless @account.fields.empty?
  39. end
  40. @account
  41. rescue Oj::ParseError
  42. nil
  43. end
  44. private
  45. def create_account
  46. @account = Account.new
  47. @account.protocol = :activitypub
  48. @account.username = @username
  49. @account.domain = @domain
  50. @account.private_key = nil
  51. @account.suspended_at = domain_block.created_at if auto_suspend?
  52. @account.suspension_origin = :local if auto_suspend?
  53. @account.silenced_at = domain_block.created_at if auto_silence?
  54. @account.save
  55. end
  56. def update_account
  57. @account.last_webfingered_at = Time.now.utc unless @options[:only_key]
  58. @account.protocol = :activitypub
  59. set_suspension!
  60. set_immediate_protocol_attributes!
  61. set_fetchable_key! unless @account.suspended? && @account.suspension_origin_local?
  62. set_immediate_attributes! unless @account.suspended?
  63. set_fetchable_attributes! unless @options[:only_key] || @account.suspended?
  64. @account.save_with_optional_media!
  65. end
  66. def set_immediate_protocol_attributes!
  67. @account.inbox_url = @json['inbox'] || ''
  68. @account.outbox_url = @json['outbox'] || ''
  69. @account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
  70. @account.followers_url = @json['followers'] || ''
  71. @account.url = url || @uri
  72. @account.uri = @uri
  73. @account.actor_type = actor_type
  74. @account.created_at = @json['published'] if @json['published'].present?
  75. end
  76. def set_immediate_attributes!
  77. @account.featured_collection_url = @json['featured'] || ''
  78. @account.devices_url = @json['devices'] || ''
  79. @account.display_name = @json['name'] || ''
  80. @account.note = @json['summary'] || ''
  81. @account.locked = @json['manuallyApprovesFollowers'] || false
  82. @account.fields = property_values || {}
  83. @account.also_known_as = as_array(@json['alsoKnownAs'] || []).map { |item| value_or_id(item) }
  84. @account.discoverable = @json['discoverable'] || false
  85. end
  86. def set_fetchable_key!
  87. @account.public_key = public_key || ''
  88. end
  89. def set_fetchable_attributes!
  90. begin
  91. @account.avatar_remote_url = image_url('icon') || '' unless skip_download?
  92. rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
  93. RedownloadAvatarWorker.perform_in(rand(30..600).seconds, @account.id)
  94. end
  95. begin
  96. @account.header_remote_url = image_url('image') || '' unless skip_download?
  97. rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
  98. RedownloadHeaderWorker.perform_in(rand(30..600).seconds, @account.id)
  99. end
  100. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  101. @account.following_count = following_total_items if following_total_items.present?
  102. @account.followers_count = followers_total_items if followers_total_items.present?
  103. @account.hide_collections = following_private? || followers_private?
  104. @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
  105. end
  106. def set_suspension!
  107. return if @account.suspended? && @account.suspension_origin_local?
  108. if @account.suspended? && !@json['suspended']
  109. @account.unsuspend!
  110. @suspension_changed = true
  111. elsif !@account.suspended? && @json['suspended']
  112. @account.suspend!(origin: :remote)
  113. @suspension_changed = true
  114. end
  115. end
  116. def after_protocol_change!
  117. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  118. end
  119. def after_key_change!
  120. RefollowWorker.perform_async(@account.id)
  121. end
  122. def after_suspension_change!
  123. if @account.suspended?
  124. Admin::SuspensionWorker.perform_async(@account.id)
  125. else
  126. Admin::UnsuspensionWorker.perform_async(@account.id)
  127. end
  128. end
  129. def check_featured_collection!
  130. ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
  131. end
  132. def check_links!
  133. VerifyAccountLinksWorker.perform_async(@account.id)
  134. end
  135. def process_duplicate_accounts!
  136. return unless Account.where(uri: @account.uri).where.not(id: @account.id).exists?
  137. AccountMergingWorker.perform_async(@account.id)
  138. end
  139. def actor_type
  140. if @json['type'].is_a?(Array)
  141. @json['type'].find { |type| ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES.include?(type) }
  142. else
  143. @json['type']
  144. end
  145. end
  146. def image_url(key)
  147. value = first_of_value(@json[key])
  148. return if value.nil?
  149. return value['url'] if value.is_a?(Hash)
  150. image = fetch_resource_without_id_validation(value)
  151. image['url'] if image
  152. end
  153. def public_key
  154. value = first_of_value(@json['publicKey'])
  155. return if value.nil?
  156. return value['publicKeyPem'] if value.is_a?(Hash)
  157. key = fetch_resource_without_id_validation(value)
  158. key['publicKeyPem'] if key
  159. end
  160. def url
  161. return if @json['url'].blank?
  162. url_candidate = url_to_href(@json['url'], 'text/html')
  163. if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
  164. nil
  165. else
  166. url_candidate
  167. end
  168. end
  169. def property_values
  170. return unless @json['attachment'].is_a?(Array)
  171. as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
  172. end
  173. def mismatching_origin?(url)
  174. needle = Addressable::URI.parse(url).host
  175. haystack = Addressable::URI.parse(@uri).host
  176. !haystack.casecmp(needle).zero?
  177. end
  178. def outbox_total_items
  179. collection_info('outbox').first
  180. end
  181. def following_total_items
  182. collection_info('following').first
  183. end
  184. def followers_total_items
  185. collection_info('followers').first
  186. end
  187. def following_private?
  188. !collection_info('following').last
  189. end
  190. def followers_private?
  191. !collection_info('followers').last
  192. end
  193. def collection_info(type)
  194. return [nil, nil] if @json[type].blank?
  195. return @collections[type] if @collections.key?(type)
  196. collection = fetch_resource_without_id_validation(@json[type])
  197. total_items = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  198. has_first_page = collection.is_a?(Hash) && collection['first'].present?
  199. @collections[type] = [total_items, has_first_page]
  200. rescue HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::LengthValidationError
  201. @collections[type] = [nil, nil]
  202. end
  203. def moved_account
  204. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  205. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true, break_on_redirect: true)
  206. account
  207. end
  208. def skip_download?
  209. @account.suspended? || domain_block&.reject_media?
  210. end
  211. def auto_suspend?
  212. domain_block&.suspend?
  213. end
  214. def auto_silence?
  215. domain_block&.silence?
  216. end
  217. def domain_block
  218. return @domain_block if defined?(@domain_block)
  219. @domain_block = DomainBlock.rule_for(@domain)
  220. end
  221. def key_changed?
  222. !@old_public_key.nil? && @old_public_key != @account.public_key
  223. end
  224. def suspension_changed?
  225. @suspension_changed
  226. end
  227. def clear_tombstones!
  228. Tombstone.where(account_id: @account.id).delete_all
  229. end
  230. def protocol_changed?
  231. !@old_protocol.nil? && @old_protocol != @account.protocol
  232. end
  233. def lock_options
  234. { redis: Redis.current, key: "process_account:#{@uri}", autorelease: 15.minutes.seconds }
  235. end
  236. def process_tags
  237. return if @json['tag'].blank?
  238. as_array(@json['tag']).each do |tag|
  239. process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
  240. end
  241. end
  242. def process_attachments
  243. return if @json['attachment'].blank?
  244. previous_proofs = @account.identity_proofs.to_a
  245. current_proofs = []
  246. as_array(@json['attachment']).each do |attachment|
  247. next unless equals_or_includes?(attachment['type'], 'IdentityProof')
  248. current_proofs << process_identity_proof(attachment)
  249. end
  250. previous_proofs.each do |previous_proof|
  251. next if current_proofs.any? { |current_proof| current_proof.id == previous_proof.id }
  252. previous_proof.delete
  253. end
  254. end
  255. def process_emoji(tag)
  256. return if skip_download?
  257. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  258. shortcode = tag['name'].delete(':')
  259. image_url = tag['icon']['url']
  260. uri = tag['id']
  261. updated = tag['updated']
  262. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  263. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
  264. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  265. emoji.image_remote_url = image_url
  266. emoji.save
  267. end
  268. def process_identity_proof(attachment)
  269. provider = attachment['signatureAlgorithm']
  270. provider_username = attachment['name']
  271. token = attachment['signatureValue']
  272. @account.identity_proofs.where(provider: provider, provider_username: provider_username).find_or_create_by(provider: provider, provider_username: provider_username, token: token)
  273. end
  274. end