Module: UNotifier::UserNotifier

Defined in:
lib/unotifier/user_notifier.rb

Class Method Summary collapse

Class Method Details

._load_providers(notification, user_settings, params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/unotifier/user_notifier.rb', line 28

def self._load_providers(notification, , params)
  providers = []

  if !params[:external_only] && _notify_onsite?(notification, )
    providers += UNotifier.configuration.site_providers
  end

  if !params[:onsite_only] && _notify_external?(notification, )
    providers += UNotifier.configuration.external_providers
  end

  providers
end

._load_user_settings(notification) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/unotifier/user_notifier.rb', line 42

def self.(notification)
  (
    notification.target.notification_settings.is_a?(Hash) &&
    notification.target.notification_settings.dig("user", notification.key)
  ) ||
    Settings::DEFAULT_URGENCY
end

._notify_external?(notification, user_settings) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/unotifier/user_notifier.rb', line 50

def self._notify_external?(notification, )
  case notification.urgency
  when "immediate"
    true
  when "regular", "optional"
     == "external" && !notification.target.online?
  when "onsite"
    false
  else
    false
  end
end

._notify_onsite?(notification, user_settings) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/unotifier/user_notifier.rb', line 63

def self._notify_onsite?(notification, )
  return false if notification.urgency == "optional" &&  == "off"

  notification.target.online?
end

._translate(provider, notification, part, params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/unotifier/user_notifier.rb', line 69

def self._translate(provider, notification, part, params)
  locale_key = UNotifier.locale_key_for(notification.key, params)
  locale_provider = UNotifier.configuration.localization_provider

  plain_key = "#{locale_key}.#{part}"
  provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.#{part}"

  key =
    if locale_provider.exists?(provider_key)
      provider_key
    elsif locale_provider.exists?(plain_key)
      plain_key
    else
      "notifications.default.#{part}"
    end

  locale_provider.t(key, params.merge(locale: notification.target.locale))
end

.call(notification, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unotifier/user_notifier.rb', line 3

def self.call(notification, params = {})
  config = UNotifier.load_notification!(notification.key)["user"]
   = (notification)
  providers = _load_providers(notification, , params)

  # By default we assume that notification is sensitive
  is_sensitive = !config.has_key?("sensitive") || config["sensitive"]

  providers.each do |provider|
    is_private = is_sensitive &&
                 provider.sensitive? &&
                 UNotifier.hide_sensitive_for?(notification.target)

    if is_private
      notification.title = _translate(provider, notification, "title_private", params)
      notification.body = ""
    else
      notification.title = _translate(provider, notification, "title", params)
      notification.body = _translate(provider, notification, "body", params)
    end

    provider.notify(notification)
  end
end