-
In v1 we had # app/notifiers/new_post_notifier.rb
#
class NewPostNotifier < ApplicationNotifier
validates :record, presence: true
deliver_by :email do |config|
config.mailer = "NotificationMailer"
config.method = :notify_new_post
config.if = :email_notifications?
end
required_params :discussion, :story
notification_methods do
def message
t(".message", discussion_title: event.discussion.title)
end
def url
story_discussion_path(event.story, event.discussion)
end
end
# @todo save to db if email_notification?
# we send notifications to all participants who have set their preference
# and also to the discussion creator if preference is set
def email_notifications?(notification)
recipient = notification.recipient
# skip when recipient is the post creator
return false if recipient.id == record.user.id
if recipient.id == discussion.user.id # discussion owner
recipient.preference&.notify_new_post_on_discussion
else # any participants in post
recipient.preference&.notify_any_post_in_discussion
end
end
def discussion
params[:discussion]
end
def story
params[:story]
end
end And the model which triggers the above notifier: # app/models/post.rb
#
class Post < ApplicationRecord
belongs_to :discussion
belongs_to :user
has_many :noticed_events, as: :record, dependent: :destroy, class_name: "Noticed::Event"
has_many :notifications, through: :noticed_events, class_name: "Noticed::Notification"
after_create_commit :notify
private
def notify
# @todo somehow use the same logic as NewPostNotifier.email_notifications? before saving to db
participants = (discussion.participants + [discussion.user]).uniq
NewPostNotifier.with(record: self, discussion:, story: discussion.story).deliver_later(participants)
end
end The condition for saving to the database is the same as sending an email notification. It would nice and DRY to not repeat the same logic of Would it make sense to bring back |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I would filter the recipients beforehand if you want to completely skip certain recipients. class Post < ApplicationRecord
def email_notifications?(recipient)
# your filter logic
end
private
def notify
participants = (discussion.participants + [discussion.user]).uniq.select{ |user| email_notifications?(user) }
NewPostNotifier.with(record: self, discussion:, story: discussion.story).deliver_later(participants)
end
end |
Beta Was this translation helpful? Give feedback.
I would filter the recipients beforehand if you want to completely skip certain recipients.