docbrown/app/services/notifications/moderation/send.rb
rhymes 4bf323bc83
Skip moderation notification if the user is the moderator (#6200)
* Skip moderation notification if the user is the moderator

* Update spec/workers/notifications/moderation_notification_worker_spec.rb

Co-Authored-By: Vaidehi Joshi <vaidehi.sj@gmail.com>

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
2020-02-20 14:09:43 -05:00

45 lines
1.3 KiB
Ruby

# Send notifications from moderation
module Notifications
module Moderation
class Send
def initialize(moderator, notifiable)
@moderator = moderator
@notifiable = notifiable
end
def self.call(*args)
new(*args).call
end
delegate :user_data, :comment_data, to: Notifications
def call
# notifiable is currently only comment
return unless notifiable_supported?(notifiable)
# do not create the notification if the comment was created by the moderator
return if moderator == notifiable.user
json_data = { user: user_data(User.dev_account) }
json_data[notifiable.class.name.downcase] = public_send "#{notifiable.class.name.downcase}_data", notifiable
new_notification = Notification.create!(
user_id: moderator.id,
notifiable_id: notifiable.id,
notifiable_type: notifiable.class.name,
action: "Moderation",
json_data: json_data,
)
moderator.update_column(:last_moderation_notification, Time.current)
new_notification
end
private
attr_reader :notifiable, :moderator
def notifiable_supported?(notifiable)
SUPPORTED.include?(notifiable.class)
end
end
end
end