unotifier 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 828e3ced7eb245219ec46c4460658554399522dd3224994b73f325f5ac824b22
4
- data.tar.gz: 29ede3c4f8a0d673088be8372b22aa45df78c275306d629531c95c7fac7233a2
3
+ metadata.gz: 8f1f5e629d8d72498909aa0362e3731b5e1fa8ca2f80db9d0fae1625ff015239
4
+ data.tar.gz: c1965f64e2948c7a9e654d6dd5a7426ee4059bd0a87f98f18ccad02cc50f2011
5
5
  SHA512:
6
- metadata.gz: f82c08b892b61a740c7e28110f3a780fe7db32dd4a463306822e9319d2a407c39923b25e8aae12ea647e4699b1e3dd2a3381ce8b28245bbd7a89be69218b3a3a
7
- data.tar.gz: 678f461b2a396118ce9476e4e8a3eb5c59c67c894d3c3d05db6a36058a5bce7dd9e181a26f1dce786d868db7e02f065ab1c3a9adbb541ae70fc232604ce6c56b
6
+ metadata.gz: cfd76b2dd66a398b8dc75fe85971ee14f716c2df8e9abf0d05d2f1786a9bc583a8e615bea830a168103b8f88934a883ee725a3a283aab028b93efd0c585802f0
7
+ data.tar.gz: 61bbd5b17d968f46efd3685f7d12e6a21ed771c11b24f6ee1ab1c44aabbedfd01eeebf5b04ed9e85fc7c3d71bbe156fa24f97ae777041c715b53fcb10678328d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unotifier (0.4.1)
4
+ unotifier (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -16,6 +16,10 @@ module UNotifier
16
16
 
17
17
  server.broadcast channel_name.call(notification), data
18
18
  end
19
+
20
+ def sensitive?
21
+ false
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -18,6 +18,10 @@ module UNotifier
18
18
  server.broadcast channel_name.call(notification), serialize_notification(notification)
19
19
  end
20
20
 
21
+ def sensitive?
22
+ false
23
+ end
24
+
21
25
  def serialize_notification(notification)
22
26
  {
23
27
  id: notification.id,
@@ -20,6 +20,10 @@ module UNotifier
20
20
 
21
21
  @mailer.public_send(sending_method, notification).deliver
22
22
  end
23
+
24
+ def sensitive?
25
+ true
26
+ end
23
27
  end
24
28
  end
25
29
  end
data/lib/unotifier.rb CHANGED
@@ -94,8 +94,8 @@ module UNotifier
94
94
 
95
95
  notification.save!
96
96
 
97
- UserNotifier.notify_user(notification, params)
98
- SystemNotifier.notify_system(notification, params[:system])
97
+ UserNotifier.call(notification, params)
98
+ SystemNotifier.call(notification, params[:system])
99
99
  end
100
100
 
101
101
  def self.notify_onsite(key, target, params)
@@ -120,6 +120,11 @@ module UNotifier
120
120
  end
121
121
  end
122
122
  end
123
+ .merge("options" => user_settings["options"])
124
+ end
125
+
126
+ def self.hide_sensitive_for?(target)
127
+ !!target.notification_settings.dig("options", "hide_sensitive")
123
128
  end
124
129
 
125
130
  def self.urgency_for(key, params = {})
@@ -1,6 +1,6 @@
1
1
  module UNotifier
2
2
  module SystemNotifier
3
- def self.notify_system(notification, params = {})
3
+ def self.call(notification, params = {})
4
4
  key = notification.key
5
5
  config = UNotifier.load_notification!(key)
6
6
  return if config["system"].nil?
@@ -1,52 +1,53 @@
1
1
  module UNotifier
2
2
  module UserNotifier
3
- def self.notify_user(notification, params = {})
4
- locale_key = UNotifier.locale_key_for(notification.key, params)
3
+ def self.call(notification, params = {})
4
+ config = UNotifier.load_notification!(notification.key)["user"]
5
+ user_settings = _load_user_settings(notification)
6
+ providers = _load_providers(notification, user_settings, params)
5
7
 
6
- user_settings =
7
- notification.target.notification_settings.is_a?(Hash) &&
8
- notification.target.notification_settings.dig("user", notification.key)
9
- user_settings ||= Settings::DEFAULT_URGENCY
8
+ # By default we assume that notification is sensitive
9
+ is_sensitive = !config.has_key?("sensitive") || config["sensitive"]
10
10
 
11
- notify_with = []
11
+ providers.each do |provider|
12
+ is_private = is_sensitive &&
13
+ provider.sensitive? &&
14
+ UNotifier.hide_sensitive_for?(notification.target)
12
15
 
13
- if !params[:external_only] && notify_onsite?(notification, user_settings)
14
- notify_with += UNotifier.configuration.site_providers
15
- end
16
+ if is_private
17
+ notification.title = _translate(provider, notification, "title_private", params)
18
+ notification.body = ""
19
+ else
20
+ notification.title = _translate(provider, notification, "title", params)
21
+ notification.body = _translate(provider, notification, "body", params)
22
+ end
16
23
 
17
- if !params[:onsite_only] && notify_external?(notification, user_settings)
18
- notify_with += UNotifier.configuration.external_providers
24
+ provider.notify(notification)
19
25
  end
26
+ end
20
27
 
21
- notify_with.each do |provider|
22
- title_provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.title"
23
- body_provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.body"
28
+ def self._load_providers(notification, user_settings, params)
29
+ providers = []
24
30
 
25
- if UNotifier.configuration.localization_provider.exists?(title_provider_key)
26
- notification.title =
27
- UNotifier.configuration.localization_provider.t(
28
- title_provider_key, params.merge(locale: notification.target.locale)
29
- )
30
- end
31
-
32
- if UNotifier.configuration.localization_provider.exists?(body_provider_key)
33
- notification.body =
34
- UNotifier.configuration.localization_provider.t(
35
- body_provider_key, params.merge(locale: notification.target.locale, default: "")
36
- )
37
- end
31
+ if !params[:external_only] && _notify_onsite?(notification, user_settings)
32
+ providers += UNotifier.configuration.site_providers
33
+ end
38
34
 
39
- provider.notify(notification)
35
+ if !params[:onsite_only] && _notify_external?(notification, user_settings)
36
+ providers += UNotifier.configuration.external_providers
40
37
  end
41
- end
42
38
 
43
- def self.notify_onsite?(notification, user_settings)
44
- return false if notification.urgency == "optional" && user_settings == "off"
39
+ providers
40
+ end
45
41
 
46
- notification.target.online?
42
+ def self._load_user_settings(notification)
43
+ (
44
+ notification.target.notification_settings.is_a?(Hash) &&
45
+ notification.target.notification_settings.dig("user", notification.key)
46
+ ) ||
47
+ Settings::DEFAULT_URGENCY
47
48
  end
48
49
 
49
- def self.notify_external?(notification, user_settings)
50
+ def self._notify_external?(notification, user_settings)
50
51
  case notification.urgency
51
52
  when "immediate"
52
53
  true
@@ -58,5 +59,30 @@ module UNotifier
58
59
  false
59
60
  end
60
61
  end
62
+
63
+ def self._notify_onsite?(notification, user_settings)
64
+ return false if notification.urgency == "optional" && user_settings == "off"
65
+
66
+ notification.target.online?
67
+ end
68
+
69
+ def self._translate(provider, notification, part, params)
70
+ locale_key = UNotifier.locale_key_for(notification.key, params)
71
+ locale_provider = UNotifier.configuration.localization_provider
72
+
73
+ plain_key = "#{locale_key}.#{part}"
74
+ provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.#{part}"
75
+
76
+ key =
77
+ if locale_provider.exists?(provider_key)
78
+ provider_key
79
+ elsif locale_provider.exists?(plain_key)
80
+ plain_key
81
+ else
82
+ "notifications.default.#{part}"
83
+ end
84
+
85
+ locale_provider.t(key, params.merge(locale: notification.target.locale))
86
+ end
61
87
  end
62
88
  end
@@ -1,3 +1,3 @@
1
1
  module UNotifier
2
- VERSION = "0.4.1".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unotifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mgpnd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2020-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler