process_status_update_service.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # frozen_string_literal: true
  2. class ActivityPub::ProcessStatusUpdateService < BaseService
  3. include JsonLdHelper
  4. include Redisable
  5. include Lockable
  6. def call(status, json, request_id: nil)
  7. raise ArgumentError, 'Status has unsaved changes' if status.changed?
  8. @json = json
  9. @status_parser = ActivityPub::Parser::StatusParser.new(@json)
  10. @uri = @status_parser.uri
  11. @status = status
  12. @account = status.account
  13. @media_attachments_changed = false
  14. @poll_changed = false
  15. @request_id = request_id
  16. # Only native types can be updated at the moment
  17. return @status if !expected_type? || already_updated_more_recently?
  18. if @status_parser.edited_at.present? && (@status.edited_at.nil? || @status_parser.edited_at > @status.edited_at)
  19. handle_explicit_update!
  20. else
  21. handle_implicit_update!
  22. end
  23. @status
  24. end
  25. private
  26. def handle_explicit_update!
  27. last_edit_date = @status.edited_at.presence || @status.created_at
  28. # Only allow processing one create/update per status at a time
  29. with_lock("create:#{@uri}") do
  30. Status.transaction do
  31. record_previous_edit!
  32. update_media_attachments!
  33. update_poll!
  34. update_immediate_attributes!
  35. update_metadata!
  36. create_edits!
  37. end
  38. download_media_files!
  39. queue_poll_notifications!
  40. next unless significant_changes?
  41. reset_preview_card!
  42. broadcast_updates!
  43. end
  44. forward_activity! if significant_changes? && @status_parser.edited_at > last_edit_date
  45. end
  46. def handle_implicit_update!
  47. with_lock("create:#{@uri}") do
  48. update_poll!(allow_significant_changes: false)
  49. queue_poll_notifications!
  50. end
  51. end
  52. def update_media_attachments!
  53. previous_media_attachments = @status.media_attachments.to_a
  54. previous_media_attachments_ids = @status.ordered_media_attachment_ids || previous_media_attachments.map(&:id)
  55. @next_media_attachments = []
  56. as_array(@json['attachment']).each do |attachment|
  57. media_attachment_parser = ActivityPub::Parser::MediaAttachmentParser.new(attachment)
  58. next if media_attachment_parser.remote_url.blank? || @next_media_attachments.size > 4
  59. begin
  60. media_attachment = previous_media_attachments.find { |previous_media_attachment| previous_media_attachment.remote_url == media_attachment_parser.remote_url }
  61. media_attachment ||= MediaAttachment.new(account: @account, remote_url: media_attachment_parser.remote_url)
  62. # If a previously existing media attachment was significantly updated, mark
  63. # media attachments as changed even if none were added or removed
  64. if media_attachment_parser.significantly_changes?(media_attachment)
  65. @media_attachments_changed = true
  66. end
  67. media_attachment.description = media_attachment_parser.description
  68. media_attachment.focus = media_attachment_parser.focus
  69. media_attachment.thumbnail_remote_url = media_attachment_parser.thumbnail_remote_url
  70. media_attachment.blurhash = media_attachment_parser.blurhash
  71. media_attachment.status_id = @status.id
  72. media_attachment.skip_download = unsupported_media_type?(media_attachment_parser.file_content_type) || skip_download?
  73. media_attachment.save!
  74. @next_media_attachments << media_attachment
  75. rescue Addressable::URI::InvalidURIError => e
  76. Rails.logger.debug "Invalid URL in attachment: #{e}"
  77. end
  78. end
  79. added_media_attachments = @next_media_attachments - previous_media_attachments
  80. @status.ordered_media_attachment_ids = @next_media_attachments.map(&:id)
  81. @media_attachments_changed = true if @status.ordered_media_attachment_ids != previous_media_attachments_ids
  82. end
  83. def download_media_files!
  84. @next_media_attachments.each do |media_attachment|
  85. next if media_attachment.skip_download
  86. media_attachment.download_file! if media_attachment.remote_url_previously_changed?
  87. media_attachment.download_thumbnail! if media_attachment.thumbnail_remote_url_previously_changed?
  88. media_attachment.save
  89. rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
  90. RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
  91. rescue Seahorse::Client::NetworkingError => e
  92. Rails.logger.warn "Error storing media attachment: #{e}"
  93. end
  94. @status.media_attachments.reload
  95. end
  96. def update_poll!(allow_significant_changes: true)
  97. previous_poll = @status.preloadable_poll
  98. @previous_expires_at = previous_poll&.expires_at
  99. poll_parser = ActivityPub::Parser::PollParser.new(@json)
  100. if poll_parser.valid?
  101. poll = previous_poll || @account.polls.new(status: @status)
  102. # If for some reasons the options were changed, it invalidates all previous
  103. # votes, so we need to remove them
  104. @poll_changed = true if poll_parser.significantly_changes?(poll)
  105. return if @poll_changed && !allow_significant_changes
  106. poll.last_fetched_at = Time.now.utc
  107. poll.options = poll_parser.options
  108. poll.multiple = poll_parser.multiple
  109. poll.expires_at = poll_parser.expires_at
  110. poll.voters_count = poll_parser.voters_count
  111. poll.cached_tallies = poll_parser.cached_tallies
  112. poll.reset_votes! if @poll_changed
  113. poll.save!
  114. @status.poll_id = poll.id
  115. elsif previous_poll.present?
  116. return unless allow_significant_changes
  117. previous_poll.destroy!
  118. @poll_changed = true
  119. @status.poll_id = nil
  120. end
  121. end
  122. def update_immediate_attributes!
  123. @status.text = @status_parser.text || ''
  124. @status.spoiler_text = @status_parser.spoiler_text || ''
  125. @status.sensitive = @account.sensitized? || @status_parser.sensitive || false
  126. @status.language = @status_parser.language
  127. @significant_changes = text_significantly_changed? || @status.spoiler_text_changed? || @media_attachments_changed || @poll_changed
  128. @status.edited_at = @status_parser.edited_at if significant_changes?
  129. @status.save!
  130. end
  131. def update_metadata!
  132. @raw_tags = []
  133. @raw_mentions = []
  134. @raw_emojis = []
  135. as_array(@json['tag']).each do |tag|
  136. if equals_or_includes?(tag['type'], 'Hashtag')
  137. @raw_tags << tag['name']
  138. elsif equals_or_includes?(tag['type'], 'Mention')
  139. @raw_mentions << tag['href']
  140. elsif equals_or_includes?(tag['type'], 'Emoji')
  141. @raw_emojis << tag
  142. end
  143. end
  144. update_tags!
  145. update_mentions!
  146. update_emojis!
  147. end
  148. def update_tags!
  149. @status.tags = Tag.find_or_create_by_names(@raw_tags)
  150. end
  151. def update_mentions!
  152. previous_mentions = @status.active_mentions.includes(:account).to_a
  153. current_mentions = []
  154. @raw_mentions.each do |href|
  155. next if href.blank?
  156. account = ActivityPub::TagManager.instance.uri_to_resource(href, Account)
  157. account ||= ActivityPub::FetchRemoteAccountService.new.call(href, request_id: @request_id)
  158. next if account.nil?
  159. mention = previous_mentions.find { |x| x.account_id == account.id }
  160. mention ||= account.mentions.new(status: @status)
  161. current_mentions << mention
  162. end
  163. current_mentions.each do |mention|
  164. mention.save if mention.new_record?
  165. end
  166. # If previous mentions are no longer contained in the text, convert them
  167. # to silent mentions, since withdrawing access from someone who already
  168. # received a notification might be more confusing
  169. removed_mentions = previous_mentions - current_mentions
  170. Mention.where(id: removed_mentions.map(&:id)).update_all(silent: true) unless removed_mentions.empty?
  171. end
  172. def update_emojis!
  173. return if skip_download?
  174. @raw_emojis.each do |raw_emoji|
  175. custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(raw_emoji)
  176. next if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
  177. emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
  178. next unless emoji.nil? || custom_emoji_parser.image_remote_url != emoji.image_remote_url || (custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
  179. begin
  180. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: custom_emoji_parser.shortcode, uri: custom_emoji_parser.uri)
  181. emoji.image_remote_url = custom_emoji_parser.image_remote_url
  182. emoji.save
  183. rescue Seahorse::Client::NetworkingError => e
  184. Rails.logger.warn "Error storing emoji: #{e}"
  185. end
  186. end
  187. end
  188. def expected_type?
  189. equals_or_includes_any?(@json['type'], %w(Note Question))
  190. end
  191. def record_previous_edit!
  192. @previous_edit = @status.build_snapshot(at_time: @status.created_at, rate_limit: false) if @status.edits.empty?
  193. end
  194. def create_edits!
  195. return unless significant_changes?
  196. @previous_edit&.save!
  197. @status.snapshot!(account_id: @account.id, rate_limit: false)
  198. end
  199. def skip_download?
  200. return @skip_download if defined?(@skip_download)
  201. @skip_download ||= DomainBlock.reject_media?(@account.domain)
  202. end
  203. def unsupported_media_type?(mime_type)
  204. mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
  205. end
  206. def significant_changes?
  207. @significant_changes
  208. end
  209. def text_significantly_changed?
  210. return false unless @status.text_changed?
  211. old, new = @status.text_change
  212. HtmlAwareFormatter.new(old, false).to_s != HtmlAwareFormatter.new(new, false).to_s
  213. end
  214. def already_updated_more_recently?
  215. @status.edited_at.present? && @status_parser.edited_at.present? && @status.edited_at > @status_parser.edited_at
  216. end
  217. def reset_preview_card!
  218. @status.preview_cards.clear
  219. LinkCrawlWorker.perform_in(rand(1..59).seconds, @status.id)
  220. end
  221. def broadcast_updates!
  222. ::DistributionWorker.perform_async(@status.id, { 'update' => true })
  223. end
  224. def queue_poll_notifications!
  225. poll = @status.preloadable_poll
  226. # If the poll had no expiration date set but now has, or now has a sooner
  227. # expiration date, and people have voted, schedule a notification
  228. return unless poll.present? && poll.expires_at.present? && poll.votes.exists?
  229. PollExpirationNotifyWorker.remove_from_scheduled(poll.id) if @previous_expires_at.present? && @previous_expires_at > poll.expires_at
  230. PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id)
  231. end
  232. def forward_activity!
  233. forwarder.forward! if forwarder.forwardable?
  234. end
  235. def forwarder
  236. @forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status)
  237. end
  238. end