From 603e9c501eae4656a6753484ac40fa22c8eeeb93 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 1 Feb 2023 23:20:49 +0100 Subject: [PATCH 1/2] Pre-load own mute relationships in follow lists --- app/controllers/user_controller.rb | 34 +++++++++++++------- app/views/shared/_userbox.html.haml | 2 +- app/views/user/_actions.html.haml | 3 +- app/views/user/show_follow.html.haml | 2 +- app/views/user/show_follow.turbo_stream.haml | 2 +- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 25c05efe..041748f8 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -19,24 +19,34 @@ class UserController < ApplicationController def followers 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) + own_relationships = find_own_relationships + locals = { + type: :friend, + own_followings: own_relationships[Relationships::Follow], + own_blocks: own_relationships[Relationships::Block], + own_mutes: own_relationships[Relationships::Mute] + } respond_to do |format| - 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: } } + format.html { render "show_follow", locals: } + format.turbo_stream { render "show_follow", locals: } end end def followings 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) + own_relationships = find_own_relationships + locals = { + type: :friend, + own_followings: own_relationships[Relationships::Follow], + own_blocks: own_relationships[Relationships::Block], + own_mutes: own_relationships[Relationships::Mute] + } respond_to do |format| - 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: } } + format.html { render "show_follow", locals: } + format.turbo_stream { render "show_follow", locals: } end end @@ -69,10 +79,12 @@ 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? + def find_own_relationships + return {} unless user_signed_in? - relationships.where(target_id: @users.map(&:id))&.select(:target_id)&.map(&:target_id) + Relationship.where(source: current_user, target_id: @users.map(&:id)) + &.select(:target_id, :type) + &.group_by(&:type) end def paginate_relationships(method) diff --git a/app/views/shared/_userbox.html.haml b/app/views/shared/_userbox.html.haml index d346dd69..5aee47b4 100644 --- a/app/views/shared/_userbox.html.haml +++ b/app/views/shared/_userbox.html.haml @@ -9,4 +9,4 @@ = user.profile.display_name .profile__screen-name = user.screen_name - = render "user/actions", user:, type:, own_followings:, own_blocks: + = render "user/actions", user:, type:, own_followings:, own_blocks:, own_mutes: diff --git a/app/views/user/_actions.html.haml b/app/views/user/_actions.html.haml index 0eae948f..23960a38 100644 --- a/app/views/user/_actions.html.haml +++ b/app/views/user/_actions.html.haml @@ -2,6 +2,7 @@ - type ||= :nil - own_followings ||= nil - own_blocks ||= nil + - own_mutes ||= nil - if user_signed_in? && user == current_user .d-grid %a.btn.btn-dark{ href: settings_profile_path } Edit profile @@ -29,7 +30,7 @@ %a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } } %i.fa.fa-minus-circle.fa-fw %span.pe-none= t("voc.block") - - if current_user.muting?(user) + - if own_mutes&.include?(user.id) || current_user.muting?(user) %a.dropdown-item{ href: '#', data: { action: :unmute, target: user.screen_name } } %i.fa.fa-volume-off.fa-fw %span.pe-none= t("voc.unmute") diff --git a/app/views/user/show_follow.html.haml b/app/views/user/show_follow.html.haml index 890698ca..699df89a 100644 --- a/app/views/user/show_follow.html.haml +++ b/app/views/user/show_follow.html.haml @@ -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:, own_followings:, own_blocks: + = render "shared/userbox", user:, type:, own_followings:, own_blocks:, own_mutes: - if @more_data_available .d-flex.justify-content-center.justify-content-sm-start#paginator diff --git a/app/views/user/show_follow.turbo_stream.haml b/app/views/user/show_follow.turbo_stream.haml index b0c52a2a..fe434f69 100644 --- a/app/views/user/show_follow.turbo_stream.haml +++ b/app/views/user/show_follow.turbo_stream.haml @@ -1,7 +1,7 @@ = turbo_stream.append "users" do - @users.each do |user| .col.pb-3 - = render "shared/userbox", user:, type:, own_followings:, own_blocks: + = render "shared/userbox", user:, type:, own_followings:, own_blocks:, own_mutes: = turbo_stream.update "paginator" do - if @more_data_available From 825454bbae547593f80768bbfe666c6f426b795e Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 1 Feb 2023 23:34:33 +0100 Subject: [PATCH 2/2] Fix incorrect type local in followers endpoint --- app/controllers/user_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 041748f8..17926429 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -21,7 +21,7 @@ class UserController < ApplicationController @users = @relationships.map(&:source) own_relationships = find_own_relationships locals = { - type: :friend, + type: :follower, own_followings: own_relationships[Relationships::Follow], own_blocks: own_relationships[Relationships::Block], own_mutes: own_relationships[Relationships::Mute]