noticed 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +249 -192
- data/app/models/concerns/noticed/readable.rb +16 -0
- data/app/models/noticed/notification.rb +0 -2
- data/lib/generators/noticed/templates/delivery_method.rb.tt +4 -8
- data/lib/noticed/delivery_method.rb +1 -0
- data/lib/noticed/delivery_methods/email.rb +10 -6
- data/lib/noticed/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75c02b0c74747522809dcb6d841016c48b91d8b5c6f637efb3fbd68729babb59
|
4
|
+
data.tar.gz: 01f8b8a9c4f185c48729a7c073915194c4e872f8f412ed1a3c94b763f7134ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a5e8023362194fbdb682faa7f608101e54c67df6916e7a5faac7252852196618518d405f233720d7fb843b6a67e4757a29da189b9dd7c084cca135b3caf1226
|
7
|
+
data.tar.gz: e475367cb48916b2fa796076b057c3eb215a9046cf286f958b11b8c99f294feec5b646b21ce5def9c0208c3293a34cbc434ecc831d4f7e1b7fc51659fc9f3512
|
data/README.md
CHANGED
@@ -1,19 +1,26 @@
|
|
1
|
-
# Noticed
|
1
|
+
# Noticed
|
2
|
+
|
2
3
|
### 🎉 Notifications for your Ruby on Rails app.
|
3
4
|
|
4
5
|
[![Build Status](https://github.com/excid3/noticed/workflows/Tests/badge.svg)](https://github.com/excid3/noticed/actions) [![Gem Version](https://badge.fury.io/rb/noticed.svg)](https://badge.fury.io/rb/noticed)
|
5
6
|
|
6
|
-
|
7
|
+
**⚠️⚠️ Upgrading from V1? Read the [Upgrade Guide](https://github.com/excid3/noticed/blob/main/UPGRADE.md)!**
|
8
|
+
|
9
|
+
Noticed is a gem that allows your application to send notifications of varying types, over various mediums, to various recipients. Be it a Slack notification to your own team when some internal event occurs or a notification to your user, sent as a text message, email, and real-time UI element in the browser, Noticed supports all of the above (at the same time)!
|
10
|
+
|
11
|
+
Noticed implements two top-level types of delivery methods:
|
12
|
+
|
13
|
+
1. Individual Deliveries: Where each recipient gets their own notification
|
7
14
|
|
8
|
-
The
|
15
|
+
Let’s use a car dealership as an example here. When someone purchases a car, a notification will be sent to the buyer with some contract details (“Congrats on your new 2024 XYZ Model...”), another to the car sales-person with different details (“You closed X deal; your commission is Y”), and another to the bank handling the loan with financial details (“New loan issued; amount $20,000...”). The event (the car being sold) necessitates multiple notifications being sent out to different recipients, but each contains its own unique information and should be separate from the others. These are individual deliveries.
|
9
16
|
|
10
|
-
|
11
|
-
2. `Noticed::Event` - When a `Notifier` is delivered, a `Noticed::Event` record is created in the database to store params for the delivery.`Notifiers` are ActiveRecord objects inherited from `Noticed::Event` using Single Table Inheritance.
|
12
|
-
3. `Noticed::Notification` - Keeps track of each recipient for `Noticed::Event` and the seen & read status for each.
|
13
|
-
4. Delivery methods are ActiveJob instances and support the same features like wait, queue, and priority.
|
17
|
+
2. Bulk Deliveries - one notification for all recipients. This is useful for sending a notification to your Slack team, for example.
|
14
18
|
|
15
|
-
|
16
|
-
|
19
|
+
Let’s continue with the car-sale example here. Consider that your development team created the car-sales application that processed the deal above and sent out the notifications to the three parties. For the sake of team morale and feeling the ‘wins’, you may want to implement a notification that notifies your internal development team whenever a car sells through your platform. In this case, you’ll be notifying many people (your development team, maybe others at your company) but with the same content (“someone just bought a car through our platform!”). This is a bulk delivery. It’s generally a single notification that many people just need to be made aware of.
|
20
|
+
|
21
|
+
Bulk deliveries are typically used to push notifications to other platforms where users are managed (Slack, Discord, etc.) instead of your own.
|
22
|
+
|
23
|
+
Delivery methods we officially support:
|
17
24
|
|
18
25
|
* [ActionCable](docs/delivery_methods/action_cable.md)
|
19
26
|
* [Apple Push Notification Service](docs/delivery_methods/ios.md)
|
@@ -25,7 +32,7 @@ Individual Delivery methods (one notification to each recipient):
|
|
25
32
|
* [Vonage SMS](docs/delivery_methods/vonage_sms.md)
|
26
33
|
* [Test](docs/delivery_methods/test.md)
|
27
34
|
|
28
|
-
Bulk delivery methods
|
35
|
+
Bulk delivery methods we support:
|
29
36
|
|
30
37
|
* [Discord](docs/bulk_delivery_methods/discord.md)
|
31
38
|
* [Slack](docs/bulk_delivery_methods/slack.md)
|
@@ -33,9 +40,9 @@ Bulk delivery methods (one notification for all recipients):
|
|
33
40
|
|
34
41
|
## 🎬 Screencast
|
35
42
|
|
36
|
-
<a href="https://www.youtube.com/watch?v=
|
43
|
+
<a href="https://www.youtube.com/watch?v=SzX-aBEqnAc"><img src="https://i.imgur.com/UvVKWwD.png" title="How to add Notifications to Rails with Noticed" width="50%" /></a>
|
37
44
|
|
38
|
-
[Watch Screencast](https://www.youtube.com/watch?v=
|
45
|
+
[Watch Screencast](https://www.youtube.com/watch?v=SzX-aBEqnAc)
|
39
46
|
|
40
47
|
## 🚀 Installation
|
41
48
|
Run the following command to add Noticed to your Gemfile:
|
@@ -44,7 +51,7 @@ Run the following command to add Noticed to your Gemfile:
|
|
44
51
|
bundle add "noticed"
|
45
52
|
```
|
46
53
|
|
47
|
-
|
54
|
+
Generate then run the migrations:
|
48
55
|
|
49
56
|
```bash
|
50
57
|
rails noticed:install:migrations
|
@@ -53,124 +60,160 @@ rails db:migrate
|
|
53
60
|
|
54
61
|
## 📝 Usage
|
55
62
|
|
56
|
-
|
63
|
+
Noticed operates with a few constructs: Notifiers, delivery methods, and Notification records.
|
57
64
|
|
58
|
-
|
65
|
+
To start, generate a Notifier:
|
59
66
|
|
60
|
-
|
61
|
-
|
67
|
+
```sh
|
68
|
+
rails generate noticed:notifier NewCommentNotifier
|
69
|
+
```
|
70
|
+
|
71
|
+
#### Notifier Objects
|
72
|
+
|
73
|
+
Notifiers are essentially the controllers of the Noticed ecosystem and represent an Event. As such, we recommend naming them with the event they model in mind — be it a `NewSaleNotifier,` `ChargeFailureNotifier`, etc.
|
74
|
+
|
75
|
+
Notifiers must inherit from `Noticed::Event`. This provides all of their functionality.
|
76
|
+
|
77
|
+
A Notifier exists to declare the various delivery methods that should be used for that event _and_ any notification helper methods necessary in those delivery mechanisms. In this example we’ll deliver by `:action_cable` to provide real-time UI updates to users’ browsers, `:email` if they’ve opted into email notifications, and a bulk notification to `:discord` to tell everyone on the Discord server there’s been a new comment.
|
62
78
|
|
63
79
|
```ruby
|
64
|
-
# app/notifiers/
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
config.
|
80
|
+
# ~/app/notifiers/new_comment_notifier.rb
|
81
|
+
|
82
|
+
class NewCommentNotifier < Noticed::Event
|
83
|
+
deliver_by :action_cable do |config|
|
84
|
+
config.channel = "NotificationsChannel"
|
85
|
+
config.stream = :some_stream
|
69
86
|
end
|
70
87
|
|
71
88
|
deliver_by :email do |config|
|
72
|
-
config.mailer = "
|
73
|
-
|
89
|
+
config.mailer = "CommentMailer"
|
90
|
+
config.if = ->(recipient) { !!recipient.preferences[:email] }
|
91
|
+
end
|
92
|
+
|
93
|
+
bulk_deliver_by :discord do |config|
|
94
|
+
config.url = "https://discord.com/xyz/xyz/123"
|
95
|
+
config.json = -> {
|
96
|
+
{
|
97
|
+
message: message,
|
98
|
+
channel: :general
|
99
|
+
}
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
notification_methods do
|
104
|
+
# I18n helpers
|
105
|
+
def message
|
106
|
+
t(".message")
|
107
|
+
end
|
108
|
+
|
109
|
+
# URL helpers are accessible in notifications
|
110
|
+
# Don't forget to set your default_url_options so Rails knows how to generate urls
|
111
|
+
def url
|
112
|
+
user_post_path(recipient, params[:post])
|
113
|
+
end
|
74
114
|
end
|
75
115
|
end
|
76
116
|
```
|
77
117
|
|
78
|
-
|
118
|
+
For deeper specifics on setting up the `:action_cable`, `:email`, and `:discord` (bulk) delivery methods, refer to their docs: [`action_cable`](docs/delivery_methods/action_cable.md), [`email`](docs/delivery_methods/email.md), and [`discord` (bulk)](docs/bulk_delivery_methods/discord.md).
|
79
119
|
|
80
|
-
|
120
|
+
##### Required Params
|
121
|
+
|
122
|
+
While explicit / required parameters are completely optional, Notifiers are able to opt in to required parameters via the `required_params` method:
|
81
123
|
|
82
124
|
```ruby
|
83
|
-
|
84
|
-
|
125
|
+
class CarSaleNotifier < Noticed::Event
|
126
|
+
deliver_by :email { |c| c.mailer = "BranchMailer" }
|
127
|
+
|
128
|
+
# `record` is the Car record, `Branch` is the dealership
|
129
|
+
required_params :record, :branch
|
130
|
+
end
|
85
131
|
```
|
86
132
|
|
87
|
-
|
133
|
+
Which will validate upon any invocation that the specified parameters are present:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
CarSaleNotifier.with(record: Car.last).deliver(Branch.last)
|
137
|
+
#=> Noticed::ValidationError("Param `branch` is required for CarSaleNotifier")
|
88
138
|
|
89
|
-
|
139
|
+
CarSaleNotifier.with(record: Car.last, branch: Branch.last).deliver(Branch.hq)
|
140
|
+
#=> OK
|
141
|
+
```
|
90
142
|
|
91
|
-
After saving, a job will be enqueued for processing this notification and delivering it to all recipients.
|
92
143
|
|
93
|
-
Each delivery method also spawns its own job. This allows you to skip email notifications if the user had already opened a push notification, for example.
|
94
144
|
|
95
|
-
|
145
|
+
##### Helper Methods
|
96
146
|
|
97
|
-
Notifiers
|
147
|
+
Notifiers can implement various helper methods, within a `notification_methods` block, that make it easier to render the resulting notification directly. These helpers can be helpful depending on where and how you choose to render notifications. A common use is rendering a user’s notifications in your web UI as standard ERB. These notification helper methods make that rendering much simpler:
|
98
148
|
|
99
|
-
```
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
config.wait = 5.minutes
|
106
|
-
end
|
107
|
-
end
|
149
|
+
```erb
|
150
|
+
<div>
|
151
|
+
<% @user.notifications.each do |notification| %>
|
152
|
+
<%= link_to notification.message, notification.url %>
|
153
|
+
<% end %>
|
154
|
+
</div>
|
108
155
|
```
|
109
156
|
|
110
|
-
|
157
|
+
On the other hand, if you’re using email delivery, ActionMailer has its own full stack for setting up objects and rendering. Your notification helper methods will always be available from the notification object, but using ActionMailer’s own paradigms may fit better for that particular delivery method. YMMV.
|
111
158
|
|
112
|
-
|
113
|
-
* `unless: :method_name` - Calls `method_name` and cancels delivery method if `true` is returned
|
114
|
-
* `wait:` - Delays the delivery for the given duration of time. Can be an `ActiveSupport::Duration`, Proc / lambda, or Symbol.
|
159
|
+
###### URL Helpers
|
115
160
|
|
116
|
-
|
161
|
+
Rails url helpers are included in Notifiers by default so you have full access to them in your notification helper methods, just like you would in your controllers and views.
|
117
162
|
|
118
|
-
|
163
|
+
_But don't forget_, you'll need to configure `default_url_options` in order for Rails to know what host and port to use when generating URLs.
|
119
164
|
|
120
165
|
```ruby
|
121
|
-
|
122
|
-
|
123
|
-
def message
|
124
|
-
t(".message")
|
125
|
-
end
|
166
|
+
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
|
167
|
+
```
|
126
168
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
post_path(params[:post])
|
131
|
-
end
|
169
|
+
###### Translations
|
170
|
+
|
171
|
+
We've also included Rails’ `translate` and `t` helpers for you to use in your notification helper methods. This also provides an easy way of scoping translations. If the key starts with a period, it will automatically scope the key under `notifiers`, the underscored name of the notifier class, and `notification`. For example:
|
132
172
|
|
133
|
-
|
173
|
+
From the above Notifier...
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
class NewCommentNotifier < Noticed::Event
|
177
|
+
# ...
|
178
|
+
|
134
179
|
notification_methods do
|
135
|
-
def
|
136
|
-
"
|
180
|
+
def message
|
181
|
+
t(".message")
|
137
182
|
end
|
138
183
|
end
|
184
|
+
|
185
|
+
# ...
|
139
186
|
end
|
140
187
|
```
|
141
188
|
|
142
|
-
|
189
|
+
Calling the `message` helper in an ERB view:
|
190
|
+
|
143
191
|
```erb
|
144
|
-
<%=
|
145
|
-
<%= link_to notification.personalized_welcome, notification.event.url %>
|
146
|
-
<% end %>
|
192
|
+
<%= @user.notifications.last.message %>
|
147
193
|
```
|
148
194
|
|
149
|
-
|
195
|
+
Will look for the following translation path:
|
150
196
|
|
151
|
-
|
197
|
+
```yml
|
198
|
+
# ~/config/locales/en.yml
|
152
199
|
|
153
|
-
|
154
|
-
|
200
|
+
en:
|
201
|
+
notifiers:
|
202
|
+
new_comment_notifier:
|
203
|
+
notification:
|
204
|
+
message: "Someone posted a new comment!"
|
155
205
|
```
|
156
206
|
|
157
|
-
|
207
|
+
Or, if you have your Notifier within another module, such as `Admin::NewCommentNotifier`, the resulting lookup path will be `en.notifiers.admin.new_comment_notifier.notification.message` (modules become nesting steps).
|
158
208
|
|
159
|
-
|
160
|
-
|
161
|
-
For example:
|
162
|
-
|
163
|
-
`t(".message")` looks up `en.notifiers.new_comment.message`
|
164
|
-
`t(".message") # in Admin::NewComment` looks up `en.notifiers.admin.new_comment.message`
|
165
|
-
|
166
|
-
##### User Preferences
|
209
|
+
##### Tip: Capture User Preferences
|
167
210
|
|
168
211
|
You can use the `if:` and `unless: ` options on your delivery methods to check the user's preferences and skip processing if they have disabled that type of notification.
|
169
212
|
|
170
213
|
For example:
|
171
214
|
|
172
215
|
```ruby
|
173
|
-
class CommentNotifier < Noticed::
|
216
|
+
class CommentNotifier < Noticed::Event
|
174
217
|
deliver_by :email do |config|
|
175
218
|
config.mailer = 'CommentMailer'
|
176
219
|
config.method = :new_comment
|
@@ -179,182 +222,198 @@ class CommentNotifier < Noticed::Base
|
|
179
222
|
end
|
180
223
|
```
|
181
224
|
|
182
|
-
|
183
|
-
|
184
|
-
### Creating a notification from an Active Record callback
|
225
|
+
**Shared Delivery Method Options**
|
185
226
|
|
186
|
-
|
227
|
+
Each of these options are available for every delivery method (individual or bulk). The value passed may be a lambda, a symbol that represents a callable method, a symbol value, or a string value.
|
187
228
|
|
188
|
-
|
189
|
-
|
190
|
-
|
229
|
+
* `config.if` — Intended for a lambda or method; runs after the `wait` if configured; cancels the delivery method if returns falsey
|
230
|
+
* `config.unless` — Intended for a lambda or method; runs after the `wait` if configured; cancels the delivery method if returns truthy
|
231
|
+
* `config.wait` — (Should yield an `ActiveSupport::Duration`) Delays the job that runs this delivery method for the given duration of time
|
232
|
+
* `config.wait_until` — (Should yield a specific time object) Delays the job that runs this delivery method until the specific time specified
|
233
|
+
* `config.queue` — Sets the ActiveJob queue name to be used for the job that runs this delivery method
|
191
234
|
|
192
|
-
|
235
|
+
#### Sending Notifications
|
193
236
|
|
194
|
-
|
237
|
+
Following the `NewCommentNotifier` example above, here’s how we might invoke the Notifier to send notifications to every author in the thread about a new comment being added:
|
195
238
|
|
196
|
-
|
197
|
-
|
198
|
-
end
|
239
|
+
```ruby
|
240
|
+
NewCommentNotifier.with(record: @comment, foo: "bar").deliver(@comment.thread.all_authors)
|
199
241
|
```
|
200
242
|
|
201
|
-
|
243
|
+
This instantiates a new `NewCommentNotifier` with params (similar to ActiveJob, any serializable params are permitted), then delivers notifications to all authors in the thread.
|
202
244
|
|
203
|
-
|
245
|
+
✨ The `record:` param is a special param that gets assigned to the `record` polymorphic association in the database. You should try to set the `record:` param where possible. This may be best understood as ‘the record/object this notification is _about_’, and allows for future queries from the record-side: “give me all notifications that were generated from this comment”.
|
204
246
|
|
205
|
-
|
247
|
+
This invocation will create a single `Noticed::Event` record and a `Noticed::Notification` record for each recipient. A background job will then process the Event and fire off a separate background job for each bulk delivery method _and_ each recipient + individual-delivery-method combination. In this case, that’d be the following jobs kicked off from this event:
|
206
248
|
|
207
|
-
|
249
|
+
- A bulk delivery job for `:discord` bulk delivery
|
250
|
+
- An individual delivery job for `:action_cable` method to the first thread author
|
251
|
+
- An individual delivery job for `:email` method to the first thread author
|
252
|
+
- An individual delivery job for `:action_cable` method to the second thread author
|
253
|
+
- An individual delivery job for `:email` method to the second thread author
|
254
|
+
- Etc...
|
208
255
|
|
209
|
-
|
256
|
+
## ✅ Best Practices
|
210
257
|
|
211
|
-
|
258
|
+
### Renaming Notifiers
|
212
259
|
|
213
|
-
|
214
|
-
Noticed::Event.where(type: YourNotifierClassName.name) # good
|
215
|
-
Noticed::Event.where(type: "YourNotifierClassName") # bad
|
216
|
-
```
|
260
|
+
If you rename a Notifier class your existing data and Noticed setup may break. This is because Noticed serializes the class name and sets it to the `type` column on the `Noticed::Event` record and the `type` column on the `Noticed::Notification` record.
|
217
261
|
|
218
|
-
When renaming a
|
262
|
+
When renaming a Notifier class you will need to backfill existing Events and Notifications to reference the new name.
|
219
263
|
|
220
264
|
```ruby
|
221
265
|
Noticed::Event.where(type: "OldNotifierClassName").update_all(type: NewNotifierClassName.name)
|
222
|
-
|
266
|
+
# and
|
267
|
+
Noticed::Notification.where(type: "OldNotifierClassName::Notification").update_all(type: "#{NewNotifierClassName.name}::Notification")
|
223
268
|
```
|
224
269
|
|
225
270
|
## 🚛 Delivery Methods
|
226
271
|
|
227
|
-
The delivery methods are modular so you can customize the way each type gets delivered.
|
272
|
+
The delivery methods are designed to be modular so you can customize the way each type gets delivered.
|
228
273
|
|
229
274
|
For example, emails will require a subject, body, and email address while an SMS requires a phone number and simple message. You can define the formats for each of these in your Notifier and the delivery method will handle the processing of it.
|
230
275
|
|
231
|
-
|
276
|
+
Individual delivery methods:
|
232
277
|
|
233
|
-
|
278
|
+
* [ActionCable](docs/delivery_methods/action_cable.md)
|
279
|
+
* [Apple Push Notification Service](docs/delivery_methods/ios.md)
|
280
|
+
* [Email](docs/delivery_methods/email.md)
|
281
|
+
* [Firebase Cloud Messaging](docs/delivery_methods/fcm.md) (iOS, Android, and web clients)
|
282
|
+
* [Microsoft Teams](docs/delivery_methods/microsoft_teams.md)
|
283
|
+
* [Slack](docs/delivery_methods/slack.md)
|
284
|
+
* [Twilio Messaging](docs/delivery_methods/twilio_messaging.md) - SMS, Whatsapp
|
285
|
+
* [Vonage SMS](docs/delivery_methods/vonage_sms.md)
|
286
|
+
* [Test](docs/delivery_methods/test.md)
|
234
287
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
288
|
+
Bulk delivery methods:
|
289
|
+
|
290
|
+
* [Discord](docs/bulk_delivery_methods/discord.md)
|
291
|
+
* [Slack](docs/bulk_delivery_methods/slack.md)
|
292
|
+
* [Webhook](docs/bulk_delivery_methods/webhook.md)
|
293
|
+
|
294
|
+
### No Delivery Methods
|
295
|
+
|
296
|
+
It’s worth pointing out that you can have a fully-functional and useful Notifier that has _no_ delivery methods. This means that invoking the Notifier and ‘sending’ the notification will only create new database records (no external surfaces like email, sms, etc.). This is still useful as it’s the database records that allow your app to render a user’s (or other object’s) notifications in your web UI.
|
297
|
+
|
298
|
+
So even with no delivery methods set, this example is still perfectly available and helpful:
|
299
|
+
|
300
|
+
```erb
|
301
|
+
<div>
|
302
|
+
<% @user.notifications.each do |notification| %>
|
303
|
+
<%= link_to notification.message, notification.url %>
|
304
|
+
<% end %>
|
305
|
+
</div>
|
240
306
|
```
|
241
307
|
|
242
|
-
|
308
|
+
Sending a notification is entirely an internal-to-your-app function. Delivery methods just get the word out! But many apps may be fully satisfied without that extra layer.
|
309
|
+
|
310
|
+
### Fallback Notifications
|
243
311
|
|
244
|
-
You can
|
312
|
+
A common pattern is to deliver a notification via a real (or real-ish)-time service, then, after some time has passed, email the user if they have not yet read the notification. You can implement this functionality by combining multiple delivery methods, the `wait` option, and the conditional `if` / `unless` option.
|
245
313
|
|
246
314
|
```ruby
|
247
|
-
class
|
248
|
-
deliver_by :
|
249
|
-
deliver_by :
|
250
|
-
|
251
|
-
|
315
|
+
class NewCommentNotifier< Noticed::Event
|
316
|
+
deliver_by :action_cable
|
317
|
+
deliver_by :email do |config|
|
318
|
+
config.mailer = "CommentMailer"
|
319
|
+
config.wait = 15.minutes
|
320
|
+
config.unless = -> { read? }
|
321
|
+
end
|
252
322
|
end
|
253
323
|
```
|
254
324
|
|
255
|
-
|
325
|
+
Here a notification will be created immediately in the database (for display directly in your app’s web interface) and sent via ActionCable. If the notification has not been marked `read` after 15 minutes, the email notification will be sent. If the notification has already been read in the app, the email will be skipped.
|
256
326
|
|
257
|
-
|
258
|
-
- Immediately issues a ping in Slack.
|
259
|
-
- If the notification remains unread after 10 minutes, it emails the team.
|
260
|
-
- If the notification remains unread after 20 minutes, it sends an SMS to the on-call phone.
|
327
|
+
_A note here: notifications expose a `#mark_as_read` method, but your app must choose when and where to call that method._
|
261
328
|
|
262
329
|
You can mix and match the options and delivery methods to suit your application specific needs.
|
263
330
|
|
264
|
-
Please note that to implement this pattern, it is essential `deliver_by :database` is one among the different delivery methods specified. Without this, a database record of the notification will not be created.
|
265
|
-
|
266
331
|
### 🚚 Custom Delivery Methods
|
267
332
|
|
268
|
-
To generate a custom delivery method, simply run
|
333
|
+
If you want to build your own delivery method to deliver notifications to a specific service or medium that Noticed doesn’t (or doesn’t _yet_) support, you’re welcome to do so! To generate a custom delivery method, simply run
|
269
334
|
|
270
335
|
`rails generate noticed:delivery_method Discord`
|
271
336
|
|
272
|
-
This will generate a new `DeliveryMethods::Discord` class inside the `app/
|
337
|
+
This will generate a new `DeliveryMethods::Discord` class inside the `app/notifiers/delivery_methods` folder, which can be used to deliver notifications to Discord.
|
273
338
|
|
274
339
|
```ruby
|
275
|
-
class DeliveryMethods::Discord < Noticed::
|
340
|
+
class DeliveryMethods::Discord < Noticed::DeliveryMethod
|
341
|
+
# Specify the config options your delivery method requires in its config block
|
342
|
+
required_options # :foo, :bar
|
343
|
+
|
276
344
|
def deliver
|
277
|
-
# Logic for sending
|
345
|
+
# Logic for sending the notification
|
278
346
|
end
|
279
347
|
end
|
348
|
+
|
280
349
|
```
|
281
350
|
|
282
351
|
You can use the custom delivery method thus created by adding a `deliver_by` line with a unique name and `class` option in your notification class.
|
283
352
|
|
284
353
|
```ruby
|
285
|
-
class MyNotifier < Noticed::
|
354
|
+
class MyNotifier < Noticed::Event
|
286
355
|
deliver_by :discord, class: "DeliveryMethods::Discord"
|
287
356
|
end
|
288
357
|
```
|
289
358
|
|
290
359
|
Delivery methods have access to the following methods and attributes:
|
291
360
|
|
292
|
-
* `
|
293
|
-
* `
|
294
|
-
* `
|
295
|
-
* `
|
296
|
-
|
297
|
-
|
361
|
+
* `event` — The `Noticed::Event` record that spawned the notification object currently being delivered
|
362
|
+
* `record` — The object originally passed into the Notifier as the `record:` param (see the ✨ note above)
|
363
|
+
* `notification` — The `Noticed::Notification` instance being delivered. All notification helper methods are available on this object
|
364
|
+
* `recipient` — The individual recipient object being delivered to for this notification (remember that each recipient gets their own instance of the Delivery Method `#deliver`)
|
365
|
+
* `config` — The hash of configuration options declared by the Notifier that generated this notification and delivery
|
366
|
+
* `params` — The parameters given to the Notifier in the invocation (via `.with()`)
|
298
367
|
|
299
|
-
|
368
|
+
#### Validating config options passed to Custom Delivery methods
|
300
369
|
|
301
|
-
|
370
|
+
The presence of delivery method config options are automatically validated when declaring them with the `required_options` method. In the following example, Noticed will ensure that any Notifier using `deliver_by :email` will specify the `mailer` and `method` config keys:
|
302
371
|
|
303
372
|
```ruby
|
304
|
-
class DeliveryMethods::
|
305
|
-
|
373
|
+
class DeliveryMethods::Email < Noticed::DeliveryMethod
|
374
|
+
required_options :mailer, :method
|
306
375
|
|
307
376
|
def deliver
|
308
|
-
#
|
309
|
-
|
310
|
-
|
311
|
-
def self.validate!(delivery_method_options)
|
312
|
-
super # Don't forget to call super, otherwise option presence won't be validated
|
313
|
-
|
314
|
-
# Custom validations
|
315
|
-
if delivery_method_options[:username].blank?
|
316
|
-
raise Noticed::ValidationError, 'the `username` option must be present'
|
317
|
-
end
|
377
|
+
# ...
|
378
|
+
method = config.method
|
318
379
|
end
|
319
380
|
end
|
320
|
-
|
321
|
-
class CommentNotifier < Noticed::Base
|
322
|
-
deliver_by :discord, class: 'DeliveryMethods::Discord'
|
323
|
-
end
|
324
381
|
```
|
325
382
|
|
326
|
-
|
327
|
-
|
328
|
-
To fix the error, the argument has to be passed correctly. For example:
|
383
|
+
If you’d like your config options to support dynamic resolution (set `config.foo` to a lambda or symbol of a method name etc.), you can use `evaluate_option`:
|
329
384
|
|
330
385
|
```ruby
|
331
|
-
class
|
332
|
-
deliver_by :
|
386
|
+
class NewSaleNotifier < Noticed::Event
|
387
|
+
deliver_by :whats_app do |config|
|
388
|
+
config.day = -> { is_tuesday? "Tuesday" : "Not Tuesday" }
|
389
|
+
end
|
333
390
|
end
|
334
|
-
```
|
335
391
|
|
336
|
-
|
392
|
+
class DeliveryMethods::WhatsApp < Noticed::DeliveryMethod
|
393
|
+
required_options :day
|
337
394
|
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
after_deliver do
|
343
|
-
# Do whatever you want
|
395
|
+
def deliver
|
396
|
+
# ...
|
397
|
+
config.day #=> #<Proc:0x000f7c8 (lambda)>
|
398
|
+
evaluate_option(config.day) #=> "Tuesday"
|
344
399
|
end
|
345
400
|
end
|
346
401
|
```
|
347
402
|
|
348
403
|
### 📦 Database Model
|
349
404
|
|
350
|
-
The
|
405
|
+
The Noticed database models include several helpful features to make working with notifications easier.
|
351
406
|
|
352
|
-
####
|
407
|
+
#### Notification
|
408
|
+
|
409
|
+
##### Class methods/scopes
|
410
|
+
|
411
|
+
(Assuming your user `has_many :notifications, as: :recipient, class_name: "Noticed::Notification"`)
|
353
412
|
|
354
413
|
Sorting notifications by newest first:
|
355
414
|
|
356
415
|
```ruby
|
357
|
-
user.notifications.newest_first
|
416
|
+
@user.notifications.newest_first
|
358
417
|
```
|
359
418
|
|
360
419
|
Query for read or unread notifications:
|
@@ -383,7 +442,9 @@ Convert back into a Noticed notifier object:
|
|
383
442
|
Mark notification as read / unread:
|
384
443
|
|
385
444
|
```ruby
|
445
|
+
@notification.mark_as_read
|
386
446
|
@notification.mark_as_read!
|
447
|
+
@notification.mark_as_unread
|
387
448
|
@notification.mark_as_unread!
|
388
449
|
```
|
389
450
|
|
@@ -396,41 +457,36 @@ Check if read / unread:
|
|
396
457
|
|
397
458
|
#### Associating Notifications
|
398
459
|
|
399
|
-
Adding notification associations to your models makes querying and
|
460
|
+
Adding notification associations to your models makes querying, rendering, and managing notifications easy (and is a pretty critical feature of most applications).
|
400
461
|
|
401
|
-
|
462
|
+
There are two ways to associate your models to notifications:
|
402
463
|
|
403
|
-
|
464
|
+
1. Where your object `has_many` notifications as the recipient (who you sent the notification to)
|
465
|
+
2. Where your object `has_many` notifications as the `record` (what the notifications were about)
|
404
466
|
|
405
|
-
|
406
|
-
2. Notifications where the record is in the notification params
|
467
|
+
In the former, we’ll use a `has_many` to `:notifications`. In the latter, we’ll actually `has_many` to `:events`, since `record`s generate notifiable _events_ (and events generate notifications).
|
407
468
|
|
408
|
-
|
469
|
+
We can illustrate that in the following:
|
409
470
|
|
410
471
|
```ruby
|
411
|
-
class
|
412
|
-
|
413
|
-
|
472
|
+
class User < ApplicationRecord
|
473
|
+
has_many :notifications, as: :recipient, dependent: :destroy, class_name: "Noticed::Notification"
|
474
|
+
end
|
414
475
|
|
415
|
-
|
416
|
-
|
476
|
+
# All of the notifications the user has been sent
|
477
|
+
# @user.notifications.each { |n| render(n) }
|
417
478
|
|
418
|
-
|
419
|
-
|
479
|
+
class Post < ApplicationRecord
|
480
|
+
has_many :noticed_events, as: :record, dependent: :destroy, class_name: "Noticed::Event"
|
420
481
|
end
|
421
482
|
|
422
|
-
#
|
423
|
-
|
424
|
-
# Lookup Notifications where params: {post: @post}
|
425
|
-
@post.notifications_as_post
|
426
|
-
|
427
|
-
CommentNotifier.with(parent: @post).deliver(user)
|
428
|
-
@post.notifications_as_parent
|
483
|
+
# All of the notification events this post generated
|
484
|
+
# @post.noticed_events.each { |ne| ne.notifications... }
|
429
485
|
```
|
430
486
|
|
431
487
|
#### Handling Deleted Records
|
432
488
|
|
433
|
-
|
489
|
+
Generally we recommend using a `dependent: ___` relationship on your models to avoid cases where Noticed Events or Notifications are left lingering when your models are destroyed. In the case that they are or data becomes mis-matched, you’ll likely run into deserialization issues. That may be globally alleviated with the following snippet, but use with caution.
|
434
490
|
|
435
491
|
```ruby
|
436
492
|
class ApplicationJob < ActiveJob::Base
|
@@ -452,3 +508,4 @@ DATABASE_URL=postgres://127.0.0.1/noticed_test rails test
|
|
452
508
|
|
453
509
|
## 📝 License
|
454
510
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
511
|
+
|
@@ -31,18 +31,34 @@ module Noticed
|
|
31
31
|
update(read_at: Time.current)
|
32
32
|
end
|
33
33
|
|
34
|
+
def mark_as_read!
|
35
|
+
update!(read_at: Time.current)
|
36
|
+
end
|
37
|
+
|
34
38
|
def mark_as_unread
|
35
39
|
update(read_at: nil)
|
36
40
|
end
|
37
41
|
|
42
|
+
def mark_as_unread!
|
43
|
+
update!(read_at: nil)
|
44
|
+
end
|
45
|
+
|
38
46
|
def mark_as_seen
|
39
47
|
update(seen_at: Time.current)
|
40
48
|
end
|
41
49
|
|
50
|
+
def mark_as_seen!
|
51
|
+
update!(seen_at: Time.current)
|
52
|
+
end
|
53
|
+
|
42
54
|
def mark_as_unseen
|
43
55
|
update(seen_at: nil)
|
44
56
|
end
|
45
57
|
|
58
|
+
def mark_as_unseen!
|
59
|
+
update!(seen_at: nil)
|
60
|
+
end
|
61
|
+
|
46
62
|
def read?
|
47
63
|
read_at?
|
48
64
|
end
|
@@ -1,12 +1,8 @@
|
|
1
|
-
class DeliveryMethods::<%= class_name %> < Noticed::
|
1
|
+
class DeliveryMethods::<%= class_name %> < Noticed::DeliveryMethod
|
2
|
+
# Specify the config options your delivery method requires in its config block
|
3
|
+
required_options # :foo, :bar
|
4
|
+
|
2
5
|
def deliver
|
3
6
|
# Logic for sending the notification
|
4
7
|
end
|
5
|
-
|
6
|
-
# You may override this method to validate options for the delivery method
|
7
|
-
# Invalid options should raise a ValidationError
|
8
|
-
#
|
9
|
-
# def self.validate!(options)
|
10
|
-
# raise ValidationError, "required_option missing" unless options[:required_option]
|
11
|
-
# end
|
12
8
|
end
|
@@ -6,14 +6,18 @@ module Noticed
|
|
6
6
|
def deliver
|
7
7
|
mailer = fetch_constant(:mailer)
|
8
8
|
email = evaluate_option(:method)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
mail = mailer.with(params)
|
13
|
-
mail = args.present? ? mail.send(email, *args) : mail.send(email)
|
14
|
-
|
9
|
+
args = evaluate_option(:args) || []
|
10
|
+
mail = mailer.with(params).send(email, *args)
|
15
11
|
(!!evaluate_option(:enqueue)) ? mail.deliver_later : mail.deliver_now
|
16
12
|
end
|
13
|
+
|
14
|
+
def params
|
15
|
+
(evaluate_option(:params) || notification&.params || {}).merge(
|
16
|
+
notification: notification,
|
17
|
+
record: notification&.record,
|
18
|
+
recipient: notification&.recipient
|
19
|
+
)
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
data/lib/noticed/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: noticed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|