Refactor mail workers to use common base

Introduce Workers::Mail::NotifierBase to be a base for all appropriate
mail workers to reduce code duplication
This commit is contained in:
cmrd Senya 2016-09-14 20:30:36 +03:00
parent 0ee34f8590
commit ef5751b808
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
18 changed files with 59 additions and 123 deletions

View file

@ -1,7 +1,7 @@
module NotificationMailers module NotificationMailers
class StartedSharing < NotificationMailers::Base class StartedSharing < NotificationMailers::Base
def set_headers def set_headers(*_args) # rubocop:disable Style/AccessorMethodName
@headers[:subject] = I18n.t('notifier.started_sharing.subject', :name => @sender.name) @headers[:subject] = I18n.t("notifier.started_sharing.subject", name: @sender.name)
end end
end end
end end

View file

@ -55,54 +55,20 @@ class Notifier < ActionMailer::Base
end end
end end
def started_sharing(recipient_id, sender_id)
send_notification(:started_sharing, recipient_id, sender_id)
end
def liked(recipient_id, sender_id, like_id)
send_notification(:liked, recipient_id, sender_id, like_id)
end
def reshared(recipient_id, sender_id, reshare_id)
send_notification(:reshared, recipient_id, sender_id, reshare_id)
end
def mentioned(recipient_id, sender_id, target_id)
send_notification(:mentioned, recipient_id, sender_id, target_id)
end
def comment_on_post(recipient_id, sender_id, comment_id)
send_notification(:comment_on_post, recipient_id, sender_id, comment_id)
end
def also_commented(recipient_id, sender_id, comment_id)
send_notification(:also_commented, recipient_id, sender_id, comment_id)
end
def private_message(recipient_id, sender_id, message_id)
send_notification(:private_message, recipient_id, sender_id, message_id)
end
def confirm_email(recipient_id)
send_notification(:confirm_email, recipient_id)
end
def csrf_token_fail(recipient_id)
send_notification(:csrf_token_fail, recipient_id)
end
private
def send_notification(type, *args) def send_notification(type, *args)
@notification = NotificationMailers.const_get(type.to_s.camelize).new(*args) @notification = NotificationMailers.const_get(type.to_s.camelize).new(*args)
with_recipient_locale do with_recipient_locale do
mail(@notification.headers) do |format| mail(@notification.headers) do |format|
self.action_name = type
format.text format.text
format.html format.html
end end
end end
end end
private
def with_recipient_locale(&block) def with_recipient_locale(&block)
I18n.with_locale(@notification.recipient.language, &block) I18n.with_locale(@notification.recipient.language, &block)
end end

View file

@ -1,13 +1,6 @@
module Workers module Workers
module Mail module Mail
class AlsoCommented < Base class AlsoCommented < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, sender_id, comment_id)
if email = Notifier.also_commented(recipient_id, sender_id, comment_id)
email.deliver_now
end
end
end end
end end
end end

View file

@ -1,11 +1,6 @@
module Workers module Workers
module Mail module Mail
class CommentOnPost < Base class CommentOnPost < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, sender_id, comment_id)
Notifier.comment_on_post(recipient_id, sender_id, comment_id).deliver_now
end
end end
end end
end end

View file

@ -1,11 +1,6 @@
module Workers module Workers
module Mail module Mail
class ConfirmEmail < Base class ConfirmEmail < NotifierBase
sidekiq_options queue: :low
def perform(user_id)
Notifier.confirm_email(user_id).deliver_now
end
end end
end end
end end

View file

@ -1,11 +1,6 @@
module Workers module Workers
module Mail module Mail
class CsrfTokenFail < Base class CsrfTokenFail < NotifierBase
sidekiq_options queue: :low
def perform(user_id)
Notifier.csrf_token_fail(user_id).deliver_now
end
end end
end end
end end

View file

