process_account_service.rb 5.0 KB

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