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.

198 lines
6.0 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. media_attachments = save_media
  24. ApplicationRecord.transaction do
  25. status = Status.create!(
  26. uri: id,
  27. url: url,
  28. account: @account,
  29. reblog: cached_reblog,
  30. text: content,
  31. spoiler_text: content_warning,
  32. created_at: @options[:override_timestamps] ? nil : published,
  33. reply: thread?,
  34. language: content_language,
  35. visibility: visibility_scope,
  36. conversation: find_or_create_conversation,
  37. thread: thread? ? find_status(thread.first) || find_activitypub_status(thread.first, thread.second) : nil
  38. )
  39. save_mentions(status)
  40. save_hashtags(status)
  41. attach_media(status, media_attachments)
  42. save_emojis(status)
  43. end
  44. if thread? && status.thread.nil?
  45. Rails.logger.debug "Trying to attach #{status.id} (#{id}) to #{thread.first}"
  46. ThreadResolveWorker.perform_async(status.id, thread.second)
  47. end
  48. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  49. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  50. DistributionWorker.perform_async(status.id) if @options[:override_timestamps]
  51. status
  52. end
  53. def content
  54. @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS).content
  55. end
  56. def content_language
  57. @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS)['xml:lang']&.presence || 'en'
  58. end
  59. def content_warning
  60. @xml.at_xpath('./xmlns:summary', xmlns: OStatus::TagManager::XMLNS)&.content || ''
  61. end
  62. def visibility_scope
  63. @xml.at_xpath('./mastodon:scope', mastodon: OStatus::TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  64. end
  65. def published
  66. @xml.at_xpath('./xmlns:published', xmlns: OStatus::TagManager::XMLNS).content
  67. end
  68. def thread?
  69. !@xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS).nil?
  70. end
  71. def thread
  72. thr = @xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS)
  73. [thr['ref'], thr['href']]
  74. end
  75. private
  76. def find_or_create_conversation
  77. uri = @xml.at_xpath('./ostatus:conversation', ostatus: OStatus::TagManager::OS_XMLNS)&.attribute('ref')&.content
  78. return if uri.nil?
  79. if OStatus::TagManager.instance.local_id?(uri)
  80. local_id = OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')
  81. return Conversation.find_by(id: local_id)
  82. end
  83. Conversation.find_by(uri: uri) || Conversation.create!(uri: uri)
  84. end
  85. def save_mentions(parent)
  86. processed_account_ids = []
  87. @xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  88. next if [OStatus::TagManager::TYPES[:group], OStatus::TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  89. mentioned_account = account_from_href(link['href'])
  90. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  91. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  92. # So we can skip duplicate mentions
  93. processed_account_ids << mentioned_account.id
  94. end
  95. end
  96. def save_hashtags(parent)
  97. tags = @xml.xpath('./xmlns:category', xmlns: OStatus::TagManager::XMLNS).map { |category| category['term'] }.select(&:present?)
  98. ProcessHashtagsService.new.call(parent, tags)
  99. end
  100. def save_media
  101. do_not_download = DomainBlock.find_by(domain: @account.domain)&.reject_media?
  102. media_attachments = []
  103. @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  104. next unless link['href']
  105. media = MediaAttachment.where(status: nil, remote_url: link['href']).first_or_initialize(account: @account, status: nil, remote_url: link['href'])
  106. parsed_url = Addressable::URI.parse(link['href']).normalize
  107. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  108. media.save
  109. media_attachments << media
  110. next if do_not_download
  111. begin
  112. media.file_remote_url = link['href']
  113. media.save!
  114. rescue ActiveRecord::RecordInvalid
  115. next
  116. end
  117. end
  118. media_attachments
  119. end
  120. def attach_media(parent, media_attachments)
  121. return if media_attachments.blank?
  122. media = MediaAttachment.where(status_id: nil, id: media_attachments.take(4).map(&:id))
  123. media.update(status_id: parent.id)
  124. end
  125. def save_emojis(parent)
  126. do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  127. return if do_not_download
  128. @xml.xpath('./xmlns:link[@rel="emoji"]', xmlns: OStatus::TagManager::XMLNS).each do |link|
  129. next unless link['href'] && link['name']
  130. shortcode = link['name'].delete(':')
  131. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: parent.account.domain)
  132. next unless emoji.nil?
  133. emoji = CustomEmoji.new(shortcode: shortcode, domain: parent.account.domain)
  134. emoji.image_remote_url = link['href']
  135. emoji.save
  136. end
  137. end
  138. def account_from_href(href)
  139. url = Addressable::URI.parse(href).normalize
  140. if TagManager.instance.web_domain?(url.host)
  141. Account.find_local(url.path.gsub('/users/', ''))
  142. else
  143. Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href)
  144. end
  145. end
  146. def lock_options
  147. { redis: Redis.current, key: "create:#{id}" }
  148. end
  149. end