The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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