Merge branch 'mailer_sendcloud_fix'

This commit is contained in:
yafeilee 2014-04-07 22:58:13 +08:00
commit 468d43094a
8 changed files with 93 additions and 8 deletions

View File

@ -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 }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -27,9 +27,15 @@ class NewCommentWorker
<p>: #{name}</p>
<p>: #{content}</p>
<p>: #{content[0..15]}...</p>
<p>: #{title}</p>
<p>&nbsp;</p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}
return response

View File

@ -29,6 +29,10 @@ class NewPostWorker
<p>访: http://yafeilee.me</p>
<p>&nbsp;</p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}

View File

@ -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
<p>#{name}, 你好:</p>
<p>:</p>
<p>: #{title}</p>
<p>: #{content[0..20]}...</p>
<p>&nbsp;</p>
<p>: <a href="http://yafeilee.me/blogs/#{id}">#{title}</a></p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}
return response
end
end

View File

@ -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