retrospring/app/controllers/user_controller.rb
2023-01-31 12:25:55 +01:00

101 lines
3.6 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
class UserController < ApplicationController
before_action :set_user
before_action :hidden_social_graph_redirect, only: %i[followers followings]
after_action :mark_notification_as_read, only: %i[show]
def show
@answers = @user.cursored_answers(last_id: params[:last_id])
@answers_last_id = @answers.map(&:id).min
@more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero?
respond_to do |format|
format.html
format.turbo_stream
end
end
def followers
@relationships = @user.cursored_follower_relationships(last_id: params[:last_id])
@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 } }
format.turbo_stream { render "show_follow", locals: { type: :follower } }
end
end
def followings
@relationships = @user.cursored_following_relationships(last_id: params[:last_id])
@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 } }
format.turbo_stream { render "show_follow", locals: { type: :friend } }
end
end
def questions
@questions = @user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: params[:last_id])
@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?
respond_to do |format|
format.html
format.turbo_stream
end
end
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
@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
redirect_to user_path(@user)
end
def direct_param
# return `nil` instead of `false` so we retrieve all questions for the user, direct or not.
# `cursored_questions` will then remove the `direct` field from the WHERE query. otherwise the query filters
# for `WHERE direct = false` ...
return if belongs_to_current_user? || moderation_view?
# page is not being viewed by the current user, and we're not in the moderation view -> only show public questions
false
end
def belongs_to_current_user? = @user == current_user
end