Use locals for own relationships

This commit is contained in:
Karina Kwiatek 2023-01-31 13:46:09 +01:00
parent 51e15ef195
commit 83eeabb525
5 changed files with 18 additions and 18 deletions

View File

@ -21,11 +21,12 @@ class UserController < ApplicationController
@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)
find_own_relationships
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
@ -34,11 +35,12 @@ class UserController < ApplicationController
@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)
find_own_relationships
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
@ -71,14 +73,10 @@ class UserController < ApplicationController
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
end
# Checks which of the displayed users are followed or blocked by the current user
#
# This prevents 𝑛+1 queries.
def find_own_relationships
return unless user_signed_in?
def find_own_relationships(relationships)
return nil if relationships.nil?
@own_followings = current_user.active_follow_relationships.where(target_id: @users.map(&:id)).select(:target_id).map(&:target_id)
@own_blocks = current_user.active_block_relationships.where(target_id: @users.map(&:id)).select(:target_id).map(&:target_id)
relationships.where(target_id: @users.map(&:id))&.select(:target_id)&.map(&:target_id)
end
def hidden_social_graph_redirect

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 @own_followings.include? user.id
- 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 @own_blocks.include? user.id
- 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