about summary refs log tree commit diff
path: root/app/controllers/api
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-09-23 23:00:12 +0200
committerGitHub <noreply@github.com>2022-09-23 23:00:12 +0200
commit0d6b878808a02aa4a544e894f06419c0f612c163 (patch)
tree119723ea46dd8525c370fee1235c3c9d42e55937 /app/controllers/api
parentd2f7e30a283a1dca1f7974884ac0c237b93903ad (diff)
Add user content translations with configurable backends (#19218)
Diffstat (limited to 'app/controllers/api')
-rw-r--r--app/controllers/api/v1/statuses/translations_controller.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/app/controllers/api/v1/statuses/translations_controller.rb b/app/controllers/api/v1/statuses/translations_controller.rb
new file mode 100644
index 000000000..540b17d00
--- /dev/null
+++ b/app/controllers/api/v1/statuses/translations_controller.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class Api::V1::Statuses::TranslationsController < Api::BaseController
+  include Authorization
+
+  before_action -> { doorkeeper_authorize! :read, :'read:statuses' }
+  before_action :set_status
+  before_action :set_translation
+
+  rescue_from TranslationService::NotConfiguredError, with: :not_found
+  rescue_from TranslationService::UnexpectedResponseError, TranslationService::QuotaExceededError, TranslationService::TooManyRequestsError, with: :service_unavailable
+
+  def create
+    render json: @translation, serializer: REST::TranslationSerializer
+  end
+
+  private
+
+  def set_status
+    @status = Status.find(params[:status_id])
+    authorize @status, :show?
+  rescue Mastodon::NotPermittedError
+    not_found
+  end
+
+  def set_translation
+    @translation = TranslateStatusService.new.call(@status, content_locale)
+  end
+end