2019-08-15 23:07:54 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class EditStatusService < BaseService
|
|
|
|
include Redisable
|
|
|
|
|
|
|
|
# Post a text status update, fetch and notify remote users mentioned
|
|
|
|
# @param [Status] status Status being edited
|
|
|
|
# @param [Hash] options
|
|
|
|
# @option [String] :text Message
|
2020-06-17 21:27:58 +01:00
|
|
|
# @option [String] :markdown Optional message in markdown
|
2019-08-15 23:07:54 +01:00
|
|
|
# @option [Boolean] :sensitive
|
|
|
|
# @option [String] :visibility
|
|
|
|
# @option [String] :spoiler_text
|
|
|
|
# @option [String] :language
|
|
|
|
# @option [Enumerable] :media_ids Optional array of media IDs to attach
|
|
|
|
# @option [Doorkeeper::Application] :application
|
|
|
|
# @option [String] :idempotency Optional idempotency key
|
|
|
|
# @return [Status]
|
|
|
|
def call(status, options = {})
|
|
|
|
@status = status
|
|
|
|
@account = status.account
|
|
|
|
@options = options
|
|
|
|
@text = @options[:text] || ''
|
2020-06-17 21:27:58 +01:00
|
|
|
@markdown = @options[:markdown] if @account.is_pro
|
2019-08-15 23:07:54 +01:00
|
|
|
|
|
|
|
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
|
|
|
|
|
2021-02-19 01:12:40 +00:00
|
|
|
validate_similarity!
|
2020-12-09 04:19:10 +00:00
|
|
|
validate_links!
|
2019-08-15 23:07:54 +01:00
|
|
|
validate_media!
|
|
|
|
preprocess_attributes!
|
2019-09-17 15:22:02 +01:00
|
|
|
revision_text = prepare_revision_text
|
2019-08-15 23:07:54 +01:00
|
|
|
|
|
|
|
process_status!
|
|
|
|
postprocess_status!
|
2019-09-17 15:22:02 +01:00
|
|
|
create_revision! revision_text
|
2019-08-15 23:07:54 +01:00
|
|
|
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
conn.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
|
|
|
|
end
|
2019-08-15 23:07:54 +01:00
|
|
|
|
|
|
|
@status
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def preprocess_attributes!
|
|
|
|
@text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
|
|
|
|
@visibility = @options[:visibility] || @account.user&.setting_default_privacy
|
|
|
|
@visibility = :unlisted if @visibility == :public && @account.silenced?
|
|
|
|
rescue ArgumentError
|
|
|
|
raise ActiveRecord::RecordInvalid
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_status!
|
2019-09-18 17:20:52 +01:00
|
|
|
@status.update!(status_attributes)
|
2019-08-15 23:07:54 +01:00
|
|
|
|
|
|
|
process_hashtags_service.call(@status)
|
|
|
|
process_mentions_service.call(@status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def postprocess_status!
|
|
|
|
LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
|
|
|
|
end
|
|
|
|
|
2019-09-17 15:22:02 +01:00
|
|
|
def prepare_revision_text
|
|
|
|
text = @status.text
|
|
|
|
current_media_ids = @status.media_attachments.pluck(:id)
|
|
|
|
new_media_ids = @options[:media_ids].take(4).map(&:to_i)
|
|
|
|
|
|
|
|
if current_media_ids.sort != new_media_ids.sort
|
|
|
|
text = "" if text == @options[:text]
|
|
|
|
text += " [Media attachments changed]"
|
|
|
|
end
|
|
|
|
|
|
|
|
text.strip()
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_revision!(text)
|
|
|
|
@status.revisions.create!({
|
|
|
|
text: text
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-08-15 23:07:54 +01:00
|
|
|
def validate_media!
|
|
|
|
return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
|
|
|
|
|
|
|
|
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4
|
|
|
|
|
2019-08-23 01:53:02 +01:00
|
|
|
@media = @account.media_attachments.where(id: @options[:media_ids].take(4).map(&:to_i))
|
2019-08-15 23:07:54 +01:00
|
|
|
|
2020-05-02 07:25:55 +01:00
|
|
|
hasVideoOrGif = @media.find(&:video?) || @media.find(&:gifv?)
|
|
|
|
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
|
2019-08-15 23:07:54 +01:00
|
|
|
end
|
|
|
|
|
2021-02-19 01:12:40 +00:00
|
|
|
def validate_similarity!
|
|
|
|
raise GabSocial::NotPermittedError if StatusSimilarityService.new.call?(@text, @account.id)
|
|
|
|
end
|
|
|
|
|
2020-12-09 04:19:10 +00:00
|
|
|
def validate_links!
|
2021-01-17 22:36:20 +00:00
|
|
|
raise GabSocial::LinkBlockedError if LinkBlock.block?(@text)
|
2020-12-09 04:19:10 +00:00
|
|
|
end
|
|
|
|
|
2019-08-15 23:07:54 +01:00
|
|
|
def language_from_option(str)
|
|
|
|
ISO_639.find(str)&.alpha2
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_mentions_service
|
|
|
|
ProcessMentionsService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_hashtags_service
|
|
|
|
ProcessHashtagsService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_key
|
|
|
|
"idempotency:status:#{@account.id}:#{@options[:idempotency]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_given?
|
|
|
|
@options[:idempotency].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_duplicate
|
|
|
|
@account.statuses.find(@idempotency_duplicate)
|
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_duplicate?
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
@idempotency_duplicate = conn.get(idempotency_key)
|
|
|
|
end
|
|
|
|
@idempotency_duplicate
|
2019-08-15 23:07:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def status_attributes
|
|
|
|
{
|
2019-09-17 15:22:02 +01:00
|
|
|
revised_at: Time.now,
|
2019-08-15 23:07:54 +01:00
|
|
|
text: @text,
|
2020-06-17 21:27:58 +01:00
|
|
|
markdown: @markdown,
|
2019-08-15 23:07:54 +01:00
|
|
|
media_attachments: @media || [],
|
|
|
|
sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
|
|
|
|
spoiler_text: @options[:spoiler_text] || '',
|
|
|
|
visibility: @visibility,
|
|
|
|
language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
|
|
|
|
application: @options[:application],
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
end
|