journalduhacker/app/models/user.rb

73 lines
2 KiB
Ruby
Raw Normal View History

class User < ActiveRecord::Base
2012-07-01 00:43:45 +02:00
has_many :stories,
:include => :user
2012-07-04 03:48:01 +02:00
has_many :comments
has_many :authored_messages,
:class_name => "Message",
:foreign_key => "author_user_id"
has_many :received_messages,
:class_name => "Message",
:foreign_key => "recipient_user_id"
has_secure_password
validates_format_of :username, :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\Z/
validates_uniqueness_of :username, :case_sensitive => false
validates_format_of :email, :with => /\A[^@]+@[^@]+\.[^@]+\Z/
validates_uniqueness_of :email, :case_sensitive => false
validates_presence_of :password, :on => :create
attr_accessible :username, :email, :password, :password_confirmation,
2012-07-01 20:31:31 +02:00
:about, :email_replies, :pushover_replies, :pushover_user_key,
2012-07-04 03:48:01 +02:00
:pushover_device, :email_messages, :pushover_messages
before_save :check_session_token
def check_session_token
if self.session_token.blank?
2012-06-30 23:41:34 +02:00
self.session_token = Utils.random_str(60)
end
end
2012-07-01 00:43:45 +02:00
def unread_message_count
2012-07-04 03:48:01 +02:00
Keystore.value_for("user:#{self.id}:unread_messages").to_i
end
def update_unread_message_count!
Keystore.put("user:#{self.id}:unread_messages",
Message.where(:recipient_user_id => self.id,
:has_been_read => false).count)
end
2012-07-01 00:43:45 +02:00
def karma
Keystore.value_for("user:#{self.id}:karma").to_i
end
2012-06-30 21:14:35 +02:00
def stories_submitted_count
2012-07-01 20:40:18 +02:00
Keystore.value_for("user:#{self.id}:stories_submitted").to_i
2012-06-30 21:14:35 +02:00
end
def comments_posted_count
2012-07-01 20:40:18 +02:00
Keystore.value_for("user:#{self.id}:comments_posted").to_i
2012-06-30 21:14:35 +02:00
end
2012-07-03 18:59:50 +02:00
def initiate_password_reset_for_ip(ip)
self.password_reset_token = Utils.random_str(40)
self.save!
2012-07-03 18:59:50 +02:00
PasswordReset.password_reset_link(self, ip).deliver
end
2012-06-30 21:14:35 +02:00
def linkified_about
2012-07-02 04:08:03 +02:00
RDiscount.new(self.about.to_s, :smart, :autolink, :safelink,
:filter_html).to_html
2012-06-30 21:14:35 +02:00
end
def recent_threads(amount)
2012-06-30 21:14:35 +02:00
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 }
end
end