2012-06-17 03:15:46 +02:00
|
|
|
class User < ActiveRecord::Base
|
2012-07-01 00:43:45 +02:00
|
|
|
has_many :stories,
|
2012-06-17 03:15:46 +02:00
|
|
|
:include => :user
|
2012-07-04 03:48:01 +02:00
|
|
|
has_many :comments
|
2013-01-24 21:06:10 +01:00
|
|
|
has_many :sent_messages,
|
2012-07-04 03:48:01 +02:00
|
|
|
:class_name => "Message",
|
|
|
|
:foreign_key => "author_user_id"
|
|
|
|
has_many :received_messages,
|
|
|
|
:class_name => "Message",
|
|
|
|
:foreign_key => "recipient_user_id"
|
2012-07-04 05:24:18 +02:00
|
|
|
has_many :tag_filters
|
2012-09-16 22:14:26 +02:00
|
|
|
belongs_to :invited_by_user,
|
|
|
|
:class_name => "User"
|
2012-07-04 05:24:18 +02:00
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
has_secure_password
|
|
|
|
|
2013-06-22 03:37:15 +02:00
|
|
|
validates :email, :format => { :with => /\A[^@ ]+@[^@ ]+\.[^@ ]+\Z/ },
|
|
|
|
:uniqueness => { :case_sensitive => false }
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2013-06-22 03:37:15 +02:00
|
|
|
validates :password, :presence => true, :on => :create
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2013-06-22 03:37:15 +02:00
|
|
|
validates :username, :format => { :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\Z/ },
|
|
|
|
:uniqueness => { :case_sensitive => false }
|
|
|
|
|
|
|
|
validates_each :username do |record,attr,value|
|
|
|
|
if BANNED_USERNAMES.include?(value.to_s.downcase)
|
|
|
|
record.errors.add(attr, "is not permitted")
|
|
|
|
end
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
attr_accessible :username, :email, :password, :password_confirmation,
|
2012-07-01 20:31:31 +02:00
|
|
|
:about, :email_replies, :pushover_replies, :pushover_user_key,
|
2012-09-18 17:22:59 +02:00
|
|
|
:pushover_device, :email_messages, :pushover_messages, :email_mentions,
|
2013-06-27 19:56:23 +02:00
|
|
|
:pushover_mentions, :mailing_list_enabled
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
before_save :check_session_token
|
2013-07-20 05:05:00 +02:00
|
|
|
before_validation :on => :create do
|
|
|
|
self.create_rss_token
|
|
|
|
self.create_mailing_list_token
|
|
|
|
end
|
2013-06-27 19:55:40 +02:00
|
|
|
after_create :create_default_tag_filters
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2013-06-22 03:37:15 +02:00
|
|
|
BANNED_USERNAMES = [ "admin", "administrator", "hostmaster", "mailer-daemon",
|
|
|
|
"postmaster", "root", "security", "support", "webmaster", ]
|
|
|
|
|
2012-12-17 03:00:41 +01:00
|
|
|
def as_json(options = {})
|
|
|
|
h = super(:only => [
|
|
|
|
:username,
|
2012-12-19 00:26:29 +01:00
|
|
|
:created_at,
|
2012-12-17 03:00:41 +01:00
|
|
|
:is_admin,
|
|
|
|
:is_moderator,
|
|
|
|
])
|
|
|
|
h[:avatar_url] = avatar_url
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_url
|
|
|
|
"https://secure.gravatar.com/avatar/" <<
|
|
|
|
Digest::MD5.hexdigest(self.email.strip.downcase) << "?r=pg&d=" <<
|
|
|
|
CGI.escape(Rails.application.routes.url_helpers.root_url +
|
|
|
|
"images/1x1t.gif") << "&s=100"
|
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def average_karma
|
|
|
|
if (k = self.karma) == 0
|
|
|
|
0
|
|
|
|
else
|
|
|
|
k.to_f / (self.stories_submitted_count + self.comments_posted_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
def check_session_token
|
|
|
|
if self.session_token.blank?
|
2012-06-30 23:41:34 +02:00
|
|
|
self.session_token = Utils.random_str(60)
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-06 20:44:42 +02:00
|
|
|
def create_default_tag_filters
|
|
|
|
Tag.where(:filtered_by_default => true).each do |t|
|
|
|
|
tf = TagFilter.new
|
|
|
|
tf.tag_id = t.id
|
|
|
|
tf.user_id = self.id
|
|
|
|
tf.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def create_mailing_list_token
|
|
|
|
if self.mailing_list_token.blank?
|
|
|
|
self.mailing_list_token = Utils.random_str(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-18 17:38:03 +02:00
|
|
|
def create_rss_token
|
|
|
|
if self.rss_token.blank?
|
|
|
|
self.rss_token = Utils.random_str(60)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def comments_posted_count
|
|
|
|
Keystore.value_for("user:#{self.id}:comments_posted").to_i
|
2013-06-22 04:19:57 +02:00
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def initiate_password_reset_for_ip(ip)
|
|
|
|
self.password_reset_token = Utils.random_str(40)
|
|
|
|
self.save!
|
2012-07-04 03:48:01 +02:00
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
PasswordReset.password_reset_link(self, ip).deliver
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def karma
|
|
|
|
Keystore.value_for("user:#{self.id}:karma").to_i
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def linkified_about
|
|
|
|
# most users are probably mentioning "@username" to mean a twitter url, not
|
|
|
|
# a link to a profile on this site
|
|
|
|
Markdowner.to_html(self.about, { :disable_profile_links => true })
|
2012-09-07 19:25:59 +02:00
|
|
|
end
|
|
|
|
|
2013-07-01 22:53:24 +02:00
|
|
|
def most_common_story_tag
|
|
|
|
tag_id = Tagging.connection.select_one("SELECT tag_id, " <<
|
|
|
|
"COUNT(taggings.id) AS tag_count FROM taggings LEFT OUTER JOIN " <<
|
|
|
|
"stories ON stories.id = taggings.story_id WHERE stories.user_id = " <<
|
|
|
|
"#{q(self.id)} GROUP BY tag_id ORDER BY tag_count DESC LIMIT 1")
|
|
|
|
|
|
|
|
if tag_id && tag_id["tag_id"]
|
|
|
|
Tag.where(:id => tag_id["tag_id"]).first
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def recent_threads(amount)
|
|
|
|
Comment.connection.select_all("SELECT DISTINCT " +
|
|
|
|
"thread_id FROM comments WHERE user_id = #{q(self.id)} ORDER BY " +
|
|
|
|
"created_at DESC LIMIT #{q(amount)}").map{|r| r.values.first }
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def stories_submitted_count
|
|
|
|
Keystore.value_for("user:#{self.id}:stories_submitted").to_i
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
|
2012-09-03 18:25:14 +02:00
|
|
|
def undeleted_received_messages
|
2013-01-24 21:06:10 +01:00
|
|
|
received_messages.where(:deleted_by_recipient => false)
|
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2013-01-24 21:06:10 +01:00
|
|
|
def undeleted_sent_messages
|
|
|
|
sent_messages.where(:deleted_by_author => 0)
|
2012-09-03 18:25:14 +02:00
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def unread_message_count
|
|
|
|
Keystore.value_for("user:#{self.id}:unread_messages").to_i
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
|
2013-07-01 22:43:51 +02:00
|
|
|
def update_unread_message_count!
|
|
|
|
Keystore.put("user:#{self.id}:unread_messages",
|
|
|
|
Message.where(:recipient_user_id => self.id,
|
|
|
|
:has_been_read => false).count)
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|