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.

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