闭社主体 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.

168 lines
4.4 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?
  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. end
  64. def after_protocol_change!
  65. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  66. end
  67. def after_key_change!
  68. RefollowWorker.perform_async(@account.id)
  69. end
  70. def image_url(key)
  71. value = first_of_value(@json[key])
  72. return if value.nil?
  73. return value['url'] if value.is_a?(Hash)
  74. image = fetch_resource_without_id_validation(value)
  75. image['url'] if image
  76. end
  77. def public_key
  78. value = first_of_value(@json['publicKey'])
  79. return if value.nil?
  80. return value['publicKeyPem'] if value.is_a?(Hash)
  81. key = fetch_resource_without_id_validation(value)
  82. key['publicKeyPem'] if key
  83. end
  84. def url
  85. return if @json['url'].blank?
  86. value = first_of_value(@json['url'])
  87. return value if value.is_a?(String)
  88. value['href']
  89. end
  90. def outbox_total_items
  91. collection_total_items('outbox')
  92. end
  93. def following_total_items
  94. collection_total_items('following')
  95. end
  96. def followers_total_items
  97. collection_total_items('followers')
  98. end
  99. def collection_total_items(type)
  100. return if @json[type].blank?
  101. return @collections[type] if @collections.key?(type)
  102. collection = fetch_resource_without_id_validation(@json[type])
  103. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  104. rescue HTTP::Error, OpenSSL::SSL::SSLError
  105. @collections[type] = nil
  106. end
  107. def skip_download?
  108. @account.suspended? || domain_block&.reject_media?
  109. end
  110. def auto_suspend?
  111. domain_block&.suspend?
  112. end
  113. def auto_silence?
  114. domain_block&.silence?
  115. end
  116. def domain_block
  117. return @domain_block if defined?(@domain_block)
  118. @domain_block = DomainBlock.find_by(domain: @domain)
  119. end
  120. def key_changed?
  121. !@old_public_key.nil? && @old_public_key != @account.public_key
  122. end
  123. def protocol_changed?
  124. !@old_protocol.nil? && @old_protocol != @account.protocol
  125. end
  126. def lock_options
  127. { redis: Redis.current, key: "process_account:#{@uri}" }
  128. end
  129. end