From 7264773a0f90e0b494a89d5304494c60a462a468 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 15 Mar 2019 02:39:20 +0100 Subject: [PATCH] Add a preferences API so apps can share basic behaviours (#10109) --- .../api/v1/preferences_controller.rb | 12 ++++++++ .../rest/preferences_serializer.rb | 30 +++++++++++++++++++ config/routes.rb | 1 + 3 files changed, 43 insertions(+) create mode 100644 app/controllers/api/v1/preferences_controller.rb create mode 100644 app/serializers/rest/preferences_serializer.rb diff --git a/app/controllers/api/v1/preferences_controller.rb b/app/controllers/api/v1/preferences_controller.rb new file mode 100644 index 00000000000000..077d39f5d740a4 --- /dev/null +++ b/app/controllers/api/v1/preferences_controller.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class Api::V1::PreferencesController < Api::BaseController + before_action -> { doorkeeper_authorize! :read, :'read:accounts' } + before_action :require_user! + + respond_to :json + + def index + render json: current_account, serializer: REST::PreferencesSerializer + end +end diff --git a/app/serializers/rest/preferences_serializer.rb b/app/serializers/rest/preferences_serializer.rb new file mode 100644 index 00000000000000..119f0e06d89a58 --- /dev/null +++ b/app/serializers/rest/preferences_serializer.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class REST::PreferencesSerializer < ActiveModel::Serializer + attribute :posting_default_privacy, key: 'posting:default:visibility' + attribute :posting_default_sensitive, key: 'posting:default:sensitive' + attribute :posting_default_language, key: 'posting:default:language' + + attribute :reading_default_sensitive_media, key: 'reading:expand:media' + attribute :reading_default_sensitive_text, key: 'reading:expand:spoilers' + + def posting_default_privacy + object.user.setting_default_privacy + end + + def posting_default_sensitive + object.user.setting_default_sensitive + end + + def posting_default_language + object.user.setting_default_language.presence + end + + def reading_default_sensitive_media + object.user.setting_display_media + end + + def reading_default_sensitive_text + object.user.setting_expand_spoilers + end +end diff --git a/config/routes.rb b/config/routes.rb index 4a75d69523de37..1bb875264207f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -286,6 +286,7 @@ resources :custom_emojis, only: [:index] resources :suggestions, only: [:index, :destroy] resources :scheduled_statuses, only: [:index, :show, :update, :destroy] + resources :preferences, only: [:index] resources :conversations, only: [:index, :destroy] do member do