From de1f3aab868f8d18198438c405d0852c58f34e10 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 16 Oct 2016 18:57:54 +0200 Subject: Fix #16 - Optimize n+1 queries when checking reblogged/favourited values for status lists in API --- app/views/api/v1/accounts/show.rabl | 6 +++--- app/views/api/v1/statuses/show.rabl | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'app/views/api') diff --git a/app/views/api/v1/accounts/show.rabl b/app/views/api/v1/accounts/show.rabl index 4f6a3ff99..0d9e268a1 100644 --- a/app/views/api/v1/accounts/show.rabl +++ b/app/views/api/v1/accounts/show.rabl @@ -5,6 +5,6 @@ attributes :id, :username, :acct, :display_name, :note node(:url) { |account| TagManager.instance.url_for(account) } node(:avatar) { |account| full_asset_url(account.avatar.url(:large, false)) } node(:header) { |account| full_asset_url(account.header.url(:medium, false)) } -node(:followers_count) { |account| account.followers.count } -node(:following_count) { |account| account.following.count } -node(:statuses_count) { |account| account.statuses.count } +node(:followers_count) { |account| account.try(:followers_count) || account.followers.count } +node(:following_count) { |account| account.try(:following_count) || account.following.count } +node(:statuses_count) { |account| account.try(:statuses_count) || account.statuses.count } diff --git a/app/views/api/v1/statuses/show.rabl b/app/views/api/v1/statuses/show.rabl index 20cb65e29..69dcd1058 100644 --- a/app/views/api/v1/statuses/show.rabl +++ b/app/views/api/v1/statuses/show.rabl @@ -6,8 +6,8 @@ node(:content) { |status| Formatter.instance.format(status) } node(:url) { |status| TagManager.instance.url_for(status) } node(:reblogs_count) { |status| status.reblogs_count } node(:favourites_count) { |status| status.favourites_count } -node(:favourited, if: proc { !current_account.nil? }) { |status| current_account.favourited?(status) } -node(:reblogged, if: proc { !current_account.nil? }) { |status| current_account.reblogged?(status) } +node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? !!@favourites_map[status.id] : current_account.favourited?(status) } +node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? !!@reblogs_map[status.id] : current_account.reblogged?(status) } child :reblog => :reblog do extends('api/v1/statuses/show') -- cgit