0
0
Fork 0

Refactor JSON templates to be generated with ActiveModelSerializers instead of Rabl (#4090)

This commit is contained in:
Eugen Rochko 2017-07-07 04:02:06 +02:00 committed by GitHub
parent 2d6128672f
commit 8b2cad5637
80 changed files with 425 additions and 301 deletions

View file

@ -6,13 +6,13 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController
def show
@account = current_account
render 'api/v1/accounts/show'
render json: @account, serializer: REST::AccountSerializer
end
def update
current_account.update!(account_params)
@account = current_account
render 'api/v1/accounts/show'
render json: @account, serializer: REST::AccountSerializer
end
private

View file

@ -9,7 +9,7 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
def index
@accounts = load_accounts
render 'api/v1/accounts/index'
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -9,7 +9,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
def index
@accounts = load_accounts
render 'api/v1/accounts/index'
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -8,16 +8,15 @@ class Api::V1::Accounts::RelationshipsController < Api::BaseController
def index
@accounts = Account.where(id: account_ids).select('id')
@following = Account.following_map(account_ids, current_user.account_id)
@followed_by = Account.followed_by_map(account_ids, current_user.account_id)
@blocking = Account.blocking_map(account_ids, current_user.account_id)
@muting = Account.muting_map(account_ids, current_user.account_id)
@requested = Account.requested_map(account_ids, current_user.account_id)
@domain_blocking = Account.domain_blocking_map(account_ids, current_user.account_id)
render json: @accounts, each_serializer: REST::RelationshipSerializer, relationships: relationships
end
private
def relationships
AccountRelationshipsPresenter.new(@accounts, current_user.account_id)
end
def account_ids
@_account_ids ||= Array(params[:id]).map(&:to_i)
end

View file

@ -8,8 +8,7 @@ class Api::V1::Accounts::SearchController < Api::BaseController
def show
@accounts = account_search
render 'api/v1/accounts/index'
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -9,6 +9,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
def index
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
@ -18,9 +19,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
end
def load_statuses
cached_account_statuses.tap do |statuses|
set_maps(statuses)
end
cached_account_statuses
end
def cached_account_statuses

View file

@ -8,49 +8,38 @@ class Api::V1::AccountsController < Api::BaseController
respond_to :json
def show; end
def show
render json: @account, serializer: REST::AccountSerializer
end
def follow
FollowService.new.call(current_user.account, @account.acct)
set_relationship
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
def block
BlockService.new.call(current_user.account, @account)
@following = { @account.id => false }
@followed_by = { @account.id => false }
@blocking = { @account.id => true }
@requested = { @account.id => false }
@muting = { @account.id => current_account.muting?(@account.id) }
@domain_blocking = { @account.id => current_account.domain_blocking?(@account.domain) }
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
def mute
MuteService.new.call(current_user.account, @account)
set_relationship
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
def unfollow
UnfollowService.new.call(current_user.account, @account)
set_relationship
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
def unblock
UnblockService.new.call(current_user.account, @account)
set_relationship
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
def unmute
UnmuteService.new.call(current_user.account, @account)
set_relationship
render :relationship
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end
private
@ -59,12 +48,7 @@ class Api::V1::AccountsController < Api::BaseController
@account = Account.find(params[:id])
end
def set_relationship
@following = Account.following_map([@account.id], current_user.account_id)
@followed_by = Account.followed_by_map([@account.id], current_user.account_id)
@blocking = Account.blocking_map([@account.id], current_user.account_id)
@muting = Account.muting_map([@account.id], current_user.account_id)
@requested = Account.requested_map([@account.id], current_user.account_id)
@domain_blocking = Account.domain_blocking_map([@account.id], current_user.account_id)
def relationships
AccountRelationshipsPresenter.new([@account.id], current_user.account_id)
end
end

View file

@ -5,6 +5,7 @@ class Api::V1::AppsController < Api::BaseController
def create
@app = Doorkeeper::Application.create!(application_options)
render json: @app, serializer: REST::ApplicationSerializer
end
private

View file

@ -9,6 +9,7 @@ class Api::V1::BlocksController < Api::BaseController
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -9,14 +9,13 @@ class Api::V1::FavouritesController < Api::BaseController
def index
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
def load_statuses
cached_favourites.tap do |statuses|
set_maps(statuses)
end
cached_favourites
end
def cached_favourites

View file

@ -7,6 +7,7 @@ class Api::V1::FollowRequestsController < Api::BaseController
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end
def authorize

View file

@ -10,7 +10,7 @@ class Api::V1::FollowsController < Api::BaseController
raise ActiveRecord::RecordNotFound if follow_params[:uri].blank?
@account = FollowService.new.call(current_user.account, target_uri).try(:target_account)
render :show
render json: @account, serializer: REST::AccountSerializer
end
private

View file

@ -3,5 +3,7 @@
class Api::V1::InstancesController < Api::BaseController
respond_to :json
def show; end
def show
render json: {}, serializer: REST::InstanceSerializer
end
end

View file

@ -11,6 +11,7 @@ class Api::V1::MediaController < Api::BaseController
def create
@media = current_account.media_attachments.create!(file: media_params[:file])
render json: @media, serializer: REST::MediaAttachmentSerializer
rescue Paperclip::Errors::NotIdentifiedByImageMagickError
render json: file_type_error, status: 422
rescue Paperclip::Error

View file

@ -9,6 +9,7 @@ class Api::V1::MutesController < Api::BaseController
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -11,11 +11,12 @@ class Api::V1::NotificationsController < Api::BaseController
def index
@notifications = load_notifications
set_maps_for_notification_target_statuses
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
end
def show
@notification = current_account.notifications.find(params[:id])
render json: @notification, serializer: REST::NotificationSerializer
end
def clear
@ -46,10 +47,6 @@ class Api::V1::NotificationsController < Api::BaseController
current_account.notifications.browserable(exclude_types)
end
def set_maps_for_notification_target_statuses
set_maps target_statuses_from_notifications
end
def target_statuses_from_notifications
@notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
end

View file

@ -9,6 +9,7 @@ class Api::V1::ReportsController < Api::BaseController
def index
@reports = current_account.reports
render json: @reports, each_serializer: REST::ReportSerializer
end
def create
@ -20,7 +21,7 @@ class Api::V1::ReportsController < Api::BaseController
User.admins.includes(:account).each { |u| AdminMailer.new_report(u.account, @report).deliver_later }
render :show
render json: @report, serializer: REST::ReportSerializer
end
private

View file

@ -6,7 +6,8 @@ class Api::V1::SearchController < Api::BaseController
respond_to :json
def index
@search = OpenStruct.new(search_results)
@search = Search.new(search_results)
render json: @search, serializer: REST::SearchSerializer
end
private

View file

@ -11,7 +11,7 @@ class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController
def index
@accounts = load_accounts
render 'api/v1/statuses/accounts'
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -10,7 +10,7 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController
def create
@status = favourited_status
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
def destroy
@ -19,7 +19,7 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController
UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
private

View file

@ -14,14 +14,14 @@ class Api::V1::Statuses::MutesController < Api::BaseController
current_account.mute_conversation!(@conversation)
@mutes_map = { @conversation.id => true }
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
def destroy
current_account.unmute_conversation!(@conversation)
@mutes_map = { @conversation.id => false }
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
private

View file

@ -11,7 +11,7 @@ class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController
def index
@accounts = load_accounts
render 'api/v1/statuses/accounts'
render json: @accounts, each_serializer: REST::AccountSerializer
end
private

View file

@ -10,7 +10,7 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController
def create
@status = ReblogService.new.call(current_user.account, status_for_reblog)
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
def destroy
@ -20,7 +20,7 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController
authorize status_for_destroy, :unreblog?
RemovalWorker.perform_async(status_for_destroy.id)
render 'api/v1/statuses/show'
render json: @status, serializer: REST::StatusSerializer
end
private

View file

@ -13,6 +13,7 @@ class Api::V1::StatusesController < Api::BaseController
def show
cached = Rails.cache.read(@status.cache_key)
@status = cached unless cached.nil?
render json: @status, serializer: REST::StatusSerializer
end
def context
@ -21,15 +22,20 @@ class Api::V1::StatusesController < Api::BaseController
loaded_ancestors = cache_collection(ancestors_results, Status)
loaded_descendants = cache_collection(descendants_results, Status)
@context = OpenStruct.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
statuses = [@status] + @context[:ancestors] + @context[:descendants]
@context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
statuses = [@status] + @context.ancestors + @context.descendants
set_maps(statuses)
render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
end
def card
@card = PreviewCard.find_by(status: @status)
render_empty if @card.nil?
if @card.nil?
render_empty
else
render json: @card, serializer: REST::PreviewCardSerializer
end
end
def create
@ -43,7 +49,7 @@ class Api::V1::StatusesController < Api::BaseController
application: doorkeeper_token.application,
idempotency: request.headers['Idempotency-Key'])
render :show
render json: @status, serializer: REST::StatusSerializer
end
def destroy

View file

@ -9,15 +9,13 @@ class Api::V1::Timelines::HomeController < Api::BaseController
def show
@statuses = load_statuses
render 'api/v1/timelines/show'
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
def load_statuses
cached_home_statuses.tap do |statuses|
set_maps(statuses)
end
cached_home_statuses
end
def cached_home_statuses

View file

@ -7,15 +7,13 @@ class Api::V1::Timelines::PublicController < Api::BaseController
def show
@statuses = load_statuses
render 'api/v1/timelines/show'
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
def load_statuses
cached_public_statuses.tap do |statuses|
set_maps(statuses)
end
cached_public_statuses
end
def cached_public_statuses

View file

@ -8,7 +8,7 @@ class Api::V1::Timelines::TagController < Api::BaseController
def show
@statuses = load_statuses
render 'api/v1/timelines/show'
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
@ -18,9 +18,7 @@ class Api::V1::Timelines::TagController < Api::BaseController
end
def load_statuses
cached_tagged_statuses.tap do |statuses|
set_maps(statuses)
end
cached_tagged_statuses
end
def cached_tagged_statuses