diff --git a/app/mailers/notification_mailers/started_sharing.rb b/app/mailers/notification_mailers/started_sharing.rb index e02c50e43..bc4a8dd44 100644 --- a/app/mailers/notification_mailers/started_sharing.rb +++ b/app/mailers/notification_mailers/started_sharing.rb @@ -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 diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 80630ebe2..69fdfe11b 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -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 diff --git a/app/workers/mail/also_commented.rb b/app/workers/mail/also_commented.rb index 52026e737..05a1ef105 100644 --- a/app/workers/mail/also_commented.rb +++ b/app/workers/mail/also_commented.rb @@ -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 diff --git a/app/workers/mail/comment_on_post.rb b/app/workers/mail/comment_on_post.rb index 070eb8e33..168b75237 100644 --- a/app/workers/mail/comment_on_post.rb +++ b/app/workers/mail/comment_on_post.rb @@ -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 diff --git a/app/workers/mail/confirm_email.rb b/app/workers/mail/confirm_email.rb index 252f030c6..efa9fb64e 100644 --- a/app/workers/mail/confirm_email.rb +++ b/app/workers/mail/confirm_email.rb @@ -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 diff --git a/app/workers/mail/csrf_token_fail.rb b/app/workers/mail/csrf_token_fail.rb index a3372b322..f9679b8c3 100644 --- a/app/workers/mail/csrf_token_fail.rb +++ b/app/workers/mail/csrf_token_fail.rb @@ -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 diff --git a/app/workers/mail/liked.rb b/app/workers/mail/liked.rb index 595af3e39..16471a542 100644 --- a/app/workers/mail/liked.rb +++ b/app/workers/mail/liked.rb @@ -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") diff --git a/app/workers/mail/mentioned.rb b/app/workers/mail/mentioned.rb index a32f30f11..4ae477d54 100644 --- a/app/workers/mail/mentioned.rb +++ b/app/workers/mail/mentioned.rb @@ -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 diff --git a/app/workers/mail/notifier_base.rb b/app/workers/mail/notifier_base.rb new file mode 100644 index 000000000..72cb4acb8 --- /dev/null +++ b/app/workers/mail/notifier_base.rb @@ -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 diff --git a/app/workers/mail/private_message.rb b/app/workers/mail/private_message.rb index d24ce0296..b851bfed0 100644 --- a/app/workers/mail/private_message.rb +++ b/app/workers/mail/private_message.rb @@ -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 diff --git a/app/workers/mail/reshared.rb b/app/workers/mail/reshared.rb index 1144147ad..7e434788d 100644 --- a/app/workers/mail/reshared.rb +++ b/app/workers/mail/reshared.rb @@ -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 diff --git a/app/workers/mail/started_sharing.rb b/app/workers/mail/started_sharing.rb index 3618d1527..ef101060c 100644 --- a/app/workers/mail/started_sharing.rb +++ b/app/workers/mail/started_sharing.rb @@ -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 diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 7593bba03..13a05a27e 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -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 diff --git a/spec/workers/mail/csrf_token_fail_spec.rb b/spec/workers/mail/csrf_token_fail_spec.rb index e57292048..8c94f8663 100644 --- a/spec/workers/mail/csrf_token_fail_spec.rb +++ b/spec/workers/mail/csrf_token_fail_spec.rb @@ -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 diff --git a/spec/workers/mail/liked_spec.rb b/spec/workers/mail/liked_spec.rb index 8bb31ef0d..7ab94586a 100644 --- a/spec/workers/mail/liked_spec.rb +++ b/spec/workers/mail/liked_spec.rb @@ -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 { diff --git a/spec/workers/mail/mentioned_spec.rb b/spec/workers/mail/mentioned_spec.rb index e63a9dfe5..5d8d76cd7 100644 --- a/spec/workers/mail/mentioned_spec.rb +++ b/spec/workers/mail/mentioned_spec.rb @@ -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 diff --git a/spec/workers/mail/private_message_spec.rb b/spec/workers/mail/private_message_spec.rb index 8722cd211..7b8401986 100644 --- a/spec/workers/mail/private_message_spec.rb +++ b/spec/workers/mail/private_message_spec.rb @@ -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 diff --git a/spec/workers/mail/reshared_spec.rb b/spec/workers/mail/reshared_spec.rb index fea1eb54d..d1ebd9908 100644 --- a/spec/workers/mail/reshared_spec.rb +++ b/spec/workers/mail/reshared_spec.rb @@ -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