module UNotifier module UserNotifier def self.notify_user(notification, params = {}) locale_key = UNotifier.locale_key_for(notification.key, params) user_settings = notification.target.notification_settings.is_a?(Hash) && notification.target.notification_settings.dig("user", notification.key) user_settings ||= Settings::DEFAULT_URGENCY notify_with = [] if !params[:external_only] && notify_onsite?(notification, user_settings) notify_with += UNotifier.configuration.site_providers end if !params[:onsite_only] && notify_external?(notification, user_settings) notify_with += UNotifier.configuration.external_providers end notify_with.each do |provider| title_provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.title" body_provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.body" if UNotifier.configuration.localization_provider.exists?(title_provider_key) notification.title = UNotifier.configuration.localization_provider.t( title_provider_key, params.merge(locale: notification.target.locale) ) end if UNotifier.configuration.localization_provider.exists?(body_provider_key) notification.body = UNotifier.configuration.localization_provider.t( body_provider_key, params.merge(locale: notification.target.locale, default: "") ) end provider.notify(notification) end end def self.notify_onsite?(notification, user_settings) return false if notification.urgency == "optional" && user_settings == "off" notification.target.online? end def self.notify_external?(notification, user_settings) case notification.urgency when "immediate" true when "regular", "optional" user_settings == "external" && !notification.target.online? when "onsite" false else false end end end end