Merge pull request #1011 from Retrospring/refactor/cleanup-user-controller

Cleanup user controller
This commit is contained in:
Karina Kwiatek 2023-01-28 22:53:48 +01:00 committed by GitHub
commit 19dfc70e2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 24 deletions

View File

@ -3,20 +3,13 @@
class UserController < ApplicationController class UserController < ApplicationController
before_action :set_user before_action :set_user
before_action :hidden_social_graph_redirect, only: %i[followers followings] before_action :hidden_social_graph_redirect, only: %i[followers followings]
after_action :mark_notification_as_read, only: %i[show]
def show def show
@answers = @user.cursored_answers(last_id: params[:last_id]) @answers = @user.cursored_answers(last_id: params[:last_id])
@answers_last_id = @answers.map(&:id).min @answers_last_id = @answers.map(&:id).min
@more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero? @more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero?
if user_signed_in?
notif = Notification.where(target_type: "Relationship", target_id: @user.active_follow_relationships.where(target_id: current_user.id).pluck(:id), recipient_id: current_user.id, new: true).first
unless notif.nil?
notif.new = false
notif.save
end
end
respond_to do |format| respond_to do |format|
format.html format.html
format.turbo_stream format.turbo_stream
@ -24,36 +17,30 @@ class UserController < ApplicationController
end end
def followers def followers
@title = "Followers"
@relationships = @user.cursored_follower_relationships(last_id: params[:last_id]) @relationships = @user.cursored_follower_relationships(last_id: params[:last_id])
@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)
@type = :follower
respond_to do |format| respond_to do |format|
format.html { render "show_follow" } format.html { render "show_follow", locals: { type: :follower } }
format.turbo_stream { render "show_follow" } format.turbo_stream { render "show_follow", locals: { type: :follower } }
end end
end end
def followings def followings
@title = "Following"
@relationships = @user.cursored_following_relationships(last_id: params[:last_id]) @relationships = @user.cursored_following_relationships(last_id: params[:last_id])
@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)
@type = :friend
respond_to do |format| respond_to do |format|
format.html { render "show_follow" } format.html { render "show_follow", locals: { type: :friend } }
format.turbo_stream { render "show_follow" } format.turbo_stream { render "show_follow", locals: { type: :friend } }
end end
end end
def questions def questions
@title = "Questions"
@questions = @user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: params[:last_id]) @questions = @user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: params[:last_id])
@questions_last_id = @questions.map(&:id).min @questions_last_id = @questions.map(&:id).min
@more_data_available = !@user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: @questions_last_id, size: 1).count.zero? @more_data_available = !@user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: @questions_last_id, size: 1).count.zero?
@ -66,6 +53,18 @@ class UserController < ApplicationController
private private
def mark_notification_as_read
return unless user_signed_in?
Notification
.where(
target_type: "Relationship",
target_id: @user.active_follow_relationships.where(target_id: current_user.id).pluck(:id),
recipient_id: current_user.id,
new: true
).update(new: false)
end
def set_user def set_user
@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

View File

@ -1,15 +1,15 @@
.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: user, type: @type = render "shared/userbox", user:, type:
- 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
= button_to t("voc.load"), @type == :follower ? show_user_followers_path(@user) : show_user_followings_path(@user), = button_to t("voc.load"), type == :follower ? show_user_followers_path(@user) : show_user_followings_path(@user),
class: "btn btn-light", class: "btn btn-light",
method: :get, method: :get,
params: { last_id: @relationships_last_id }, params: { last_id: @relationships_last_id },
form: { data: { turbo_stream: true } } form: { data: { turbo_stream: true } }
- provide(:title, user_title(@user, 'friends and followers')) - provide(:title, t(".title.#{type}", user: @user.profile.safe_name))
- parent_layout 'user/profile' - parent_layout "user/profile"

View File

@ -1,11 +1,11 @@
= 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: user, type: @type = render "shared/userbox", user:, type:
= turbo_stream.update "paginator" do = turbo_stream.update "paginator" do
- if @more_data_available - if @more_data_available
= button_to t("voc.load"), @type == :follower ? show_user_followers_path(@user) : show_user_followings_path(@user), = button_to t("voc.load"), type == :follower ? show_user_followers_path(@user) : show_user_followings_path(@user),
class: "btn btn-light", class: "btn btn-light",
method: :get, method: :get,
params: { last_id: @relationships_last_id }, params: { last_id: @relationships_last_id },

View File

@ -651,6 +651,10 @@ en:
index: index:
title: "Questions from %{author_identifier}" title: "Questions from %{author_identifier}"
user: user:
show_follow:
title:
follower: "%{user}'s followers"
friend: "%{user}'s followings"
actions: actions:
view_inbox: "View inbox" view_inbox: "View inbox"
privilege: "Check %{user}'s privileges" privilege: "Check %{user}'s privileges"