2012-06-25 00:09:44 +08:00
|
|
|
class Comment
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2014-03-29 21:59:15 +08:00
|
|
|
|
|
|
|
field :name, :type => String
|
2012-06-25 00:09:44 +08:00
|
|
|
field :content, :type => String
|
2014-03-29 21:59:15 +08:00
|
|
|
field :email, :type=>String
|
|
|
|
|
|
|
|
belongs_to :post
|
|
|
|
|
|
|
|
validates :name, presence: true
|
2014-04-02 23:40:05 +08:00
|
|
|
validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: '地址无效' }
|
2012-06-25 00:09:44 +08:00
|
|
|
validates :content, presence: true
|
2014-03-30 22:14:59 +08:00
|
|
|
validates_presence_of :post_id
|
2014-04-02 22:06:27 +08:00
|
|
|
|
2014-04-07 22:56:21 +08:00
|
|
|
def reply_emails
|
|
|
|
Comment.where(post_id: self.post_id).where(:id.ne => self.id).collect(&:email).uniq
|
|
|
|
end
|
|
|
|
|
2014-04-02 22:06:27 +08:00
|
|
|
after_create do
|
|
|
|
if ENV['SENDCLOUD_USER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/
|
2014-04-03 00:23:33 +08:00
|
|
|
Rails.logger.info 'comment created, comment worker start'
|
2014-04-02 22:06:27 +08:00
|
|
|
NewCommentWorker.perform_async(self.name, self.content, self.post.title, ENV['ADMIN_USER'])
|
|
|
|
end
|
2014-04-07 22:56:21 +08:00
|
|
|
|
|
|
|
if ENV['SENDCLOUD_USER'].present?
|
|
|
|
Rails.logger.info 'comment created, reply worker start'
|
|
|
|
reply_emails.each do |comment|
|
|
|
|
next if Subscribe.unsubscribe?(email)
|
|
|
|
NewReplyPostWorker.perform_async(self.name, self.post.title, self.content, self.post.id.to_s, email)
|
|
|
|
end
|
|
|
|
end
|
2014-04-02 22:06:27 +08:00
|
|
|
end
|
2012-06-25 00:09:44 +08:00
|
|
|
end
|