From 6493775aed16571d0ba338c2c04fc331385105ed Mon Sep 17 00:00:00 2001 From: naokinaokiboo Date: Thu, 14 Mar 2024 13:03:20 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E7=94=A8Job=E3=81=AB?= =?UTF-8?q?=E4=BE=8B=E5=A4=96=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/post_announcement_job.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/jobs/post_announcement_job.rb b/app/jobs/post_announcement_job.rb index ed60cefe9b3..5df8f587ad1 100644 --- a/app/jobs/post_announcement_job.rb +++ b/app/jobs/post_announcement_job.rb @@ -5,7 +5,24 @@ class PostAnnouncementJob < ApplicationJob def perform(announcement, receivers) receivers.each do |receiver| - ActivityDelivery.with(announcement:, receiver:).notify!(:post_announcement) + send_mail_notification(announcement, receiver) + send_on_site_notification(announcement, receiver) end end + + private + + def send_mail_notification(announcement, receiver) + ActivityMailer.post_announcement(announcement:, receiver:).deliver_now + rescue Postmark::InactiveRecipientError => e + Rails.logger.warn "[Postmark] 受信者由来のエラーのためメールを送信できませんでした。:#{e.message}" + rescue StandardError => e + Rails.logger.warn(e) + end + + def send_on_site_notification(announcement, receiver) + ActivityNotifier.post_announcement(announcement:, receiver:).notify_now + rescue StandardError => e + Rails.logger.warn(e) + end end From f41755b8e85dd9f020fbecd94e95ca14894faa77 Mon Sep 17 00:00:00 2001 From: naokinaokiboo Date: Thu, 14 Mar 2024 13:08:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?ActivityNotifier=E3=81=A7=E3=81=AE=E3=81=8A?= =?UTF-8?q?=E7=9F=A5=E3=82=89=E3=81=9B=E3=81=AE=E3=82=B5=E3=82=A4=E3=83=88?= =?UTF-8?q?=E5=86=85=E9=80=9A=E7=9F=A5=E3=82=92=E3=82=84=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/post_announcement_job.rb | 9 ++++++++- app/notifiers/activity_notifier.rb | 15 --------------- test/jobs/post_announcement_job_test.rb | 3 +-- test/notifiers/activity_notifier_test.rb | 12 ------------ 4 files changed, 9 insertions(+), 30 deletions(-) diff --git a/app/jobs/post_announcement_job.rb b/app/jobs/post_announcement_job.rb index 5df8f587ad1..05a4807323e 100644 --- a/app/jobs/post_announcement_job.rb +++ b/app/jobs/post_announcement_job.rb @@ -21,7 +21,14 @@ def send_mail_notification(announcement, receiver) end def send_on_site_notification(announcement, receiver) - ActivityNotifier.post_announcement(announcement:, receiver:).notify_now + Notification.create!( + kind: Notification.kinds[:announced], + user: receiver, + sender: announcement.sender, + link: Rails.application.routes.url_helpers.polymorphic_path(announcement), + message: "お知らせ「#{announcement.title}」", + read: false + ) rescue StandardError => e Rails.logger.warn(e) end diff --git a/app/notifiers/activity_notifier.rb b/app/notifiers/activity_notifier.rb index 0c54de1ba5b..b4f679d6439 100644 --- a/app/notifiers/activity_notifier.rb +++ b/app/notifiers/activity_notifier.rb @@ -126,21 +126,6 @@ def first_report(params = {}) ) end - def post_announcement(params = {}) - params.merge!(@params) - announce = params[:announcement] - receiver = params[:receiver] - - notification( - body: "お知らせ「#{announce.title}」", - kind: :announced, - sender: announce.user, - receiver:, - link: Rails.application.routes.url_helpers.polymorphic_path(announce), - read: false - ) - end - def retired(params = {}) params.merge!(@params) sender = params[:sender] diff --git a/test/jobs/post_announcement_job_test.rb b/test/jobs/post_announcement_job_test.rb index c3d31c7a68a..57c855e8dd7 100644 --- a/test/jobs/post_announcement_job_test.rb +++ b/test/jobs/post_announcement_job_test.rb @@ -5,14 +5,13 @@ class PostAnnouncementJobTest < ActiveJob::TestCase teardown do ActionMailer::Base.deliveries.clear - AbstractNotifier::Testing::Driver.deliveries.clear end test '#perform' do announcement = announcements(:announcement1) receivers = [users(:machida), users(:adminonly), users(:sotugyou_with_job)] - assert_difference [-> { ActionMailer::Base.deliveries.count }, -> { AbstractNotifier::Testing::Driver.deliveries.count }], 3 do + assert_difference [-> { ActionMailer::Base.deliveries.count }, -> { Notification.count }], 3 do perform_enqueued_jobs do PostAnnouncementJob.perform_later(announcement, receivers) end diff --git a/test/notifiers/activity_notifier_test.rb b/test/notifiers/activity_notifier_test.rb index 7359a9cc983..fa082caa190 100644 --- a/test/notifiers/activity_notifier_test.rb +++ b/test/notifiers/activity_notifier_test.rb @@ -62,18 +62,6 @@ class ActivityNotifierTest < ActiveSupport::TestCase end end - test '#announcement' do - notification = ActivityNotifier.with(announcement: announcements(:announcement1), receiver: users(:kimura)).post_announcement - - assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do - notification.notify_now - end - - assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do - notification.notify_later - end - end - test '#hibernated' do notification = ActivityNotifier.with(sender: users(:kimura), receiver: users(:komagata)).hibernated