1
0
mirror of https://github.com/whippyshou/mastodon synced 2024-12-04 01:38:55 +09:00

Add GET /api/v1/instance/languages to REST API (#24443)

Co-authored-by: Eugen Rochko <eugen@zeonfederated.com>
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Daniel M Brasil 2023-08-03 06:25:47 -03:00 committed by GitHub
parent 6b896b20cc
commit 3a4d3e9d4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
class Api::V1::Instances::LanguagesController < Api::BaseController
skip_before_action :require_authenticated_user!, unless: :limited_federation_mode?
skip_around_action :set_locale
before_action :set_languages
vary_by ''
def show
cache_even_if_authenticated!
render json: @languages, each_serializer: REST::LanguageSerializer
end
private
def set_languages
@languages = LanguagesHelper::SUPPORTED_LOCALES.keys.map { |code| LanguagePresenter.new(code) }
end
end

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
class LanguagePresenter < ActiveModelSerializers::Model
attributes :code, :name, :native_name
def initialize(code)
super()
@code = code
@item = LanguagesHelper::SUPPORTED_LOCALES[code]
end
def name
@item[0]
end
def native_name
@item[1]
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class REST::LanguageSerializer < ActiveModel::Serializer
attributes :code, :name
end

View File

@ -120,6 +120,7 @@ namespace :api, format: false do
resource :privacy_policy, only: [:show], controller: 'instances/privacy_policies'
resource :extended_description, only: [:show], controller: 'instances/extended_descriptions'
resource :translation_languages, only: [:show], controller: 'instances/translation_languages'
resource :languages, only: [:show], controller: 'instances/languages'
resource :activity, only: [:show], controller: 'instances/activity'
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Languages' do
describe 'GET /api/v1/instance/languages' do
before do
get '/api/v1/instance/languages'
end
it 'returns http success' do
expect(response).to have_http_status(200)
end
it 'returns the supported languages' do
expect(body_as_json.pluck(:code)).to match_array LanguagesHelper::SUPPORTED_LOCALES.keys.map(&:to_s)
end
end
end