translations_controller.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::TranslationsController < Api::BaseController
  3. include Authorization
  4. before_action -> { doorkeeper_authorize! :read, :'read:statuses' }
  5. before_action :set_status
  6. before_action :set_translation
  7. rescue_from TranslationService::NotConfiguredError, with: :not_found
  8. rescue_from TranslationService::UnexpectedResponseError, with: :service_unavailable
  9. rescue_from TranslationService::QuotaExceededError do
  10. render json: { error: I18n.t('translation.errors.quota_exceeded') }, status: 503
  11. end
  12. rescue_from TranslationService::TooManyRequestsError do
  13. render json: { error: I18n.t('translation.errors.too_many_requests') }, status: 503
  14. end
  15. def create
  16. render json: @translation, serializer: REST::TranslationSerializer
  17. end
  18. private
  19. def set_status
  20. @status = Status.find(params[:status_id])
  21. authorize @status, :show?
  22. rescue Mastodon::NotPermittedError
  23. not_found
  24. end
  25. def set_translation
  26. @translation = TranslateStatusService.new.call(@status, content_locale)
  27. end
  28. end