Merge pull request #1029 from Retrospring/fix/relationship-list-n+1
Prevent 𝑛+1 on follower/following lists
This commit is contained in:
commit
279ed82d6a
|
@ -17,26 +17,26 @@ class UserController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def followers
|
def followers
|
||||||
@relationships = @user.cursored_follower_relationships(last_id: params[:last_id])
|
paginate_relationships(:cursored_follower_relationships)
|
||||||
@relationships_last_id = @relationships.map(&:id).min
|
|
||||||
@more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero?
|
|
||||||
@users = @relationships.map(&:source)
|
@users = @relationships.map(&:source)
|
||||||
|
own_followings = find_own_relationships(current_user&.active_follow_relationships)
|
||||||
|
own_blocks = find_own_relationships(current_user&.active_block_relationships)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render "show_follow", locals: { type: :follower } }
|
format.html { render "show_follow", locals: { type: :follower, own_followings:, own_blocks: } }
|
||||||
format.turbo_stream { render "show_follow", locals: { type: :follower } }
|
format.turbo_stream { render "show_follow", locals: { type: :follower, own_followings:, own_blocks: } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def followings
|
def followings
|
||||||
@relationships = @user.cursored_following_relationships(last_id: params[:last_id])
|
paginate_relationships(:cursored_following_relationships)
|
||||||
@relationships_last_id = @relationships.map(&:id).min
|
|
||||||
@more_data_available = !@user.cursored_following_relationships(last_id: @relationships_last_id, size: 1).count.zero?
|
|
||||||
@users = @relationships.map(&:target)
|
@users = @relationships.map(&:target)
|
||||||
|
own_followings = find_own_relationships(current_user&.active_follow_relationships)
|
||||||
|
own_blocks = find_own_relationships(current_user&.active_block_relationships)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render "show_follow", locals: { type: :friend } }
|
format.html { render "show_follow", locals: { type: :friend, own_followings:, own_blocks: } }
|
||||||
format.turbo_stream { render "show_follow", locals: { type: :friend } }
|
format.turbo_stream { render "show_follow", locals: { type: :friend, own_followings:, own_blocks: } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,6 +69,18 @@ class UserController < ApplicationController
|
||||||
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
|
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_own_relationships(relationships)
|
||||||
|
return nil if relationships.nil?
|
||||||
|
|
||||||
|
relationships.where(target_id: @users.map(&:id))&.select(:target_id)&.map(&:target_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def paginate_relationships(method)
|
||||||
|
@relationships = @user.public_send(method, last_id: params[:last_id])
|
||||||
|
@relationships_last_id = @relationships.map(&:id).min
|
||||||
|
@more_data_available = !@user.public_send(method, last_id: @relationships_last_id, size: 1).count.zero?
|
||||||
|
end
|
||||||
|
|
||||||
def hidden_social_graph_redirect
|
def hidden_social_graph_redirect
|
||||||
return if belongs_to_current_user? || !@user.privacy_hide_social_graph
|
return if belongs_to_current_user? || !@user.privacy_hide_social_graph
|
||||||
|
|
||||||
|
|
|
@ -9,4 +9,4 @@
|
||||||
= user.profile.display_name
|
= user.profile.display_name
|
||||||
.profile__screen-name
|
.profile__screen-name
|
||||||
= user.screen_name
|
= user.screen_name
|
||||||
= render 'user/actions', user: user, type: type
|
= render "user/actions", user:, type:, own_followings:, own_blocks:
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
.profile__actions
|
.profile__actions
|
||||||
- type ||= :nil
|
- type ||= :nil
|
||||||
|
- own_followings ||= nil
|
||||||
|
- own_blocks ||= nil
|
||||||
- if user_signed_in? && user == current_user
|
- if user_signed_in? && user == current_user
|
||||||
.d-grid
|
.d-grid
|
||||||
%a.btn.btn-dark{ href: settings_profile_path } Edit profile
|
%a.btn.btn-dark{ href: settings_profile_path } Edit profile
|
||||||
- elsif user_signed_in?
|
- elsif user_signed_in?
|
||||||
.d-grid.gap-2
|
.d-grid.gap-2
|
||||||
- if current_user.following? user
|
- if own_followings&.include?(user.id) || current_user.following?(user)
|
||||||
%button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :unfollow, type: type, target: user.screen_name } }
|
%button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :unfollow, type: type, target: user.screen_name } }
|
||||||
= t("voc.unfollow")
|
= t("voc.unfollow")
|
||||||
- else
|
- else
|
||||||
|
@ -19,7 +21,7 @@
|
||||||
%a.dropdown-item.d-block.d-sm-none{ href: '#', data: { bs_target: '#modal-list-memberships', bs_toggle: :modal } }
|
%a.dropdown-item.d-block.d-sm-none{ href: '#', data: { bs_target: '#modal-list-memberships', bs_toggle: :modal } }
|
||||||
%i.fa.fa-list.fa-fw
|
%i.fa.fa-list.fa-fw
|
||||||
= t(".list")
|
= t(".list")
|
||||||
- if current_user.blocking?(user)
|
- if own_blocks&.include?(user.id) || current_user.blocking?(user)
|
||||||
%a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } }
|
%a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } }
|
||||||
%i.fa.fa-minus-circle.fa-fw
|
%i.fa.fa-minus-circle.fa-fw
|
||||||
%span.pe-none= t("voc.unblock")
|
%span.pe-none= t("voc.unblock")
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
.row.row-cols-1.row-cols-sm-2.row-cols-md-3#users
|
.row.row-cols-1.row-cols-sm-2.row-cols-md-3#users
|
||||||
- @users.each do |user|
|
- @users.each do |user|
|
||||||
.col.pb-3
|
.col.pb-3
|
||||||
= render "shared/userbox", user:, type:
|
= render "shared/userbox", user:, type:, own_followings:, own_blocks:
|
||||||
|
|
||||||
- if @more_data_available
|
- if @more_data_available
|
||||||
.d-flex.justify-content-center.justify-content-sm-start#paginator
|
.d-flex.justify-content-center.justify-content-sm-start#paginator
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
= turbo_stream.append "users" do
|
= turbo_stream.append "users" do
|
||||||
- @users.each do |user|
|
- @users.each do |user|
|
||||||
.col.pb-3
|
.col.pb-3
|
||||||
= render "shared/userbox", user:, type:
|
= render "shared/userbox", user:, type:, own_followings:, own_blocks:
|
||||||
|
|
||||||
= turbo_stream.update "paginator" do
|
= turbo_stream.update "paginator" do
|
||||||
- if @more_data_available
|
- if @more_data_available
|
||||||
|
|
Loading…
Reference in New Issue