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 @relationships_last_id = @relationships.map(&:id).min
@more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero? @more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero?
@users = @relationships.map(&:source) @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| 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
@ -34,11 +35,12 @@ class UserController < ApplicationController
@relationships_last_id = @relationships.map(&:id).min @relationships_last_id = @relationships.map(&:id).min
@more_data_available = !@user.cursored_following_relationships(last_id: @relationships_last_id, size: 1).count.zero? @more_data_available = !@user.cursored_following_relationships(last_id: @relationships_last_id, size: 1).count.zero?
@users = @relationships.map(&:target) @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| 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
@ -71,14 +73,10 @@ 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
# Checks which of the displayed users are followed or blocked by the current user def find_own_relationships(relationships)
# return nil if relationships.nil?
# This prevents 𝑛+1 queries.
def find_own_relationships
return unless user_signed_in?
@own_followings = current_user.active_follow_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)
@own_blocks = current_user.active_block_relationships.where(target_id: @users.map(&:id)).select(:target_id).map(&:target_id)
end end
def hidden_social_graph_redirect def hidden_social_graph_redirect

View File

@ -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:

View File

@ -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 @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 } } %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 @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 } } %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")

View File

@ -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

View File

@ -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