scinote-web/app/notifications/base_notification.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class BaseNotification < Noticed::Base
deliver_by :database, if: :database_notification?
2023-11-29 18:09:10 +08:00
deliver_by :email, mailer: 'AppMailer', method: :general_notification, if: :email_notification?
def self.send_notifications(params, later: true)
recipients_class =
2023-11-24 21:58:46 +08:00
"Recipients::#{NotificationExtends::NOTIFICATIONS_TYPES[subtype || params[:type]][:recipients_module]}".constantize
recipients_class.new(params).recipients.each do |recipient|
if later
with(params).deliver_later(recipient)
else
with(params).deliver(recipient)
end
end
end
2023-11-24 21:58:46 +08:00
def self.subtype; end
def subtype
2023-11-24 21:58:46 +08:00
self.class.subtype || params[:type]
end
def subject; end
2023-12-11 21:32:22 +08:00
def message
params[:message]
end
def title
params[:title]
end
private
def database_notification?
2023-12-12 21:08:42 +08:00
# always save all notifications,
# but flag if they should display in app or not
params[:hide_in_app] = recipient.notifications_settings.dig(notification_subgroup.to_s, 'in_app') != true
true
end
def email_notification?
recipient.notifications_settings.dig(notification_subgroup.to_s, 'email')
end
def notification_subgroup
NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge).find do |_sg, n|
n.include?(subtype.to_sym)
end[0]
end
end