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

View file

@ -55,54 +55,20 @@ class Notifier < ActionMailer::Base
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)
@notification = NotificationMailers.const_get(type.to_s.camelize).new(*args)
with_recipient_locale do
mail(@notification.headers) do |format|
self.action_name = type
format.text
format.html
end
end
end
private
def with_recipient_locale(&block)
I18n.with_locale(@notification.recipient.language, &block)
end

View file

@ -1,13 +1,6 @@
module Workers
module Mail
class AlsoCommented < Base
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
class AlsoCommented < NotifierBase
end
end
end

View file

@ -1,11 +1,6 @@
module Workers
module Mail
class CommentOnPost < Base
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
class CommentOnPost < NotifierBase
end
end
end

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -64,7 +64,7 @@ describe Notifier, type: :mailer do
end
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
expect(request_mail.to).to eq([bob.email])
@ -136,7 +136,7 @@ describe Notifier, type: :mailer do
before do
@post = FactoryGirl.create(:status_message, author: alice.person, public: true)
@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
it "TO: goes to the right person" do
@ -158,7 +158,7 @@ describe Notifier, type: :mailer do
it "can handle a reshare" do
reshare = FactoryGirl.create(:reshare)
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
@ -166,7 +166,7 @@ describe Notifier, type: :mailer do
before do
@post = FactoryGirl.create(:status_message, author: alice.person, public: true)
@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
it "TO: goes to the right person" do
@ -205,7 +205,7 @@ describe Notifier, type: :mailer do
@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
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") }
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
expect(comment_mail.to).to eq([bob.email])
@ -289,7 +289,7 @@ describe Notifier, type: :mailer do
end
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
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") }
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
expect(mail.to).to eq([alice.email])
@ -369,7 +369,7 @@ describe Notifier, type: :mailer do
describe ".comment_on_post" do
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
expect(mail.to).to eq([alice.email])
@ -400,7 +400,7 @@ describe Notifier, type: :mailer do
describe ".liked" do
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
expect(mail.to).to eq([alice.email])
@ -436,7 +436,7 @@ describe Notifier, type: :mailer do
describe ".confirm_email" do
before do
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
it "goes to the right person" do
@ -461,7 +461,7 @@ describe Notifier, type: :mailer do
end
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
expect(email.to).to eq([alice.email])
@ -495,7 +495,7 @@ describe Notifier, type: :mailer do
it "handles idn addresses" do
bob.update_attribute(:email, "ŧoo@ŧexample.com")
expect {
Notifier.started_sharing(bob.id, person.id)
Notifier.send_notification("started_sharing", bob.id, person.id)
}.to_not raise_error
end
end

View file

@ -3,12 +3,12 @@
# the COPYRIGHT file.
describe Workers::Mail::CsrfTokenFail do
describe "#perfom" do
describe "#perform" do
it "should call .deliver on the notifier object" do
user = alice
mail_double = double
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)
end

View file

@ -1,12 +1,13 @@
describe Workers::Mail::Liked do
describe "#perfom" do
describe "#perform" do
it "should call .deliver_now on the notifier object" do
sm = FactoryGirl.build(:status_message, author: bob.person, public: true)
like = FactoryGirl.build(:like, author: alice.person, target: sm)
mail_double = double
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)
end
@ -15,7 +16,7 @@ describe Workers::Mail::Liked do
sm = FactoryGirl.build(:status_message, author: bob.person, public: true)
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"))
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)
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"))
expect {

View file

@ -3,15 +3,15 @@
# the COPYRIGHT file.
describe Workers::Mail::Mentioned do
describe '#perfom' do
it 'should call .deliver on the notifier object' do
describe "#perform" do
it "should call .deliver on the notifier object" do
user = alice
sm = FactoryGirl.build(:status_message)
m = Mention.new(:person => user.person, :post=> sm)
mail_double = double()
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)
end

View file

@ -3,8 +3,8 @@
# the COPYRIGHT file.
describe Workers::Mail::PrivateMessage do
describe '#perfom_delegate' do
it 'should call .deliver on the notifier object' do
describe "#perform" do
it "should call .deliver on the notifier object" do
user1 = alice
user2 = bob
participant_ids = [user1.contacts.first.person.id, user1.person.id]
@ -17,9 +17,10 @@ describe Workers::Mail::PrivateMessage do
mail_double = double()
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

View file

@ -3,14 +3,15 @@
# the COPYRIGHT file.
describe Workers::Mail::Reshared do
describe '#perfom' do
it 'should call .deliver on the notifier object' do
describe "#perform" do
it "should call .deliver on the notifier object" do
sm = FactoryGirl.build(:status_message, :author => bob.person, :public => true)
reshare = FactoryGirl.build(:reshare, :author => alice.person, :root=> sm)
mail_double = double()
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)
end