Skip to content

Commit 59f10e9

Browse files
authored
FIX: Do not translate bot posts (#213)
Bot (e.g. Discobot) posts happen a lot during signups to users of various locales. We do not want to translate these.
1 parent e635b47 commit 59f10e9

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

app/jobs/scheduled/automatic_translation_backfill.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def fetch_untranslated_model_ids(
4646
)
4747
AND m.deleted_at IS NULL
4848
AND m.#{content_column} != ''
49+
AND m.user_id > 0
4950
ORDER BY m.id DESC
5051
LIMIT :limit
5152
SQL

plugin.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ module ::DiscourseTranslator
3333
end
3434

3535
on(:post_process_cooked) do |_, post|
36-
if Guardian.new.can_detect_language?(post)
36+
if Guardian.new.can_detect_language?(post) && post.user_id > 0
3737
Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, post.id)
3838
end
3939
end
4040

4141
on(:post_process_cooked) do |_, post|
42-
if SiteSetting.automatic_translation_target_languages.present?
42+
if SiteSetting.automatic_translation_target_languages.present? && post.user_id > 0
4343
Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id)
4444
end
4545
end
4646

4747
on(:topic_created) do |topic|
48-
if SiteSetting.automatic_translation_target_languages.present?
48+
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
4949
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
5050
end
5151
end
5252

5353
on(:topic_edited) do |topic|
54-
if SiteSetting.automatic_translation_target_languages.present?
54+
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
5555
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
5656
end
5757
end

spec/jobs/automatic_translation_backfill_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,13 @@ def expect_google_translate(text)
193193
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
194194
expect(result).not_to include(post_1.id)
195195
end
196+
197+
it "does not return posts by bots" do
198+
post_1.update(user: Discourse.system_user)
199+
200+
result = described_class.new.fetch_untranslated_model_ids(Post, "cooked", 50, %w[de es])
201+
202+
expect(result).not_to include(post_1.id)
203+
end
196204
end
197205
end

spec/models/post_spec.rb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
require "rails_helper"
44

55
RSpec.describe Post do
6-
before { SiteSetting.translator_enabled = true }
6+
before do
7+
SiteSetting.translator_enabled = true
8+
SiteSetting.create_topic_allowed_groups = Group::AUTO_GROUPS[:everyone]
9+
end
710

811
describe "translatable" do
912
fab!(:post)
@@ -70,10 +73,7 @@
7073
fab!(:topic)
7174
fab!(:user) { Fabricate(:user, groups: [group]) }
7275

73-
before do
74-
Jobs.run_immediately!
75-
SiteSetting.create_topic_allowed_groups = Group::AUTO_GROUPS[:everyone]
76-
end
76+
before { Jobs.run_immediately! }
7777

7878
it "queues the post for language detection when user and posts are in the right group" do
7979
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
@@ -92,6 +92,19 @@
9292
).to be_truthy
9393
end
9494

95+
it "does not queue bot posts for language detection" do
96+
SiteSetting.restrict_translation_by_poster_group = Group::AUTO_GROUPS[:everyone]
97+
post =
98+
PostCreator.new(
99+
Discourse.system_user,
100+
{ title: "hello world topic", raw: "my name is cat", category: Fabricate(:category).id },
101+
).create
102+
103+
expect(
104+
Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id),
105+
).to be_falsey
106+
end
107+
95108
context "when user and posts are not in the right group" do
96109
it "does not queue the post for language detection" do
97110
SiteSetting.restrict_translation_by_poster_group = "#{group.id + 1}"
@@ -111,4 +124,38 @@
111124
end
112125
end
113126
end
127+
128+
describe "automatic translation job" do
129+
fab!(:user)
130+
131+
it "enqueues translate_translatable job when post cooked" do
132+
SiteSetting.automatic_translation_target_languages = "es"
133+
post = Fabricate(:post, user: user)
134+
CookedPostProcessor.new(post).post_process
135+
136+
expect_job_enqueued(
137+
job: :translate_translatable,
138+
args: {
139+
type: "Post",
140+
translatable_id: post.id,
141+
},
142+
)
143+
end
144+
145+
it "does not enqueues translate_translatable job for bot posts" do
146+
SiteSetting.automatic_translation_target_languages = "es"
147+
post = Fabricate(:post, user: Discourse.system_user)
148+
CookedPostProcessor.new(post).post_process
149+
150+
expect(
151+
job_enqueued?(
152+
job: :translate_translatable,
153+
args: {
154+
type: "Post",
155+
translatable_id: post.id,
156+
},
157+
),
158+
).to eq false
159+
end
160+
end
114161
end

0 commit comments

Comments
 (0)