增加评论通知与订阅通知功能

This commit is contained in:
yafeilee 2014-04-02 22:06:27 +08:00
parent 199a8ae617
commit b6f0502a93
9 changed files with 76 additions and 4 deletions

View File

@ -6,5 +6,6 @@ class Admin::DashboardController < ApplicationController
@posts_count = Post.all.size @posts_count = Post.all.size
@comments_count = Comment.all.size @comments_count = Comment.all.size
@visited_count = Post.all.inject(0) { |res, p| res + p.visited_count } @visited_count = Post.all.inject(0) { |res, p| res + p.visited_count }
@subscribes_count = Subscribe.all.size
end end
end end

View File

@ -12,4 +12,10 @@ class Comment
validates :email, presence: true,:format => /@/ validates :email, presence: true,:format => /@/
validates :content, presence: true validates :content, presence: true
validates_presence_of :post_id validates_presence_of :post_id
after_create do
if ENV['SENDCLOUD_USER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/
NewCommentWorker.perform_async(self.name, self.content, self.post.title, ENV['ADMIN_USER'])
end
end
end end

View File

@ -3,9 +3,9 @@ class Subscribe
field :email, type: String field :email, type: String
field :enable, type: Mongoid::Boolean, default: true field :enable, type: Mongoid::Boolean, default: true
validates :email, presence: true, format: /@/ validates :email, presence: true, uniqueness: true, format: /@/
def self.subscribe_list def self.subscribe_list
Subscribe.all.map(&:email).join(";") Subscribe.all.where(enable: true).map(&:email).join(";")
end end
end end

View File

@ -17,4 +17,7 @@
tr tr
td 总浏览量 td 总浏览量
td #{@visited_count} td #{@visited_count}
tr
td 总订阅量
td #{@subscribes_count}

View File

@ -0,0 +1,34 @@
class NewCommentWorker
include Sidekiq::Worker
def perform(name, content, title, to)
logger.info "[mail] new comment mail: name=#{name}, content=#{content}, title=#{title}, to=#{to}"
response = send_mail(name, content, title, to)
logger.info "[mail] result is #{response}"
ensure
logger.info "[mail] new comment mail end: name=#{name}, content=#{content}, title=#{title}, to=#{to}"
end
def send_mail(name, content, title, to)
response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
{
:api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org",
:api_key => ENV['SENDCLOUD_PASSWORD'],
:from => ENV['SENDCLOUD_FROM'],
:fromname => ENV['SENDCLOUD_FROMNAME'],
:to => to,
:subject => "你的博客又有新的评论",
:html => <<-EOF
<p>, :</p>
<p>: #{name}</p>
<p>: #{content}</p>
<p>: #{title}</p>
EOF
}
return response
end
end

View File

@ -2,7 +2,11 @@ class NewPostWorker
include Sidekiq::Worker include Sidekiq::Worker
def perform(title, to) def perform(title, to)
send_mail(title, to) logger.info "[mail] new post mail: title=#{title}, to=#{to}"
response = send_mail(title, to)
logger.info "[mail] result is #{response}"
ensure
logger.info "[mail] new post mail end: title=#{title}, to=#{to}"
end end
def send_mail(title, to) def send_mail(title, to)

View File

@ -1,6 +1,7 @@
# 必须配置的 # 必须配置的
SITE_NAME: "WinDy's Blog" SITE_NAME: "WinDy's Blog"
# 后台登录的用户名与密码, 不填的话无法管理后台 # 后台登录的用户名与密码, 不填的话无法管理后台
# 建议使用个人邮箱, 如果同时配置了 SENDCLOUD_USER, 则启用邮件通知, 源代码参考: /app/models/comment.rb
ADMIN_USER: 'admin' ADMIN_USER: 'admin'
ADMIN_PASSWORD: 'admin' ADMIN_PASSWORD: 'admin'

View File

@ -2,7 +2,7 @@
FactoryGirl.define do FactoryGirl.define do
factory :subscribe do factory :subscribe do
email "MyString" email "tester@mail.com"
enable false enable false
end end
end end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe Subscribe do
it "validates should be ok" do
expect(create(:subscribe)).to be_true
end
it 'uniqueness' do
create(:subscribe)
two = build(:subscribe)
expect(two.save).to be_false
end
it "default is true" do
subscribe = Subscribe.create(email: 'a@b')
expect(subscribe.enable).to be_true
end
it "subscribe_list" do
subscribe = Subscribe.create(email: 'a@b')
subscribe = Subscribe.create(email: 'a1@b')
expect(Subscribe.subscribe_list.split(';').size).to eq(2)
end
end