Skip to content

Commit

Permalink
Fix post translation erroring out (v4.1.x) (mastodon#26990)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Sep 20, 2023
1 parent af0ee12 commit 5d93c5f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/lib/translation_service/deepl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def transform_response(str)

raise UnexpectedResponseError unless json.is_a?(Hash)

Translation.new(text: json.dig('translations', 0, 'text'), detected_source_language: json.dig('translations', 0, 'detected_source_language')&.downcase, provider: 'DeepL.com')
Translation.new(text: Sanitize.fragment(json.dig('translations', 0, 'text'), Sanitize::Config::MASTODON_STRICT), detected_source_language: json.dig('translations', 0, 'detected_source_language')&.downcase, provider: 'DeepL.com')
rescue Oj::ParseError
raise UnexpectedResponseError
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/translation_service/libre_translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def transform_response(str, source_language)

raise UnexpectedResponseError unless json.is_a?(Hash)

Translation.new(text: json['translatedText'], detected_source_language: source_language, provider: 'LibreTranslate')
Translation.new(text: Sanitize.fragment(json['translatedText'], Sanitize::Config::MASTODON_STRICT), detected_source_language: source_language, provider: 'LibreTranslate')
rescue Oj::ParseError
raise UnexpectedResponseError
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/translate_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def call(status, target_language)
@content = status_content_format(@status)
@target_language = target_language

Rails.cache.fetch("translations/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) do
Sanitize.fragment(translation_backend.translate(@content, @status.language, @target_language), Sanitize::Config::MASTODON_STRICT)
Rails.cache.fetch("translations:v2/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) do
translation_backend.translate(@content, @status.language, @target_language)
end
end

Expand Down

0 comments on commit 5d93c5f

Please sign in to comment.