diff --git a/app/jobs/scheduled/automatic_translation_backfill.rb b/app/jobs/scheduled/automatic_translation_backfill.rb index 538a0040..2b8a2f06 100644 --- a/app/jobs/scheduled/automatic_translation_backfill.rb +++ b/app/jobs/scheduled/automatic_translation_backfill.rb @@ -46,6 +46,7 @@ def fetch_untranslated_model_ids( ) AND m.deleted_at IS NULL AND m.#{content_column} != '' + AND m.user_id > 0 ORDER BY m.id DESC LIMIT :limit SQL diff --git a/plugin.rb b/plugin.rb index f1d380d1..b2d9de0a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -33,25 +33,25 @@ module ::DiscourseTranslator end on(:post_process_cooked) do |_, post| - if Guardian.new.can_detect_language?(post) + if Guardian.new.can_detect_language?(post) && post.user_id > 0 Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, post.id) end end on(:post_process_cooked) do |_, post| - if SiteSetting.automatic_translation_target_languages.present? + if SiteSetting.automatic_translation_target_languages.present? && post.user_id > 0 Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id) end end on(:topic_created) do |topic| - if SiteSetting.automatic_translation_target_languages.present? + if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0 Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end on(:topic_edited) do |topic| - if SiteSetting.automatic_translation_target_languages.present? + if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0 Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id) end end diff --git a/spec/jobs/automatic_translation_backfill_spec.rb b/spec/jobs/automatic_translation_backfill_spec.rb index 83b1115b..7652eb01 100644 --- a/spec/jobs/automatic_translation_backfill_spec.rb +++ b/spec/jobs/automatic_translation_backfill_spec.rb @@ -193,5 +193,13 @@ def expect_google_translate(text) result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es]) expect(result).not_to include(post_1.id) end + + it "does not return posts by bots" do + post_1.update(user: Discourse.system_user) + + result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es]) + + expect(result).not_to include(post_1.id) + end end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 7cf507ec..a7bdb6ad 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -3,7 +3,10 @@ require "rails_helper" RSpec.describe Post do - before { SiteSetting.translator_enabled = true } + before do + SiteSetting.translator_enabled = true + SiteSetting.create_topic_allowed_groups = Group::AUTO_GROUPS[:everyone] + end describe "translatable" do fab!(:post) @@ -70,10 +73,7 @@ fab!(:topic) fab!(:user) { Fabricate(:user, groups: [group]) } - before do - Jobs.run_immediately! - SiteSetting.create_topic_allowed_groups = Group::AUTO_GROUPS[:everyone] - end + before { Jobs.run_immediately! } it "queues the post for language detection when user and posts are in the right group" do SiteSetting.restrict_translation_by_poster_group = "#{group.id}" @@ -92,6 +92,19 @@ ).to be_truthy end + it "does not queue bot posts for language detection" do + SiteSetting.restrict_translation_by_poster_group = Group::AUTO_GROUPS[:everyone] + post = + PostCreator.new( + Discourse.system_user, + { title: "hello world topic", raw: "my name is cat", category: Fabricate(:category).id }, + ).create + + expect( + Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id), + ).to be_falsey + end + context "when user and posts are not in the right group" do it "does not queue the post for language detection" do SiteSetting.restrict_translation_by_poster_group = "#{group.id + 1}" @@ -111,4 +124,38 @@ end end end + + describe "automatic translation job" do + fab!(:user) + + it "enqueues translate_translatable job when post cooked" do + SiteSetting.automatic_translation_target_languages = "es" + post = Fabricate(:post, user: user) + CookedPostProcessor.new(post).post_process + + expect_job_enqueued( + job: :translate_translatable, + args: { + type: "Post", + translatable_id: post.id, + }, + ) + end + + it "does not enqueues translate_translatable job for bot posts" do + SiteSetting.automatic_translation_target_languages = "es" + post = Fabricate(:post, user: Discourse.system_user) + CookedPostProcessor.new(post).post_process + + expect( + job_enqueued?( + job: :translate_translatable, + args: { + type: "Post", + translatable_id: post.id, + }, + ), + ).to eq false + end + end end