Skip to content

Commit

Permalink
FIX: Avoid publishing post if there is no change (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
nattsw committed May 3, 2024
1 parent 1c84ffe commit 0de3f3f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ en:
not_supported: "This language is not supported by the translator."
too_long: "This post is too long to be translated by the translator."
not_available: "The translator service is currently not available."
amazon:
invalid_credentials: "The provided credentials for AWS translate are invalid."

microsoft:
missing_token: "The translator was unable to retrieve a valid token."
Expand Down
6 changes: 4 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def execute(args)

DistributedMutex.synchronize("detect_translation_#{post.id}") do
"DiscourseTranslator::#{SiteSetting.translator}".constantize.detect(post)
post.save_custom_fields unless post.custom_fields_clean?
post.publish_change_to_clients! :revised
if !post.custom_fields_clean?
post.save_custom_fields
post.publish_change_to_clients! :revised
end
end
end
end
Expand Down
24 changes: 14 additions & 10 deletions services/discourse_translator/amazon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,20 @@ def self.detect(topic_or_post)

return if text.blank?

detected_lang =
client.translate_text(
{
text: text,
source_language_code: "auto",
target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale],
},
)&.source_language_code

assign_lang_custom_field(topic_or_post, detected_lang)
begin
detected_lang =
client.translate_text(
{
text: text,
source_language_code: "auto",
target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale],
},
)&.source_language_code

assign_lang_custom_field(topic_or_post, detected_lang)
rescue Aws::Errors::MissingCredentialsError
raise I18n.t("translator.amazon.invalid_credentials")
end
end

def self.translate(topic_or_post)
Expand Down
12 changes: 11 additions & 1 deletion spec/jobs/detect_translation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@
it "detects translation" do
post = Fabricate(:post, raw: "this is a sample post")

Jobs::DetectTranslation.new.execute(post_id: post.id)
messages = MessageBus.track_publish { Jobs::DetectTranslation.new.execute(post_id: post.id) }

expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).to eq("en")
expect(messages.size).to eq(1)
end

it "does not publish change if no change in translation" do
post = Fabricate(:post, raw: "this is a sample post")
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en"
post.save_custom_fields

messages = MessageBus.track_publish { Jobs::DetectTranslation.new.execute(post_id: post.id) }
expect(messages.size).to eq(0)
end
end
end

0 comments on commit 0de3f3f

Please sign in to comment.