logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

process_account_service.rb (6534B)


  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. end
  61. def set_fetchable_attributes!
  62. @account.avatar_remote_url = image_url('icon') unless skip_download?
  63. @account.header_remote_url = image_url('image') unless skip_download?
  64. @account.public_key = public_key || ''
  65. @account.statuses_count = outbox_total_items if outbox_total_items.present?
  66. @account.following_count = following_total_items if following_total_items.present?
  67. @account.followers_count = followers_total_items if followers_total_items.present?
  68. @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
  69. end
  70. def after_protocol_change!
  71. ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
  72. end
  73. def after_key_change!
  74. RefollowWorker.perform_async(@account.id)
  75. end
  76. def check_featured_collection!
  77. ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
  78. end
  79. def image_url(key)
  80. value = first_of_value(@json[key])
  81. return if value.nil?
  82. return value['url'] if value.is_a?(Hash)
  83. image = fetch_resource_without_id_validation(value)
  84. image['url'] if image
  85. end
  86. def public_key
  87. value = first_of_value(@json['publicKey'])
  88. return if value.nil?
  89. return value['publicKeyPem'] if value.is_a?(Hash)
  90. key = fetch_resource_without_id_validation(value)
  91. key['publicKeyPem'] if key
  92. end
  93. def url
  94. return if @json['url'].blank?
  95. url_candidate = url_to_href(@json['url'], 'text/html')
  96. if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
  97. nil
  98. else
  99. url_candidate
  100. end
  101. end
  102. def property_values
  103. return unless @json['attachment'].is_a?(Array)
  104. @json['attachment'].select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
  105. end
  106. def mismatching_origin?(url)
  107. needle = Addressable::URI.parse(url).host
  108. haystack = Addressable::URI.parse(@uri).host
  109. !haystack.casecmp(needle).zero?
  110. end
  111. def outbox_total_items
  112. collection_total_items('outbox')
  113. end
  114. def following_total_items
  115. collection_total_items('following')
  116. end
  117. def followers_total_items
  118. collection_total_items('followers')
  119. end
  120. def collection_total_items(type)
  121. return if @json[type].blank?
  122. return @collections[type] if @collections.key?(type)
  123. collection = fetch_resource_without_id_validation(@json[type])
  124. @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
  125. rescue HTTP::Error, OpenSSL::SSL::SSLError
  126. @collections[type] = nil
  127. end
  128. def moved_account
  129. account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
  130. account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true)
  131. account
  132. end
  133. def skip_download?
  134. @account.suspended? || domain_block&.reject_media?
  135. end
  136. def auto_suspend?
  137. domain_block&.suspend?
  138. end
  139. def auto_silence?
  140. domain_block&.silence?
  141. end
  142. def domain_block
  143. return @domain_block if defined?(@domain_block)
  144. @domain_block = DomainBlock.find_by(domain: @domain)
  145. end
  146. def key_changed?
  147. !@old_public_key.nil? && @old_public_key != @account.public_key
  148. end
  149. def protocol_changed?
  150. !@old_protocol.nil? && @old_protocol != @account.protocol
  151. end
  152. def lock_options
  153. { redis: Redis.current, key: "process_account:#{@uri}" }
  154. end
  155. def process_tags
  156. return if @json['tag'].blank?
  157. as_array(@json['tag']).each do |tag|
  158. case tag['type']
  159. when 'Emoji'
  160. process_emoji tag
  161. end
  162. end
  163. end
  164. def process_emoji(tag)
  165. return if skip_download?
  166. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  167. shortcode = tag['name'].delete(':')
  168. image_url = tag['icon']['url']
  169. uri = tag['id']
  170. updated = tag['updated']
  171. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  172. return unless emoji.nil? || emoji.updated_at >= updated
  173. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  174. emoji.image_remote_url = image_url
  175. emoji.save
  176. end
  177. end