update_status_service.rb 5.8 KB

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