Retrospring/app/controllers/user_controller.rb

218 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
2020-04-18 16:45:50 -07: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)
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).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_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
2020-05-02 09:45:11 -07:00
user_attributes = params.require(:user).permit(:display_name, :motivation_header, :website, :show_foreign_themes, :location, :bio,
:profile_picture_x, :profile_picture_y, :profile_picture_w, :profile_picture_h,
: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')
end
2014-11-03 04:21:41 -08:00
redirect_to edit_user_profile_path
2014-11-02 08:57:37 -08:00
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)
if current_user.update_attributes(user_attributes)
2015-06-07 10:03:57 -07:00
flash[:success] = t('flash.user.update_privacy.success')
else
2015-06-07 10:03:57 -07:00
flash[:error] = t('flash.user.update_privacy.error')
end
redirect_to edit_user_privacy_path
end
# endregion
# region Lists
def lists
2015-01-11 21:57:43 -08:00
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
@lists = if current_user == @user
@user.lists
2015-01-11 21:57:43 -08:00
else
@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'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).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
2014-12-08 10:51:34 -08:00
def friends
2014-12-08 08:03:06 -08:00
@title = 'Following'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
@users = @user.cursored_friends(last_id: params[:last_id])
@users_last_id = @users.map(&:id).min
@more_data_available = !@user.cursored_friends(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
2014-12-19 13:34:24 -08:00
def questions
@title = 'Questions'
@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])
@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] = 'Theme saved.'
else
2015-08-25 12:50:17 -07:00
flash[:error] = 'Theme saving failed. ' + current_user.theme.errors.messages.flatten.join(' ')
end
elsif current_user.theme.update_attributes(update_attributes)
flash[:success] = 'Theme saved.'
else
2015-08-25 12:50:17 -07:00
flash[:error] = 'Theme saving failed. ' + 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
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
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
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
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] = 'Two factor authentication has been disabled for your account.'
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
2014-11-02 08:57:37 -08:00
end