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.

202 lines
6.3 KiB

  1. # frozen_string_literal: true
  2. class OStatus::Activity::Creation < OStatus::Activity::Base
  3. def perform
  4. if redis.exists("delete_upon_arrival:#{@account.id}:#{id}")
  5. Rails.logger.debug "Delete for status #{id} was queued, ignoring"
  6. return [nil, false]
  7. end
  8. return [nil, false] if @account.suspended?
  9. RedisLock.acquire(lock_options) do |lock|
  10. if lock.acquired?
  11. # Return early if status already exists in db
  12. @status = find_status(id)
  13. return [@status, false] unless @status.nil?
  14. @status = process_status
  15. end
  16. end
  17. [@status, true]
  18. end
  19. def process_status
  20. Rails.logger.debug "Creating remote status #{id}"
  21. cached_reblog = reblog
  22. status = nil
  23. # Skip if the reblogged status is not public
  24. return if cached_reblog && !(cached_reblog.public_visibility? || cached_reblog.unlisted_visibility?)
  25. media_attachments = save_media.take(4)
  26. ApplicationRecord.transaction do
  27. status = Status.create!(
  28. uri: id,
  29. url: url,
  30. account: @account,
  31. reblog: cached_reblog,
  32. text: content,
  33. spoiler_text: content_warning,
  34. created_at: published,
  35. override_timestamps: @options[:override_timestamps],
  36. reply: thread?,
  37. language: content_language,
  38. visibility: visibility_scope,
  39. conversation: find_or_create_conversation,
  40. thread: thread? ? find_status(thread.first) || find_activitypub_status(thread.first, thread.second) : nil,
  41. media_attachment_ids: media_attachments.map(&:id)
  42. )
  43. save_mentions(status)
  44. save_hashtags(status)
  45. save_emojis(status)
  46. end
  47. if thread? && status.thread.nil?
  48. Rails.logger.debug "Trying to attach #{status.id} (#{id}) to #{thread.first}"
  49. ThreadResolveWorker.perform_async(status.id, thread.second)
  50. end
  51. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  52. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  53. # Only continue if the status is supposed to have arrived in real-time.
  54. # Note that if @options[:override_timestamps] isn't set, the status
  55. # may have a lower snowflake id than other existing statuses, potentially
  56. # "hiding" it from paginated API calls
  57. return status unless @options[:override_timestamps] || status.within_realtime_window?
  58. DistributionWorker.perform_async(status.id)
  59. status
  60. end
  61. def content
  62. @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS).content
  63. end
  64. def content_language
  65. @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS)['xml:lang']&.presence || 'en'
  66. end
  67. def content_warning
  68. @xml.at_xpath('./xmlns:summary', xmlns: OStatus::TagManager::XMLNS)&.content || ''
  69. end
  70. def visibility_scope
  71. @xml.at_xpath('./mastodon:scope', mastodon: OStatus::TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  72. end
  73. def published
  74. @xml.at_xpath('./xmlns:published', xmlns: OStatus::TagManager::XMLNS).content
  75. end
  76. def thread?
  77. !@xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS).nil?
  78. end
  79. def thread
  80. thr = @xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS)
  81. [thr['ref'], thr['href']]
  82. end
  83. private
  84. def find_or_create_conversation
  85. uri = @xml.at_xpath('./ostatus:conversation', ostatus: OStatus::TagManager::OS_XMLNS)&.attribute('ref')&.content
  86. return if uri.nil?
  87. if OStatus::TagManager.instance.local_id?(uri)
  88. local_id = OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')
  89. return Conversation.find_by(id: local_id)
  90. end
  91. Conversation.find_by(uri: uri) || Conversation.create!(uri: uri)
  92. end
  93. def save_mentions(parent)
  94. processed_account_ids = []
  95. @xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  96. next if [OStatus::TagManager::TYPES[:group], OStatus::TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  97. mentioned_account = account_from_href(link['href'])
  98. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  99. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  100. # So we can skip duplicate mentions
  101. processed_account_ids << mentioned_account.id
  102. end
  103. end
  104. def save_hashtags(parent)
  105. tags = @xml.xpath('./xmlns:category', xmlns: OStatus::TagManager::XMLNS).map { |category| category['term'] }.select(&:present?)
  106. ProcessHashtagsService.new.call(parent, tags)
  107. end
  108. def save_media
  109. do_not_download = DomainBlock.find_by(domain: @account.domain)&.reject_media?
  110. media_attachments = []
  111. @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  112. next unless link['href']
  113. media = MediaAttachment.where(status: nil, remote_url: link['href']).first_or_initialize(account: @account, status: nil, remote_url: link['href'])
  114. parsed_url = Addressable::URI.parse(link['href']).normalize
  115. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  116. media.save
  117. media_attachments << media
  118. next if do_not_download
  119. begin
  120. media.file_remote_url = link['href']
  121. media.save!
  122. rescue ActiveRecord::RecordInvalid
  123. next
  124. end
  125. end
  126. media_attachments
  127. end
  128. def save_emojis(parent)
  129. do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  130. return if do_not_download
  131. @xml.xpath('./xmlns:link[@rel="emoji"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  132. next unless link['href'] && link['name']
  133. shortcode = link['name'].delete(':')
  134. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: parent.account.domain)
  135. next unless emoji.nil?
  136. emoji = CustomEmoji.new(shortcode: shortcode, domain: parent.account.domain)
  137. emoji.image_remote_url = link['href']
  138. emoji.save
  139. end
  140. end
  141. def account_from_href(href)
  142. url = Addressable::URI.parse(href).normalize
  143. if TagManager.instance.web_domain?(url.host)
  144. Account.find_local(url.path.gsub('/users/', ''))
  145. else
  146. Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href)
  147. end
  148. end
  149. def lock_options
  150. { redis: Redis.current, key: "create:#{id}" }
  151. end
  152. end