Skip to content

Commit

Permalink
Merge pull request #5768 from fjordllc/feature/notify-when-regular-ev…
Browse files Browse the repository at this point in the history
…ents-change

定期イベントの内容が更新されたら参加者に通知する
  • Loading branch information
komagata authored Nov 16, 2022
2 parents 0bb1a43 + 1348fd1 commit 59a3456
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/regular_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def edit; end
def update
set_wip
if @regular_event.update(regular_event_params)
Newspaper.publish(:regular_event_update, @regular_event)
redirect_to @regular_event, notice: notice_message(@regular_event)
else
render :edit
Expand Down
8 changes: 8 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NotificationMailer < ApplicationMailer # rubocop:disable Metrics/ClassLeng
@sender = params[:sender]
@event = params[:event]
@page = params[:page]
@regular_event = params[:regular_event]
end

# required params: comment, receiver, message
Expand Down Expand Up @@ -185,4 +186,11 @@ def signed_up
subject = "[FBC] #{@sender.login_name}さん#{roles}が新しく入会しました!"
mail to: @user.email, subject: subject
end

def update_regular_event
@user = @receiver
@notification = @user.notifications.find_by(link: "/regular_events/#{@regular_event.id}", kind: Notification.kinds[:regular_event_updated])
subject = "[FBC] 定期イベント【#{@regular_event.title}】が更新されました。"
mail to: @user.email, subject: subject
end
end
3 changes: 2 additions & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Notification < ApplicationRecord
product_update: 17,
graduated: 18,
hibernated: 19,
signed_up: 20
signed_up: 20,
regular_event_updated: 21
}

scope :unreads, -> { where(read: false) }
Expand Down
10 changes: 10 additions & 0 deletions app/models/notification_facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,14 @@ def self.signed_up(sender, receiver)
receiver: receiver
).signed_up.deliver_later(wait: 5)
end

def self.update_regular_event(regular_event, receiver)
ActivityNotifier.with(regular_event: regular_event, receiver: receiver).update_regular_event.notify_now
return unless receiver.mail_notification? && !receiver.retired?

NotificationMailer.with(
regular_event: regular_event,
receiver: receiver
).update_regular_event.deliver_later(wait: 5)
end
end
10 changes: 10 additions & 0 deletions app/models/regular_event_update_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class RegularEventUpdateNotifier
def call(regular_event)
participants = regular_event.participants
participants.each do |target|
NotificationFacade.update_regular_event(regular_event, target) if regular_event.user != target
end
end
end
15 changes: 15 additions & 0 deletions app/notifiers/activity_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,19 @@ def watching_notification(params = {})
read: false
)
end

def update_regular_event(params = {})
params.merge!(@params)
regular_event = params[:regular_event]
receiver = params[:receiver]

notification(
body: "定期イベント【#{regular_event.title}】が更新されました。",
kind: :regular_event_updated,
receiver: receiver,
sender: regular_event.user,
link: Rails.application.routes.url_helpers.polymorphic_path(regular_event),
read: false
)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render 'notification_mailer_template', title: "定期イベント【#{@regular_event.title}】の詳細が更新されました。", link_url: notification_url(@notification), link_text: '定期イベント詳細へ'
1 change: 1 addition & 0 deletions config/initializers/newspaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
Newspaper.subscribe(:correct_answer_save, CorrectAnswerNotifier.new)

Newspaper.subscribe(:user_create, SignUpNotifier.new)
Newspaper.subscribe(:regular_event_update, RegularEventUpdateNotifier.new)
end
8 changes: 8 additions & 0 deletions db/fixtures/notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,11 @@ notification_hibernated:
message: hatsunoさんが休会しました。
link: "/users/<%= ActiveRecord::FixtureSet.identify(:hatsuno) %>"
read: false

notification_regular_event_updated:
kind: 21
user: hatsuno
sender: komagata
message: "[FBC] 定期イベント【開発MTG】が更新されました。"
link: "/regular_events/<%= ActiveRecord::FixtureSet.identify(:regular_event1) %>"
read: false
3 changes: 3 additions & 0 deletions db/fixtures/regular_event_participations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
regular_event_participation1:
user: hatsuno
regular_event: regular_event1
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
talks
regular_events
regular_event_repeat_rules
regular_event_participations
organizers
hibernations
footprints
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,11 @@ notification_graduated:
message: 🎉 sotugyouさんが卒業しました。
link: "/users/<%= ActiveRecord::FixtureSet.identify(:sotugyou) %>"
read: false

notification_regular_event_updated:
kind: 21
user: hatsuno
sender: komagata
message: "[FBC] 定期イベント【開発MTG】が更新されました。"
link: "/regular_events/<%= ActiveRecord::FixtureSet.identify(:regular_event1) %>"
read: false
20 changes: 20 additions & 0 deletions test/mailers/notification_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,24 @@ class NotificationMailerTest < ActionMailer::TestCase
assert_equal '[FBC] kimuraさんが休会しました。', email.subject
assert_match(/休会/, email.body.to_s)
end

test 'update_regular_event' do
regular_event = regular_events(:regular_event1)
notification = notifications(:notification_regular_event_updated)
mailer = NotificationMailer.with(
regular_event: regular_event,
receiver: notification.user
).update_regular_event

perform_enqueued_jobs do
mailer.deliver_later
end

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
assert_equal ['[email protected]'], email.from
assert_equal ['[email protected]'], email.to
assert_equal '[FBC] 定期イベント【開発MTG】が更新されました。', email.subject
assert_match(/定期イベント/, email.body.to_s)
end
end
7 changes: 7 additions & 0 deletions test/mailers/previews/notification_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,11 @@ def hibernated

NotificationMailer.with(sender: sender, receiver: receiver).hibernated
end

def update_regular_event
regular_event = RegularEvent.find(ActiveRecord::FixtureSet.identify(:regular_event1))
receiver = User.find(ActiveRecord::FixtureSet.identify(:hatsuno))

NotificationMailer.with(regular_event: regular_event, receiver: receiver).update_regular_event
end
end
12 changes: 12 additions & 0 deletions test/notifiers/activity_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ class ActivityNotifierTest < ActiveSupport::TestCase
notification.notify_later
end
end

test '#update_regular_event' do
notification = ActivityNotifier.with(regular_event: regular_events(:regular_event1), receiver: users(:hatsuno)).update_regular_event

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
end
29 changes: 29 additions & 0 deletions test/system/notification/regular_events_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'application_system_test_case'

class Notification::ProductsTest < ApplicationSystemTestCase
setup do
@delivery_mode = AbstractNotifier.delivery_mode
AbstractNotifier.delivery_mode = :normal
end

teardown do
AbstractNotifier.delivery_mode = @delivery_mode
end

test 'Notify when a regular event change' do
regular_event = regular_events(:regular_event1)
visit_with_auth "/regular_events/#{regular_event.id}/edit", 'komagata'
within('form[name=regular_event]') do
fill_in('regular_event[description]', with: 'updated')
end
click_button '内容変更'
assert_text '定期イベントを更新しました。'

visit_with_auth '/notifications', 'hatsuno'
within first('.card-list-item.is-unread') do
assert_text "定期イベント【#{regular_event.title}】が更新されました。"
end
end
end

0 comments on commit 59a3456

Please sign in to comment.