Fix timeline emoji_reactions N+1 problem
This commit is contained in:
parent
7c387becb6
commit
c0ff0754a3
12 changed files with 84 additions and 16 deletions
|
@ -9,7 +9,9 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
|
|||
def index
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::BookmarksController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::EmojiReactionsController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ 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)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -9,7 +9,9 @@ class Api::V1::Statuses::ReferredByStatusesController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -9,11 +9,13 @@ class Api::V1::Timelines::HomeController < Api::BaseController
|
|||
with_read_replica do
|
||||
@statuses = load_statuses
|
||||
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
@emoji_reactions = EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
render json: @statuses,
|
||||
each_serializer: REST::StatusSerializer,
|
||||
relationships: @relationships,
|
||||
emoji_reaction_permitted_account_ids: @emoji_reactions,
|
||||
status: account_home_feed.regenerating? ? 206 : 200
|
||||
end
|
||||
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::Timelines::PublicController < Api::BaseController
|
|||
def show
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -8,7 +8,9 @@ class Api::V1::Timelines::TagController < Api::BaseController
|
|||
def show
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -357,21 +357,30 @@ class Status < ApplicationRecord
|
|||
return [] if account.present? && !self.account.show_emoji_reaction?(account)
|
||||
return [] if account.nil? && !options[:force] && self.account.emoji_reaction_policy != :allow
|
||||
|
||||
permitted_account_ids = options[:permitted_account_ids]
|
||||
|
||||
(Oj.load(status_stat&.emoji_reactions || '', mode: :strict) || []).tap do |emoji_reactions|
|
||||
if account.present?
|
||||
remove_emoji_reactions = []
|
||||
public_emoji_reactions = []
|
||||
|
||||
emoji_reactions.each do |emoji_reaction|
|
||||
emoji_reaction['me'] = emoji_reaction['account_ids'].include?(account.id.to_s)
|
||||
emoji_reaction['account_ids'] -= account.excluded_from_timeline_account_ids.map(&:to_s)
|
||||
|
||||
accounts = Account.where(id: emoji_reaction['account_ids'], silenced_at: nil, suspended_at: nil)
|
||||
accounts = accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.excluded_from_timeline_domains.size.positive?
|
||||
emoji_reaction['account_ids'] = accounts.pluck(:id).map(&:to_s)
|
||||
accounts = []
|
||||
if permitted_account_ids
|
||||
emoji_reaction['account_ids'] = emoji_reaction['account_ids'] & permitted_account_ids.map(&:to_s)
|
||||
else
|
||||
accounts = Account.where(id: emoji_reaction['account_ids'], silenced_at: nil, suspended_at: nil)
|
||||
accounts = accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.excluded_from_timeline_domains.size.positive?
|
||||
emoji_reaction['account_ids'] = accounts.pluck(:id).map(&:to_s)
|
||||
end
|
||||
|
||||
emoji_reaction['count'] = emoji_reaction['account_ids'].size
|
||||
remove_emoji_reactions << emoji_reaction if emoji_reaction['count'] <= 0
|
||||
public_emoji_reactions << emoji_reaction if (emoji_reaction['count']).positive?
|
||||
end
|
||||
emoji_reactions - remove_emoji_reactions
|
||||
|
||||
public_emoji_reactions
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -463,6 +472,11 @@ class Status < ApplicationRecord
|
|||
EmojiReaction.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |e, h| h[e.status_id] = true }
|
||||
end
|
||||
|
||||
def emoji_reaction_allows_map(status_ids, account_id)
|
||||
my_account = Account.find_by(id: account_id)
|
||||
Status.where(id: status_ids).pluck(:account_id).uniq.index_with { |a| Account.find_by(id: a).show_emoji_reaction?(my_account) }
|
||||
end
|
||||
|
||||
def reload_stale_associations!(cached_items)
|
||||
account_ids = []
|
||||
|
||||
|
|
22
app/presenters/emoji_reaction_accounts_presenter.rb
Normal file
22
app/presenters/emoji_reaction_accounts_presenter.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class EmojiReactionAccountsPresenter
|
||||
attr_reader :permitted_account_ids
|
||||
|
||||
def initialize(statuses, current_account_id = nil, **_options)
|
||||
@current_account_id = current_account_id
|
||||
|
||||
statuses = statuses.compact
|
||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||
emoji_reactions = EmojiReaction.where(status_id: status_ids)
|
||||
account_ids = emoji_reactions.pluck(:account_id).uniq
|
||||
|
||||
permitted_accounts = Account.where(id: account_ids, silenced_at: nil, suspended_at: nil)
|
||||
if current_account_id.present?
|
||||
account = Account.find(current_account_id)
|
||||
permitted_accounts = permitted_accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.present? && account.excluded_from_timeline_domains.size.positive?
|
||||
end
|
||||
|
||||
@permitted_account_ids = permitted_accounts.pluck(:id)
|
||||
end
|
||||
end
|
|
@ -4,7 +4,7 @@ class StatusRelationshipsPresenter
|
|||
PINNABLE_VISIBILITIES = %w(public public_unlisted unlisted login private).freeze
|
||||
|
||||
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
|
||||
:bookmarks_map, :filters_map, :emoji_reactions_map, :attributes_map
|
||||
:bookmarks_map, :filters_map, :emoji_reactions_map, :attributes_map, :emoji_reaction_allows_map
|
||||
|
||||
def initialize(statuses, current_account_id = nil, **options)
|
||||
@current_account_id = current_account_id
|
||||
|
@ -17,6 +17,7 @@ class StatusRelationshipsPresenter
|
|||
@pins_map = {}
|
||||
@filters_map = {}
|
||||
@emoji_reactions_map = {}
|
||||
@emoji_reaction_allows_map = nil
|
||||
else
|
||||
statuses = statuses.compact
|
||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||
|
@ -30,6 +31,7 @@ class StatusRelationshipsPresenter
|
|||
@mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
|
||||
@pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
|
||||
@emoji_reactions_map = Status.emoji_reactions_map(status_ids, current_account_id).merge(options[:emoji_reactions_map] || {})
|
||||
@emoji_reaction_allows_map = Status.emoji_reaction_allows_map(status_ids, current_account_id).merge(options[:emoji_reaction_allows_map] || {})
|
||||
@attributes_map = options[:attributes_map] || {}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -122,7 +122,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
end
|
||||
|
||||
def emoji_reactions
|
||||
object.emoji_reactions_grouped_by_name(current_user&.account)
|
||||
show_emoji_reaction? ? object.emoji_reactions_grouped_by_name(current_user&.account, permitted_account_ids: emoji_reaction_permitted_account_ids) : []
|
||||
end
|
||||
|
||||
def emoji_reactions_count
|
||||
|
@ -131,7 +131,17 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
|
||||
object.account.emoji_reaction_policy == :allow ? object.emoji_reactions_count : 0
|
||||
else
|
||||
object.account.show_emoji_reaction?(current_user.account) ? object.emoji_reactions_count : 0
|
||||
show_emoji_reaction? ? object.emoji_reactions_count : 0
|
||||
end
|
||||
end
|
||||
|
||||
def show_emoji_reaction?
|
||||
if relationships
|
||||
return true if relationships.emoji_reaction_allows_map.nil?
|
||||
|
||||
relationships.emoji_reaction_allows_map[object.account_id] || false
|
||||
else
|
||||
object.account.show_emoji_reaction?(current_user.account)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -211,6 +221,10 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
instance_options && instance_options[:relationships]
|
||||
end
|
||||
|
||||
def emoji_reaction_permitted_account_ids
|
||||
current_user.present? && instance_options && instance_options[:emoji_reaction_permitted_account_ids]&.permitted_account_ids
|
||||
end
|
||||
|
||||
class ApplicationSerializer < ActiveModel::Serializer
|
||||
attributes :name, :website
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue