2014-11-02 08:57:37 -08:00
|
|
|
class UserController < ApplicationController
|
2022-06-25 17:58:22 -07:00
|
|
|
before_action :authenticate_user!, only: %w(edit update edit_privacy update_privacy data export begin_export edit_security update_2fa destroy_2fa reset_user_recovery_codes edit_mute)
|
2014-12-29 02:21:43 -08:00
|
|
|
|
2014-11-02 08:57:37 -08:00
|
|
|
def show
|
2021-12-30 13:15:59 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
2020-04-20 14:03:57 -07:00
|
|
|
@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?
|
2015-02-09 21:53:50 -08:00
|
|
|
|
|
|
|
if user_signed_in?
|
2021-12-31 13:19:21 -08:00
|
|
|
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
|
2015-02-09 21:53:50 -08:00
|
|
|
unless notif.nil?
|
|
|
|
notif.new = false
|
|
|
|
notif.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-08 06:23:04 -08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2020-05-08 19:39:09 -07:00
|
|
|
format.js { render layout: false }
|
2014-12-08 06:23:04 -08:00
|
|
|
end
|
2014-11-02 08:57:37 -08:00
|
|
|
end
|
|
|
|
|
2015-01-03 12:58:56 -08:00
|
|
|
# region Account settings
|
2014-11-02 08:57:37 -08:00
|
|
|
def edit
|
2014-11-03 04:21:41 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-12-21 14:56:57 -08:00
|
|
|
user_attributes = params.require(:user).permit(:show_foreign_themes, :profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
|
2020-05-02 09:45:11 -07:00
|
|
|
:profile_header_x, :profile_header_y, :profile_header_w, :profile_header_h, :profile_picture, :profile_header)
|
2022-01-11 17:24:38 -08:00
|
|
|
if current_user.update(user_attributes)
|
2022-01-29 16:15:34 -08:00
|
|
|
text = t(".success")
|
|
|
|
text += t(".notice.profile_picture") if user_attributes[:profile_picture]
|
|
|
|
text += t(".notice.profile_header") if user_attributes[:profile_header]
|
2014-12-29 05:54:32 -08:00
|
|
|
flash[:success] = text
|
|
|
|
else
|
2022-01-29 16:15:34 -08:00
|
|
|
flash[:error] = t(".error")
|
2014-11-11 11:20:00 -08:00
|
|
|
end
|
2014-11-03 04:21:41 -08:00
|
|
|
redirect_to edit_user_profile_path
|
2014-11-02 08:57:37 -08:00
|
|
|
end
|
2021-12-21 14:56:57 -08:00
|
|
|
|
|
|
|
def update_profile
|
2022-06-21 05:51:22 -07:00
|
|
|
profile_attributes = params.require(:profile).permit(:display_name, :motivation_header, :website, :location, :description, :anon_display_name)
|
2021-12-21 14:56:57 -08:00
|
|
|
|
2022-01-11 17:24:38 -08:00
|
|
|
if current_user.profile.update(profile_attributes)
|
2022-01-29 16:15:34 -08:00
|
|
|
flash[:success] = t(".success")
|
2021-12-21 14:56:57 -08:00
|
|
|
else
|
2022-01-29 16:15:34 -08:00
|
|
|
flash[:error] = t(".error")
|
2021-12-21 14:56:57 -08:00
|
|
|
end
|
|
|
|
redirect_to edit_user_profile_path
|
|
|
|
end
|
2015-01-03 12:58:56 -08:00
|
|
|
# endregion
|
2014-12-08 08:03:06 -08:00
|
|
|
|
2015-01-03 12:58:56 -08:00
|
|
|
# region Privacy settings
|
|
|
|
def edit_privacy
|
|
|
|
end
|
2015-01-02 12:34:56 -08:00
|
|
|
|
2015-01-03 12:58:56 -08:00
|
|
|
def update_privacy
|
|
|
|
user_attributes = params.require(:user).permit(:privacy_allow_anonymous_questions,
|
|
|
|
:privacy_allow_public_timeline,
|
|
|
|
:privacy_allow_stranger_answers,
|
|
|
|
:privacy_show_in_search)
|
2022-01-11 17:24:38 -08:00
|
|
|
if current_user.update(user_attributes)
|
2022-02-19 08:45:34 -08:00
|
|
|
flash[:success] = t(".success")
|
2015-01-03 12:58:56 -08:00
|
|
|
else
|
2022-02-19 08:45:34 -08:00
|
|
|
flash[:error] = t(".error")
|
2015-01-03 12:58:56 -08:00
|
|
|
end
|
|
|
|
redirect_to edit_user_privacy_path
|
2015-01-02 12:34:56 -08:00
|
|
|
end
|
2015-01-03 12:58:56 -08:00
|
|
|
# endregion
|
2015-01-02 12:34:56 -08:00
|
|
|
|
2014-12-08 08:03:06 -08:00
|
|
|
def followers
|
|
|
|
@title = 'Followers'
|
2021-12-31 06:35:02 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
2022-06-18 08:29:23 -07:00
|
|
|
@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)
|
2014-12-08 10:48:12 -08:00
|
|
|
@type = :friend
|
2020-05-08 19:39:09 -07:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render "show_follow" }
|
|
|
|
format.js { render "show_follow", layout: false }
|
|
|
|
end
|
2014-12-08 08:03:06 -08:00
|
|
|
end
|
|
|
|
|
2022-01-16 09:51:27 -08:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
2021-12-31 13:19:21 -08:00
|
|
|
def followings
|
2014-12-08 08:03:06 -08:00
|
|
|
@title = 'Following'
|
2021-12-31 06:35:02 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
2022-06-18 08:29:23 -07:00
|
|
|
@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)
|
2014-12-08 10:48:12 -08:00
|
|
|
@type = :friend
|
2020-05-08 19:39:09 -07:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render "show_follow" }
|
|
|
|
format.js { render "show_follow", layout: false }
|
|
|
|
end
|
2014-12-08 08:03:06 -08:00
|
|
|
end
|
2022-01-16 09:51:27 -08:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
2014-12-19 13:34:24 -08:00
|
|
|
|
|
|
|
def questions
|
|
|
|
@title = 'Questions'
|
2021-12-31 06:35:02 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
2020-04-22 18:31:07 -07:00
|
|
|
@questions = @user.cursored_questions(author_is_anonymous: false, last_id: params[:last_id])
|
2020-04-20 14:03:57 -07:00
|
|
|
@questions_last_id = @questions.map(&:id).min
|
2020-04-22 18:31:07 -07:00
|
|
|
@more_data_available = !@user.cursored_questions(author_is_anonymous: false, last_id: @questions_last_id, size: 1).count.zero?
|
2020-05-08 19:39:09 -07:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.js { render layout: false }
|
|
|
|
end
|
2014-12-19 13:34:24 -08:00
|
|
|
end
|
2015-06-20 11:38:07 -07:00
|
|
|
|
|
|
|
def data
|
|
|
|
end
|
2015-07-24 10:12:14 -07:00
|
|
|
|
2016-01-05 11:54:38 -08:00
|
|
|
def export
|
|
|
|
if current_user.export_processing
|
2022-02-12 17:26:15 -08:00
|
|
|
flash[:info] = t(".info")
|
2016-01-05 11:54:38 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_export
|
|
|
|
if current_user.can_export?
|
|
|
|
ExportWorker.perform_async(current_user.id)
|
2022-02-12 17:26:15 -08:00
|
|
|
flash[:success] = t(".success")
|
2016-01-05 11:54:38 -08:00
|
|
|
else
|
2022-02-12 17:26:15 -08:00
|
|
|
flash[:error] = t(".error")
|
2016-01-05 11:54:38 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to user_export_path
|
|
|
|
end
|
2020-10-18 01:39:46 -07:00
|
|
|
|
|
|
|
def edit_security
|
2020-10-21 04:44:00 -07:00
|
|
|
if current_user.otp_module_disabled?
|
2020-11-15 01:21:06 -08:00
|
|
|
current_user.otp_secret_key = User.otp_random_secret(25)
|
2020-10-23 11:45:06 -07:00
|
|
|
current_user.save
|
2020-10-18 01:39:46 -07:00
|
|
|
|
2020-10-21 04:44:00 -07:00
|
|
|
qr_code = RQRCode::QRCode.new(current_user.provisioning_uri("Retrospring:#{current_user.screen_name}", issuer: "Retrospring"))
|
2020-10-19 05:56:13 -07:00
|
|
|
|
2022-02-13 10:23:40 -08:00
|
|
|
@qr_svg = qr_code.as_svg({ offset: 4, module_size: 4, color: "000;fill:var(--primary)" }).html_safe
|
2020-11-01 09:41:37 -08:00
|
|
|
else
|
2020-11-15 13:08:18 -08:00
|
|
|
@recovery_code_count = current_user.totp_recovery_codes.count
|
2020-10-21 04:44:00 -07:00
|
|
|
end
|
2020-10-18 01:39:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_2fa
|
2020-10-23 11:45:06 -07:00
|
|
|
req_params = params.require(:user).permit(:otp_validation)
|
2020-10-18 10:48:12 -07:00
|
|
|
current_user.otp_module = :enabled
|
2020-10-18 01:39:46 -07:00
|
|
|
|
2020-10-23 15:24:04 -07:00
|
|
|
if current_user.authenticate_otp(req_params[:otp_validation], drift: APP_CONFIG.fetch(:otp_drift_period, 30).to_i)
|
2020-11-15 01:21:06 -08:00
|
|
|
@recovery_keys = TotpRecoveryCode.generate_for(current_user)
|
2020-10-18 01:39:46 -07:00
|
|
|
current_user.save!
|
2020-11-01 08:55:31 -08:00
|
|
|
|
2022-02-13 09:52:02 -08:00
|
|
|
render "settings/security/recovery_keys"
|
2020-10-18 01:39:46 -07:00
|
|
|
else
|
2022-02-13 09:52:02 -08:00
|
|
|
flash[:error] = t(".error")
|
2020-11-01 08:55:31 -08:00
|
|
|
redirect_to edit_user_security_path
|
2020-10-18 01:39:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_2fa
|
2020-10-18 10:48:12 -07:00
|
|
|
current_user.otp_module = :disabled
|
|
|
|
current_user.save!
|
2020-11-15 13:08:18 -08:00
|
|
|
current_user.totp_recovery_codes.delete_all
|
2022-02-13 09:52:02 -08:00
|
|
|
flash[:success] = t(".success")
|
2020-10-18 10:48:12 -07:00
|
|
|
redirect_to edit_user_security_path
|
2020-10-18 01:39:46 -07:00
|
|
|
end
|
2020-11-01 09:52:42 -08:00
|
|
|
|
|
|
|
def reset_user_recovery_codes
|
2020-11-15 13:08:18 -08:00
|
|
|
current_user.totp_recovery_codes.delete_all
|
2020-11-15 01:21:06 -08:00
|
|
|
@recovery_keys = TotpRecoveryCode.generate_for(current_user)
|
2020-11-01 09:52:42 -08:00
|
|
|
render 'settings/security/recovery_keys'
|
|
|
|
end
|
2021-12-22 15:03:42 -08:00
|
|
|
|
|
|
|
# region Muting
|
|
|
|
def edit_mute
|
|
|
|
@rules = MuteRule.where(user: current_user)
|
|
|
|
end
|
|
|
|
# endregion
|
2022-06-23 11:03:11 -07:00
|
|
|
|
|
|
|
def edit_blocks
|
|
|
|
@blocks = Relationships::Block.where(source: current_user)
|
|
|
|
@anonymous_blocks = AnonymousBlock.where(user: current_user)
|
|
|
|
end
|
2014-11-02 08:57:37 -08:00
|
|
|
end
|