Merge pull request #1029 from Retrospring/fix/relationship-list-n+1

Prevent 𝑛+1 on follower/following lists
This commit is contained in:
Karina Kwiatek 2023-01-31 22:15:32 +01:00 committed by GitHub
commit 279ed82d6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 15 deletions

View file

@ -17,26 +17,26 @@ class UserController < ApplicationController
end
def followers
@relationships = @user.cursored_follower_relationships(last_id: params[:last_id])
@relationships_last_id = @relationships.map(&:id).min
@more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero?
paginate_relationships(:cursored_follower_relationships)
@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|
format.html { render "show_follow", locals: { type: :follower } }
format.turbo_stream { 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, own_followings:, own_blocks: } }
end
end
def followings
@relationships = @user.cursored_following_relationships(last_id: params[:last_id])
@relationships_last_id = @relationships.map(&:id).min
@more_data_available = !@user.cursored_following_relationships(last_id: @relationships_last_id, size: 1).count.zero?
paginate_relationships(:cursored_following_relationships)
@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|
format.html { render "show_follow", locals: { type: :friend } }
format.turbo_stream { 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, own_followings:, own_blocks: } }
end
end
@ -69,6 +69,18 @@ class UserController < ApplicationController
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
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
return if belongs_to_current_user? || !@user.privacy_hide_social_graph

View file

@ -9,4 +9,4 @@
= user.profile.display_name
.profile__screen-name
= user.screen_name
= render 'user/actions', user: user, type: type
= render "user/actions", user:, type:, own_followings:, own_blocks:

View file

@ -1,11 +1,13 @@
.profile__actions
- type ||= :nil
- own_followings ||= nil
- own_blocks ||= nil
- if user_signed_in? && user == current_user
.d-grid
%a.btn.btn-dark{ href: settings_profile_path } Edit profile
- elsif user_signed_in?
.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 } }
= t("voc.unfollow")
- else
@ -19,7 +21,7 @@
%a.dropdown-item.d-block.d-sm-none{ href: '#', data: { bs_target: '#modal-list-memberships', bs_toggle: :modal } }
%i.fa.fa-list.fa-fw
= 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 } }
%i.fa.fa-minus-circle.fa-fw
%span.pe-none= t("voc.unblock")

View file

@ -1,7 +1,7 @@
.row.row-cols-1.row-cols-sm-2.row-cols-md-3#users
- @users.each do |user|
.col.pb-3
= render "shared/userbox", user:, type:
= render "shared/userbox", user:, type:, own_followings:, own_blocks:
- if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator

View file

@ -1,7 +1,7 @@
= turbo_stream.append "users" do
- @users.each do |user|
.col.pb-3
= render "shared/userbox", user:, type:
= render "shared/userbox", user:, type:, own_followings:, own_blocks:
= turbo_stream.update "paginator" do
- if @more_data_available