update_status_service.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # frozen_string_literal: true
  2. class UpdateStatusService < BaseService
  3. include Redisable
  4. include LanguagesHelper
  5. class NoChangesSubmittedError < StandardError; end
  6. # @param [Status] status
  7. # @param [Integer] account_id
  8. # @param [Hash] options
  9. # @option options [Array<Integer>] :media_ids
  10. # @option options [Array<Hash>] :media_attributes
  11. # @option options [Hash] :poll
  12. # @option options [String] :text
  13. # @option options [String] :spoiler_text
  14. # @option options [Boolean] :sensitive
  15. # @option options [String] :language
  16. def call(status, account_id, options = {})
  17. @status = status
  18. @options = options
  19. @account_id = account_id
  20. @media_attachments_changed = false
  21. @poll_changed = false
  22. Status.transaction do
  23. create_previous_edit!
  24. update_media_attachments! if @options.key?(:media_ids)
  25. update_poll! if @options.key?(:poll)
  26. update_immediate_attributes!
  27. create_edit!
  28. end
  29. queue_poll_notifications!
  30. reset_preview_card!
  31. update_metadata!
  32. broadcast_updates!
  33. @status
  34. rescue NoChangesSubmittedError
  35. # For calls that result in no changes, swallow the error
  36. # but get back to the original state
  37. @status.reload
  38. end
  39. private
  40. def update_media_attachments!
  41. previous_media_attachments = @status.ordered_media_attachments.to_a
  42. next_media_attachments = validate_media!
  43. added_media_attachments = next_media_attachments - previous_media_attachments
  44. (@options[:media_attributes] || []).each do |attributes|
  45. media = next_media_attachments.find { |attachment| attachment.id == attributes[:id].to_i }
  46. next if media.nil?
  47. media.update!(attributes.slice(:thumbnail, :description, :focus))
  48. @media_attachments_changed ||= media.significantly_changed?
  49. end
  50. MediaAttachment.where(id: added_media_attachments.map(&:id)).update_all(status_id: @status.id)
  51. @status.ordered_media_attachment_ids = (@options[:media_ids] || []).map(&:to_i) & next_media_attachments.map(&:id)
  52. @media_attachments_changed ||= previous_media_attachments.map(&:id) != @status.ordered_media_attachment_ids
  53. @status.media_attachments.reload
  54. end
  55. def validate_media!
  56. return [] if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  57. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
  58. media_attachments = @status.account.media_attachments.where(status_id: [nil, @status.id]).where(scheduled_status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i)).to_a
  59. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media_attachments.size > 1 && media_attachments.find(&:audio_or_video?)
  60. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if media_attachments.any?(&:not_processed?)
  61. media_attachments
  62. end
  63. def update_poll!
  64. previous_poll = @status.preloadable_poll
  65. @previous_expires_at = previous_poll&.expires_at
  66. if @options[:poll].present?
  67. poll = previous_poll || @status.account.polls.new(status: @status, votes_count: 0)
  68. # If for some reasons the options were changed, it invalidates all previous
  69. # votes, so we need to remove them
  70. @poll_changed = true if @options[:poll][:options] != poll.options || ActiveModel::Type::Boolean.new.cast(@options[:poll][:multiple]) != poll.multiple
  71. poll.options = @options[:poll][:options]
  72. poll.hide_totals = @options[:poll][:hide_totals] || false
  73. poll.multiple = @options[:poll][:multiple] || false
  74. poll.expires_in = @options[:poll][:expires_in]
  75. poll.reset_votes! if @poll_changed
  76. poll.save!
  77. @status.poll_id = poll.id
  78. elsif previous_poll.present?
  79. previous_poll.destroy
  80. @poll_changed = true
  81. @status.poll_id = nil
  82. end
  83. @poll_changed = true if @previous_expires_at != @status.preloadable_poll&.expires_at
  84. end
  85. def update_immediate_attributes!
  86. @status.text = @options[:text].presence || @options.delete(:spoiler_text) || '' if @options.key?(:text)
  87. @status.spoiler_text = @options[:spoiler_text] || '' if @options.key?(:spoiler_text)
  88. @status.sensitive = @options[:sensitive] || @options[:spoiler_text].present? if @options.key?(:sensitive) || @options.key?(:spoiler_text)
  89. @status.language = valid_locale_cascade(@options[:language], @status.language, @status.account.user&.preferred_posting_language, I18n.default_locale)
  90. # We raise here to rollback the entire transaction
  91. raise NoChangesSubmittedError unless significant_changes?
  92. @status.edited_at = Time.now.utc
  93. @status.save!
  94. end
  95. def reset_preview_card!
  96. return unless @status.text_previously_changed?
  97. @status.preview_cards.clear
  98. LinkCrawlWorker.perform_async(@status.id)
  99. end
  100. def update_metadata!
  101. ProcessHashtagsService.new.call(@status)
  102. ProcessMentionsService.new.call(@status)
  103. end
  104. def broadcast_updates!
  105. DistributionWorker.perform_async(@status.id, { 'update' => true })
  106. ActivityPub::StatusUpdateDistributionWorker.perform_async(@status.id)
  107. end
  108. def queue_poll_notifications!
  109. poll = @status.preloadable_poll
  110. # If the poll had no expiration date set but now has, or now has a sooner
  111. # expiration date, schedule a notification
  112. return unless poll.present? && poll.expires_at.present?
  113. PollExpirationNotifyWorker.remove_from_scheduled(poll.id) if @previous_expires_at.present? && @previous_expires_at > poll.expires_at
  114. PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id)
  115. end
  116. def create_previous_edit!
  117. # We only need to create a previous edit when no previous edits exist, e.g.
  118. # when the status has never been edited. For other cases, we always create
  119. # an edit, so the step can be skipped
  120. return if @status.edits.any?
  121. @status.snapshot!(at_time: @status.created_at, rate_limit: false)
  122. end
  123. def create_edit!
  124. @status.snapshot!(account_id: @account_id)
  125. end
  126. def significant_changes?
  127. @status.changed? || @poll_changed || @media_attachments_changed
  128. end
  129. end