tolliver 1.0.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +71 -13
- data/app/models/tolliver/notification.rb +0 -1
- data/app/models/tolliver/notification_attachment.rb +16 -0
- data/app/models/tolliver/notification_delivery.rb +1 -3
- data/app/models/tolliver/notification_receiver.rb +0 -2
- data/app/models/tolliver/notification_template.rb +0 -1
- data/app/views/tolliver/notification_mailer/notify.html.erb +1 -0
- data/app/views/tolliver/notification_mailer/notify.text.erb +1 -0
- data/config/locales/en.yml +1 -1
- data/db/migrate/20160121093817_create_notifications.rb +1 -12
- data/db/migrate/20160121094838_create_notification_receivers.rb +6 -4
- data/db/migrate/20160509144238_create_notification_templates.rb +4 -4
- data/db/migrate/20170630190600_create_notification_deliveries.rb +10 -3
- data/db/migrate/20201027150000_create_notification_attachments.rb +17 -0
- data/db/migrate/20210415120000_add_notification_attachments_url.rb +6 -0
- data/lib/tolliver.rb +66 -54
- data/lib/tolliver/errors/bad_request.rb +17 -0
- data/lib/tolliver/errors/not_found.rb +17 -0
- data/lib/tolliver/errors/standard_error.rb +17 -0
- data/lib/tolliver/jobs/batch_policy_job.rb +19 -0
- data/lib/tolliver/jobs/delivery_job.rb +18 -0
- data/lib/tolliver/mailers/notification_mailer.rb +10 -13
- data/lib/tolliver/models/notification.rb +11 -64
- data/lib/tolliver/models/notification_attachment.rb +47 -0
- data/lib/tolliver/models/notification_delivery.rb +32 -40
- data/lib/tolliver/models/notification_receiver.rb +3 -25
- data/lib/tolliver/models/notification_template.rb +6 -10
- data/lib/tolliver/services/delivery_service.rb +63 -0
- data/lib/tolliver/services/methods/email.rb +55 -0
- data/lib/tolliver/services/methods/email/mailgun.rb +55 -0
- data/lib/tolliver/services/{delivery.rb → methods/email/smtp.rb} +11 -13
- data/lib/tolliver/services/methods/sms.rb +54 -0
- data/lib/tolliver/services/methods/sms/plivo.rb +50 -0
- data/lib/tolliver/services/notification_service.rb +179 -0
- data/lib/tolliver/services/policies/batch.rb +82 -0
- data/lib/tolliver/services/policies/instantly.rb +50 -0
- metadata +27 -16
- data/app/views/notification_mailer/notify.html.erb +0 -1
- data/app/views/notification_mailer/notify.text.erb +0 -1
- data/lib/tolliver/models/notification_delivery/batch.rb +0 -77
- data/lib/tolliver/models/notification_delivery/instantly.rb +0 -55
- data/lib/tolliver/models/notification_receiver/email.rb +0 -46
- data/lib/tolliver/models/notification_receiver/sms.rb +0 -45
- data/lib/tolliver/services/notification.rb +0 -234
- data/lib/tolliver/services/sms/plivo.rb +0 -49
@@ -1,234 +0,0 @@
|
|
1
|
-
# *****************************************************************************
|
2
|
-
# * Copyright (c) 2019 Matěj Outlý
|
3
|
-
# *****************************************************************************
|
4
|
-
# *
|
5
|
-
# * Notification service
|
6
|
-
# *
|
7
|
-
# * Author: Matěj Outlý
|
8
|
-
# * Date : 19. 4. 2017
|
9
|
-
# *
|
10
|
-
# *****************************************************************************
|
11
|
-
|
12
|
-
module Tolliver
|
13
|
-
module Services
|
14
|
-
module Notification
|
15
|
-
extend ActiveSupport::Concern
|
16
|
-
|
17
|
-
module ClassMethods
|
18
|
-
|
19
|
-
def notify(content, receivers, options = {})
|
20
|
-
|
21
|
-
# Object
|
22
|
-
notification = Tolliver.notification_model.new
|
23
|
-
|
24
|
-
Tolliver.notification_model.transaction do
|
25
|
-
|
26
|
-
# Get subject, message and params
|
27
|
-
subject, message, params, notification_template = parse_content(content)
|
28
|
-
|
29
|
-
if !message.blank?
|
30
|
-
|
31
|
-
# Interpret params and store it in DB
|
32
|
-
notification.message = interpret_params(message, params)
|
33
|
-
notification.subject = interpret_params(subject, params)
|
34
|
-
|
35
|
-
# Notification template
|
36
|
-
notification.notification_template = notification_template
|
37
|
-
|
38
|
-
# Kind
|
39
|
-
if options[:kind]
|
40
|
-
notification.kind = options[:kind]
|
41
|
-
end
|
42
|
-
|
43
|
-
# Sender
|
44
|
-
if options[:sender]
|
45
|
-
notification.sender = options[:sender]
|
46
|
-
end
|
47
|
-
|
48
|
-
# URL
|
49
|
-
if options[:url]
|
50
|
-
notification.url = options[:url]
|
51
|
-
end
|
52
|
-
|
53
|
-
# Attachment
|
54
|
-
if options[:attachment]
|
55
|
-
notification.attachment = options[:attachment]
|
56
|
-
end
|
57
|
-
|
58
|
-
# Signature
|
59
|
-
if !options[:signature].blank?
|
60
|
-
notification.message += options[:signature].to_s
|
61
|
-
end
|
62
|
-
|
63
|
-
# Save to DB
|
64
|
-
notification.save
|
65
|
-
|
66
|
-
if !notification_template || notification_template.dry != true
|
67
|
-
|
68
|
-
# Delivery kinds
|
69
|
-
if options[:delivery_kinds] # Select only wanted delivery kinds, but valid according to module config
|
70
|
-
delivery_kinds = options[:delivery_kinds]
|
71
|
-
delivery_kinds = delivery_kinds.delete_if { |delivery_kind| !Tolliver.delivery_kinds.include?(delivery_kind.to_sym) }
|
72
|
-
else
|
73
|
-
delivery_kinds = Tolliver.delivery_kinds
|
74
|
-
end
|
75
|
-
|
76
|
-
# Get valid receivers
|
77
|
-
receivers = parse_receivers(receivers)
|
78
|
-
|
79
|
-
delivery_kinds.each do |delivery_kind|
|
80
|
-
|
81
|
-
# Delivery object
|
82
|
-
notification_delivery = notification.notification_deliveries.create(kind: delivery_kind)
|
83
|
-
|
84
|
-
# Filter out receivers not valid for this delivery kind
|
85
|
-
if delivery_kind == :email
|
86
|
-
filtered_receivers = receivers.delete_if { |receiver| !receiver.respond_to?(:email) }
|
87
|
-
elsif delivery_kind == :sms
|
88
|
-
filtered_receivers = receivers.delete_if { |receiver| !receiver.respond_to?(:phone) }
|
89
|
-
else
|
90
|
-
filtered_receivers = receivers.dup
|
91
|
-
end
|
92
|
-
|
93
|
-
# Save to DB
|
94
|
-
filtered_receivers.each do |receiver|
|
95
|
-
notification_delivery.notification_receivers.create(receiver: receiver)
|
96
|
-
end
|
97
|
-
notification_delivery.sent_count = 0
|
98
|
-
notification_delivery.receivers_count = filtered_receivers.size
|
99
|
-
notification_delivery.save
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
else
|
106
|
-
notification = nil # Do not create notification with empty message
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
# Enqueue for delivery
|
112
|
-
notification.enqueue_for_delivery if !notification.nil?
|
113
|
-
|
114
|
-
return notification
|
115
|
-
end
|
116
|
-
|
117
|
-
def parse_content(content)
|
118
|
-
|
119
|
-
# Arrayize
|
120
|
-
if !content.is_a?(Array)
|
121
|
-
content = [content]
|
122
|
-
end
|
123
|
-
|
124
|
-
# Check for empty
|
125
|
-
if content.length == 0
|
126
|
-
raise "Notification is incorrectly defined."
|
127
|
-
end
|
128
|
-
|
129
|
-
# Extract content definition and params
|
130
|
-
content_def = content.shift
|
131
|
-
|
132
|
-
# Preset
|
133
|
-
params = content
|
134
|
-
subject = nil
|
135
|
-
message = nil
|
136
|
-
notification_template = nil
|
137
|
-
|
138
|
-
if content_def.is_a?(Symbol)
|
139
|
-
|
140
|
-
notification_template = Tolliver.notification_template_model.where(ref: content_def.to_s).first
|
141
|
-
if notification_template.nil?
|
142
|
-
raise "Notification template #{content_def.to_s} not found."
|
143
|
-
end
|
144
|
-
if notification_template.disabled != true
|
145
|
-
message = notification_template.message
|
146
|
-
subject = notification_template.subject
|
147
|
-
end
|
148
|
-
|
149
|
-
elsif content_def.is_a?(Hash) # Defined by hash containing subject and message
|
150
|
-
|
151
|
-
subject = content_def[:subject]
|
152
|
-
message = content_def[:message]
|
153
|
-
|
154
|
-
elsif content_def.is_a?(String)
|
155
|
-
|
156
|
-
message = content_def
|
157
|
-
|
158
|
-
else
|
159
|
-
raise "Notification is incorrectly defined."
|
160
|
-
end
|
161
|
-
|
162
|
-
# First parameter is message (all other parameters are indexed from 1)
|
163
|
-
params.unshift(message)
|
164
|
-
|
165
|
-
return [subject, message, params, notification_template]
|
166
|
-
end
|
167
|
-
|
168
|
-
def parse_receivers(receivers)
|
169
|
-
|
170
|
-
# Arrayize
|
171
|
-
if !receivers.is_a?(Array)
|
172
|
-
receivers = [receivers]
|
173
|
-
end
|
174
|
-
|
175
|
-
# Automatic receivers defined by string
|
176
|
-
new_receivers = []
|
177
|
-
receivers.each do |receiver|
|
178
|
-
if receiver.is_a?(String) || receiver.is_a?(Symbol)
|
179
|
-
if Tolliver.people_selector_model && Tolliver.people_selector_model.respond_to?(:decode_value) && Tolliver.people_selector_model.respond_to?(:people)
|
180
|
-
ref, params = Tolliver.people_selector_model.decode_value(receiver.to_s)
|
181
|
-
new_receivers.concat(Tolliver.people_selector_model.people(ref, params).to_a) # Use people selector to generate receivers
|
182
|
-
end
|
183
|
-
else
|
184
|
-
new_receivers << receiver
|
185
|
-
end
|
186
|
-
end
|
187
|
-
receivers = new_receivers
|
188
|
-
|
189
|
-
# Receivers responding to email or users
|
190
|
-
new_receivers = []
|
191
|
-
receivers.each do |receiver|
|
192
|
-
if receiver.respond_to?(:email)
|
193
|
-
new_receivers << receiver
|
194
|
-
else
|
195
|
-
if receiver.respond_to?(:user)
|
196
|
-
new_receivers << receiver.user
|
197
|
-
else
|
198
|
-
if receiver.respond_to?(:users)
|
199
|
-
new_receivers.concat(receiver.users.to_a)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
receivers = new_receivers
|
205
|
-
|
206
|
-
return receivers
|
207
|
-
end
|
208
|
-
|
209
|
-
#
|
210
|
-
# Interpret params into given text
|
211
|
-
#
|
212
|
-
def interpret_params(text, params)
|
213
|
-
return text.gsub(/%{[^{}]+}/) do |match|
|
214
|
-
|
215
|
-
# Substitude all %1, %2, %3, ... to a form which can be evaluated
|
216
|
-
template_to_eval = match[2..-2].gsub(/%([0-9]+)/, "params[\\1]")
|
217
|
-
|
218
|
-
# Evaluate match
|
219
|
-
begin
|
220
|
-
evaluated_match = eval(template_to_eval)
|
221
|
-
rescue
|
222
|
-
evaluated_match = ""
|
223
|
-
end
|
224
|
-
|
225
|
-
# Result
|
226
|
-
evaluated_match
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
end
|
231
|
-
|
232
|
-
end
|
233
|
-
end
|
234
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# *****************************************************************************
|
2
|
-
# * Copyright (c) 2019 Matěj Outlý
|
3
|
-
# *****************************************************************************
|
4
|
-
# *
|
5
|
-
# * Plivo SMS provider
|
6
|
-
# *
|
7
|
-
# * Author: Matěj Outlý
|
8
|
-
# * Date : 1. 12. 2017
|
9
|
-
# *
|
10
|
-
# *****************************************************************************
|
11
|
-
|
12
|
-
require "plivo"
|
13
|
-
|
14
|
-
module Tolliver
|
15
|
-
module Services
|
16
|
-
module Sms
|
17
|
-
class Plivo
|
18
|
-
|
19
|
-
def initialize(params = {})
|
20
|
-
if params[:auth_id].blank? || params[:auth_token].blank?
|
21
|
-
raise "Please provide Auth ID and Auth Token in provider params."
|
22
|
-
end
|
23
|
-
@auth_id = params[:auth_id]
|
24
|
-
@auth_token = params[:auth_token]
|
25
|
-
@api = ::Plivo::RestAPI.new(@auth_id, @auth_token)
|
26
|
-
end
|
27
|
-
|
28
|
-
def deliver(receiver, message)
|
29
|
-
|
30
|
-
# Check message length.
|
31
|
-
if message.bytesize > 200
|
32
|
-
raise "Message too long."
|
33
|
-
end
|
34
|
-
|
35
|
-
# Request API
|
36
|
-
response = @api.send_message({
|
37
|
-
"src" => Tolliver.sms_sender, # TODO: This should be improved to take sender from number pool and remember number / message mapping
|
38
|
-
"dst" => receiver.to_s,
|
39
|
-
"text" => message.to_s,
|
40
|
-
"method" => "POST"
|
41
|
-
})
|
42
|
-
|
43
|
-
return true
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|