Pre-load own mute relationships in follow lists

This commit is contained in:
Karina Kwiatek 2023-02-01 23:20:49 +01:00
parent da711dcfd4
commit 603e9c501e
5 changed files with 28 additions and 15 deletions

View File

@ -19,24 +19,34 @@ class UserController < ApplicationController
def followers def followers
paginate_relationships(:cursored_follower_relationships) paginate_relationships(:cursored_follower_relationships)
@users = @relationships.map(&:source) @users = @relationships.map(&:source)
own_followings = find_own_relationships(current_user&.active_follow_relationships) own_relationships = find_own_relationships
own_blocks = find_own_relationships(current_user&.active_block_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| respond_to do |format|
format.html { render "show_follow", locals: { type: :follower, own_followings:, own_blocks: } } format.html { render "show_follow", locals: }
format.turbo_stream { render "show_follow", locals: { type: :follower, own_followings:, own_blocks: } } format.turbo_stream { render "show_follow", locals: }
end end
end end
def followings def followings
paginate_relationships(:cursored_following_relationships) paginate_relationships(:cursored_following_relationships)
@users = @relationships.map(&:target) @users = @relationships.map(&:target)
own_followings = find_own_relationships(current_user&.active_follow_relationships) own_relationships = find_own_relationships
own_blocks = find_own_relationships(current_user&.active_block_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| respond_to do |format|
format.html { render "show_follow", locals: { type: :friend, own_followings:, own_blocks: } } format.html { render "show_follow", locals: }
format.turbo_stream { render "show_follow", locals: { type: :friend, own_followings:, own_blocks: } } format.turbo_stream { render "show_follow", locals: }
end end
end end
@ -69,10 +79,12 @@ 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
def find_own_relationships(relationships) def find_own_relationships
return nil if relationships.nil? 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 end
def paginate_relationships(method) def paginate_relationships(method)

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:, type:, own_followings:, own_blocks: = render "user/actions", user:, type:, own_followings:, own_blocks:, own_mutes:

View File

@ -2,6 +2,7 @@
- type ||= :nil - type ||= :nil
- own_followings ||= nil - own_followings ||= nil
- own_blocks ||= nil - own_blocks ||= nil
- own_mutes ||= 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
@ -29,7 +30,7 @@
%a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } } %a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } }
%i.fa.fa-minus-circle.fa-fw %i.fa.fa-minus-circle.fa-fw
%span.pe-none= t("voc.block") %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 } } %a.dropdown-item{ href: '#', data: { action: :unmute, target: user.screen_name } }
%i.fa.fa-volume-off.fa-fw %i.fa.fa-volume-off.fa-fw
%span.pe-none= t("voc.unmute") %span.pe-none= t("voc.unmute")

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:, own_followings:, own_blocks: = render "shared/userbox", user:, type:, own_followings:, own_blocks:, own_mutes:
- 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:, own_followings:, own_blocks: = render "shared/userbox", user:, type:, own_followings:, own_blocks:, own_mutes:
= turbo_stream.update "paginator" do = turbo_stream.update "paginator" do
- if @more_data_available - if @more_data_available