Retrospring/app/controllers/user_controller.rb

125 lines
4.3 KiB
Ruby

class UserController < ApplicationController
before_action :authenticate_user!, only: %w[data edit_security update_2fa destroy_2fa reset_user_recovery_codes edit_mute edit_blocks]
def show
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
@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?
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|
format.html
format.js { render layout: false }
end
end
def followers
@title = 'Followers'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
@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)
@type = :friend
respond_to do |format|
format.html { render "show_follow" }
format.js { render "show_follow", layout: false }
end
end
# rubocop:disable Metrics/AbcSize
def followings
@title = 'Following'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
@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)
@type = :friend
respond_to do |format|
format.html { render "show_follow" }
format.js { render "show_follow", layout: false }
end
end
# rubocop:enable Metrics/AbcSize
def questions
@title = 'Questions'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
@questions = @user.cursored_questions(author_is_anonymous: false, last_id: params[:last_id])
@questions_last_id = @questions.map(&:id).min
@more_data_available = !@user.cursored_questions(author_is_anonymous: false, last_id: @questions_last_id, size: 1).count.zero?
respond_to do |format|
format.html
format.js { render layout: false }
end
end
def data
end
def edit_security
if current_user.otp_module_disabled?
current_user.otp_secret_key = User.otp_random_secret(25)
current_user.save
qr_code = RQRCode::QRCode.new(current_user.provisioning_uri("Retrospring:#{current_user.screen_name}", issuer: "Retrospring"))
@qr_svg = qr_code.as_svg({ offset: 4, module_size: 4, color: "000;fill:var(--primary)" }).html_safe
else
@recovery_code_count = current_user.totp_recovery_codes.count
end
end
def update_2fa
req_params = params.require(:user).permit(:otp_validation)
current_user.otp_module = :enabled
if current_user.authenticate_otp(req_params[:otp_validation], drift: APP_CONFIG.fetch(:otp_drift_period, 30).to_i)
@recovery_keys = TotpRecoveryCode.generate_for(current_user)
current_user.save!
render "settings/security/recovery_keys"
else
flash[:error] = t(".error")
redirect_to edit_user_security_path
end
end
def destroy_2fa
current_user.otp_module = :disabled
current_user.save!
current_user.totp_recovery_codes.delete_all
flash[:success] = t(".success")
redirect_to edit_user_security_path
end
def reset_user_recovery_codes
current_user.totp_recovery_codes.delete_all
@recovery_keys = TotpRecoveryCode.generate_for(current_user)
render 'settings/security/recovery_keys'
end
# region Muting
def edit_mute
@rules = MuteRule.where(user: current_user)
end
# endregion
def edit_blocks
@blocks = Relationships::Block.where(source: current_user)
@anonymous_blocks = AnonymousBlock.where(user: current_user)
end
end