discourse/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb
Penar Musaraj 90baea1ea7
FEATURE: Switch from opt-in to opt-out for categories in AI translations (#40169)
This PR changes Discourse AI translations from an opt-in category model
to an opt-out model: instead of translating only selected
`ai_translation_target_categories`, it introduces
`ai_translation_excluded_categories`, updates the admin UI copy and save
flow, changes topic/post/category candidate selection and detection jobs
to translate all non-excluded categories by default, and adds a
migration that converts existing target-category settings into the
equivalent excluded-category list for existing sites.

It also updates all related specs.

---------

Co-authored-by: discourse-patch-triage[bot] <272280883+discourse-patch-triage[bot]@users.noreply.github.com>
2026-05-26 14:51:04 -04:00

83 lines
2.7 KiB
Ruby
Vendored

# frozen_string_literal: true
module Jobs
class DetectTranslateTopic < ::Jobs::Base
cluster_concurrency 1
sidekiq_options retry: false
def execute(args)
return if !DiscourseAi::Translation.enabled?
return if args[:topic_id].blank?
unless DiscourseAi::Translation.credits_available_for_topic_detection?
Rails.logger.info(
"Translation skipped for topic: insufficient credits. Will resume when credits reset.",
)
return
end
topic = Topic.find_by(id: args[:topic_id])
return if topic.blank? || topic.title.blank? || topic.deleted_at.present?
force = args[:force] || false
return if topic.user_id <= 0 && !force && !SiteSetting.ai_translation_include_bot_content
if force
# no restrictions
elsif topic.archetype == Archetype.private_message
case SiteSetting.ai_translation_personal_messages
when "all"
# allow
when "group"
return unless TopicAllowedGroup.exists?(topic_id: topic.id)
else
return
end
else
return if DiscourseAi::Translation.category_excluded?(topic.category_id)
end
if (detected_locale = topic.locale).blank?
begin
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
rescue FinalDestination::SSRFDetector::LookupFailedError
# this job is non-critical
# the backfill job will handle failures
return
end
end
return if detected_locale.blank?
locales = DiscourseAi::Translation.locales
return if locales.blank?
existing_base_locales =
TopicLocalization
.where(topic_id: topic.id)
.pluck(:locale)
.map { |l| l.split("_").first }
.to_set
locales.each do |locale|
next if LocaleNormalizer.is_same?(locale, detected_locale)
base_locale = locale.split("_").first
exists = existing_base_locales.include?(base_locale)
has_quota = DiscourseAi::Translation::TopicLocalizer.has_relocalize_quota?(topic, locale)
next if !force && exists && !has_quota
begin
DiscourseAi::Translation::TopicLocalizer.localize(topic, locale)
rescue FinalDestination::SSRFDetector::LookupFailedError
# do nothing, there are too many sporadic lookup failures
rescue => e
DiscourseAi::Translation::VerboseLogger.log(
"Failed to translate topic #{topic.id} to #{locale}: #{e.message}\n\n#{e.backtrace[0..3].join("\n")}",
)
end
end
MessageBus.publish("/topic/#{topic.id}", reload_topic: true)
end
end
end