Skip to content

Commit f947d3f

Browse files
committed
updoots
1 parent add68d4 commit f947d3f

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

plugin.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ def process_batch(post_ids)
174174
end
175175
end
176176

177-
on(:post_process) { |post| Jobs.enqueue(:detect_translation, post_id: post.id) }
178-
179177
topic_view_post_custom_fields_allowlister { [::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] }
180178

181179
require_relative "lib/discourse_translator/guardian_extension"

spec/jobs/detect_posts_translation_spec.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
describe Jobs::DetectPostsTranslation do
66
fab!(:posts) { Fabricate.times(5, :post) }
7+
let(:redis_key) { DiscourseTranslator::LANG_DETECT_NEEDED }
78

89
before do
910
SiteSetting.translator_enabled = true
@@ -14,7 +15,7 @@
1415
{ translated_text: "大丈夫", source_language_code: "en", target_language_code: "jp" },
1516
)
1617
Aws::Translate::Client.stubs(:new).returns(client)
17-
posts.each { |post| Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, post.id) }
18+
posts.each { |post| Discourse.redis.sadd?(redis_key, post.id) }
1819
end
1920

2021
it "processes posts in batches and updates their translations" do
@@ -25,7 +26,7 @@
2526
expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).not_to be_nil
2627
end
2728

28-
expect(Discourse.redis.smembers(DiscourseTranslator::LANG_DETECT_NEEDED)).to be_empty
29+
expect(Discourse.redis.smembers(redis_key)).to be_empty
2930
end
3031

3132
it "does not process posts if the translator is disabled" do
@@ -37,48 +38,39 @@
3738
expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).to be_nil
3839
end
3940

40-
expect(Discourse.redis.smembers(DiscourseTranslator::LANG_DETECT_NEEDED)).to match_array(
41-
posts.map(&:id).map(&:to_s),
42-
)
41+
expect(Discourse.redis.smembers(redis_key)).to match_array(posts.map(&:id).map(&:to_s))
4342
end
4443

4544
it "processes a maximum of MAX_QUEUE_SIZE posts per run" do
4645
large_number = 2000
47-
large_number.times { |i| Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, i + 1) }
46+
large_number.times { |i| Discourse.redis.sadd?(redis_key, i + 1) }
4847
described_class.new.execute({})
4948

50-
remaining = Discourse.redis.scard(DiscourseTranslator::LANG_DETECT_NEEDED)
49+
remaining = Discourse.redis.scard(redis_key)
5150
expect(remaining).to eq(large_number - Jobs::DetectPostsTranslation::MAX_QUEUE_SIZE)
5251
end
5352

5453
it "handles an empty Redis queue gracefully" do
55-
Discourse.redis.del(DiscourseTranslator::LANG_DETECT_NEEDED)
54+
Discourse.redis.del(redis_key)
5655
expect { described_class.new.execute({}) }.not_to raise_error
5756
end
5857

5958
it "removes successfully processed posts from Redis" do
6059
described_class.new.execute({})
6160

62-
posts.each do |post|
63-
expect(
64-
Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id),
65-
).to be_falsey
66-
end
61+
posts.each { |post| expect(Discourse.redis.sismember(redis_key, post.id)).to be_falsey }
6762
end
6863

6964
it "skips posts that no longer exist" do
7065
non_existent_post_id = -1
71-
Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, non_existent_post_id)
66+
Discourse.redis.sadd?(redis_key, non_existent_post_id)
7267

7368
expect { described_class.new.execute({}) }.not_to raise_error
7469

75-
expect(
76-
Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, non_existent_post_id),
77-
).to be_falsey
70+
expect(Discourse.redis.sismember(redis_key, non_existent_post_id)).to be_falsey
7871
end
7972

8073
it "ensures posts are processed within a distributed mutex" do
81-
mutex_spy = instance_spy(DistributedMutex)
8274
allow(DistributedMutex).to receive(:synchronize).and_yield
8375

8476
described_class.new.execute({})

spec/models/post_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require "rails_helper"
44

55
RSpec.describe Post do
6+
before { SiteSetting.translator_enabled = true }
7+
68
describe "translator custom fields" do
79
let(:post) do
810
Fabricate(
@@ -17,10 +19,6 @@
1719
)
1820
end
1921

20-
before { SiteSetting.translator_enabled = true }
21-
22-
after { SiteSetting.translator_enabled = false }
23-
2422
it "should reset custom fields when post has been updated" do
2523
post.update!(raw: "this is an updated post")
2624

spec/serializers/post_serializer_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@
8181
post.custom_fields["detected_language"] = nil
8282
post.save_custom_fields
8383

84-
serializer.can_translate
85-
86-
expect(
87-
Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id),
88-
).to eq(true)
84+
expect { serializer.can_translate }.to change {
85+
Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id)
86+
}.from(false).to(true)
8987
end
9088
end
9189

0 commit comments

Comments
 (0)