@ -1,10 +1,8 @@
module Workers module Workers
module Mail module Mail
class Liked < Base class Liked < NotifierBase
sidekiq_options queue: :low def perform(*args)
super
def perform(recipient_id, sender_id, like_id)
Notifier.liked(recipient_id, sender_id, like_id).deliver_now
rescue ActiveRecord::RecordNotFound => e rescue ActiveRecord::RecordNotFound => e
logger.warn("failed to send liked notification mail: #{e.message}") logger.warn("failed to send liked notification mail: #{e.message}")
raise e unless e.message.start_with?("Couldn't find Like with") raise e unless e.message.start_with?("Couldn't find Like with")

View file

@ -5,12 +5,7 @@
module Workers module Workers
module Mail module Mail
class Mentioned < Base class Mentioned < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, actor_id, target_id)
Notifier.mentioned( recipient_id, actor_id, target_id).deliver_now
end
end end
end end
end end

View file

@ -0,0 +1,11 @@
module Workers
module Mail
class NotifierBase < Base
sidekiq_options queue: :low
def perform(*args)
Notifier.send_notification(self.class.name.gsub("Workers::Mail::", "").underscore, *args).deliver_now
end
end
end
end

View file

@ -5,12 +5,7 @@
module Workers module Workers
module Mail module Mail
class PrivateMessage < Base class PrivateMessage < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, actor_id, target_id)
Notifier.private_message( recipient_id, actor_id, target_id).deliver_now
end
end end
end end
end end

View file

@ -1,11 +1,6 @@
module Workers module Workers
module Mail module Mail
class Reshared < Base class Reshared < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, sender_id, reshare_id)
Notifier.reshared(recipient_id, sender_id, reshare_id).deliver_now
end
end end
end end
end end

View file

@ -5,12 +5,7 @@
module Workers module Workers
module Mail module Mail
class StartedSharing < Base class StartedSharing < NotifierBase
sidekiq_options queue: :low
def perform(recipient_id, sender_id, target_id)
Notifier.started_sharing(recipient_id, sender_id).deliver_now
end
end end
end end
end end

View file

