about summary refs log tree commit diff
path: root/app/services/translate_status_service.rb
blob: 92d8b62a05d6fa2e1f2b7562b2e47db108b8d6e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

class TranslateStatusService < BaseService
  CACHE_TTL = 1.day.freeze

  include FormattingHelper

  def call(status, target_language)
    raise Mastodon::NotPermittedError unless status.translatable?

    @status = status
    @content = status_content_format(@status)
    @target_language = target_language

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

  private

  def translation_backend
    TranslationService.configured
  end

  def content_hash
    Digest::SHA256.base64digest(@content)
  end
end