diff --git a/app/controllers/subscribes_controller.rb b/app/controllers/subscribes_controller.rb index d77cffc..3d9042e 100644 --- a/app/controllers/subscribes_controller.rb +++ b/app/controllers/subscribes_controller.rb @@ -1,6 +1,8 @@ class SubscribesController < ApplicationController + def index end + def create subscribe = Subscribe.find_or_initialize_by(email: params[:email]) subscribe.enable = true @@ -13,10 +15,9 @@ class SubscribesController < ApplicationController end def cancel - if subscribe = Subscribe.where(email: params[:email]).first - subscribe.enable = false - subscribe.save - end + subscribe = Subscribe.find_or_initialize_by(email: params[:email]) + subscribe.enable = false + subscribe.save flash[:notice] = "退订成功: #{params[:email]}" render :json => { success: true } diff --git a/app/models/comment.rb b/app/models/comment.rb index 1ef6522..4bcc817 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -13,10 +13,22 @@ class Comment validates :content, presence: true validates_presence_of :post_id + def reply_emails + Comment.where(post_id: self.post_id).where(:id.ne => self.id).collect(&:email).uniq + end + after_create do if ENV['SENDCLOUD_USER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/ Rails.logger.info 'comment created, comment worker start' NewCommentWorker.perform_async(self.name, self.content, self.post.title, ENV['ADMIN_USER']) end + + 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 end end diff --git a/app/models/post.rb b/app/models/post.rb index a3a9006..7ce03ad 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -24,7 +24,9 @@ class Post after_create do if ENV['SENDCLOUD_USER'].present? - NewPostWorker.perform_async(self.title, Subscribe.subscribe_list) + Subscribe.subscribe_list.each do |email| + NewPostWorker.perform_async(self.title, email) + end end end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index 7ce6ade..b0ffaf5 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -6,6 +6,10 @@ class Subscribe validates :email, presence: true, uniqueness: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: '地址无效' } def self.subscribe_list - Subscribe.all.where(enable: true).map(&:email).join(";") + Subscribe.all.where(enable: true).map(&:email) + end + + def self.unsubscribe?(email) + Subscribe.where(email: email, enable: false).first.present? end end diff --git a/app/workers/new_comment_worker.rb b/app/workers/new_comment_worker.rb index 43baddf..b245b65 100644 --- a/app/workers/new_comment_worker.rb +++ b/app/workers/new_comment_worker.rb @@ -27,9 +27,15 @@ class NewCommentWorker

评论人: #{name}

-

评论内容: #{content}

+

评论内容: #{content[0..15]}...

被评论博客: #{title}

+ +

 

+ +

---------- 退订地址请点击:

+ +

点此退订

EOF } return response diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb index b2148bd..014acc3 100644 --- a/app/workers/new_post_worker.rb +++ b/app/workers/new_post_worker.rb @@ -29,6 +29,10 @@ class NewPostWorker

具体内容请访问: http://yafeilee.me

+

 

+ +

---------- 退订地址请点击:

+

点此退订

EOF } diff --git a/app/workers/new_reply_post_worker.rb b/app/workers/new_reply_post_worker.rb new file mode 100644 index 0000000..b9eee93 --- /dev/null +++ b/app/workers/new_reply_post_worker.rb @@ -0,0 +1,51 @@ +class NewReplyPostWorker + include Sidekiq::Worker + + sidekiq_retry_in do |count| + 3 + end + + # name: 评论人名字 + # title: 博客标题 + # content: 回复内容 + # id: 博客id + # to: 邮件递送人 + def perform(name, title, content, id, to) + logger.info "[mail] new reply mail: title=#{title}, content=#{content}, to=#{to}" + response = send_mail(name, title, content, id, to) + logger.info "[mail] result is #{response}" + ensure + logger.info "[mail] new reply mail end: title=#{title}, content=#{content}, to=#{to}" + end + + def send_mail(name, title, content, id, 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}, 你好:

+ +

你回复过的博客有新的回复:

+ +

博客标题: #{title}

+ +

回复内容: #{content[0..20]}...

+ +

 

+ +

详细链接: #{title}

+ +

---------- 退订地址请点击:

+ +

点此退订

+ EOF + } + return response + end + +end diff --git a/spec/models/subscribe_spec.rb b/spec/models/subscribe_spec.rb index ef71aa8..0d1f6a4 100644 --- a/spec/models/subscribe_spec.rb +++ b/spec/models/subscribe_spec.rb @@ -18,6 +18,11 @@ describe Subscribe do it "subscribe_list" do subscribe = Subscribe.create(email: 'tester@test.com') subscribe = Subscribe.create(email: 'tester1@test.com') - expect(Subscribe.subscribe_list.split(';').size).to eq(2) + expect(Subscribe.subscribe_list.size).to eq(2) + end + + it "unsubscribe?" do + subscribe = Subscribe.create(email: 'tester@test.com', enable: false) + expect(Subscribe.unsubscribe?('tester@test.com')).to be_true end end