闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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