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

170 lines
4.7 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. @account.moved_to_account = moved_account if @json['movedTo'].present?
  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_to_href(@json['url'], 'text/html')
  88. end
  89. def outbox_total_items
  90. collection_total_items('outbox')
  91. end
  92. def following_total_items
  93. collection_total_items('following')
  94. end
  95. def followers_total_items
  96. collection_total_items('followers')
  97. end
  98. def collection_total_items(type)
  99. return if @json[type].blank?
  100. return @collections[type] if @collections.key?(type)
  101. collection = fetch_resource_without_id_validation(@json[type])
  102. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  103. rescue HTTP::Error, OpenSSL::SSL::SSLError
  104. @collections[type] = nil
  105. end
  106. def moved_account
  107. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  108. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true)
  109. account
  110. end
  111. def skip_download?
  112. @account.suspended? || domain_block&.reject_media?
  113. end
  114. def auto_suspend?
  115. domain_block&.suspend?
  116. end
  117. def auto_silence?
  118. domain_block&.silence?
  119. end
  120. def domain_block
  121. return @domain_block if defined?(@domain_block)
  122. @domain_block = DomainBlock.find_by(domain: @domain)
  123. end
  124. def key_changed?
  125. !@old_public_key.nil? && @old_public_key != @account.public_key
  126. end
  127. def protocol_changed?
  128. !@old_protocol.nil? && @old_protocol != @account.protocol
  129. end
  130. def lock_options
  131. { redis: Redis.current, key: "process_account:#{@uri}" }
  132. end
  133. end