about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAkihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>2017-05-20 22:13:51 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-20 15:13:51 +0200
commitf55480756337dd4df7513e89673e81e003f1201a (patch)
treebfd4aabb606f82f2fe01a16bcceec340ec11ecd3
parentd972845ff663605becec1dde904271897fe2e347 (diff)
Use joins for account properties (#3167)
-rw-r--r--app/controllers/api/v1/accounts_controller.rb28
-rw-r--r--app/controllers/api/v1/blocks_controller.rb12
-rw-r--r--app/controllers/api/v1/follow_requests_controller.rb11
-rw-r--r--app/controllers/api/v1/mutes_controller.rb12
-rw-r--r--app/controllers/api/v1/statuses_controller.rb26
-rw-r--r--app/services/block_domain_service.rb2
6 files changed, 53 insertions, 38 deletions
diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb
index 12c7dd2b0..664ac0ba5 100644
--- a/app/controllers/api/v1/accounts_controller.rb
+++ b/app/controllers/api/v1/accounts_controller.rb
@@ -23,12 +23,14 @@ class Api::V1::AccountsController < ApiController
   end
 
   def following
-    results   = Follow.where(account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.target_account_id] }
+    @accounts = Account.includes(:followers)
+                       .references(:followers)
+                       .merge(Follow.where(account: @account)
+                                    .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
+                       .to_a
 
-    next_path = following_api_v1_account_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = following_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    next_path = following_api_v1_account_url(pagination_params(max_id: @accounts.last.followers.last.id))     if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = following_api_v1_account_url(pagination_params(since_id: @accounts.first.followers.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
 
@@ -36,12 +38,16 @@ class Api::V1::AccountsController < ApiController
   end
 
   def followers
-    results   = Follow.where(target_account: @account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.account_id] }
-
-    next_path = followers_api_v1_account_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = followers_api_v1_account_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    @accounts = Account.includes(:following)
+                       .references(:following)
+                       .merge(Follow.where(target_account: @account)
+                                    .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT),
+                                                        params[:max_id],
+                                                        params[:since_id]))
+                       .to_a
+
+    next_path = followers_api_v1_account_url(pagination_params(max_id: @accounts.last.following.last.id))     if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = followers_api_v1_account_url(pagination_params(since_id: @accounts.first.following.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
 
diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb
index 742717ba2..37f5ea219 100644
--- a/app/controllers/api/v1/blocks_controller.rb
+++ b/app/controllers/api/v1/blocks_controller.rb
@@ -7,12 +7,14 @@ class Api::V1::BlocksController < ApiController
   respond_to :json
 
   def index
-    results   = Block.where(account: current_account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.target_account_id] }.compact
+    @accounts = Account.includes(:blocked_by)
+                       .references(:blocked_by)
+                       .merge(Block.where(account: current_account)
+                                   .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
+                       .to_a
 
-    next_path = api_v1_blocks_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = api_v1_blocks_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    next_path = api_v1_blocks_url(pagination_params(max_id: @accounts.last.blocked_bies.last.id))     if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = api_v1_blocks_url(pagination_params(since_id: @accounts.first.blocked_bies.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
   end
diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb
index 73cfaf10a..4716019a8 100644
--- a/app/controllers/api/v1/follow_requests_controller.rb
+++ b/app/controllers/api/v1/follow_requests_controller.rb
@@ -5,12 +5,13 @@ class Api::V1::FollowRequestsController < ApiController
   before_action :require_user!
 
   def index
-    results   = FollowRequest.where(target_account: current_account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.account_id] }
+    @accounts = Account.includes(:follow_requests)
+                       .references(:follow_requests)
+                       .merge(FollowRequest.where(target_account: current_account)
+                                           .paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]))
 
-    next_path = api_v1_follow_requests_url(pagination_params(max_id: results.last.id))    if results.size == DEFAULT_ACCOUNTS_LIMIT
-    prev_path = api_v1_follow_requests_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    next_path = api_v1_follow_requests_url(pagination_params(max_id: @accounts.last.follow_requests.last.id))     if @accounts.size == DEFAULT_ACCOUNTS_LIMIT
+    prev_path = api_v1_follow_requests_url(pagination_params(since_id: @accounts.first.follow_requests.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
   end
diff --git a/app/controllers/api/v1/mutes_controller.rb b/app/controllers/api/v1/mutes_controller.rb
index cbd98732b..baf1fc7ef 100644
--- a/app/controllers/api/v1/mutes_controller.rb
+++ b/app/controllers/api/v1/mutes_controller.rb
@@ -7,12 +7,14 @@ class Api::V1::MutesController < ApiController
   respond_to :json
 
   def index
-    results   = Mute.where(account: current_account).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.target_account_id] }
+    @accounts = Account.includes(:muting)
+                       .references(:muting)
+                       .merge(Mute.where(account: current_account)
+                                  .paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
+                       .to_a
 
-    next_path = api_v1_mutes_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = api_v1_mutes_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    next_path = api_v1_mutes_url(pagination_params(max_id: @accounts.last.mutings.last.id))     if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = api_v1_mutes_url(pagination_params(since_id: @accounts.first.mutings.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
   end
diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb
index 9312282ed..1fead10e7 100644
--- a/app/controllers/api/v1/statuses_controller.rb
+++ b/app/controllers/api/v1/statuses_controller.rb
@@ -32,12 +32,14 @@ class Api::V1::StatusesController < ApiController
   end
 
   def reblogged_by
-    results   = @status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |r| accounts[r.account_id] }
+    @accounts = Account.includes(statuses: :reblogs)
+                       .references(statuses: :reblogs)
+                       .where(statuses: { id: @status.id })
+                       .merge(@status.reblogs.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
+                       .to_a
 
-    next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    next_path = reblogged_by_api_v1_status_url(pagination_params(max_id: @accounts.last.statuses.last.reblogs.last.id))      if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = reblogged_by_api_v1_status_url(pagination_params(since_id: @accounts.first.statuses.first.reblogs.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
 
@@ -45,12 +47,14 @@ class Api::V1::StatusesController < ApiController
   end
 
   def favourited_by
-    results   = @status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
-    accounts  = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
-    @accounts = results.map { |f| accounts[f.account_id] }
-
-    next_path = favourited_by_api_v1_status_url(pagination_params(max_id: results.last.id))    if results.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
-    prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: results.first.id)) unless results.empty?
+    @accounts = Account.includes(statuses: :favourites)
+                       .references(statuses: :favourites)
+                       .where(statuses: { id: @status.id })
+                       .merge(@status.favourites.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]))
+                       .to_a
+
+    next_path = favourited_by_api_v1_status_url(pagination_params(max_id: @accounts.last.statuses.last.favourites.last.id))      if @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
+    prev_path = favourited_by_api_v1_status_url(pagination_params(since_id: @accounts.first.statuses.first.favourites.first.id)) unless @accounts.empty?
 
     set_pagination_headers(next_path, prev_path)
 
diff --git a/app/services/block_domain_service.rb b/app/services/block_domain_service.rb
index 3f8f5b385..e8b3a870d 100644
--- a/app/services/block_domain_service.rb
+++ b/app/services/block_domain_service.rb
@@ -60,6 +60,6 @@ class BlockDomainService < BaseService
   end
 
   def media_from_blocked_domain
-    MediaAttachment.where(account: blocked_domain_accounts).reorder(nil)
+    MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
   end
 end