2017-09-15 01:02:11 -04:00
|
|
|
require_dependency 'jobs/base'
|
2013-04-18 14:27:04 -04:00
|
|
|
require_dependency 'email'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'email_token'
|
2016-12-21 04:00:45 -05:00
|
|
|
require_dependency 'email_validator'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'trust_level'
|
2013-03-06 07:12:16 -05:00
|
|
|
require_dependency 'pbkdf2'
|
2013-05-10 16:58:23 -04:00
|
|
|
require_dependency 'discourse'
|
2013-06-05 16:00:45 -04:00
|
|
|
require_dependency 'post_destroyer'
|
2013-06-06 10:40:10 -04:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-06-23 00:32:46 -04:00
|
|
|
require_dependency 'pretty_text'
|
2013-11-22 13:18:45 -05:00
|
|
|
require_dependency 'url_helper'
|
2014-05-30 00:17:35 -04:00
|
|
|
require_dependency 'letter_avatar'
|
2014-06-16 20:46:30 -04:00
|
|
|
require_dependency 'promotion'
|
2017-09-04 21:39:56 -04:00
|
|
|
require_dependency 'password_validator'
|
2017-09-15 01:02:11 -04:00
|
|
|
require_dependency 'notification_serializer'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2017-08-15 11:46:57 -04:00
|
|
|
include Searchable
|
2013-06-06 18:07:59 -04:00
|
|
|
include Roleable
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2018-02-20 01:44:51 -05:00
|
|
|
include SecondFactorManager
|
2013-06-06 18:07:59 -04:00
|
|
|
|
2017-11-06 21:06:42 -05:00
|
|
|
# TODO: Remove this after 7th Jan 2018
|
|
|
|
self.ignored_columns = %w{email}
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :posts
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :notifications, dependent: :destroy
|
|
|
|
has_many :topic_users, dependent: :destroy
|
2015-09-02 14:43:15 -04:00
|
|
|
has_many :category_users, dependent: :destroy
|
2016-05-04 14:02:47 -04:00
|
|
|
has_many :tag_users, dependent: :destroy
|
2016-08-16 03:06:33 -04:00
|
|
|
has_many :user_api_keys, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :topics
|
2013-04-11 16:04:20 -04:00
|
|
|
has_many :user_open_ids, dependent: :destroy
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :user_actions, dependent: :destroy
|
|
|
|
has_many :post_actions, dependent: :destroy
|
2015-09-02 14:43:15 -04:00
|
|
|
has_many :user_badges, -> { where('user_badges.badge_id IN (SELECT id FROM badges WHERE enabled)') }, dependent: :destroy
|
2014-07-09 01:31:49 -04:00
|
|
|
has_many :badges, through: :user_badges
|
2014-11-28 14:20:43 -05:00
|
|
|
has_many :email_logs, dependent: :delete_all
|
2016-04-18 16:58:30 -04:00
|
|
|
has_many :incoming_emails, dependent: :delete_all
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :post_timings
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :topic_allowed_users, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :topics_allowed, through: :topic_allowed_users, source: :topic
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :email_tokens, dependent: :destroy
|
|
|
|
has_many :user_visits, dependent: :destroy
|
|
|
|
has_many :invites, dependent: :destroy
|
|
|
|
has_many :topic_links, dependent: :destroy
|
2013-11-22 12:29:07 -05:00
|
|
|
has_many :uploads
|
2017-04-15 00:11:02 -04:00
|
|
|
has_many :user_warnings
|
2015-12-22 19:09:17 -05:00
|
|
|
has_many :user_archived_messages, dependent: :destroy
|
2016-03-07 14:40:11 -05:00
|
|
|
has_many :email_change_requests, dependent: :destroy
|
2017-01-16 11:46:05 -05:00
|
|
|
has_many :directory_items, dependent: :delete_all
|
2017-01-31 17:21:37 -05:00
|
|
|
has_many :user_auth_tokens, dependent: :destroy
|
2017-04-26 14:47:36 -04:00
|
|
|
has_many :user_emails, dependent: :destroy
|
2015-12-22 19:09:17 -05:00
|
|
|
|
2017-07-25 11:44:46 -04:00
|
|
|
has_one :primary_email, -> { where(primary: true) }, class_name: 'UserEmail', dependent: :destroy
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
has_one :user_option, dependent: :destroy
|
2014-05-22 03:37:02 -04:00
|
|
|
has_one :user_avatar, dependent: :destroy
|
2013-06-24 10:03:51 -04:00
|
|
|
has_one :facebook_user_info, dependent: :destroy
|
2013-04-11 16:04:20 -04:00
|
|
|
has_one :twitter_user_info, dependent: :destroy
|
|
|
|
has_one :github_user_info, dependent: :destroy
|
2014-09-25 01:50:54 -04:00
|
|
|
has_one :google_user_info, dependent: :destroy
|
2013-08-18 00:43:59 -04:00
|
|
|
has_one :oauth2_user_info, dependent: :destroy
|
2018-03-01 06:10:27 -05:00
|
|
|
has_one :instagram_user_info, dependent: :destroy
|
2017-12-21 20:18:12 -05:00
|
|
|
has_one :user_second_factor, dependent: :destroy
|
2013-09-11 14:50:26 -04:00
|
|
|
has_one :user_stat, dependent: :destroy
|
2014-06-10 01:19:08 -04:00
|
|
|
has_one :user_profile, dependent: :destroy, inverse_of: :user
|
2014-02-24 22:30:49 -05:00
|
|
|
has_one :single_sign_on_record, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :approved_by, class_name: 'User'
|
2014-04-23 22:42:04 -04:00
|
|
|
belongs_to :primary_group, class_name: 'Group'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :group_users, dependent: :destroy
|
2013-04-29 02:33:24 -04:00
|
|
|
has_many :groups, through: :group_users
|
|
|
|
has_many :secure_categories, through: :groups, source: :categories
|
|
|
|
|
2015-03-23 20:55:22 -04:00
|
|
|
has_many :muted_user_records, class_name: 'MutedUser'
|
|
|
|
has_many :muted_users, through: :muted_user_records
|
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
has_one :api_key, dependent: :destroy
|
2013-05-22 15:33:33 -04:00
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
belongs_to :uploaded_avatar, class_name: 'Upload'
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
has_many :acting_group_histories, dependent: :destroy, foreign_key: :acting_user_id, class_name: 'GroupHistory'
|
|
|
|
has_many :targeted_group_histories, dependent: :destroy, foreign_key: :target_user_id, class_name: 'GroupHistory'
|
2016-12-11 10:36:15 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
delegate :last_sent_email_address, to: :email_logs
|
2013-11-15 10:27:43 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :username
|
2017-08-31 00:06:56 -04:00
|
|
|
validate :username_validator, if: :will_save_change_to_username?
|
2013-02-05 14:16:51 -05:00
|
|
|
validate :password_validator
|
2017-08-31 00:06:56 -04:00
|
|
|
validates :name, user_full_name: true, if: :will_save_change_to_name?, length: { maximum: 255 }
|
2017-07-27 21:20:09 -04:00
|
|
|
validates :ip_address, allowed_ip_address: { on: :create, message: :signup_not_allowed }
|
2017-07-30 22:45:18 -04:00
|
|
|
validates :primary_email, presence: true
|
2017-09-11 13:22:04 -04:00
|
|
|
validates_associated :primary_email, message: -> (_, user_email) { user_email[:value]&.errors[:email]&.first }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
after_initialize :add_trust_level
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2017-10-25 01:02:18 -04:00
|
|
|
before_validation :set_skip_validate_email
|
2017-08-08 22:56:08 -04:00
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
after_create :create_email_token
|
2013-09-11 14:50:26 -04:00
|
|
|
after_create :create_user_stat
|
2016-02-16 23:46:19 -05:00
|
|
|
after_create :create_user_option
|
2014-05-27 13:54:04 -04:00
|
|
|
after_create :create_user_profile
|
2014-06-16 20:46:30 -04:00
|
|
|
after_create :ensure_in_trust_level_group
|
2015-08-21 14:39:21 -04:00
|
|
|
after_create :set_default_categories_preferences
|
2014-08-13 16:17:16 -04:00
|
|
|
|
|
|
|
before_save :update_username_lower
|
|
|
|
before_save :ensure_password_is_hashed
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
after_save :expire_tokens_if_password_changed
|
2014-08-13 16:17:16 -04:00
|
|
|
after_save :clear_global_notice_if_needed
|
2014-05-22 03:37:02 -04:00
|
|
|
after_save :refresh_avatar
|
2014-07-22 21:42:24 -04:00
|
|
|
after_save :badge_grant
|
2015-06-05 13:50:06 -04:00
|
|
|
after_save :expire_old_email_tokens
|
2016-12-21 21:13:14 -05:00
|
|
|
after_save :index_search
|
2017-03-16 04:02:34 -04:00
|
|
|
after_commit :trigger_user_created_event, on: :create
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
before_destroy do
|
|
|
|
# These tables don't have primary keys, so destroying them with activerecord is tricky:
|
2017-08-31 00:06:56 -04:00
|
|
|
PostTiming.where(user_id: self.id).delete_all
|
|
|
|
TopicViewItem.where(user_id: self.id).delete_all
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
|
2016-09-07 14:05:46 -04:00
|
|
|
# Skip validating email, for example from a particular auth provider plugin
|
|
|
|
attr_accessor :skip_email_validation
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Whether we need to be sending a system message after creation
|
|
|
|
attr_accessor :send_welcome_message
|
|
|
|
|
|
|
|
# This is just used to pass some information into the serializer
|
|
|
|
attr_accessor :notification_channel_position
|
|
|
|
|
2014-08-13 16:17:16 -04:00
|
|
|
# set to true to optimize creation and save for imports
|
|
|
|
attr_accessor :import_mode
|
|
|
|
|
2017-04-26 14:47:36 -04:00
|
|
|
scope :with_email, ->(email) { joins(:user_emails).where(user_emails: { email: email }) }
|
|
|
|
|
2017-03-11 01:25:09 -05:00
|
|
|
scope :human_users, -> { where('users.id > 0') }
|
|
|
|
|
2015-05-10 19:10:10 -04:00
|
|
|
# excluding fake users like the system user or anonymous users
|
2017-03-11 01:25:09 -05:00
|
|
|
scope :real, -> { human_users.where('NOT EXISTS(
|
2015-05-10 19:10:10 -04:00
|
|
|
SELECT 1
|
|
|
|
FROM user_custom_fields ucf
|
|
|
|
WHERE
|
|
|
|
ucf.user_id = users.id AND
|
|
|
|
ucf.name = ? AND
|
|
|
|
ucf.value::int > 0
|
|
|
|
)', 'master_id') }
|
2013-03-29 02:29:58 -04:00
|
|
|
|
2014-09-03 17:50:19 -04:00
|
|
|
# TODO-PERF: There is no indexes on any of these
|
|
|
|
# and NotifyMailingListSubscribers does a select-all-and-loop
|
2017-11-10 12:18:08 -05:00
|
|
|
# may want to create an index on (active, silence, suspended_till)?
|
2017-11-13 13:41:36 -05:00
|
|
|
scope :silenced, -> { where("silenced_till IS NOT NULL AND silenced_till > ?", Time.zone.now) }
|
|
|
|
scope :not_silenced, -> { where("silenced_till IS NULL OR silenced_till <= ?", Time.zone.now) }
|
2014-09-03 17:50:19 -04:00
|
|
|
scope :suspended, -> { where('suspended_till IS NOT NULL AND suspended_till > ?', Time.zone.now) }
|
|
|
|
scope :not_suspended, -> { where('suspended_till IS NULL OR suspended_till <= ?', Time.zone.now) }
|
|
|
|
scope :activated, -> { where(active: true) }
|
|
|
|
|
2013-02-14 01:32:58 -05:00
|
|
|
module NewTopicDuration
|
2013-02-25 11:42:20 -05:00
|
|
|
ALWAYS = -1
|
2013-02-14 01:32:58 -05:00
|
|
|
LAST_VISIT = -2
|
|
|
|
end
|
2014-03-07 12:58:53 -05:00
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
def self.max_password_length
|
|
|
|
200
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.username_length
|
2014-07-16 12:25:24 -04:00
|
|
|
SiteSetting.min_username_length.to_i..SiteSetting.max_username_length.to_i
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-12-12 05:33:35 -05:00
|
|
|
def self.username_available?(username, email = nil)
|
2013-06-06 10:40:10 -04:00
|
|
|
lower = username.downcase
|
2017-12-12 05:26:00 -05:00
|
|
|
return false if reserved_username?(lower)
|
|
|
|
return true if !User.exists?(username_lower: lower)
|
|
|
|
# staged users can use the same username since they will take over the account
|
|
|
|
email.present? && User.joins(:user_emails).exists?(staged: true, username_lower: lower, user_emails: { primary: true, email: email })
|
2017-04-12 22:44:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reserved_username?(username)
|
|
|
|
lower = username.downcase
|
2016-08-31 09:49:45 -04:00
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
SiteSetting.reserved_usernames.split("|").any? do |reserved|
|
|
|
|
!!lower.match("^#{Regexp.escape(reserved).gsub('\*', '.*')}$")
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-03-11 15:52:18 -05:00
|
|
|
def self.plugin_staff_user_custom_fields
|
|
|
|
@plugin_staff_user_custom_fields ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_plugin_staff_custom_field(custom_field_name, plugin)
|
|
|
|
plugin_staff_user_custom_fields[custom_field_name] = plugin
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.whitelisted_user_custom_fields(guardian)
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
if SiteSetting.public_user_custom_fields.present?
|
|
|
|
fields += SiteSetting.public_user_custom_fields.split('|')
|
|
|
|
end
|
|
|
|
|
|
|
|
if guardian.is_staff?
|
|
|
|
if SiteSetting.staff_user_custom_fields.present?
|
|
|
|
fields += SiteSetting.staff_user_custom_fields.split('|')
|
|
|
|
end
|
|
|
|
plugin_staff_user_custom_fields.each do |k, v|
|
|
|
|
fields << k if v.enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fields.uniq
|
|
|
|
end
|
|
|
|
|
2015-02-05 22:38:51 -05:00
|
|
|
def effective_locale
|
|
|
|
if SiteSetting.allow_user_locale && self.locale.present?
|
|
|
|
self.locale
|
|
|
|
else
|
|
|
|
SiteSetting.default_locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-31 12:51:13 -04:00
|
|
|
EMAIL = %r{([^@]+)@([^\.]+)}
|
2018-01-19 09:29:15 -05:00
|
|
|
FROM_STAGED = "from_staged".freeze
|
2013-03-31 12:51:13 -04:00
|
|
|
|
2013-04-12 18:46:55 -04:00
|
|
|
def self.new_from_params(params)
|
|
|
|
user = User.new
|
|
|
|
user.name = params[:name]
|
|
|
|
user.email = params[:email]
|
|
|
|
user.password = params[:password]
|
|
|
|
user.username = params[:username]
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2018-01-19 09:29:15 -05:00
|
|
|
def self.unstage(params)
|
|
|
|
if user = User.where(staged: true).with_email(params[:email].strip.downcase).first
|
|
|
|
params.each { |k, v| user.send("#{k}=", v) }
|
|
|
|
user.staged = false
|
|
|
|
user.active = false
|
|
|
|
user.custom_fields[FROM_STAGED] = true
|
2018-01-22 12:04:30 -05:00
|
|
|
user.notifications.destroy_all
|
2018-01-19 11:56:24 -05:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:user_unstaged, user)
|
2018-01-19 09:29:15 -05:00
|
|
|
end
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.suggest_name(email)
|
2015-08-20 05:59:28 -04:00
|
|
|
return "" if email.blank?
|
2016-01-18 18:57:55 -05:00
|
|
|
email[/\A[^@]+/].tr(".", " ").titleize
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def self.find_by_username_or_email(username_or_email)
|
2013-10-28 01:29:07 -04:00
|
|
|
if username_or_email.include?('@')
|
|
|
|
find_by_email(username_or_email)
|
2013-06-18 20:31:19 -04:00
|
|
|
else
|
2013-10-28 01:29:07 -04:00
|
|
|
find_by_username(username_or_email)
|
2013-06-18 20:31:19 -04:00
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-10-24 03:59:58 -04:00
|
|
|
def self.find_by_email(email)
|
2017-04-26 14:47:36 -04:00
|
|
|
self.with_email(Email.downcase(email)).first
|
2013-10-24 03:59:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_username(username)
|
2014-05-06 09:41:59 -04:00
|
|
|
find_by(username_lower: username.downcase)
|
2013-10-24 03:59:58 -04:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def enqueue_welcome_message(message_type)
|
|
|
|
return unless SiteSetting.send_welcome_message?
|
|
|
|
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def change_username(new_username, actor = nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
UsernameChanger.change(self, new_username, actor)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
def created_topic_count
|
2014-07-28 13:17:37 -04:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.topic_count
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
2013-02-26 11:27:59 -05:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
alias_method :topic_count, :created_topic_count
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# tricky, we need our bus to be subscribed from the right spot
|
|
|
|
def sync_notification_channel_position
|
|
|
|
@unread_notifications_by_type = nil
|
2015-05-03 22:21:00 -04:00
|
|
|
self.notification_channel_position = MessageBus.last_id("/notification/#{id}")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def invited_by
|
|
|
|
used_invite = invites.where("redeemed_at is not null").includes(:invited_by).first
|
2013-02-28 08:08:56 -05:00
|
|
|
used_invite.try(:invited_by)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-08-08 22:56:08 -04:00
|
|
|
def should_validate_email_address?
|
2017-04-26 14:47:36 -04:00
|
|
|
!skip_email_validation && !staged?
|
2016-09-07 14:05:46 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Approve this user
|
2017-07-27 21:20:09 -04:00
|
|
|
def approve(approved_by, send_mail = true)
|
2013-02-05 14:16:51 -05:00
|
|
|
self.approved = true
|
2013-07-10 21:21:39 -04:00
|
|
|
|
2017-04-15 00:11:02 -04:00
|
|
|
if approved_by.is_a?(Integer)
|
2013-07-10 21:21:39 -04:00
|
|
|
self.approved_by_id = approved_by
|
|
|
|
else
|
|
|
|
self.approved_by = approved_by
|
|
|
|
end
|
|
|
|
|
2017-03-04 22:09:19 -05:00
|
|
|
self.approved_at = Time.zone.now
|
2013-06-05 23:16:31 -04:00
|
|
|
|
2016-09-13 04:03:17 -04:00
|
|
|
if result = save
|
|
|
|
send_approval_email if send_mail
|
|
|
|
DiscourseEvent.trigger(:user_approved, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.email_hash(email)
|
2013-02-05 21:44:49 -05:00
|
|
|
Digest::MD5.hexdigest(email.strip.downcase)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_hash
|
2013-02-28 08:08:56 -05:00
|
|
|
User.email_hash(email)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
2015-04-17 02:01:20 -04:00
|
|
|
@unread_notifications = nil
|
2014-10-13 06:26:30 -04:00
|
|
|
@unread_total_notifications = nil
|
2013-05-16 02:37:47 -04:00
|
|
|
@unread_pms = nil
|
2017-12-15 21:16:22 -05:00
|
|
|
@user_fields = nil
|
2013-02-05 14:16:51 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-12-12 14:20:25 -05:00
|
|
|
def unread_notifications_of_type(notification_type)
|
|
|
|
# perf critical, much more efficient than AR
|
|
|
|
sql = "
|
|
|
|
SELECT COUNT(*) FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
n.notification_type = :type AND
|
|
|
|
n.user_id = :user_id AND
|
|
|
|
NOT read"
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
User.exec_sql(sql, user_id: id, type: notification_type).getvalue(0, 0).to_i
|
2016-12-12 14:20:25 -05:00
|
|
|
end
|
2015-04-17 02:01:20 -04:00
|
|
|
|
2016-12-12 14:20:25 -05:00
|
|
|
def unread_private_messages
|
|
|
|
@unread_pms ||= unread_notifications_of_type(Notification.types[:private_message])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_notifications
|
2015-04-17 02:01:20 -04:00
|
|
|
@unread_notifications ||=
|
|
|
|
begin
|
|
|
|
# perf critical, much more efficient than AR
|
|
|
|
sql = "
|
|
|
|
SELECT COUNT(*) FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
n.notification_type <> :pm AND
|
|
|
|
n.user_id = :user_id AND
|
|
|
|
NOT read AND
|
|
|
|
n.id > :seen_notification_id"
|
|
|
|
|
|
|
|
User.exec_sql(sql, user_id: id,
|
|
|
|
seen_notification_id: seen_notification_id,
|
|
|
|
pm: Notification.types[:private_message])
|
2017-07-27 21:20:09 -04:00
|
|
|
.getvalue(0, 0).to_i
|
2015-04-17 02:01:20 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
|
2014-10-13 06:26:30 -04:00
|
|
|
def total_unread_notifications
|
|
|
|
@unread_total_notifications ||= notifications.where("read = false").count
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def saw_notification_id(notification_id)
|
2016-09-15 22:02:19 -04:00
|
|
|
if seen_notification_id.to_i < notification_id.to_i
|
|
|
|
update_columns(seen_notification_id: notification_id.to_i)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-11-16 03:20:38 -05:00
|
|
|
TRACK_FIRST_NOTIFICATION_READ_DURATION = 1.week.to_i
|
|
|
|
|
2016-11-08 03:12:40 -05:00
|
|
|
def read_first_notification?
|
2017-02-17 10:28:38 -05:00
|
|
|
if (trust_level > TrustLevel[1] ||
|
2018-01-19 11:56:24 -05:00
|
|
|
(first_seen_at.present? && first_seen_at < TRACK_FIRST_NOTIFICATION_READ_DURATION.seconds.ago))
|
2016-11-16 03:20:38 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-11-16 20:44:00 -05:00
|
|
|
self.seen_notification_id == 0 ? false : true
|
2016-11-08 03:12:40 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def publish_notifications_state
|
2015-09-03 23:20:33 -04:00
|
|
|
# publish last notification json with the message so we
|
|
|
|
# can apply an update
|
|
|
|
notification = notifications.visible.order('notifications.id desc').first
|
|
|
|
json = NotificationSerializer.new(notification).as_json if notification
|
|
|
|
|
2016-02-15 03:29:35 -05:00
|
|
|
sql = "
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT n.id, n.read FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
n.notification_type = :type AND
|
|
|
|
n.user_id = :user_id AND
|
|
|
|
NOT read
|
|
|
|
ORDER BY n.id DESC
|
|
|
|
LIMIT 20
|
|
|
|
) AS x
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT n.id, n.read FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
(n.notification_type <> :type OR read) AND
|
|
|
|
n.user_id = :user_id
|
|
|
|
ORDER BY n.id DESC
|
|
|
|
LIMIT 20
|
|
|
|
) AS y
|
|
|
|
"
|
|
|
|
|
2017-09-15 04:18:30 -04:00
|
|
|
recent = User.exec_sql(sql,
|
|
|
|
user_id: id,
|
|
|
|
type: Notification.types[:private_message]
|
|
|
|
).values.map! do |id, read|
|
|
|
|
[id.to_i, read]
|
2016-02-15 03:29:35 -05:00
|
|
|
end
|
|
|
|
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/notification/#{id}",
|
2017-07-27 21:20:09 -04:00
|
|
|
{ unread_notifications: unread_notifications,
|
|
|
|
unread_private_messages: unread_private_messages,
|
|
|
|
total_unread_notifications: total_unread_notifications,
|
|
|
|
read_first_notification: read_first_notification?,
|
|
|
|
last_notification: json,
|
|
|
|
recent: recent,
|
|
|
|
seen_notification_id: seen_notification_id
|
2015-09-03 23:20:33 -04:00
|
|
|
},
|
2013-06-06 10:40:10 -04:00
|
|
|
user_ids: [id] # only publish the notification to this user
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# A selection of people to autocomplete on @mention
|
|
|
|
def self.mentionable_usernames
|
2013-02-05 21:44:49 -05:00
|
|
|
User.select(:username).order('last_posted_at desc').limit(20)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def password=(password)
|
2013-02-05 21:44:49 -05:00
|
|
|
# special case for passwordless accounts
|
2015-06-05 13:09:02 -04:00
|
|
|
unless password.blank?
|
|
|
|
@raw_password = password
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password
|
|
|
|
'' # so that validator doesn't complain that a password attribute doesn't exist
|
|
|
|
end
|
|
|
|
|
2013-02-12 15:42:04 -05:00
|
|
|
# Indicate that this is NOT a passwordless account for the purposes of validation
|
2013-02-28 08:08:56 -05:00
|
|
|
def password_required!
|
2013-02-12 15:42:04 -05:00
|
|
|
@password_required = true
|
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password_required?
|
|
|
|
!!@password_required
|
|
|
|
end
|
|
|
|
|
2017-11-30 23:19:24 -05:00
|
|
|
def password_validation_required?
|
|
|
|
password_required? || @raw_password.present?
|
|
|
|
end
|
|
|
|
|
2014-01-21 12:42:20 -05:00
|
|
|
def has_password?
|
|
|
|
password_hash.present?
|
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password_validator
|
|
|
|
PasswordValidator.new(attributes: :password).validate_each(self, :password, @raw_password)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def confirm_password?(password)
|
2013-02-28 08:08:56 -05:00
|
|
|
return false unless password_hash && salt
|
|
|
|
self.password_hash == hash_password(password, salt)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-10-11 13:33:23 -04:00
|
|
|
|
2016-06-20 16:38:15 -04:00
|
|
|
def new_user_posting_on_first_day?
|
2015-03-26 01:48:36 -04:00
|
|
|
!staff? &&
|
|
|
|
trust_level < TrustLevel[2] &&
|
2016-06-20 16:38:15 -04:00
|
|
|
(self.first_post_created_at.nil? || self.first_post_created_at >= 24.hours.ago)
|
2015-03-26 01:48:36 -04:00
|
|
|
end
|
|
|
|
|
2013-10-11 13:33:23 -04:00
|
|
|
def new_user?
|
2015-03-26 01:04:32 -04:00
|
|
|
(created_at >= 24.hours.ago || trust_level == TrustLevel[0]) &&
|
|
|
|
trust_level < TrustLevel[2] &&
|
|
|
|
!staff?
|
2013-10-11 13:33:23 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 00:41:04 -05:00
|
|
|
def seen_before?
|
|
|
|
last_seen_at.present?
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def create_visit_record!(date, opts = {})
|
2015-07-07 12:31:07 -04:00
|
|
|
user_stat.update_column(:days_visited, user_stat.days_visited + 1)
|
|
|
|
user_visits.create!(visited_at: date, posts_read: opts[:posts_read] || 0, mobile: opts[:mobile] || false)
|
|
|
|
end
|
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
def visit_record_for(date)
|
2014-05-06 09:41:59 -04:00
|
|
|
user_visits.find_by(visited_at: date)
|
2013-02-12 00:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_visit_record!(date)
|
2014-01-24 15:19:20 -05:00
|
|
|
create_visit_record!(date) unless visit_record_for(date)
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def update_posts_read!(num_posts, opts = {})
|
2015-07-07 12:31:07 -04:00
|
|
|
now = opts[:at] || Time.zone.now
|
|
|
|
_retry = opts[:retry] || false
|
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
if user_visit = visit_record_for(now.to_date)
|
|
|
|
user_visit.posts_read += num_posts
|
2015-07-07 12:31:07 -04:00
|
|
|
user_visit.mobile = true if opts[:mobile]
|
2014-01-24 15:19:20 -05:00
|
|
|
user_visit.save
|
|
|
|
user_visit
|
|
|
|
else
|
2015-05-31 21:55:07 -04:00
|
|
|
begin
|
2015-07-07 12:31:07 -04:00
|
|
|
create_visit_record!(now.to_date, posts_read: num_posts, mobile: opts.fetch(:mobile, false))
|
2015-05-31 21:55:07 -04:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
if !_retry
|
2017-07-27 21:20:09 -04:00
|
|
|
update_posts_read!(num_posts, opts.merge(retry: true))
|
2015-05-31 21:55:07 -04:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
2013-02-12 00:41:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 05:42:04 -05:00
|
|
|
def update_ip_address!(new_ip_address)
|
2013-03-08 20:24:10 -05:00
|
|
|
unless ip_address == new_ip_address || new_ip_address.blank?
|
2013-02-24 06:56:08 -05:00
|
|
|
update_column(:ip_address, new_ip_address)
|
2013-02-24 05:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def update_last_seen!(now = Time.zone.now)
|
2013-10-23 17:24:50 -04:00
|
|
|
now_date = now.to_date
|
2013-02-05 14:16:51 -05:00
|
|
|
# Only update last seen once every minute
|
2013-10-23 17:24:50 -04:00
|
|
|
redis_key = "user:#{id}:#{now_date}"
|
|
|
|
return unless $redis.setnx(redis_key, "1")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-23 17:24:50 -04:00
|
|
|
$redis.expire(redis_key, SiteSetting.active_user_rate_limit_secs)
|
|
|
|
update_previous_visit(now)
|
|
|
|
# using update_column to avoid the AR transaction
|
|
|
|
update_column(:last_seen_at, now)
|
2016-05-21 09:17:54 -04:00
|
|
|
update_column(:first_seen_at, now) unless self.first_seen_at
|
2017-03-31 18:30:59 -04:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:user_seen, self)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def self.gravatar_template(email)
|
2013-02-05 14:16:51 -05:00
|
|
|
email_hash = self.email_hash(email)
|
2014-04-25 05:40:38 -04:00
|
|
|
"//www.gravatar.com/avatar/#{email_hash}.png?s={size}&r=pg&d=identicon"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-08 15:58:37 -05:00
|
|
|
# Don't pass this up to the client - it's meant for server side use
|
2013-08-13 16:08:29 -04:00
|
|
|
# This is used in
|
|
|
|
# - self oneboxes in open graph data
|
|
|
|
# - emails
|
2013-03-08 15:58:37 -05:00
|
|
|
def small_avatar_url
|
2014-05-22 03:37:02 -04:00
|
|
|
avatar_template_url.gsub("{size}", "45")
|
2013-03-08 15:58:37 -05:00
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
def avatar_template_url
|
2015-06-12 06:02:36 -04:00
|
|
|
UrlHelper.schemaless UrlHelper.absolute avatar_template
|
2013-09-10 15:18:22 -04:00
|
|
|
end
|
|
|
|
|
2015-06-26 13:37:50 -04:00
|
|
|
def self.default_template(username)
|
|
|
|
if SiteSetting.default_avatars.present?
|
|
|
|
split_avatars = SiteSetting.default_avatars.split("\n")
|
|
|
|
if split_avatars.present?
|
|
|
|
hash = username.each_char.reduce(0) do |result, char|
|
|
|
|
[((result << 5) - result) + char.ord].pack('L').unpack('l').first
|
|
|
|
end
|
|
|
|
|
2015-09-10 23:18:07 -04:00
|
|
|
split_avatars[hash.abs % split_avatars.size]
|
2015-06-26 13:37:50 -04:00
|
|
|
end
|
|
|
|
else
|
2015-09-11 04:14:34 -04:00
|
|
|
system_avatar_template(username)
|
2015-06-26 13:37:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-10 20:12:40 -04:00
|
|
|
def self.avatar_template(username, uploaded_avatar_id)
|
2014-05-22 03:37:02 -04:00
|
|
|
username ||= ""
|
2015-09-11 09:04:29 -04:00
|
|
|
return default_template(username) if !uploaded_avatar_id
|
2015-05-29 12:51:17 -04:00
|
|
|
hostname = RailsMultisite::ConnectionManagement.current_hostname
|
|
|
|
UserAvatar.local_avatar_template(hostname, username.downcase, uploaded_avatar_id)
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
2015-09-11 04:14:34 -04:00
|
|
|
def self.system_avatar_template(username)
|
|
|
|
# TODO it may be worth caching this in a distributed cache, should be benched
|
|
|
|
if SiteSetting.external_system_avatars_enabled
|
|
|
|
url = SiteSetting.external_system_avatars_url.dup
|
2017-06-23 17:12:06 -04:00
|
|
|
url = "#{Discourse::base_uri}#{url}" unless url =~ /^https?:\/\//
|
2015-09-13 19:42:21 -04:00
|
|
|
url.gsub! "{color}", letter_avatar_color(username.downcase)
|
2015-09-11 04:14:34 -04:00
|
|
|
url.gsub! "{username}", username
|
|
|
|
url.gsub! "{first_letter}", username[0].downcase
|
2015-10-02 03:27:54 -04:00
|
|
|
url.gsub! "{hostname}", Discourse.current_hostname
|
2015-09-11 04:14:34 -04:00
|
|
|
url
|
2015-09-10 20:12:40 -04:00
|
|
|
else
|
|
|
|
"#{Discourse.base_uri}/letter_avatar/#{username.downcase}/{size}/#{LetterAvatar.version}.png"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.letter_avatar_color(username)
|
2015-09-11 09:04:29 -04:00
|
|
|
username ||= ""
|
2015-09-10 20:12:40 -04:00
|
|
|
color = LetterAvatar::COLORS[Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length]
|
2015-09-11 04:14:34 -04:00
|
|
|
color.map { |c| c.to_s(16).rjust(2, '0') }.join
|
2014-05-30 00:17:35 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def avatar_template
|
2015-09-10 20:12:40 -04:00
|
|
|
self.class.avatar_template(username, uploaded_avatar_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
2013-03-06 02:52:24 -05:00
|
|
|
# They might need to be denormalized
|
2013-02-05 14:16:51 -05:00
|
|
|
def like_count
|
2013-02-28 08:08:56 -05:00
|
|
|
UserAction.where(user_id: id, action_type: UserAction::WAS_LIKED).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-08-22 15:23:10 -04:00
|
|
|
def like_given_count
|
|
|
|
UserAction.where(user_id: id, action_type: UserAction::LIKE).count
|
2014-08-22 14:37:00 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def post_count
|
2014-07-28 13:17:37 -04:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.post_count
|
2014-02-20 12:29:40 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def flags_given_count
|
2017-10-17 13:31:45 -04:00
|
|
|
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types_without_custom.values).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-08 11:11:56 -04:00
|
|
|
def warnings_received_count
|
2017-04-15 00:11:02 -04:00
|
|
|
user_warnings.count
|
2014-09-08 11:11:56 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def flags_received_count
|
2017-10-17 13:31:45 -04:00
|
|
|
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types_without_custom.values).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def private_topics_count
|
|
|
|
topics_allowed.where(archetype: Archetype.private_message).count
|
|
|
|
end
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
def posted_too_much_in_topic?(topic_id)
|
2016-04-18 16:08:42 -04:00
|
|
|
# Does not apply to staff and non-new members...
|
|
|
|
return false if staff? || (trust_level != TrustLevel[0])
|
|
|
|
# ... your own topics or in private messages
|
|
|
|
topic = Topic.where(id: topic_id).first
|
|
|
|
return false if topic.try(:private_message?) || (topic.try(:user_id) == self.id)
|
2014-01-02 12:57:40 -05:00
|
|
|
|
2014-04-29 12:59:14 -04:00
|
|
|
last_action_in_topic = UserAction.last_action_in_topic(id, topic_id)
|
|
|
|
since_reply = Post.where(user_id: id, topic_id: topic_id)
|
|
|
|
since_reply = since_reply.where('id > ?', last_action_in_topic) if last_action_in_topic
|
|
|
|
|
|
|
|
(since_reply.count >= SiteSetting.newuser_max_replies_per_topic)
|
2013-12-19 13:45:55 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 02:11:56 -05:00
|
|
|
def delete_all_posts!(guardian)
|
|
|
|
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2015-04-24 16:04:44 -04:00
|
|
|
QueuedPost.where(user_id: id).delete_all
|
|
|
|
|
2013-02-07 02:11:56 -05:00
|
|
|
posts.order("post_number desc").each do |p|
|
2013-06-05 16:00:45 -04:00
|
|
|
PostDestroyer.new(guardian.user, p).destroy
|
2013-02-07 02:11:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspended?
|
2017-11-28 13:44:24 -05:00
|
|
|
!!(suspended_till && suspended_till > Time.zone.now)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-11-13 13:41:36 -05:00
|
|
|
def silenced?
|
2017-11-28 13:44:24 -05:00
|
|
|
!!(silenced_till && silenced_till > Time.zone.now)
|
2017-11-13 13:41:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def silenced_record
|
|
|
|
UserHistory.for(self, :silence_user).order('id DESC').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def silence_reason
|
|
|
|
silenced_record.try(:details) if silenced?
|
|
|
|
end
|
|
|
|
|
|
|
|
def silenced_at
|
|
|
|
silenced_record.try(:created_at) if silenced?
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspend_record
|
|
|
|
UserHistory.for(self, :suspend_user).order('id DESC').first
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 13:20:42 -05:00
|
|
|
def full_suspend_reason
|
|
|
|
return suspend_record.try(:details) if suspended?
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspend_reason
|
2017-12-07 13:20:42 -05:00
|
|
|
if details = full_suspend_reason
|
|
|
|
return details.split("\n")[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Use this helper to determine if the user has a particular trust level.
|
|
|
|
# Takes into account admin, etc.
|
2013-02-05 21:44:49 -05:00
|
|
|
def has_trust_level?(level)
|
2016-05-29 23:38:04 -04:00
|
|
|
unless TrustLevel.valid?(level)
|
|
|
|
raise InvalidTrustLevel.new("Invalid trust level #{level}")
|
|
|
|
end
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
admin? || moderator? || staged? || TrustLevel.compare(trust_level, level)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-19 19:51:39 -04:00
|
|
|
# a touch faster than automatic
|
2013-03-31 12:51:13 -04:00
|
|
|
def admin?
|
2013-03-19 19:51:39 -04:00
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def guardian
|
|
|
|
Guardian.new(self)
|
|
|
|
end
|
|
|
|
|
2013-02-07 18:23:41 -05:00
|
|
|
def username_format_validator
|
2013-07-07 07:05:18 -04:00
|
|
|
UsernameValidator.perform_validation(self, 'username')
|
2013-02-07 18:23:41 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
def email_confirmed?
|
2013-02-28 08:08:56 -05:00
|
|
|
email_tokens.where(email: email, confirmed: true).present? || email_tokens.empty?
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
|
2013-05-07 21:58:34 -04:00
|
|
|
def activate
|
2017-09-12 11:36:17 -04:00
|
|
|
if email_token = self.email_tokens.active.where(email: self.email).first
|
2018-02-14 09:52:05 -05:00
|
|
|
user = EmailToken.confirm(email_token.token)
|
|
|
|
self.update!(active: true) if user.nil?
|
2013-05-07 21:58:34 -04:00
|
|
|
else
|
2017-09-13 03:33:59 -04:00
|
|
|
self.update!(active: true)
|
2013-05-07 21:58:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate
|
2017-09-13 03:33:59 -04:00
|
|
|
self.update!(active: false)
|
2013-05-07 21:58:34 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def change_trust_level!(level, opts = nil)
|
2014-06-16 20:46:30 -04:00
|
|
|
Promotion.new(self).change_trust_level!(level, opts)
|
|
|
|
end
|
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
def readable_name
|
2013-03-06 15:17:07 -05:00
|
|
|
return "#{name} (#{username})" if name.present? && name != username
|
|
|
|
username
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
|
2014-04-16 06:22:21 -04:00
|
|
|
def badge_count
|
2014-07-16 02:21:46 -04:00
|
|
|
user_badges.select('distinct badge_id').count
|
2014-04-16 06:22:21 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def featured_user_badges(limit = 3)
|
2016-11-14 15:53:24 -05:00
|
|
|
tl_badge_ids = Badge.trust_level_badge_ids
|
|
|
|
|
|
|
|
query = user_badges
|
2017-07-27 21:20:09 -04:00
|
|
|
.group(:badge_id)
|
|
|
|
.select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" },
|
2016-11-14 15:53:24 -05:00
|
|
|
'COUNT(*) AS "count"',
|
|
|
|
'MAX(badges.badge_type_id) AS badges_badge_type_id',
|
|
|
|
'MAX(badges.grant_count) AS badges_grant_count')
|
2017-07-27 21:20:09 -04:00
|
|
|
.joins(:badge)
|
|
|
|
.order('badges_badge_type_id ASC, badges_grant_count ASC, badge_id DESC')
|
|
|
|
.includes(:user, :granted_by, { badge: :badge_type }, post: :topic)
|
2016-11-14 15:53:24 -05:00
|
|
|
|
|
|
|
tl_badge = query.where("user_badges.badge_id IN (:tl_badge_ids)",
|
|
|
|
tl_badge_ids: tl_badge_ids)
|
2017-07-27 21:20:09 -04:00
|
|
|
.limit(1)
|
2016-11-14 15:53:24 -05:00
|
|
|
|
|
|
|
other_badges = query.where("user_badges.badge_id NOT IN (:tl_badge_ids)",
|
|
|
|
tl_badge_ids: tl_badge_ids)
|
2017-07-27 21:20:09 -04:00
|
|
|
.limit(limit)
|
2016-11-14 15:53:24 -05:00
|
|
|
|
|
|
|
(tl_badge + other_badges).take(limit)
|
2014-04-16 06:11:11 -04:00
|
|
|
end
|
|
|
|
|
2017-04-13 05:10:55 -04:00
|
|
|
def self.count_by_signup_date(start_date, end_date, group_id = nil)
|
2016-03-30 17:11:00 -04:00
|
|
|
result = where('users.created_at >= ? AND users.created_at <= ?', start_date, end_date)
|
2016-02-02 21:29:51 -05:00
|
|
|
|
|
|
|
if group_id
|
|
|
|
result = result.joins("INNER JOIN group_users ON group_users.user_id = users.id")
|
|
|
|
result = result.where("group_users.group_id = ?", group_id)
|
|
|
|
end
|
|
|
|
result.group('date(users.created_at)').order('date(users.created_at)').count
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def secure_category_ids
|
2014-02-06 22:11:52 -05:00
|
|
|
cats = self.admin? ? Category.where(read_restricted: true) : secure_categories.references(:categories)
|
2013-09-10 00:29:02 -04:00
|
|
|
cats.pluck('categories.id').sort
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
def topic_create_allowed_category_ids
|
|
|
|
Category.topic_create_allowed(self.id).select(:id)
|
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Flag all posts from a user as spam
|
|
|
|
def flag_linked_posts_as_spam
|
2016-04-25 17:03:17 -04:00
|
|
|
disagreed_flag_post_ids = PostAction.where(post_action_type_id: PostActionType.types[:spam])
|
2017-07-27 21:20:09 -04:00
|
|
|
.where.not(disagreed_at: nil)
|
|
|
|
.pluck(:post_id)
|
2015-10-16 15:16:44 -04:00
|
|
|
|
2016-04-25 17:03:17 -04:00
|
|
|
topic_links.includes(:post)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where.not(post_id: disagreed_flag_post_ids)
|
|
|
|
.each do |tl|
|
2013-05-10 16:58:23 -04:00
|
|
|
begin
|
2016-04-25 17:03:17 -04:00
|
|
|
message = I18n.t('flag_reason.spam_hosts', domain: tl.domain)
|
|
|
|
PostAction.act(Discourse.system_user, tl.post, PostActionType.types[:spam], message: message)
|
2013-05-10 16:58:23 -04:00
|
|
|
rescue PostAction::AlreadyActed
|
|
|
|
# If the user has already acted, just ignore it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def has_uploaded_avatar
|
|
|
|
uploaded_avatar.present?
|
|
|
|
end
|
2013-05-24 06:58:26 -04:00
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
def generate_api_key(created_by)
|
|
|
|
if api_key.present?
|
|
|
|
api_key.regenerate!(created_by)
|
|
|
|
api_key
|
|
|
|
else
|
|
|
|
ApiKey.create(user: self, key: SecureRandom.hex(32), created_by: created_by)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_api_key
|
|
|
|
ApiKey.where(user_id: self.id).delete_all
|
|
|
|
end
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
def find_email
|
2017-04-15 05:18:05 -04:00
|
|
|
last_sent_email_address.present? && EmailValidator.email_regex =~ last_sent_email_address ? last_sent_email_address : email
|
2013-11-15 10:27:43 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 20:19:26 -04:00
|
|
|
def tl3_requirements
|
2014-09-05 01:20:39 -04:00
|
|
|
@lq ||= TrustLevel3Requirements.new(self)
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 20:19:26 -04:00
|
|
|
def on_tl3_grace_period?
|
2014-09-13 16:55:26 -04:00
|
|
|
UserHistory.for(self, :auto_trust_level_change)
|
|
|
|
.where('created_at >= ?', SiteSetting.tl3_promotion_min_duration.to_i.days.ago)
|
|
|
|
.where(previous_value: TrustLevel[2].to_s)
|
|
|
|
.where(new_value: TrustLevel[3].to_s)
|
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
def refresh_avatar
|
2014-08-13 16:17:16 -04:00
|
|
|
return if @import_mode
|
|
|
|
|
2014-05-28 02:54:21 -04:00
|
|
|
avatar = user_avatar || create_user_avatar
|
2014-05-22 03:37:02 -04:00
|
|
|
|
2014-05-28 02:54:21 -04:00
|
|
|
if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt
|
2016-04-18 06:44:09 -04:00
|
|
|
Jobs.cancel_scheduled_job(:update_gravatar, user_id: self.id, avatar_id: avatar.id)
|
|
|
|
Jobs.enqueue_in(1.second, :update_gravatar, user_id: self.id, avatar_id: avatar.id)
|
2014-07-03 03:29:44 -04:00
|
|
|
end
|
2015-03-30 06:31:10 -04:00
|
|
|
|
2015-04-24 05:14:10 -04:00
|
|
|
# mark all the user's quoted posts as "needing a rebake"
|
2017-08-31 00:06:56 -04:00
|
|
|
Post.rebake_all_quoted_posts(self.id) if self.will_save_change_to_uploaded_avatar_id?
|
2014-07-03 03:29:44 -04:00
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def first_post_created_at
|
|
|
|
user_stat.try(:first_post_created_at)
|
|
|
|
end
|
|
|
|
|
2014-09-25 01:50:54 -04:00
|
|
|
def associated_accounts
|
|
|
|
result = []
|
|
|
|
|
2016-07-18 03:02:41 -04:00
|
|
|
result << "Twitter(#{twitter_user_info.screen_name})" if twitter_user_info
|
|
|
|
result << "Facebook(#{facebook_user_info.username})" if facebook_user_info
|
|
|
|
result << "Google(#{google_user_info.email})" if google_user_info
|
2018-03-01 06:10:27 -05:00
|
|
|
result << "GitHub(#{github_user_info.screen_name})" if github_user_info
|
|
|
|
result << "Instagram(#{instagram_user_info.screen_name})" if instagram_user_info
|
2016-07-18 03:02:41 -04:00
|
|
|
result << "#{oauth2_user_info.provider}(#{oauth2_user_info.email})" if oauth2_user_info
|
2014-09-25 01:50:54 -04:00
|
|
|
|
|
|
|
user_open_ids.each do |oid|
|
|
|
|
result << "OpenID #{oid.url[0..20]}...(#{oid.email})"
|
|
|
|
end
|
|
|
|
|
|
|
|
result.empty? ? I18n.t("user.no_accounts_associated") : result.join(", ")
|
|
|
|
end
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
def user_fields
|
|
|
|
return @user_fields if @user_fields
|
|
|
|
user_field_ids = UserField.pluck(:id)
|
|
|
|
if user_field_ids.present?
|
|
|
|
@user_fields = {}
|
|
|
|
user_field_ids.each do |fid|
|
|
|
|
@user_fields[fid.to_s] = custom_fields["user_field_#{fid}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@user_fields
|
|
|
|
end
|
|
|
|
|
2014-10-07 19:26:18 -04:00
|
|
|
def title=(val)
|
|
|
|
write_attribute(:title, val)
|
|
|
|
if !new_record? && user_profile
|
|
|
|
user_profile.update_column(:badge_granted_title, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-19 12:11:07 -05:00
|
|
|
def number_of_deleted_posts
|
|
|
|
Post.with_deleted
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(user_id: self.id)
|
|
|
|
.where.not(deleted_at: nil)
|
|
|
|
.count
|
2015-02-19 12:11:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_flagged_posts
|
|
|
|
Post.with_deleted
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(user_id: self.id)
|
|
|
|
.where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
2015-02-19 12:11:07 -05:00
|
|
|
.where(disagreed_at: nil)
|
|
|
|
.select(:post_id))
|
2017-07-27 21:20:09 -04:00
|
|
|
.count
|
2015-02-19 12:11:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_flags_given
|
|
|
|
PostAction.where(user_id: self.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(disagreed_at: nil)
|
|
|
|
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
|
|
|
.count
|
2015-02-19 12:11:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_suspensions
|
|
|
|
UserHistory.for(self, :suspend_user).count
|
|
|
|
end
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
def create_user_profile
|
|
|
|
UserProfile.create(user_id: id)
|
|
|
|
end
|
|
|
|
|
2015-04-07 22:29:43 -04:00
|
|
|
def anonymous?
|
|
|
|
SiteSetting.allow_anonymous_posting &&
|
|
|
|
trust_level >= 1 &&
|
|
|
|
custom_fields["master_id"].to_i > 0
|
|
|
|
end
|
|
|
|
|
2016-04-26 13:08:19 -04:00
|
|
|
def is_singular_admin?
|
2017-03-14 02:33:06 -04:00
|
|
|
User.where(admin: true).where.not(id: id).human_users.blank?
|
2016-04-26 13:08:19 -04:00
|
|
|
end
|
|
|
|
|
2016-07-04 05:20:30 -04:00
|
|
|
def logged_out
|
|
|
|
MessageBus.publish "/logout", self.id, user_ids: [self.id]
|
|
|
|
DiscourseEvent.trigger(:user_logged_out, self)
|
|
|
|
end
|
|
|
|
|
2017-06-01 04:19:42 -04:00
|
|
|
def logged_in
|
|
|
|
DiscourseEvent.trigger(:user_logged_in, self)
|
|
|
|
|
|
|
|
if !self.seen_before?
|
|
|
|
DiscourseEvent.trigger(:user_first_logged_in, self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-14 13:20:18 -04:00
|
|
|
def set_automatic_groups
|
|
|
|
return unless active && email_confirmed? && !staged
|
|
|
|
|
|
|
|
Group.where(automatic: false)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0")
|
|
|
|
.each do |group|
|
2017-06-14 13:20:18 -04:00
|
|
|
|
|
|
|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
|
|
|
|
|
|
|
if email =~ Regexp.new("@(#{domains})$", true) && !group.users.include?(self)
|
|
|
|
group.add(self)
|
|
|
|
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-26 14:47:36 -04:00
|
|
|
def email
|
|
|
|
primary_email.email
|
|
|
|
end
|
|
|
|
|
2018-03-02 03:41:02 -05:00
|
|
|
def email=(new_email)
|
2017-04-26 14:47:36 -04:00
|
|
|
if primary_email
|
2018-03-02 03:41:02 -05:00
|
|
|
new_record? ? primary_email.email = new_email : primary_email.update(email: new_email)
|
2017-04-26 14:47:36 -04:00
|
|
|
else
|
2018-03-02 03:41:02 -05:00
|
|
|
build_primary_email(email: new_email)
|
2017-04-26 14:47:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-14 16:39:07 -05:00
|
|
|
def recent_time_read
|
|
|
|
self.created_at && self.created_at < 60.days.ago ?
|
|
|
|
self.user_visits.where('visited_at >= ?', 60.days.ago).sum(:time_read) :
|
|
|
|
self.user_stat&.time_read
|
|
|
|
end
|
|
|
|
|
2018-01-19 09:29:15 -05:00
|
|
|
def from_staged?
|
|
|
|
custom_fields[User::FROM_STAGED]
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
protected
|
|
|
|
|
2014-07-22 21:42:24 -04:00
|
|
|
def badge_grant
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
|
|
|
end
|
|
|
|
|
2015-06-05 13:50:06 -04:00
|
|
|
def expire_old_email_tokens
|
2017-08-31 00:06:56 -04:00
|
|
|
if saved_change_to_password_hash? && !saved_change_to_id?
|
2015-06-05 13:50:06 -04:00
|
|
|
email_tokens.where('not expired').update_all(expired: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-21 21:13:14 -05:00
|
|
|
def index_search
|
|
|
|
SearchIndexer.index(self)
|
|
|
|
end
|
|
|
|
|
2014-03-24 03:03:39 -04:00
|
|
|
def clear_global_notice_if_needed
|
2017-03-14 02:33:06 -04:00
|
|
|
return if id < 0
|
2017-02-13 10:53:45 -05:00
|
|
|
|
2014-03-24 03:03:39 -04:00
|
|
|
if admin && SiteSetting.has_login_hint
|
|
|
|
SiteSetting.has_login_hint = false
|
|
|
|
SiteSetting.global_notice = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
def ensure_in_trust_level_group
|
|
|
|
Group.user_trust_level_change!(id, trust_level)
|
|
|
|
end
|
|
|
|
|
2013-09-11 14:50:26 -04:00
|
|
|
def create_user_stat
|
2014-03-03 14:31:29 -05:00
|
|
|
stat = UserStat.new(new_since: Time.now)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.user_id = id
|
2013-09-11 14:50:26 -04:00
|
|
|
stat.save!
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
def create_user_option
|
|
|
|
UserOption.create(user_id: id)
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def create_email_token
|
|
|
|
email_tokens.create(email: email)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def ensure_password_is_hashed
|
|
|
|
if @raw_password
|
|
|
|
self.salt = SecureRandom.hex(16)
|
|
|
|
self.password_hash = hash_password(@raw_password, salt)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
def expire_tokens_if_password_changed
|
|
|
|
# NOTE: setting raw password is the only valid way of changing a password
|
|
|
|
# the password field in the DB is actually hashed, nobody should be amending direct
|
|
|
|
if @raw_password
|
|
|
|
# Association in model may be out-of-sync
|
|
|
|
UserAuthToken.where(user_id: id).destroy_all
|
|
|
|
# We should not carry this around after save
|
|
|
|
@raw_password = nil
|
2017-11-30 23:19:24 -05:00
|
|
|
@password_required = false
|
2017-01-31 17:21:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def hash_password(password, salt)
|
2016-05-29 23:38:04 -04:00
|
|
|
raise StandardError.new("password is too long") if password.size > User.max_password_length
|
2013-07-22 21:36:01 -04:00
|
|
|
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def add_trust_level
|
2013-12-21 02:19:22 -05:00
|
|
|
# there is a possibility we did not load trust level column, skip it
|
2013-06-06 10:40:10 -04:00
|
|
|
return unless has_attribute? :trust_level
|
|
|
|
self.trust_level ||= SiteSetting.default_trust_level
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def update_username_lower
|
|
|
|
self.username_lower = username.downcase
|
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def username_validator
|
|
|
|
username_format_validator || begin
|
|
|
|
lower = username.downcase
|
2014-05-06 09:41:59 -04:00
|
|
|
existing = User.find_by(username_lower: lower)
|
2017-08-31 00:06:56 -04:00
|
|
|
if will_save_change_to_username? && existing && existing.id != self.id
|
2013-06-06 10:40:10 -04:00
|
|
|
errors.add(:username, I18n.t(:'user.username.unique'))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 18:23:41 -05:00
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def send_approval_email
|
2015-05-04 15:30:25 -04:00
|
|
|
if SiteSetting.must_approve_users
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.enqueue(:critical_user_email,
|
2015-05-04 15:30:25 -04:00
|
|
|
type: :signup_after_approval,
|
2018-02-01 08:59:37 -05:00
|
|
|
user_id: id
|
2015-05-04 15:30:25 -04:00
|
|
|
)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
end
|
2013-07-07 06:40:35 -04:00
|
|
|
|
2015-08-21 14:39:21 -04:00
|
|
|
def set_default_categories_preferences
|
2016-06-14 10:45:47 -04:00
|
|
|
return if self.staged?
|
|
|
|
|
2015-08-21 14:39:21 -04:00
|
|
|
values = []
|
|
|
|
|
2016-11-09 13:37:54 -05:00
|
|
|
%w{watching watching_first_post tracking muted}.each do |s|
|
2015-08-21 14:39:21 -04:00
|
|
|
category_ids = SiteSetting.send("default_categories_#{s}").split("|")
|
|
|
|
category_ids.each do |category_id|
|
|
|
|
values << "(#{self.id}, #{category_id}, #{CategoryUser.notification_levels[s.to_sym]})"
|
2013-08-23 17:35:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-21 14:39:21 -04:00
|
|
|
if values.present?
|
|
|
|
exec_sql("INSERT INTO category_users (user_id, category_id, notification_level) VALUES #{values.join(",")}")
|
2014-01-02 15:27:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 00:36:25 -05:00
|
|
|
# Delete unactivated accounts (without verified email) that are over a week old
|
|
|
|
def self.purge_unactivated
|
2017-08-25 15:20:06 -04:00
|
|
|
return [] if SiteSetting.purge_unactivated_users_grace_period_days <= 0
|
|
|
|
|
2014-08-13 14:13:41 -04:00
|
|
|
to_destroy = User.where(active: false)
|
2017-07-27 21:20:09 -04:00
|
|
|
.joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
|
|
|
|
.where("created_at < ?", SiteSetting.purge_unactivated_users_grace_period_days.days.ago)
|
|
|
|
.where('NOT admin AND NOT moderator')
|
|
|
|
.limit(200)
|
2014-08-13 14:13:41 -04:00
|
|
|
|
|
|
|
destroyer = UserDestroyer.new(Discourse.system_user)
|
|
|
|
to_destroy.each do |u|
|
2014-08-19 13:46:40 -04:00
|
|
|
begin
|
2014-08-27 16:04:46 -04:00
|
|
|
destroyer.destroy(u, context: I18n.t(:purge_reason))
|
2015-05-04 11:37:49 -04:00
|
|
|
rescue Discourse::InvalidAccess, UserDestroyer::PostsExistError
|
2014-08-19 13:46:40 -04:00
|
|
|
# if for some reason the user can't be deleted, continue on to the next one
|
|
|
|
end
|
2014-08-13 14:13:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-07 06:40:35 -04:00
|
|
|
private
|
|
|
|
|
2013-10-23 17:24:50 -04:00
|
|
|
def previous_visit_at_update_required?(timestamp)
|
2014-01-16 22:38:08 -05:00
|
|
|
seen_before? && (last_seen_at < (timestamp - SiteSetting.previous_visit_timeout_hours.hours))
|
2013-10-23 17:24:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_previous_visit(timestamp)
|
|
|
|
update_visit_record!(timestamp.to_date)
|
|
|
|
if previous_visit_at_update_required?(timestamp)
|
|
|
|
update_column(:previous_visit_at, last_seen_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-16 03:36:27 -04:00
|
|
|
def trigger_user_created_event
|
2017-03-16 04:02:34 -04:00
|
|
|
DiscourseEvent.trigger(:user_created, self)
|
2017-03-16 03:36:27 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-10-25 01:02:18 -04:00
|
|
|
def set_skip_validate_email
|
2017-08-08 22:56:08 -04:00
|
|
|
if self.primary_email
|
2017-10-25 01:02:18 -04:00
|
|
|
self.primary_email.skip_validate_email = !should_validate_email_address?
|
2017-08-08 22:56:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
2017-11-23 15:55:44 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# username :string(60) not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-02-20 01:28:58 -05:00
|
|
|
# name :string
|
2017-11-23 15:55:44 -05:00
|
|
|
# seen_notification_id :integer default(0), not null
|
|
|
|
# last_posted_at :datetime
|
|
|
|
# password_hash :string(64)
|
|
|
|
# salt :string(32)
|
|
|
|
# active :boolean default(FALSE), not null
|
|
|
|
# username_lower :string(60) not null
|
|
|
|
# last_seen_at :datetime
|
|
|
|
# admin :boolean default(FALSE), not null
|
|
|
|
# last_emailed_at :datetime
|
|
|
|
# trust_level :integer not null
|
|
|
|
# approved :boolean default(FALSE), not null
|
|
|
|
# approved_by_id :integer
|
|
|
|
# approved_at :datetime
|
|
|
|
# previous_visit_at :datetime
|
|
|
|
# suspended_at :datetime
|
|
|
|
# suspended_till :datetime
|
|
|
|
# date_of_birth :date
|
|
|
|
# views :integer default(0), not null
|
|
|
|
# flag_level :integer default(0), not null
|
|
|
|
# ip_address :inet
|
|
|
|
# moderator :boolean default(FALSE)
|
2018-02-20 01:28:58 -05:00
|
|
|
# title :string
|
2017-11-23 15:55:44 -05:00
|
|
|
# uploaded_avatar_id :integer
|
2017-12-05 10:29:14 -05:00
|
|
|
# locale :string(10)
|
2018-02-20 01:28:58 -05:00
|
|
|
# primary_group_id :integer
|
2017-11-23 15:55:44 -05:00
|
|
|
# registration_ip_address :inet
|
|
|
|
# staged :boolean default(FALSE), not null
|
|
|
|
# first_seen_at :datetime
|
|
|
|
# silenced_till :datetime
|
|
|
|
# group_locked_trust_level :integer
|
|
|
|
# manual_locked_trust_level :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2016-11-23 21:13:03 -05:00
|
|
|
# idx_users_admin (id)
|
|
|
|
# idx_users_moderator (id)
|
|
|
|
# index_users_on_last_posted_at (last_posted_at)
|
|
|
|
# index_users_on_last_seen_at (last_seen_at)
|
|
|
|
# index_users_on_uploaded_avatar_id (uploaded_avatar_id)
|
|
|
|
# index_users_on_username (username) UNIQUE
|
|
|
|
# index_users_on_username_lower (username_lower) UNIQUE
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|