2022-02-10 08:15:30 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UpdateStatusService < BaseService
|
|
|
|
include Redisable
|
2022-02-10 11:09:44 +09:00
|
|
|
include LanguagesHelper
|
2022-02-10 08:15:30 +09:00
|
|
|
|
2022-03-26 08:38:44 +09:00
|
|
|
class NoChangesSubmittedError < StandardError; end
|
|
|
|
|
2022-02-10 08:15:30 +09:00
|
|
|
# @param [Status] status
|
|
|
|
# @param [Integer] account_id
|
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Array<Integer>] :media_ids
|
|
|
|
# @option options [Hash] :poll
|
|
|
|
# @option options [String] :text
|
|
|
|
# @option options [String] :spoiler_text
|
|
|
|
# @option options [Boolean] :sensitive
|
|
|
|
# @option options [String] :language
|
2022-02-10 17:13:27 +09:00
|
|
|
# @option options [String] :content_type
|
2022-02-10 08:15:30 +09:00
|
|
|
def call(status, account_id, options = {})
|
|
|
|
@status = status
|
|
|
|
@options = options
|
|
|
|
@account_id = account_id
|
2022-03-26 08:38:44 +09:00
|
|
|
@media_attachments_changed = false
|
|
|
|
@poll_changed = false
|
2022-02-10 08:15:30 +09:00
|
|
|
|
|
|
|
Status.transaction do
|
|
|
|
create_previous_edit!
|
2022-03-02 06:20:29 +09:00
|
|
|
update_media_attachments! if @options.key?(:media_ids)
|
|
|
|
update_poll! if @options.key?(:poll)
|
2022-02-10 08:15:30 +09:00
|
|
|
update_immediate_attributes!
|
|
|
|
create_edit!
|
|
|
|
end
|
|
|
|
|
|
|
|
queue_poll_notifications!
|
|
|
|
reset_preview_card!
|
|
|
|
update_metadata!
|
|
|
|
broadcast_updates!
|
|
|
|
|
|
|
|
@status
|
2022-03-26 08:38:44 +09:00
|
|
|
rescue NoChangesSubmittedError
|
|
|
|
# For calls that result in no changes, swallow the error
|
|
|
|
# but get back to the original state
|
|
|
|
|
|
|
|
@status.reload
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_media_attachments!
|
2022-03-26 08:38:44 +09:00
|
|
|
previous_media_attachments = @status.ordered_media_attachments.to_a
|
2022-02-10 08:15:30 +09:00
|
|
|
next_media_attachments = validate_media!
|
|
|
|
added_media_attachments = next_media_attachments - previous_media_attachments
|
|
|
|
|
|
|
|
MediaAttachment.where(id: added_media_attachments.map(&:id)).update_all(status_id: @status.id)
|
|
|
|
|
2022-03-09 17:06:17 +09:00
|
|
|
@status.ordered_media_attachment_ids = (@options[:media_ids] || []).map(&:to_i) & next_media_attachments.map(&:id)
|
2022-03-26 08:38:44 +09:00
|
|
|
@media_attachments_changed = previous_media_attachments.map(&:id) != @status.ordered_media_attachment_ids
|
2022-02-10 08:15:30 +09:00
|
|
|
@status.media_attachments.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_media!
|
|
|
|
return [] if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
|
|
|
|
|
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media_attachments.size > 1 && media_attachments.find(&:audio_or_video?)
|
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if media_attachments.any?(&:not_processed?)
|
|
|
|
|
|
|
|
media_attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_poll!
|
|
|
|
previous_poll = @status.preloadable_poll
|
|
|
|
@previous_expires_at = previous_poll&.expires_at
|
|
|
|
|
|
|
|
if @options[:poll].present?
|
|
|
|
poll = previous_poll || @status.account.polls.new(status: @status, votes_count: 0)
|
|
|
|
|
|
|
|
# If for some reasons the options were changed, it invalidates all previous
|
|
|
|
# votes, so we need to remove them
|
2022-03-26 08:38:44 +09:00
|
|
|
@poll_changed = true if @options[:poll][:options] != poll.options || ActiveModel::Type::Boolean.new.cast(@options[:poll][:multiple]) != poll.multiple
|
2022-02-10 08:15:30 +09:00
|
|
|
|
|
|
|
poll.options = @options[:poll][:options]
|
|
|
|
poll.hide_totals = @options[:poll][:hide_totals] || false
|
|
|
|
poll.multiple = @options[:poll][:multiple] || false
|
|
|
|
poll.expires_in = @options[:poll][:expires_in]
|
2022-03-26 08:38:44 +09:00
|
|
|
poll.reset_votes! if @poll_changed
|
2022-02-10 08:15:30 +09:00
|
|
|
poll.save!
|
|
|
|
|
|
|
|
@status.poll_id = poll.id
|
|
|
|
elsif previous_poll.present?
|
|
|
|
previous_poll.destroy
|
2022-03-26 08:38:44 +09:00
|
|
|
@poll_changed = true
|
2022-02-10 08:15:30 +09:00
|
|
|
@status.poll_id = nil
|
|
|
|
end
|
2022-03-26 08:38:44 +09:00
|
|
|
|
|
|
|
@poll_changed = true if @previous_expires_at != @status.preloadable_poll&.expires_at
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_immediate_attributes!
|
2022-03-02 06:20:29 +09:00
|
|
|
@status.text = @options[:text].presence || @options.delete(:spoiler_text) || '' if @options.key?(:text)
|
|
|
|
@status.spoiler_text = @options[:spoiler_text] || '' if @options.key?(:spoiler_text)
|
|
|
|
@status.sensitive = @options[:sensitive] || @options[:spoiler_text].present? if @options.key?(:sensitive) || @options.key?(:spoiler_text)
|
2022-03-09 16:46:05 +09:00
|
|
|
@status.language = valid_locale_cascade(@options[:language], @status.language, @status.account.user&.preferred_posting_language, I18n.default_locale)
|
2022-02-10 17:13:27 +09:00
|
|
|
@status.content_type = @options[:content_type] || @status.content_type
|
2022-02-10 08:15:30 +09:00
|
|
|
|
2022-03-26 08:38:44 +09:00
|
|
|
# We raise here to rollback the entire transaction
|
|
|
|
raise NoChangesSubmittedError unless significant_changes?
|
|
|
|
|
|
|
|
@status.edited_at = Time.now.utc
|
2022-02-10 08:15:30 +09:00
|
|
|
@status.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_preview_card!
|
|
|
|
return unless @status.text_previously_changed?
|
|
|
|
|
|
|
|
@status.preview_cards.clear
|
|
|
|
LinkCrawlWorker.perform_async(@status.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_metadata!
|
|
|
|
ProcessHashtagsService.new.call(@status)
|
|
|
|
ProcessMentionsService.new.call(@status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_updates!
|
|
|
|
DistributionWorker.perform_async(@status.id, { 'update' => true })
|
2022-02-12 22:39:01 +09:00
|
|
|
ActivityPub::StatusUpdateDistributionWorker.perform_async(@status.id) unless @status.local_only?
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def queue_poll_notifications!
|
|
|
|
poll = @status.preloadable_poll
|
|
|
|
|
|
|
|
# If the poll had no expiration date set but now has, or now has a sooner
|
|
|
|
# expiration date, and people have voted, schedule a notification
|
|
|
|
|
|
|
|
return unless poll.present? && poll.expires_at.present? && poll.votes.exists?
|
|
|
|
|
|
|
|
PollExpirationNotifyWorker.remove_from_scheduled(poll.id) if @previous_expires_at.present? && @previous_expires_at > poll.expires_at
|
|
|
|
PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_previous_edit!
|
|
|
|
# We only need to create a previous edit when no previous edits exist, e.g.
|
|
|
|
# when the status has never been edited. For other cases, we always create
|
|
|
|
# an edit, so the step can be skipped
|
|
|
|
|
|
|
|
return if @status.edits.any?
|
|
|
|
|
2022-03-10 04:06:51 +09:00
|
|
|
@status.snapshot!(at_time: @status.created_at, rate_limit: false)
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_edit!
|
2022-03-09 17:06:17 +09:00
|
|
|
@status.snapshot!(account_id: @account_id)
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|
2022-03-26 08:38:44 +09:00
|
|
|
|
|
|
|
def significant_changes?
|
|
|
|
@status.changed? || @poll_changed || @media_attachments_changed
|
|
|
|
end
|
2022-02-10 08:15:30 +09:00
|
|
|
end
|