mirror of
https://github.com/glitch-soc/mastodon.git
synced 2024-11-23 08:34:13 -05: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:
parent
6b896b20cc
commit
3a4d3e9d4b
21
app/controllers/api/v1/instances/languages_controller.rb
Normal file
21
app/controllers/api/v1/instances/languages_controller.rb
Normal 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
|
20
app/presenters/language_presenter.rb
Normal file
20
app/presenters/language_presenter.rb
Normal 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
|
5
app/serializers/rest/language_serializer.rb
Normal file
5
app/serializers/rest/language_serializer.rb
Normal file
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::LanguageSerializer < ActiveModel::Serializer
|
||||
attributes :code, :name
|
||||
end
|
@ -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
|
||||
|
||||
|
19
spec/requests/api/v1/instances/languages_spec.rb
Normal file
19
spec/requests/api/v1/instances/languages_spec.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user