2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/app/controllers/topic_localizations_controller.rb

52 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class TopicLocalizationsController < ApplicationController
before_action :ensure_logged_in
def show
topic = Topic.find_by(id: params[:topic_id])
raise Discourse::NotFound unless topic
guardian.ensure_can_see!(topic)
localization = TopicLocalization.find_by(topic_id: topic.id, locale: params[:locale])
raise Discourse::NotFound unless localization
render json: TopicLocalizationSerializer.new(localization, root: false)
end
def create_or_update
topic_id, locale, title = params.require(%i[topic_id locale title])
topic = Topic.find_by(id: topic_id)
raise Discourse::NotFound unless topic
guardian.ensure_can_localize_topic!(topic)
topic_localization = TopicLocalization.find_by(topic_id: topic.id, locale: params[:locale])
if topic_localization
TopicLocalizationUpdater.update(
topic:,
locale: params[:locale],
title: params[:title],
user: current_user,
)
render json: success_json, status: :ok
else
TopicLocalizationCreator.create(topic:, locale:, title:, user: current_user)
render json: success_json, status: :created
end
end
def destroy
topic_id, locale = params.require(%i[topic_id locale])
topic = Topic.find_by(id: topic_id)
raise Discourse::NotFound unless topic
guardian.ensure_can_localize_topic!(topic)
TopicLocalizationDestroyer.destroy(topic:, locale:, acting_user: current_user)
head :no_content
end
end