2022-09-24 07:00:12 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TranslateStatusService < BaseService
|
|
|
|
CACHE_TTL = 1.day.freeze
|
|
|
|
|
2022-09-28 09:02:01 +10:00
|
|
|
include FormattingHelper
|
|
|
|
|
2022-09-24 07:00:12 +10:00
|
|
|
def call(status, target_language)
|
|
|
|
raise Mastodon::NotPermittedError unless status.public_visibility? || status.unlisted_visibility?
|
|
|
|
|
|
|
|
@status = status
|
2022-09-28 09:02:01 +10:00
|
|
|
@content = status_content_format(@status)
|
2022-09-24 07:00:12 +10:00
|
|
|
@target_language = target_language
|
|
|
|
|
2023-09-20 23:59:57 +10:00
|
|
|
Rails.cache.fetch("translations:v2/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) do
|
|
|
|
translation_backend.translate(@content, @status.language, @target_language)
|
2023-09-20 00:53:21 +10:00
|
|
|
end
|
2022-09-24 07:00:12 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def translation_backend
|
|
|
|
TranslationService.configured
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_hash
|
2022-09-28 09:02:01 +10:00
|
|
|
Digest::SHA256.base64digest(@content)
|
2022-09-24 07:00:12 +10:00
|
|
|
end
|
|
|
|
end
|