diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index ad94a2d1..b9e8e59c 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -21,6 +21,7 @@ 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 respond_to do |format| format.html { render "show_follow", locals: { type: :follower } } @@ -33,6 +34,7 @@ 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 respond_to do |format| format.html { render "show_follow", locals: { type: :friend } } @@ -69,6 +71,16 @@ 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? + + @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) + end + def hidden_social_graph_redirect return if belongs_to_current_user? || !@user.privacy_hide_social_graph diff --git a/app/views/user/_actions.html.haml b/app/views/user/_actions.html.haml index 909efe22..157e7a6d 100644 --- a/app/views/user/_actions.html.haml +++ b/app/views/user/_actions.html.haml @@ -5,7 +5,7 @@ %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) %button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :unfollow, type: type, target: user.screen_name } } = t("voc.unfollow") - else @@ -19,7 +19,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) %a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } } %i.fa.fa-minus-circle.fa-fw %span.pe-none= t("voc.unblock")