2014-11-02 08:57:37 -08:00
|
|
|
class UserController < ApplicationController
|
2015-07-29 09:54:33 -07:00
|
|
|
include ThemeHelper
|
|
|
|
|
2021-12-29 12:26:39 -08:00
|
|
|
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)
|
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?
|
|
|
|
notif = Notification.where(target_type: "Relationship", target_id: @user.active_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
|
|
|
|
|
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)
|
2014-12-29 05:54:32 -08:00
|
|
|
if current_user.update_attributes(user_attributes)
|
2015-06-07 10:03:57 -07:00
|
|
|
text = t('flash.user.update.text')
|
|
|
|
text += t('flash.user.update.avatar') if user_attributes[:profile_picture]
|
|
|
|
text += t('flash.user.update.header') if user_attributes[:profile_header]
|
2014-12-29 05:54:32 -08:00
|
|
|
flash[:success] = text
|
|
|
|
else
|
2015-06-07 10:03:57 -07:00
|
|
|
flash[:error] = t('flash.user.update.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
|
|
|
|
profile_attributes = params.require(:profile).permit(:display_name, :motivation_header, :website, :location, :description)
|
|
|
|
|
|
|
|
if current_user.profile.update_attributes(profile_attributes)
|
|
|
|
flash[:success] = t('flash.user.update.text')
|
|
|
|
else
|
|
|
|
flash[:error] = t('flash.user.update.error')
|
|
|
|
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)
|
|
|
|
if current_user.update_attributes(user_attributes)
|
2015-06-07 10:03:57 -07:00
|
|
|
flash[:success] = t('flash.user.update_privacy.success')
|
2015-01-03 12:58:56 -08:00
|
|
|
else
|
2015-06-07 10:03:57 -07:00
|
|
|
flash[:error] = t('flash.user.update_privacy.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
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
# region Lists
|
|
|
|
def lists
|
2015-01-11 21:57:43 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
2020-05-25 09:04:54 -07:00
|
|
|
@lists = if current_user == @user
|
|
|
|
@user.lists
|
2015-01-11 21:57:43 -08:00
|
|
|
else
|
2020-05-25 09:04:54 -07:00
|
|
|
@user.lists.where(private: false)
|
2015-01-11 22:08:06 -08:00
|
|
|
end.all
|
2015-01-11 21:57:43 -08:00
|
|
|
end
|
|
|
|
# endregion
|
|
|
|
|
2014-12-08 08:03:06 -08:00
|
|
|
def followers
|
|
|
|
@title = 'Followers'
|
2014-12-21 06:32:49 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
2020-04-20 14:03:57 -07:00
|
|
|
@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?
|
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
|
|
|
|
|
2014-12-08 10:51:34 -08:00
|
|
|
def friends
|
2014-12-08 08:03:06 -08:00
|
|
|
@title = 'Following'
|
2014-12-21 06:32:49 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
2021-12-30 13:15:59 -08:00
|
|
|
@users = @user.cursored_friends(last_id: params[:last_id]).includes(:profile)
|
2020-04-20 14:03:57 -07:00
|
|
|
@users_last_id = @users.map(&:id).min
|
|
|
|
@more_data_available = !@user.cursored_friends(last_id: @users_last_id, size: 1).count.zero?
|
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
|
2014-12-19 13:34:24 -08:00
|
|
|
|
|
|
|
def questions
|
|
|
|
@title = 'Questions'
|
2014-12-21 06:32:49 -08:00
|
|
|
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).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
|
|
|
|
2015-08-25 01:26:36 -07:00
|
|
|
def edit_theme
|
|
|
|
end
|
|
|
|
|
2015-08-26 16:56:07 -07:00
|
|
|
def delete_theme
|
|
|
|
current_user.theme.destroy!
|
2020-05-06 13:19:30 -07:00
|
|
|
redirect_to edit_user_theme_path
|
2015-08-26 16:56:07 -07:00
|
|
|
end
|
|
|
|
|
2015-08-25 01:26:36 -07:00
|
|
|
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,
|
2020-05-04 17:02:16 -07:00
|
|
|
:dark_color, :dark_text,
|
|
|
|
:light_color, :light_text,
|
|
|
|
:raised_background, :raised_accent,
|
|
|
|
:background_color, :body_text,
|
|
|
|
:muted_text, :input_color,
|
|
|
|
:input_text
|
2015-08-25 01:26:36 -07:00
|
|
|
])
|
|
|
|
|
|
|
|
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
|
2015-08-25 11:27:06 -07:00
|
|
|
flash[:success] = 'Theme saved.'
|
2015-08-25 01:26:36 -07:00
|
|
|
else
|
2015-08-25 12:50:17 -07:00
|
|
|
flash[:error] = 'Theme saving failed. ' + current_user.theme.errors.messages.flatten.join(' ')
|
2015-08-25 01:26:36 -07:00
|
|
|
end
|
2015-08-25 11:27:06 -07:00
|
|
|
elsif current_user.theme.update_attributes(update_attributes)
|
|
|
|
flash[:success] = 'Theme saved.'
|
2015-08-25 01:26:36 -07:00
|
|
|
else
|
2015-08-25 12:50:17 -07:00
|
|
|
flash[:error] = 'Theme saving failed. ' + current_user.theme.errors.messages.flatten.join(' ')
|
2015-08-25 01:26:36 -07:00
|
|
|
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
|
|
|
|
flash[:info] = 'An export is currently in progress for this account.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_export
|
|
|
|
if current_user.can_export?
|
|
|
|
ExportWorker.perform_async(current_user.id)
|
|
|
|
flash[:success] = 'Your account is currently being exported. This will take a little while.'
|
|
|
|
else
|
|
|
|
flash[:error] = 'Nice try, kid.'
|
|
|
|
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
|
|
|
|
2020-10-21 04:44:00 -07: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
|
|
|
|
|
|
|
render 'settings/security/recovery_keys'
|
2020-10-18 01:39:46 -07:00
|
|
|
else
|
2020-10-23 11:58:42 -07:00
|
|
|
flash[:error] = t('views.auth.2fa.errors.invalid_code')
|
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
|
2020-10-18 10:48:12 -07:00
|
|
|
flash[:success] = 'Two factor authentication has been disabled for your account.'
|
|
|
|
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
|
2014-11-02 08:57:37 -08:00
|
|
|
end
|