2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'email_token'
|
|
|
|
require_dependency 'trust_level'
|
2013-03-06 20:12:16 +08:00
|
|
|
require_dependency 'pbkdf2'
|
2013-03-09 04:04:37 +08:00
|
|
|
require_dependency 'summarize'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
attr_accessible :name, :username, :password, :email, :bio_raw, :website
|
|
|
|
|
|
|
|
has_many :posts
|
|
|
|
has_many :notifications
|
|
|
|
has_many :topic_users
|
|
|
|
has_many :topics
|
2013-04-12 04:04:20 +08:00
|
|
|
has_many :user_open_ids, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
has_many :user_actions
|
|
|
|
has_many :post_actions
|
|
|
|
has_many :email_logs
|
|
|
|
has_many :post_timings
|
|
|
|
has_many :topic_allowed_users
|
|
|
|
has_many :topics_allowed, through: :topic_allowed_users, source: :topic
|
|
|
|
has_many :email_tokens
|
|
|
|
has_many :views
|
|
|
|
has_many :user_visits
|
|
|
|
has_many :invites
|
2013-04-12 04:04:20 +08:00
|
|
|
has_one :twitter_user_info, dependent: :destroy
|
|
|
|
has_one :github_user_info, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :approved_by, class_name: 'User'
|
|
|
|
|
|
|
|
validates_presence_of :username
|
|
|
|
validates_presence_of :email
|
|
|
|
validates_uniqueness_of :email
|
|
|
|
validate :username_validator
|
2013-02-28 21:08:56 +08:00
|
|
|
validate :email_validator, if: :email_changed?
|
2013-02-06 03:16:51 +08:00
|
|
|
validate :password_validator
|
|
|
|
|
|
|
|
before_save :cook
|
|
|
|
before_save :update_username_lower
|
|
|
|
before_save :ensure_password_is_hashed
|
|
|
|
after_initialize :add_trust_level
|
|
|
|
|
|
|
|
after_save :update_tracked_topics
|
|
|
|
|
2013-02-06 10:44:49 +08:00
|
|
|
after_create :create_email_token
|
2013-02-06 03:16:51 +08: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
|
|
|
|
|
2013-03-29 14:29:58 +08:00
|
|
|
scope :admins, ->{ where(admin: true) }
|
|
|
|
scope :moderators, ->{ where(moderator: true) }
|
|
|
|
|
2013-02-14 14:32:58 +08:00
|
|
|
module NewTopicDuration
|
2013-02-26 00:42:20 +08:00
|
|
|
ALWAYS = -1
|
2013-02-14 14:32:58 +08:00
|
|
|
LAST_VISIT = -2
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.username_length
|
2013-02-06 10:44:49 +08:00
|
|
|
3..15
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-01 00:51:13 +08:00
|
|
|
def self.sanitize_username!(name)
|
|
|
|
name.gsub!(/^[^A-Za-z0-9]+|[^A-Za-z0-9_]+$/, "")
|
2013-02-06 03:16:51 +08:00
|
|
|
name.gsub!(/[^A-Za-z0-9_]+/, "_")
|
2013-04-01 00:51:13 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-01 00:51:13 +08:00
|
|
|
def self.pad_missing_chars_with_1s!(name)
|
2013-02-06 03:16:51 +08:00
|
|
|
missing_chars = User.username_length.begin - name.length
|
|
|
|
name << ('1' * missing_chars) if missing_chars > 0
|
2013-04-01 00:51:13 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-01 00:51:13 +08:00
|
|
|
def self.find_available_username_based_on(name)
|
2013-02-06 03:16:51 +08:00
|
|
|
i = 1
|
|
|
|
attempt = name
|
2013-04-01 00:51:13 +08:00
|
|
|
until username_available?(attempt)
|
2013-02-06 03:16:51 +08:00
|
|
|
suffix = i.to_s
|
2013-02-28 21:08:56 +08:00
|
|
|
max_length = User.username_length.end - suffix.length - 1
|
2013-02-06 03:16:51 +08:00
|
|
|
attempt = "#{name[0..max_length]}#{suffix}"
|
2013-02-28 21:08:56 +08:00
|
|
|
i += 1
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
attempt
|
|
|
|
end
|
|
|
|
|
2013-04-01 00:51:13 +08:00
|
|
|
EMAIL = %r{([^@]+)@([^\.]+)}
|
|
|
|
|
|
|
|
def self.suggest_username(name)
|
|
|
|
return unless name.present?
|
|
|
|
|
|
|
|
if name =~ EMAIL
|
|
|
|
# When 'walter@white.com' take 'walter'
|
|
|
|
name = Regexp.last_match[1]
|
|
|
|
|
|
|
|
# When 'me@eviltrout.com' take 'eviltrout'
|
|
|
|
name = Regexp.last_match[2] if ['i', 'me'].include?(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
sanitize_username!(name)
|
|
|
|
pad_missing_chars_with_1s!(name)
|
|
|
|
|
|
|
|
# Trim extra length
|
|
|
|
name = name[0..User.username_length.end-1]
|
|
|
|
find_available_username_based_on(name)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.create_for_email(email, opts={})
|
|
|
|
username = suggest_username(email)
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
if SiteSetting.call_discourse_hub?
|
2013-02-06 03:16:51 +08:00
|
|
|
begin
|
2013-02-28 21:08:56 +08:00
|
|
|
match, available, suggestion = DiscourseHub.nickname_match?(username, email)
|
|
|
|
username = suggestion unless match || available
|
2013-02-06 03:16:51 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user = User.new(email: email, username: username, name: username)
|
|
|
|
user.trust_level = opts[:trust_level] if opts[:trust_level].present?
|
|
|
|
user.save!
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
if SiteSetting.call_discourse_hub?
|
2013-02-06 03:16:51 +08:00
|
|
|
begin
|
2013-02-28 21:08:56 +08:00
|
|
|
DiscourseHub.register_nickname(username, email)
|
2013-02-06 03:16:51 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.username_available?(username)
|
|
|
|
lower = username.downcase
|
2013-02-28 21:08:56 +08:00
|
|
|
User.where(username_lower: lower).blank?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-08 07:23:41 +08:00
|
|
|
def self.username_valid?(username)
|
|
|
|
u = User.new(username: username)
|
|
|
|
u.username_format_validator
|
2013-02-28 21:08:56 +08:00
|
|
|
u.errors[:username].blank?
|
2013-02-08 07:23:41 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def enqueue_welcome_message(message_type)
|
|
|
|
return unless SiteSetting.send_welcome_message?
|
2013-02-28 21:08:56 +08:00
|
|
|
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.suggest_name(email)
|
|
|
|
return "" unless email
|
|
|
|
name = email.split(/[@\+]/)[0]
|
2013-02-28 21:08:56 +08:00
|
|
|
name = name.gsub(".", " ")
|
|
|
|
name.titleize
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_username(new_username)
|
2013-02-28 21:08:56 +08:00
|
|
|
current_username, self.username = username, new_username
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
if SiteSetting.call_discourse_hub? && valid?
|
2013-02-06 03:16:51 +08:00
|
|
|
begin
|
2013-02-28 21:08:56 +08:00
|
|
|
DiscourseHub.change_nickname(current_username, new_username)
|
2013-02-15 01:57:26 +08:00
|
|
|
rescue DiscourseHub::NicknameUnavailable
|
2013-02-28 21:08:56 +08:00
|
|
|
false
|
2013-02-06 03:16:51 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message + "\n" + e.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
save
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use a temporary key to find this user, store it in redis with an expiry
|
2013-02-06 10:44:49 +08:00
|
|
|
def temporary_key
|
2013-02-06 03:16:51 +08:00
|
|
|
key = SecureRandom.hex(32)
|
|
|
|
$redis.setex "temporary_key:#{key}", 1.week, id.to_s
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find a user by temporary key, nil if not found or key is invalid
|
|
|
|
def self.find_by_temporary_key(key)
|
|
|
|
user_id = $redis.get("temporary_key:#{key}")
|
|
|
|
if user_id.present?
|
2013-02-27 00:27:59 +08:00
|
|
|
where(id: user_id.to_i).first
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-27 00:27:59 +08:00
|
|
|
def self.find_by_username_or_email(username_or_email)
|
|
|
|
where("username_lower = :user or lower(username) = :user or lower(email) = :user or lower(name) = :user", user: username_or_email.downcase)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# tricky, we need our bus to be subscribed from the right spot
|
|
|
|
def sync_notification_channel_position
|
|
|
|
@unread_notifications_by_type = nil
|
2013-02-25 06:31:38 +08:00
|
|
|
self.notification_channel_position = MessageBus.last_id("/notification/#{id}")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invited_by
|
|
|
|
used_invite = invites.where("redeemed_at is not null").includes(:invited_by).first
|
2013-02-28 21:08:56 +08:00
|
|
|
used_invite.try(:invited_by)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Approve this user
|
|
|
|
def approve(approved_by)
|
|
|
|
self.approved = true
|
|
|
|
self.approved_by = approved_by
|
|
|
|
self.approved_at = Time.now
|
|
|
|
enqueue_welcome_message('welcome_approved') if save
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.email_hash(email)
|
2013-02-06 10:44:49 +08:00
|
|
|
Digest::MD5.hexdigest(email.strip.downcase)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_hash
|
2013-02-28 21:08:56 +08:00
|
|
|
User.email_hash(email)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_notifications_by_type
|
|
|
|
@unread_notifications_by_type ||= notifications.where("id > ? and read = false", seen_notification_id).group(:notification_type).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@unread_notifications_by_type = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def unread_private_messages
|
2013-03-01 20:07:44 +08:00
|
|
|
unread_notifications_by_type[Notification.types[:private_message]] || 0
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_notifications
|
2013-03-01 20:07:44 +08:00
|
|
|
unread_notifications_by_type.except(Notification.types[:private_message]).values.sum
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-06 10:44:49 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def saw_notification_id(notification_id)
|
|
|
|
User.update_all ["seen_notification_id = ?", notification_id], ["seen_notification_id < ?", notification_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_notifications_state
|
2013-02-28 21:08:56 +08:00
|
|
|
MessageBus.publish("/notification/#{id}",
|
|
|
|
{ unread_notifications: unread_notifications,
|
|
|
|
unread_private_messages: unread_private_messages },
|
|
|
|
user_ids: [id] # only publish the notification to this user
|
2013-02-06 03:16:51 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# A selection of people to autocomplete on @mention
|
|
|
|
def self.mentionable_usernames
|
2013-02-06 10:44:49 +08:00
|
|
|
User.select(:username).order('last_posted_at desc').limit(20)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
def moderator?
|
2013-04-01 00:51:13 +08:00
|
|
|
# this saves us from checking both, admins are always moderators
|
2013-03-20 12:05:19 +08:00
|
|
|
#
|
|
|
|
# in future we may split this out
|
|
|
|
admin || moderator
|
2013-02-28 21:08:56 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def regular?
|
2013-02-28 21:08:56 +08:00
|
|
|
!(admin? || moderator?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-06 10:44:49 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def password=(password)
|
2013-02-06 10:44:49 +08:00
|
|
|
# special case for passwordless accounts
|
2013-02-28 21:08:56 +08:00
|
|
|
@raw_password = password unless password.blank?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-13 04:42:04 +08:00
|
|
|
# Indicate that this is NOT a passwordless account for the purposes of validation
|
2013-02-28 21:08:56 +08:00
|
|
|
def password_required!
|
2013-02-13 04:42:04 +08:00
|
|
|
@password_required = true
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def confirm_password?(password)
|
2013-02-28 21:08:56 +08:00
|
|
|
return false unless password_hash && salt
|
|
|
|
self.password_hash == hash_password(password, salt)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 13:41:04 +08:00
|
|
|
def seen?(date)
|
2013-02-28 21:08:56 +08:00
|
|
|
last_seen_at.to_date >= date if last_seen_at.present?
|
2013-02-12 13:41:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def seen_before?
|
|
|
|
last_seen_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_visit_record?(date)
|
2013-03-01 02:54:12 +08:00
|
|
|
user_visits.where(visited_at: date).first
|
2013-02-12 13:41:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_visit_record!(date)
|
2013-02-28 21:08:56 +08:00
|
|
|
unless seen_before?
|
2013-04-05 14:53:39 +08:00
|
|
|
user_visits.create!(visited_at: date)
|
2013-02-12 13:41:04 +08:00
|
|
|
update_column(:days_visited, 1)
|
|
|
|
end
|
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
unless seen?(date) || has_visit_record?(date)
|
2013-04-05 14:53:39 +08:00
|
|
|
user_visits.create!(visited_at: date)
|
2013-04-05 14:43:48 +08:00
|
|
|
User.update_all('days_visited = days_visited + 1', id: self.id)
|
2013-02-12 13:41:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 18:42:04 +08:00
|
|
|
def update_ip_address!(new_ip_address)
|
2013-03-09 09:24:10 +08:00
|
|
|
unless ip_address == new_ip_address || new_ip_address.blank?
|
2013-02-24 19:56:08 +08:00
|
|
|
update_column(:ip_address, new_ip_address)
|
2013-02-24 18:42:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def update_last_seen!
|
|
|
|
now = DateTime.now
|
|
|
|
now_date = now.to_date
|
|
|
|
# Only update last seen once every minute
|
|
|
|
redis_key = "user:#{self.id}:#{now_date.to_s}"
|
|
|
|
if $redis.setnx(redis_key, "1")
|
|
|
|
$redis.expire(redis_key, SiteSetting.active_user_rate_limit_secs)
|
|
|
|
|
2013-02-12 13:41:04 +08:00
|
|
|
update_visit_record!(now_date)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-12 13:41:04 +08:00
|
|
|
# using update_column to avoid the AR transaction
|
2013-02-06 03:16:51 +08:00
|
|
|
# Keep track of our last visit
|
2013-02-12 13:41:04 +08:00
|
|
|
if seen_before? && (self.last_seen_at < (now - SiteSetting.previous_visit_timeout_hours.hours))
|
|
|
|
previous_visit_at = last_seen_at
|
2013-02-28 21:08:56 +08:00
|
|
|
update_column(:previous_visit_at, previous_visit_at)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-28 21:08:56 +08:00
|
|
|
update_column(:last_seen_at, now)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.avatar_template(email)
|
|
|
|
email_hash = self.email_hash(email)
|
|
|
|
# robohash was possibly causing caching issues
|
|
|
|
# robohash = CGI.escape("http://robohash.org/size_") << "{size}x{size}" << CGI.escape("/#{email_hash}.png")
|
2013-03-01 04:58:36 +08:00
|
|
|
"https://www.gravatar.com/avatar/#{email_hash}.png?s={size}&r=pg&d=identicon"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-09 04:58:37 +08:00
|
|
|
# Don't pass this up to the client - it's meant for server side use
|
|
|
|
def small_avatar_url
|
2013-03-09 05:17:56 +08:00
|
|
|
"https://www.gravatar.com/avatar/#{email_hash}.png?s=200&r=pg&d=identicon"
|
2013-03-09 04:58:37 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# return null for local avatars, a template for gravatar
|
|
|
|
def avatar_template
|
2013-02-28 21:08:56 +08:00
|
|
|
User.avatar_template(email)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Updates the denormalized view counts for all users
|
|
|
|
def self.update_view_counts
|
|
|
|
# Update denormalized topics_entered
|
|
|
|
exec_sql "UPDATE users SET topics_entered = x.c
|
|
|
|
FROM
|
2013-02-06 10:44:49 +08:00
|
|
|
(SELECT v.user_id,
|
2013-02-06 03:16:51 +08:00
|
|
|
COUNT(DISTINCT parent_id) AS c
|
|
|
|
FROM views AS v
|
|
|
|
WHERE parent_type = 'Topic'
|
|
|
|
GROUP BY v.user_id) AS X
|
|
|
|
WHERE x.user_id = users.id"
|
|
|
|
|
|
|
|
# Update denormalzied posts_read_count
|
|
|
|
exec_sql "UPDATE users SET posts_read_count = x.c
|
|
|
|
FROM
|
|
|
|
(SELECT pt.user_id,
|
|
|
|
COUNT(*) AS c
|
|
|
|
FROM post_timings AS pt
|
|
|
|
GROUP BY pt.user_id) AS X
|
|
|
|
WHERE x.user_id = users.id"
|
|
|
|
end
|
|
|
|
|
|
|
|
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
2013-03-06 15:52:24 +08:00
|
|
|
# They might need to be denormalized
|
2013-02-06 03:16:51 +08:00
|
|
|
def like_count
|
2013-02-28 21:08:56 +08:00
|
|
|
UserAction.where(user_id: id, action_type: UserAction::WAS_LIKED).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_count
|
|
|
|
posts.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def flags_given_count
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types.values).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def flags_received_count
|
2013-03-01 20:07:44 +08:00
|
|
|
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types.values).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def private_topics_count
|
|
|
|
topics_allowed.where(archetype: Archetype.private_message).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_excerpt
|
|
|
|
PrettyText.excerpt(bio_cooked, 350)
|
|
|
|
end
|
|
|
|
|
2013-02-07 15:11:56 +08:00
|
|
|
def delete_all_posts!(guardian)
|
|
|
|
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-07 15:11:56 +08:00
|
|
|
posts.order("post_number desc").each do |p|
|
|
|
|
if p.post_number == 1
|
|
|
|
p.topic.destroy
|
2013-04-12 04:04:20 +08:00
|
|
|
# TODO: But the post is not destroyed. Why?
|
2013-02-07 15:11:56 +08:00
|
|
|
else
|
2013-04-12 04:04:20 +08:00
|
|
|
# TODO: This should be using the PostDestroyer!
|
2013-02-07 15:11:56 +08:00
|
|
|
p.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def is_banned?
|
2013-02-28 21:08:56 +08:00
|
|
|
banned_till && banned_till > DateTime.now
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use this helper to determine if the user has a particular trust level.
|
|
|
|
# Takes into account admin, etc.
|
2013-02-06 10:44:49 +08:00
|
|
|
def has_trust_level?(level)
|
2013-02-28 21:08:56 +08:00
|
|
|
raise "Invalid trust level #{level}" unless TrustLevel.valid_level?(level)
|
2013-03-20 12:05:19 +08:00
|
|
|
admin? || moderator? || TrustLevel.compare(trust_level, level)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-20 07:51:39 +08:00
|
|
|
# a touch faster than automatic
|
2013-04-01 00:51:13 +08:00
|
|
|
def admin?
|
2013-03-20 07:51:39 +08:00
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
2013-02-13 06:58:08 +08:00
|
|
|
def change_trust_level(level)
|
2013-02-28 21:08:56 +08:00
|
|
|
raise "Invalid trust level #{level}" unless TrustLevel.valid_level?(level)
|
2013-03-01 20:07:44 +08:00
|
|
|
self.trust_level = TrustLevel.levels[level]
|
2013-02-13 06:58:08 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def guardian
|
|
|
|
Guardian.new(self)
|
|
|
|
end
|
|
|
|
|
2013-02-08 07:23:41 +08:00
|
|
|
def username_format_validator
|
2013-02-08 22:52:56 +08:00
|
|
|
validator = UsernameValidator.new(username)
|
|
|
|
unless validator.valid_format?
|
2013-02-09 03:12:48 +08:00
|
|
|
validator.errors.each { |e| errors.add(:username, e) }
|
2013-02-08 07:23:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 00:18:26 +08:00
|
|
|
def email_confirmed?
|
2013-02-28 21:08:56 +08:00
|
|
|
email_tokens.where(email: email, confirmed: true).present? || email_tokens.empty?
|
2013-02-12 00:18:26 +08:00
|
|
|
end
|
|
|
|
|
2013-02-14 14:32:58 +08:00
|
|
|
def treat_as_new_topic_start_date
|
2013-02-26 00:42:20 +08:00
|
|
|
duration = new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes
|
|
|
|
case duration
|
2013-02-14 14:32:58 +08:00
|
|
|
when User::NewTopicDuration::ALWAYS
|
|
|
|
created_at
|
|
|
|
when User::NewTopicDuration::LAST_VISIT
|
|
|
|
previous_visit_at || created_at
|
|
|
|
else
|
|
|
|
duration.minutes.ago
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-14 14:32:58 +08:00
|
|
|
end
|
2013-02-08 07:23:41 +08:00
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
MAX_TIME_READ_DIFF = 100
|
|
|
|
# attempt to add total read time to user based on previous time this was called
|
2013-02-26 00:42:20 +08:00
|
|
|
def update_time_read!
|
2013-02-25 15:42:42 +08:00
|
|
|
last_seen_key = "user-last-seen:#{id}"
|
|
|
|
last_seen = $redis.get(last_seen_key)
|
|
|
|
if last_seen.present?
|
|
|
|
diff = (Time.now.to_f - last_seen.to_f).round
|
|
|
|
if diff > 0 && diff < MAX_TIME_READ_DIFF
|
2013-03-01 02:54:12 +08:00
|
|
|
User.update_all ["time_read = time_read + ?", diff], id: id, time_read: time_read
|
2013-02-25 15:42:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
$redis.set(last_seen_key, Time.now.to_f)
|
|
|
|
end
|
|
|
|
|
2013-02-22 02:20:00 +08:00
|
|
|
def readable_name
|
2013-03-07 04:17:07 +08:00
|
|
|
return "#{name} (#{username})" if name.present? && name != username
|
|
|
|
username
|
2013-02-22 02:20:00 +08:00
|
|
|
end
|
|
|
|
|
2013-03-09 04:04:37 +08:00
|
|
|
def bio_summary
|
|
|
|
return nil unless bio_cooked.present?
|
|
|
|
Summarize.new(bio_cooked).summary
|
|
|
|
end
|
|
|
|
|
2013-04-04 01:25:52 +08:00
|
|
|
def self.count_by_signup_date(sinceDaysAgo=30)
|
|
|
|
where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
2013-03-16 06:08:46 +08:00
|
|
|
def self.counts_by_trust_level
|
|
|
|
group('trust_level').count
|
|
|
|
end
|
|
|
|
|
2013-04-05 13:48:38 +08:00
|
|
|
def update_topic_reply_count
|
2013-04-05 14:44:56 +08:00
|
|
|
self.topic_reply_count =
|
2013-04-05 13:48:38 +08:00
|
|
|
Topic
|
|
|
|
.where(['id in (
|
|
|
|
SELECT topic_id FROM posts p
|
|
|
|
JOIN topics t2 ON t2.id = p.topic_id
|
2013-04-05 14:43:48 +08:00
|
|
|
WHERE p.deleted_at IS NULL AND
|
2013-04-05 13:48:38 +08:00
|
|
|
t2.user_id <> p.user_id AND
|
|
|
|
p.user_id = ?
|
|
|
|
)', self.id])
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def cook
|
2013-02-28 21:08:56 +08:00
|
|
|
if bio_raw.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
self.bio_cooked = PrettyText.cook(bio_raw) if bio_raw_changed?
|
|
|
|
else
|
|
|
|
self.bio_cooked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_tracked_topics
|
2013-03-07 04:17:07 +08:00
|
|
|
return unless auto_track_topics_after_msecs_changed?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-07 04:17:07 +08:00
|
|
|
where_conditions = {notifications_reason_id: nil, user_id: id}
|
|
|
|
if auto_track_topics_after_msecs < 0
|
|
|
|
TopicUser.update_all({notification_level: TopicUser.notification_levels[:regular]}, where_conditions)
|
|
|
|
else
|
|
|
|
TopicUser.update_all(["notification_level = CASE WHEN total_msecs_viewed < ? THEN ? ELSE ? END",
|
|
|
|
auto_track_topics_after_msecs, TopicUser.notification_levels[:regular], TopicUser.notification_levels[:tracking]], where_conditions)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def create_email_token
|
2013-02-28 21:08:56 +08:00
|
|
|
email_tokens.create(email: email)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_password_is_hashed
|
|
|
|
if @raw_password
|
|
|
|
self.salt = SecureRandom.hex(16)
|
2013-02-06 10:44:49 +08:00
|
|
|
self.password_hash = hash_password(@raw_password, salt)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash_password(password, salt)
|
2013-03-06 20:12:16 +08:00
|
|
|
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 10:44:49 +08:00
|
|
|
def add_trust_level
|
2013-03-19 14:44:21 +08:00
|
|
|
# there is a possiblity we did not load trust level column, skip it
|
|
|
|
return unless has_attribute? :trust_level
|
2013-02-06 03:16:51 +08:00
|
|
|
self.trust_level ||= SiteSetting.default_trust_level
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_username_lower
|
|
|
|
self.username_lower = username.downcase
|
|
|
|
end
|
2013-02-06 10:44:49 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def username_validator
|
2013-02-08 07:23:41 +08:00
|
|
|
username_format_validator || begin
|
|
|
|
lower = username.downcase
|
|
|
|
if username_changed? && User.where(username_lower: lower).exists?
|
2013-02-28 21:08:56 +08:00
|
|
|
errors.add(:username, I18n.t(:'user.username.unique'))
|
2013-02-08 07:23:41 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-08 07:23:41 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-14 01:28:23 +08:00
|
|
|
def email_validator
|
2013-03-19 03:48:56 +08:00
|
|
|
if (setting = SiteSetting.email_domains_whitelist).present?
|
|
|
|
unless email_in_restriction_setting?(setting)
|
|
|
|
errors.add(:email, I18n.t(:'user.email.not_allowed'))
|
|
|
|
end
|
|
|
|
elsif (setting = SiteSetting.email_domains_blacklist).present?
|
|
|
|
if email_in_restriction_setting?(setting)
|
2013-02-28 21:08:56 +08:00
|
|
|
errors.add(:email, I18n.t(:'user.email.not_allowed'))
|
2013-02-14 01:28:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-01 00:51:13 +08:00
|
|
|
|
2013-03-19 03:48:56 +08:00
|
|
|
def email_in_restriction_setting?(setting)
|
|
|
|
domains = setting.gsub('.', '\.')
|
|
|
|
regexp = Regexp.new("@(#{domains})", true)
|
|
|
|
self.email =~ regexp
|
|
|
|
end
|
2013-02-14 01:28:23 +08:00
|
|
|
|
2013-02-08 07:23:41 +08:00
|
|
|
def password_validator
|
2013-03-05 08:42:44 +08:00
|
|
|
if (@raw_password && @raw_password.length < 6) || (@password_required && !@raw_password)
|
2013-02-28 21:08:56 +08:00
|
|
|
errors.add(:password, "must be 6 letters or longer")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-04-05 13:48:38 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|