2022-11-15 21:41:05 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-02 17:57:37 +01:00
|
|
|
class UserController < ApplicationController
|
2023-02-17 00:35:03 +01:00
|
|
|
include PaginatesAnswers
|
|
|
|
|
2022-11-15 21:36:06 +01:00
|
|
|
before_action :set_user
|
|
|
|
before_action :hidden_social_graph_redirect, only: %i[followers followings]
|
2023-01-28 18:48:14 +01:00
|
|
|
after_action :mark_notification_as_read, only: %i[show]
|
2022-11-15 21:36:06 +01:00
|
|
|
|
2014-11-02 17:57:37 +01:00
|
|
|
def show
|
2023-11-26 19:32:50 +01:00
|
|
|
@pinned_answers = @user.answers.for_user(current_user).pinned.includes([{ user: :profile }, :question]).order(pinned_at: :desc).limit(10).load_async
|
|
|
|
paginate_answers { |args| @user.cursored_answers(current_user_id: current_user, **args) }
|
2015-02-10 06:53:50 +01:00
|
|
|
|
2014-12-08 15:23:04 +01:00
|
|
|
respond_to do |format|
|
2023-03-19 16:00:15 +01:00
|
|
|
format.html
|
|
|
|
format.turbo_stream { render layout: false }
|
2014-12-08 15:23:04 +01:00
|
|
|
end
|
2014-11-02 17:57:37 +01:00
|
|
|
end
|
|
|
|
|
2014-12-08 17:03:06 +01:00
|
|
|
def followers
|
2023-01-31 13:59:05 +01:00
|
|
|
paginate_relationships(:cursored_follower_relationships)
|
2022-06-18 17:29:23 +02:00
|
|
|
@users = @relationships.map(&:source)
|
2023-02-01 23:20:49 +01:00
|
|
|
own_relationships = find_own_relationships
|
|
|
|
locals = {
|
2023-02-01 23:34:33 +01:00
|
|
|
type: :follower,
|
2023-02-01 23:20:49 +01:00
|
|
|
own_followings: own_relationships[Relationships::Follow],
|
|
|
|
own_blocks: own_relationships[Relationships::Block],
|
|
|
|
own_mutes: own_relationships[Relationships::Mute]
|
|
|
|
}
|
2020-05-09 04:39:09 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
2023-02-01 23:20:49 +01:00
|
|
|
format.html { render "show_follow", locals: }
|
|
|
|
format.turbo_stream { render "show_follow", locals: }
|
2020-05-09 04:39:09 +02:00
|
|
|
end
|
2014-12-08 17:03:06 +01:00
|
|
|
end
|
|
|
|
|
2021-12-31 22:19:21 +01:00
|
|
|
def followings
|
2023-01-31 13:59:05 +01:00
|
|
|
paginate_relationships(:cursored_following_relationships)
|
2022-06-18 17:29:23 +02:00
|
|
|
@users = @relationships.map(&:target)
|
2023-02-01 23:20:49 +01:00
|
|
|
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]
|
|
|
|
}
|
2020-05-09 04:39:09 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
2023-02-01 23:20:49 +01:00
|
|
|
format.html { render "show_follow", locals: }
|
|
|
|
format.turbo_stream { render "show_follow", locals: }
|
2020-05-09 04:39:09 +02:00
|
|
|
end
|
2014-12-08 17:03:06 +01:00
|
|
|
end
|
2014-12-19 22:34:24 +01:00
|
|
|
|
|
|
|
def questions
|
2022-12-27 23:06:24 +01:00
|
|
|
@questions = @user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: params[:last_id])
|
2020-04-20 23:03:57 +02:00
|
|
|
@questions_last_id = @questions.map(&:id).min
|
2022-12-27 23:06:24 +01:00
|
|
|
@more_data_available = !@user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: @questions_last_id, size: 1).count.zero?
|
2020-05-09 04:39:09 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2022-09-07 23:20:47 +02:00
|
|
|
format.turbo_stream
|
2020-05-09 04:39:09 +02:00
|
|
|
end
|
2014-12-19 22:34:24 +01:00
|
|
|
end
|
2022-08-19 04:25:33 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-01-28 18:48:14 +01:00
|
|
|
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
|
|
|
|
|
2022-11-15 21:36:06 +01:00
|
|
|
def set_user
|
2022-11-15 21:41:05 +01:00
|
|
|
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
|
2022-11-15 21:36:06 +01:00
|
|
|
end
|
|
|
|
|
2023-02-01 23:20:49 +01:00
|
|
|
def find_own_relationships
|
|
|
|
return {} unless user_signed_in?
|
2023-01-31 12:25:55 +01:00
|
|
|
|
2023-02-01 23:20:49 +01:00
|
|
|
Relationship.where(source: current_user, target_id: @users.map(&:id))
|
|
|
|
&.select(:target_id, :type)
|
|
|
|
&.group_by(&:type)
|
2023-01-31 12:25:55 +01:00
|
|
|
end
|
|
|
|
|
2023-01-31 13:59:05 +01:00
|
|
|
def paginate_relationships(method)
|
|
|
|
@relationships = @user.public_send(method, last_id: params[:last_id])
|
|
|
|
@relationships_last_id = @relationships.map(&:id).min
|
|
|
|
@more_data_available = !@user.public_send(method, last_id: @relationships_last_id, size: 1).count.zero?
|
|
|
|
end
|
|
|
|
|
2022-11-15 21:36:06 +01:00
|
|
|
def hidden_social_graph_redirect
|
2022-11-15 21:41:05 +01:00
|
|
|
return if belongs_to_current_user? || !@user.privacy_hide_social_graph
|
|
|
|
|
|
|
|
redirect_to user_path(@user)
|
2022-11-15 21:36:06 +01:00
|
|
|
end
|
|
|
|
|
2022-12-27 23:06:24 +01:00
|
|
|
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
|
|
|
|
|
2022-08-19 04:25:33 +02:00
|
|
|
def belongs_to_current_user? = @user == current_user
|
2014-11-02 17:57:37 +01:00
|
|
|
end
|