From b6f0502a93f8114520c431a3c44b9ef00773aa80 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 2 Apr 2014 22:06:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=84=E8=AE=BA=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E4=B8=8E=E8=AE=A2=E9=98=85=E9=80=9A=E7=9F=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/dashboard_controller.rb | 1 + app/models/comment.rb | 6 ++++ app/models/subscribe.rb | 4 +-- app/views/admin/dashboard/index.html.slim | 3 ++ app/workers/new_comment_worker.rb | 34 +++++++++++++++++++ app/workers/new_post_worker.rb | 6 +++- config/application.yml.example | 1 + spec/factories/subscribes.rb | 2 +- spec/models/subscribe_spec.rb | 23 +++++++++++++ 9 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app/workers/new_comment_worker.rb create mode 100644 spec/models/subscribe_spec.rb diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 9baa8e3..a20e153 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -6,5 +6,6 @@ class Admin::DashboardController < ApplicationController @posts_count = Post.all.size @comments_count = Comment.all.size @visited_count = Post.all.inject(0) { |res, p| res + p.visited_count } + @subscribes_count = Subscribe.all.size end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 563add8..0639888 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -12,4 +12,10 @@ class Comment validates :email, presence: true,:format => /@/ validates :content, presence: true 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 diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 0128a44..643dda0 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -3,9 +3,9 @@ class Subscribe field :email, type: String field :enable, type: Mongoid::Boolean, default: true - validates :email, presence: true, format: /@/ + validates :email, presence: true, uniqueness: true, format: /@/ def self.subscribe_list - Subscribe.all.map(&:email).join(";") + Subscribe.all.where(enable: true).map(&:email).join(";") end end diff --git a/app/views/admin/dashboard/index.html.slim b/app/views/admin/dashboard/index.html.slim index b8241ce..61c3023 100644 --- a/app/views/admin/dashboard/index.html.slim +++ b/app/views/admin/dashboard/index.html.slim @@ -17,4 +17,7 @@ tr td 总浏览量 td #{@visited_count} + tr + td 总订阅量 + td #{@subscribes_count} diff --git a/app/workers/new_comment_worker.rb b/app/workers/new_comment_worker.rb new file mode 100644 index 0000000..f7d68e8 --- /dev/null +++ b/app/workers/new_comment_worker.rb @@ -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 +

很高兴的通知你, 你的博客有新的评论:

+ +

评论人: #{name}

+ +

评论内容: #{content}

+ +

被评论博客: #{title}

+ EOF + } + return response + end +end + diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index ce88596..1abb5e7 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -2,7 +2,11 @@ class NewPostWorker include Sidekiq::Worker 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 def send_mail(title, to) diff --git a/config/application.yml.example b/config/application.yml.example index dd24f92..1704a94 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -1,6 +1,7 @@ # 必须配置的 SITE_NAME: "WinDy's Blog" # 后台登录的用户名与密码, 不填的话无法管理后台 +# 建议使用个人邮箱, 如果同时配置了 SENDCLOUD_USER, 则启用邮件通知, 源代码参考: /app/models/comment.rb ADMIN_USER: 'admin' ADMIN_PASSWORD: 'admin' diff --git a/spec/factories/subscribes.rb b/spec/factories/subscribes.rb index f517ea7..de9712f 100644 --- a/spec/factories/subscribes.rb +++ b/spec/factories/subscribes.rb @@ -2,7 +2,7 @@ FactoryGirl.define do factory :subscribe do - email "MyString" + email "tester@mail.com" enable false end end diff --git a/spec/models/subscribe_spec.rb b/spec/models/subscribe_spec.rb new file mode 100644 index 0000000..84ac295 --- /dev/null +++ b/spec/models/subscribe_spec.rb @@ -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