From 277a785c922414fef18f6062d943269efbc53294 Mon Sep 17 00:00:00 2001 From: tomo Date: Mon, 29 May 2023 20:21:13 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E3=83=99=E3=82=B9=E3=83=88=E3=82=A2?= =?UTF-8?q?=E3=83=B3=E3=82=B5=E3=83=BC=E3=81=AA=E3=81=97=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E3=82=92active=5Fdelivery=E5=8C=96=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mailers/activity_mailer.rb | 18 ++++++++++++++++++ app/mailers/notification_mailer.rb | 8 -------- app/models/notification_facade.rb | 10 ---------- app/models/question.rb | 2 +- .../no_correct_answer.html.slim | 1 + .../no_correct_answer.html.slim | 1 - 6 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 app/views/activity_mailer/no_correct_answer.html.slim delete mode 100644 app/views/notification_mailer/no_correct_answer.html.slim diff --git a/app/mailers/activity_mailer.rb b/app/mailers/activity_mailer.rb index 353d765dfbb..335f339e86e 100644 --- a/app/mailers/activity_mailer.rb +++ b/app/mailers/activity_mailer.rb @@ -326,6 +326,24 @@ def update_regular_event(args = {}) message end + # required params: question, receiver + def no_correct_answer(args = {}) + @question ||= args[:question] + @receiver ||= args[:receiver] + @user = @receiver + + @link_url = notification_redirector_url( + link: "/questions/#{@question.id}", + kind: Notification.kinds[:no_correct_answer] + ) + + subject = "[FBC] #{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。" + message = mail to: @user.email, subject: subject + message.perform_deliveries = @user.mail_notification? && !@user.retired? + + message + end + # required params: sender, receiver def signed_up(args = {}) @sender ||= args[:sender] diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index f3853b75630..6b94ae64e3f 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -68,12 +68,4 @@ def create_page subject = "[FBC] #{@page.user.login_name}さんがDocsに#{@page.title}を投稿しました。" mail to: @user.email, subject: subject end - - # required params: question, receiver - def no_correct_answer - @user = @receiver - @notification = @user.notifications.find_by(link: "/questions/#{@question.id}", kind: Notification.kinds[:no_correct_answer]) - subject = "[FBC] #{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。" - mail to: @user.email, subject: subject - end end diff --git a/app/models/notification_facade.rb b/app/models/notification_facade.rb index d4ab724248c..cc34d28b23c 100644 --- a/app/models/notification_facade.rb +++ b/app/models/notification_facade.rb @@ -30,14 +30,4 @@ def self.trainee_report(report, receiver) def self.coming_soon_regular_events(today_events, tomorrow_events) DiscordNotifier.with(today_events: today_events, tomorrow_events: tomorrow_events).coming_soon_regular_events.notify_now end - - def self.no_correct_answer(question, receiver) - ActivityNotifier.with(question: question, receiver: receiver).no_correct_answer.notify_now - return unless receiver.mail_notification? && !receiver.retired? - - NotificationMailer.with( - question: question, - receiver: receiver - ).no_correct_answer.deliver_later(wait: 5) - end end diff --git a/app/models/question.rb b/app/models/question.rb index 617d694a141..d08cf1ba982 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -39,7 +39,7 @@ def notify_certain_period_passed_after_last_answer return if Question.not_solved_and_certain_period_has_passed.blank? Question.not_solved_and_certain_period_has_passed.each do |not_solved_question| - NotificationFacade.no_correct_answer(not_solved_question, not_solved_question.user) + ActivityDelivery.with(question: not_solved_question, receiver: not_solved_question.user).notify(:no_correct_answer) end end diff --git a/app/views/activity_mailer/no_correct_answer.html.slim b/app/views/activity_mailer/no_correct_answer.html.slim new file mode 100644 index 00000000000..d9f68aa4e55 --- /dev/null +++ b/app/views/activity_mailer/no_correct_answer.html.slim @@ -0,0 +1 @@ += render '/notification_mailer/notification_mailer_template', title: "#{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。", link_url: @link_url, link_text: "#{@user.login_name}さんの質問へ" diff --git a/app/views/notification_mailer/no_correct_answer.html.slim b/app/views/notification_mailer/no_correct_answer.html.slim deleted file mode 100644 index 38877c95751..00000000000 --- a/app/views/notification_mailer/no_correct_answer.html.slim +++ /dev/null @@ -1 +0,0 @@ -= render 'notification_mailer_template', title: "#{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。", link_url: notification_url(@notification), link_text: "#{@user.login_name}さんの質問へ" From 3dbd80dfdfa5908a63ec48285cfb80cca791e9ff Mon Sep 17 00:00:00 2001 From: tomo Date: Mon, 29 May 2023 21:03:26 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=A1=E3=83=BC=E3=83=AB=E3=81=AE?= =?UTF-8?q?=E3=83=97=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/mailers/previews/activity_mailer_preview.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/mailers/previews/activity_mailer_preview.rb b/test/mailers/previews/activity_mailer_preview.rb index aaed95efe96..83fdd3dc11a 100644 --- a/test/mailers/previews/activity_mailer_preview.rb +++ b/test/mailers/previews/activity_mailer_preview.rb @@ -153,4 +153,11 @@ def chose_correct_answer ActivityMailer.with(answer: answer, receiver: receiver).chose_correct_answer end + + def no_correct_answer + question = Question.find(ActiveRecord::FixtureSet.identify(:question1)) + receiver = User.find(question.user_id) + + ActivityMailer.with(question: question, receiver: receiver).no_correct_answer + end end From 1a4eb717d21877ac3f35a15a73fae35ac10b93a2 Mon Sep 17 00:00:00 2001 From: tomo Date: Mon, 29 May 2023 21:08:59 +0900 Subject: [PATCH 3/4] =?UTF-8?q?activity=5Fdelivery=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/deliveries/activity_delivery_test.rb | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/deliveries/activity_delivery_test.rb b/test/deliveries/activity_delivery_test.rb index 605c4d4ce6b..257867c989a 100644 --- a/test/deliveries/activity_delivery_test.rb +++ b/test/deliveries/activity_delivery_test.rb @@ -368,4 +368,28 @@ class ActivityDeliveryTest < ActiveSupport::TestCase ActivityDelivery.with(**params).notify(:chose_correct_answer) end end + + test '.notify(:no_correct_answer)' do + question = questions(:question1) + params = { + question: question, + receiver: question.user + } + + assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do + ActivityDelivery.notify!(:no_correct_answer, **params) + end + + assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do + ActivityDelivery.notify(:no_correct_answer, **params) + end + + assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do + ActivityDelivery.with(**params).notify!(:no_correct_answer) + end + + assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do + ActivityDelivery.with(**params).notify(:no_correct_answer) + end + end end From 18685d3fba6209afa0867ebfce3f0c879e1b9671 Mon Sep 17 00:00:00 2001 From: tomo Date: Mon, 29 May 2023 21:33:48 +0900 Subject: [PATCH 4/4] =?UTF-8?q?activity=5Fmailer=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/mailers/activity_mailer_test.rb | 34 ++++++++++++++++++++++++ test/mailers/notification_mailer_test.rb | 28 ------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/test/mailers/activity_mailer_test.rb b/test/mailers/activity_mailer_test.rb index b2771479020..334c07bbf69 100644 --- a/test/mailers/activity_mailer_test.rb +++ b/test/mailers/activity_mailer_test.rb @@ -989,4 +989,38 @@ class ActivityMailerTest < ActionMailer::TestCase assert ActionMailer::Base.deliveries.empty? end + + test 'no_correct_answer using synchronous mailer' do + question = questions(:question1) + receiver = question.user + + ActivityMailer.no_correct_answer(question: question, receiver: receiver).deliver_now + + assert_not ActionMailer::Base.deliveries.empty? + email = ActionMailer::Base.deliveries.last + query = CGI.escapeHTML({ kind: Notification.kinds[:no_correct_answer], link: "/questions/#{question.id}" }.to_param) + assert_equal ['noreply@bootcamp.fjord.jp'], email.from + assert_equal [receiver.email], email.to + assert_equal "[FBC] #{receiver.login_name}さんの質問【 #{question.title} 】のベストアンサーがまだ選ばれていません。", email.subject + assert_match(%r{#{receiver.login_name}さんの質問へ}, email.body.to_s) + end + + test 'no_correct_answer with params using asynchronous mailer' do + question = questions(:question1) + receiver = question.user + + mailer = ActivityMailer.with(question: question, receiver: receiver).no_correct_answer + + perform_enqueued_jobs do + mailer.deliver_later + end + + assert_not ActionMailer::Base.deliveries.empty? + email = ActionMailer::Base.deliveries.last + query = CGI.escapeHTML({ kind: Notification.kinds[:no_correct_answer], link: "/questions/#{question.id}" }.to_param) + assert_equal ['noreply@bootcamp.fjord.jp'], email.from + assert_equal [receiver.email], email.to + assert_equal "[FBC] #{receiver.login_name}さんの質問【 #{question.title} 】のベストアンサーがまだ選ばれていません。", email.subject + assert_match(%r{#{receiver.login_name}さんの質問へ}, email.body.to_s) + end end diff --git a/test/mailers/notification_mailer_test.rb b/test/mailers/notification_mailer_test.rb index d3c5eb20be2..cf1798c2da0 100644 --- a/test/mailers/notification_mailer_test.rb +++ b/test/mailers/notification_mailer_test.rb @@ -43,32 +43,4 @@ class NotificationMailerTest < ActionMailer::TestCase assert_equal '[FBC] kensyuさんが日報【 研修の日報 】を書きました!', email.subject assert_match(/日報/, email.body.to_s) end - - test 'no_correct_answer' do - user = users(:kimura) - question = questions(:question8) - Notification.create!( - kind: 22, - sender: user, - user: user, - message: 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。', - link: "/questions/#{question.id}", - read: false - ) - mailer = NotificationMailer.with( - question: question, - receiver: user - ).no_correct_answer - - perform_enqueued_jobs do - mailer.deliver_later - end - - assert_not ActionMailer::Base.deliveries.empty? - email = ActionMailer::Base.deliveries.last - assert_equal ['noreply@bootcamp.fjord.jp'], email.from - assert_equal ['kimura@fjord.jp'], email.to - assert_equal '[FBC] kimuraさんの質問【 テストの質問 】のベストアンサーがまだ選ばれていません。', email.subject - assert_match(/まだ選ばれていません/, email.body.to_s) - end end