@ -64,7 +64,7 @@ describe Notifier, type: :mailer do
end end
describe ".started_sharing" do describe ".started_sharing" do
let!(:request_mail) { Notifier.started_sharing(bob.id, person.id) } let!(:request_mail) { Notifier.send_notification("started_sharing", bob.id, person.id) }
it "goes to the right person" do it "goes to the right person" do
expect(request_mail.to).to eq([bob.email]) expect(request_mail.to).to eq([bob.email])
@ -136,7 +136,7 @@ describe Notifier, type: :mailer do
before do before do
@post = FactoryGirl.create(:status_message, author: alice.person, public: true) @post = FactoryGirl.create(:status_message, author: alice.person, public: true)
@like = @post.likes.create!(author: bob.person) @like = @post.likes.create!(author: bob.person)
@mail = Notifier.liked(alice.id, @like.author.id, @like.id) @mail = Notifier.send_notification("liked", alice.id, @like.author.id, @like.id)
end end
it "TO: goes to the right person" do it "TO: goes to the right person" do
@ -158,7 +158,7 @@ describe Notifier, type: :mailer do
it "can handle a reshare" do it "can handle a reshare" do
reshare = FactoryGirl.create(:reshare) reshare = FactoryGirl.create(:reshare)
like = reshare.likes.create!(author: bob.person) like = reshare.likes.create!(author: bob.person)
Notifier.liked(alice.id, like.author.id, like.id) Notifier.send_notification("liked", alice.id, like.author.id, like.id)
end end
end end
@ -166,7 +166,7 @@ describe Notifier, type: :mailer do
before do before do
@post = FactoryGirl.create(:status_message, author: alice.person, public: true) @post = FactoryGirl.create(:status_message, author: alice.person, public: true)
@reshare = FactoryGirl.create(:reshare, root: @post, author: bob.person) @reshare = FactoryGirl.create(:reshare, root: @post, author: bob.person)
@mail = Notifier.reshared(alice.id, @reshare.author.id, @reshare.id) @mail = Notifier.send_notification("reshared", alice.id, @reshare.author.id, @reshare.id)
end end
it "TO: goes to the right person" do it "TO: goes to the right person" do
@ -205,7 +205,7 @@ describe Notifier, type: :mailer do
@cnv = Conversation.create(@create_hash) @cnv = Conversation.create(@create_hash)
@mail = Notifier.private_message(bob.id, @cnv.author.id, @cnv.messages.first.id) @mail = Notifier.send_notification("private_message", bob.id, @cnv.author.id, @cnv.messages.first.id)
end end
it "TO: goes to the right person" do it "TO: goes to the right person" do
@ -248,7 +248,7 @@ describe Notifier, type: :mailer do
let(:comment) { eve.comment!(commented_post, "Totally is") } let(:comment) { eve.comment!(commented_post, "Totally is") }
describe ".comment_on_post" do describe ".comment_on_post" do
let(:comment_mail) { Notifier.comment_on_post(bob.id, person.id, comment.id).deliver_now } let(:comment_mail) { Notifier.send_notification("comment_on_post", bob.id, person.id, comment.id).deliver_now }
it "TO: goes to the right person" do it "TO: goes to the right person" do
expect(comment_mail.to).to eq([bob.email]) expect(comment_mail.to).to eq([bob.email])
@ -289,7 +289,7 @@ describe Notifier, type: :mailer do
end end
describe ".also_commented" do describe ".also_commented" do
let(:comment_mail) { Notifier.also_commented(bob.id, person.id, comment.id) } let(:comment_mail) { Notifier.send_notification("also_commented", bob.id, person.id, comment.id) }
it "TO: goes to the right person" do it "TO: goes to the right person" do
expect(comment_mail.to).to eq([bob.email]) expect(comment_mail.to).to eq([bob.email])
@ -344,7 +344,7 @@ describe Notifier, type: :mailer do
let(:comment) { bob.comment!(limited_post, "Totally is") } let(:comment) { bob.comment!(limited_post, "Totally is") }
describe ".also_commented" do describe ".also_commented" do
let(:mail) { Notifier.also_commented(alice.id, bob.person.id, comment.id) } let(:mail) { Notifier.send_notification("also_commented", alice.id, bob.person.id, comment.id) }
it "TO: goes to the right person" do it "TO: goes to the right person" do
expect(mail.to).to eq([alice.email]) expect(mail.to).to eq([alice.email])
@ -369,7 +369,7 @@ describe Notifier, type: :mailer do
describe ".comment_on_post" do describe ".comment_on_post" do
let(:comment) { bob.comment!(limited_post, "Totally is") } let(:comment) { bob.comment!(limited_post, "Totally is") }
let(:mail) { Notifier.comment_on_post(alice.id, bob.person.id, comment.id) } let(:mail) { Notifier.send_notification("comment_on_post", alice.id, bob.person.id, comment.id) }
it "TO: goes to the right person" do it "TO: goes to the right person" do
expect(mail.to).to eq([alice.email]) expect(mail.to).to eq([alice.email])
@ -400,7 +400,7 @@ describe Notifier, type: :mailer do
describe ".liked" do describe ".liked" do
let(:like) { bob.like!(limited_post) } let(:like) { bob.like!(limited_post) }
let(:mail) { Notifier.liked(alice.id, bob.person.id, like.id) } let(:mail) { Notifier.send_notification("liked", alice.id, bob.person.id, like.id) }
it "TO: goes to the right person" do it "TO: goes to the right person" do
expect(mail.to).to eq([alice.email]) expect(mail.to).to eq([alice.email])
@ -436,7 +436,7 @@ describe Notifier, type: :mailer do
describe ".confirm_email" do describe ".confirm_email" do
before do before do
bob.update_attribute(:unconfirmed_email, "my@newemail.com") bob.update_attribute(:unconfirmed_email, "my@newemail.com")
@confirm_email = Notifier.confirm_email(bob.id) @confirm_email = Notifier.send_notification("confirm_email", bob.id)
end end
it "goes to the right person" do it "goes to the right person" do
@ -461,7 +461,7 @@ describe Notifier, type: :mailer do
end end
describe ".csrf_token_fail" do describe ".csrf_token_fail" do
let(:email) { Notifier.csrf_token_fail(alice.id) } let(:email) { Notifier.send_notification("csrf_token_fail", alice.id) }
it "goes to the right person" do it "goes to the right person" do
expect(email.to).to eq([alice.email]) expect(email.to).to eq([alice.email])
@ -495,7 +495,7 @@ describe Notifier, type: :mailer do
it "handles idn addresses" do it "handles idn addresses" do
bob.update_attribute(:email, "ŧoo@ŧexample.com") bob.update_attribute(:email, "ŧoo@ŧexample.com")
expect { expect {
Notifier.started_sharing(bob.id, person.id) Notifier.send_notification("started_sharing", bob.id, person.id)
}.to_not raise_error }.to_not raise_error
end end
end end

