Retrospring/app/controllers/user_controller.rb

190 lines
6.3 KiB
Ruby
Raw Normal View History

2014-11-02 08:57:37 -08:00
class UserController < ApplicationController
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
@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?
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
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
# 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)
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
flash[:error] = t(".error")
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
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)
flash[:success] = t(".success")
2021-12-21 14:56:57 -08:00
else
flash[:error] = t(".error")
2021-12-21 14:56:57 -08:00
end
redirect_to edit_user_profile_path
end
# endregion
2014-12-08 08:03:06 -08:00
# region Privacy settings
def edit_privacy
end
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)
flash[:success] = t(".success")
else
flash[:error] = t(".error")
end
redirect_to edit_user_privacy_path
end
# endregion
2014-12-08 08:03:06 -08:00
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
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'
@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
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'
@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])
@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
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)
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
render "settings/security/recovery_keys"
2020-10-18 01:39:46 -07:00
else
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
current_user.otp_module = :disabled
current_user.save!
2020-11-15 13:08:18 -08:00
current_user.totp_recovery_codes.delete_all
flash[:success] = t(".success")
redirect_to edit_user_security_path
2020-10-18 01:39:46 -07:00
end
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)
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
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