From dd79b3deda077986a8e2179632521f41dcf91dfd Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Mon, 24 Mar 2025 15:23:34 -0300 Subject: [PATCH 1/3] DEV: Disable automatic translation via reactive events when automatic rate is zero --- .../automatic_translations.rb | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/discourse_translator/automatic_translations.rb b/lib/discourse_translator/automatic_translations.rb index d85e15bc..df7971d3 100644 --- a/lib/discourse_translator/automatic_translations.rb +++ b/lib/discourse_translator/automatic_translations.rb @@ -4,22 +4,37 @@ module DiscourseTranslator class AutomaticTranslations def inject(plugin) plugin.on(:post_process_cooked) do |_, post| - if SiteSetting.automatic_translation_target_languages.present? && post.user_id > 0 + if translatable?(content) Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id) end end plugin.on(:topic_created) do |topic| - if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0 + if translatable?(content) Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end plugin.on(:topic_edited) do |topic| - if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0 + if translatable?(content) Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end end + + def translatable?(content) + return false if SiteSetting.automatic_translation_target_languages.blank? + return false if content.user_id <= 0 + return false if SiteSetting.automatic_translation_backfill_rate <= 0 + return true unless SiteSetting.automatic_translation_backfill_limit_to_public_content + + public_categories = Category.where(read_restricted: false).pluck(:id) + + if content.class == Post + public_categories.include?(content.topic.category_id) + elsif content.class == Topic + public_categories.include?(content.category_id) + end + end end end From b6b844675a4975c2748a7cf7c6be9e9fad865ea1 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Mon, 24 Mar 2025 15:28:50 -0300 Subject: [PATCH 2/3] fix --- lib/discourse_translator/automatic_translations.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/discourse_translator/automatic_translations.rb b/lib/discourse_translator/automatic_translations.rb index df7971d3..f7d9b895 100644 --- a/lib/discourse_translator/automatic_translations.rb +++ b/lib/discourse_translator/automatic_translations.rb @@ -4,19 +4,19 @@ module DiscourseTranslator class AutomaticTranslations def inject(plugin) plugin.on(:post_process_cooked) do |_, post| - if translatable?(content) + if translatable?(post) Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id) end end plugin.on(:topic_created) do |topic| - if translatable?(content) + if translatable?(topic) Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end plugin.on(:topic_edited) do |topic| - if translatable?(content) + if translatable?(topic) Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end From 058ea1f4d8043970e80226facf5a96bf42348263 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Mon, 24 Mar 2025 16:58:55 -0300 Subject: [PATCH 3/3] Update lib/discourse_translator/automatic_translations.rb Co-authored-by: Keegan George --- lib/discourse_translator/automatic_translations.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/discourse_translator/automatic_translations.rb b/lib/discourse_translator/automatic_translations.rb index f7d9b895..c08f8735 100644 --- a/lib/discourse_translator/automatic_translations.rb +++ b/lib/discourse_translator/automatic_translations.rb @@ -30,10 +30,12 @@ def translatable?(content) public_categories = Category.where(read_restricted: false).pluck(:id) - if content.class == Post + if content.is_a(Post) public_categories.include?(content.topic.category_id) - elsif content.class == Topic + elsif content.is_a(Topic) public_categories.include?(content.category_id) + else + false end end end