logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

creation.rb (6115B)


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