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.

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