update_status_service.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 > Status::MEDIA_ATTACHMENTS_LIMIT || @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(Status::MEDIA_ATTACHMENTS_LIMIT).map(&:to_i)).to_a
  59. not_found_ids = @options[:media_ids].map(&:to_i) - media_attachments.map(&:id)
  60. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_found', ids: not_found_ids.join(', ')) if not_found_ids.any?
  61. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media_attachments.size > 1 && media_attachments.find(&:audio_or_video?)
  62. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if media_attachments.any?(&:not_processed?)
  63. media_attachments
  64. end
  65. def update_poll!
  66. previous_poll = @status.preloadable_poll
  67. @previous_expires_at = previous_poll&.expires_at
  68. if @options[:poll].present?
  69. poll = previous_poll || @status.account.polls.new(status: @status, votes_count: 0)
  70. # If for some reasons the options were changed, it invalidates all previous
  71. # votes, so we need to remove them
  72. @poll_changed = true if @options[:poll][:options] != poll.options || ActiveModel::Type::Boolean.new.cast(@options[:poll][:multiple]) != poll.multiple
  73. poll.options = @options[:poll][:options]
  74. poll.hide_totals = @options[:poll][:hide_totals] || false
  75. poll.multiple = @options[:poll][:multiple] || false
  76. poll.expires_in = @options[:poll][:expires_in]
  77. poll.reset_votes! if @poll_changed
  78. poll.save!
  79. @status.poll_id = poll.id
  80. elsif previous_poll.present?
  81. previous_poll.destroy
  82. @poll_changed = true
  83. @status.poll_id = nil
  84. end
  85. @poll_changed = true if @previous_expires_at != @status.preloadable_poll&.expires_at
  86. end
  87. def update_immediate_attributes!
  88. @status.text = @options[:text].presence || @options.delete(:spoiler_text) || '' if @options.key?(:text)
  89. @status.spoiler_text = @options[:spoiler_text] || '' if @options.key?(:spoiler_text)
  90. @status.sensitive = @options[:sensitive] || @options[:spoiler_text].present? if @options.key?(:sensitive) || @options.key?(:spoiler_text)
  91. @status.language = valid_locale_cascade(@options[:language], @status.language, @status.account.user&.preferred_posting_language, I18n.default_locale)
  92. # We raise here to rollback the entire transaction
  93. raise NoChangesSubmittedError unless significant_changes?
  94. @status.edited_at = Time.now.utc
  95. @status.save!
  96. end
  97. def reset_preview_card!
  98. return unless @status.text_previously_changed?
  99. @status.reset_preview_card!
  100. LinkCrawlWorker.perform_async(@status.id)
  101. end
  102. def update_metadata!
  103. ProcessHashtagsService.new.call(@status)
  104. ProcessMentionsService.new.call(@status)
  105. end
  106. def broadcast_updates!
  107. DistributionWorker.perform_async(@status.id, { 'update' => true })
  108. ActivityPub::StatusUpdateDistributionWorker.perform_async(@status.id)
  109. end
  110. def queue_poll_notifications!
  111. poll = @status.preloadable_poll
  112. # If the poll had no expiration date set but now has, or now has a sooner
  113. # expiration date, schedule a notification
  114. return unless poll.present? && poll.expires_at.present?
  115. PollExpirationNotifyWorker.remove_from_scheduled(poll.id) if @previous_expires_at.present? && @previous_expires_at > poll.expires_at
  116. PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id)
  117. end
  118. def create_previous_edit!
  119. # We only need to create a previous edit when no previous edits exist, e.g.
  120. # when the status has never been edited. For other cases, we always create
  121. # an edit, so the step can be skipped
  122. return if @status.edits.any?
  123. @status.snapshot!(at_time: @status.created_at, rate_limit: false)
  124. end
  125. def create_edit!
  126. @status.snapshot!(account_id: @account_id)
  127. end
  128. def significant_changes?
  129. @status.changed? || @poll_changed || @media_attachments_changed
  130. end
  131. end