Merge branch 'mailer_sendcloud_fix'
This commit is contained in:
commit
468d43094a
|
@ -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 }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -27,9 +27,15 @@ class NewCommentWorker
|
|||
|
||||
<p>评论人: #{name}</p>
|
||||
|
||||
<p>评论内容: #{content}</p>
|
||||
<p>评论内容: #{content[0..15]}...</p>
|
||||
|
||||
<p>被评论博客: #{title}</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>---------- 退订地址请点击:</p>
|
||||
|
||||
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">点此退订</a></p>
|
||||
EOF
|
||||
}
|
||||
return response
|
||||
|
|
|
@ -29,6 +29,10 @@ class NewPostWorker
|
|||
|
||||
<p>具体内容请访问: http://yafeilee.me</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>---------- 退订地址请点击:</p>
|
||||
|
||||
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">点此退订</a></p>
|
||||
EOF
|
||||
}
|
||||
|
|
|
@ -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> </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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue