Skip to content
Snippets Groups Projects
Commit 8b2cad56 authored by Eugen Rochko's avatar Eugen Rochko Committed by GitHub
Browse files

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

parent 2d612867
No related branches found
No related tags found
No related merge requests found
Showing
with 50 additions and 54 deletions
......@@ -18,6 +18,7 @@ gem 'aws-sdk', '~> 2.9'
gem 'paperclip', '~> 5.1'
gem 'paperclip-av-transcoder', '~> 0.6'
gem 'active_model_serializers', '~> 0.10'
gem 'addressable', '~> 2.5'
gem 'bootsnap'
gem 'browser'
......
......@@ -24,6 +24,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
active_model_serializers (0.10.6)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.2)
active_record_query_trace (1.5.4)
activejob (5.1.2)
activesupport (= 5.1.2)
......@@ -101,6 +106,8 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
case_transform (0.2)
activesupport
chunky_png (1.3.8)
cld3 (3.1.3)
ffi (>= 1.1.0, < 1.10.0)
......@@ -200,6 +207,7 @@ GEM
terminal-table (>= 1.5.1)
jmespath (1.3.1)
json (2.1.0)
jsonapi-renderer (0.1.2)
kaminari (1.0.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.0.1)
......@@ -476,6 +484,7 @@ PLATFORMS
ruby
DEPENDENCIES
active_model_serializers (~> 0.10)
active_record_query_trace (~> 1.5)
addressable (~> 2.5)
annotate (~> 2.7)
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -9,6 +9,7 @@ class Api::V1::BlocksController < Api::BaseController
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end
private
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -9,6 +9,7 @@ class Api::V1::MutesController < Api::BaseController
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end
private
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment