Retrospring/app/controllers/user_controller.rb

225 lines
7.2 KiB
Ruby
Raw Normal View History

2014-11-02 08:57:37 -08:00
class UserController < ApplicationController
2015-07-29 09:54:33 -07:00
include ThemeHelper
before_action :authenticate_user!, only: %w(edit update edit_privacy update_privacy edit_theme update_theme preview_theme delete_theme 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)
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!
@users = @user.cursored_followers(last_id: params[:last_id])
@users_last_id = @users.map(&:id).min
@more_data_available = !@user.cursored_followers(last_id: @users_last_id, size: 1).count.zero?
@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!
2021-12-31 13:19:21 -08:00
@users = @user.cursored_followings(last_id: params[:last_id])
@users_last_id = @users.map(&:id).min
2021-12-31 13:19:21 -08:00
@more_data_available = !@user.cursored_followings(last_id: @users_last_id, size: 1).count.zero?
@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
def edit_theme
end
def delete_theme
current_user.theme.destroy!
redirect_to edit_user_theme_path
end
def update_theme
update_attributes = params.require(:theme).permit([
:primary_color, :primary_text,
:danger_color, :danger_text,
:success_color, :success_text,
:warning_color, :warning_text,
:info_color, :info_text,
:dark_color, :dark_text,
:light_color, :light_text,
:raised_background, :raised_accent,
:background_color, :body_text,
:muted_text, :input_color,
:input_text
])
if current_user.theme.nil?
current_user.theme = Theme.new update_attributes
current_user.theme.user_id = current_user.id
if current_user.theme.save
flash[:success] = t(".success")
else
2022-02-12 16:06:38 -08:00
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
2022-01-11 17:24:38 -08:00
elsif current_user.theme.update(update_attributes)
flash[:success] = t(".success")
else
2022-02-12 16:06:38 -08:00
flash[:error] = t(".error", errors: current_user.theme.errors.messages.flatten.join(" "))
end
redirect_to edit_user_theme_path
2015-07-24 10:12:14 -07:00
end
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
2014-11-02 08:57:37 -08:00
end