View file

@ -3,12 +3,12 @@
# the COPYRIGHT file. # the COPYRIGHT file.
describe Workers::Mail::CsrfTokenFail do describe Workers::Mail::CsrfTokenFail do
describe "#perfom" do describe "#perform" do
it "should call .deliver on the notifier object" do it "should call .deliver on the notifier object" do
user = alice user = alice
mail_double = double mail_double = double
expect(mail_double).to receive(:deliver_now) expect(mail_double).to receive(:deliver_now)
expect(Notifier).to receive(:csrf_token_fail).with(user.id).and_return(mail_double) expect(Notifier).to receive(:send_notification).with("csrf_token_fail", user.id).and_return(mail_double)
Workers::Mail::CsrfTokenFail.new.perform(user.id) Workers::Mail::CsrfTokenFail.new.perform(user.id)
end end

View file

@ -1,12 +1,13 @@
describe Workers::Mail::Liked do describe Workers::Mail::Liked do
describe "#perfom" do describe "#perform" do
it "should call .deliver_now on the notifier object" do it "should call .deliver_now on the notifier object" do
sm = FactoryGirl.build(:status_message, author: bob.person, public: true) sm = FactoryGirl.build(:status_message, author: bob.person, public: true)
like = FactoryGirl.build(:like, author: alice.person, target: sm) like = FactoryGirl.build(:like, author: alice.person, target: sm)
mail_double = double mail_double = double
expect(mail_double).to receive(:deliver_now) expect(mail_double).to receive(:deliver_now)
expect(Notifier).to receive(:liked).with(bob.id, like.author.id, like.id).and_return(mail_double) expect(Notifier).to receive(:send_notification)
.with("liked", bob.id, like.author.id, like.id).and_return(mail_double)
Workers::Mail::Liked.new.perform(bob.id, like.author.id, like.id) Workers::Mail::Liked.new.perform(bob.id, like.author.id, like.id)
end end
@ -15,7 +16,7 @@ describe Workers::Mail::Liked do
sm = FactoryGirl.build(:status_message, author: bob.person, public: true) sm = FactoryGirl.build(:status_message, author: bob.person, public: true)
like = FactoryGirl.build(:like, author: alice.person, target: sm) like = FactoryGirl.build(:like, author: alice.person, target: sm)
expect(Notifier).to receive(:liked).with(bob.id, like.author.id, like.id) expect(Notifier).to receive(:send_notification).with("liked", bob.id, like.author.id, like.id)
.and_raise(ActiveRecord::RecordNotFound.new("Couldn't find Like with 'id'=42")) .and_raise(ActiveRecord::RecordNotFound.new("Couldn't find Like with 'id'=42"))
Workers::Mail::Liked.new.perform(bob.id, like.author.id, like.id) Workers::Mail::Liked.new.perform(bob.id, like.author.id, like.id)
@ -25,7 +26,7 @@ describe Workers::Mail::Liked do
sm = FactoryGirl.build(:status_message, author: bob.person, public: true) sm = FactoryGirl.build(:status_message, author: bob.person, public: true)
like = FactoryGirl.build(:like, author: alice.person, target: sm) like = FactoryGirl.build(:like, author: alice.person, target: sm)
expect(Notifier).to receive(:liked).with(bob.id, like.author.id, like.id) expect(Notifier).to receive(:send_notification).with("liked", bob.id, like.author.id, like.id)
.and_raise(ActiveRecord::RecordNotFound.new("Couldn't find Person with 'id'=42")) .and_raise(ActiveRecord::RecordNotFound.new("Couldn't find Person with 'id'=42"))
expect { expect {

View file

@ -3,15 +3,15 @@
# the COPYRIGHT file. # the COPYRIGHT file.
describe Workers::Mail::Mentioned do describe Workers::Mail::Mentioned do
describe '#perfom' do describe "#perform" do
it 'should call .deliver on the notifier object' do it "should call .deliver on the notifier object" do
user = alice user = alice
sm = FactoryGirl.build(:status_message) sm = FactoryGirl.build(:status_message)
m = Mention.new(:person => user.person, :post=> sm) m = Mention.new(:person => user.person, :post=> sm)
mail_double = double() mail_double = double()
expect(mail_double).to receive(:deliver_now) expect(mail_double).to receive(:deliver_now)
expect(Notifier).to receive(:mentioned).with(user.id, sm.author.id, m.id).and_return(mail_double) expect(Notifier).to receive(:send_notification).with("mentioned", user.id, sm.author.id, m.id).and_return(mail_double)
Workers::Mail::Mentioned.new.perform(user.id, sm.author.id, m.id) Workers::Mail::Mentioned.new.perform(user.id, sm.author.id, m.id)
end end

View file

@ -3,8 +3,8 @@
# the COPYRIGHT file. # the COPYRIGHT file.
describe Workers::Mail::PrivateMessage do describe Workers::Mail::PrivateMessage do
describe '#perfom_delegate' do describe "#perform" do
it 'should call .deliver on the notifier object' do it "should call .deliver on the notifier object" do
user1 = alice user1 = alice
user2 = bob user2 = bob
participant_ids = [user1.contacts.first.person.id, user1.person.id] participant_ids = [user1.contacts.first.person.id, user1.person.id]
@ -17,9 +17,10 @@ describe Workers::Mail::PrivateMessage do
mail_double = double() mail_double = double()
expect(mail_double).to receive(:deliver_now) expect(mail_double).to receive(:deliver_now)
expect(Notifier).to receive(:mentioned).with(user2.id, user1.person.id, message.id).and_return(mail_double) expect(Notifier).to receive(:send_notification)
.with("private_message", user2.id, user1.person.id, message.id).and_return(mail_double)
Workers::Mail::Mentioned.new.perform(user2.id, user1.person.id, message.id) Workers::Mail::PrivateMessage.new.perform(user2.id, user1.person.id, message.id)
end end
end end
end end

View file

@ -3,14 +3,15 @@
# the COPYRIGHT file. # the COPYRIGHT file.
describe Workers::Mail::Reshared do describe Workers::Mail::Reshared do
describe '#perfom' do describe "#perform" do
it 'should call .deliver on the notifier object' do it "should call .deliver on the notifier object" do
sm = FactoryGirl.build(:status_message, :author => bob.person, :public => true) sm = FactoryGirl.build(:status_message, :author => bob.person, :public => true)
reshare = FactoryGirl.build(:reshare, :author => alice.person, :root=> sm) reshare = FactoryGirl.build(:reshare, :author => alice.person, :root=> sm)
mail_double = double() mail_double = double()
expect(mail_double).to receive(:deliver_now) expect(mail_double).to receive(:deliver_now)
expect(Notifier).to receive(:reshared).with(bob.id, reshare.author.id, reshare.id).and_return(mail_double) expect(Notifier).to receive(:send_notification)
.with("reshared", bob.id, reshare.author.id, reshare.id).and_return(mail_double)
Workers::Mail::Reshared.new.perform(bob.id, reshare.author.id, reshare.id) Workers::Mail::Reshared.new.perform(bob.id, reshare.author.id, reshare.id)
end end