-
Notifications
You must be signed in to change notification settings - Fork 69
Scheduled job to send push notifications for Post resource #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ea38581
Add PushNotification#to
enricostano 8069d51
Add title to PushNotification
enricostano 2e8b31d
Add PushNotification fabricator
enricostano ae9ef90
Add token to DeviceToken fabricator
enricostano cb16c03
Create new PushNotifications with title
enricostano 2352b79
Remove unused services
enricostano 67c4fe8
Add scheduled job to send push notifications
enricostano 13ff3d1
Upgrade elasticsearch gems
enricostano 5c114f4
Flag push_notifications as processed
enricostano 9313474
Set notifications as processed after publish
enricostano 005e683
Revert "Upgrade elasticsearch gems"
enricostano 20eb8cb
Remove Expo client gem
enricostano 7751766
Use standard ruby net http instead of Expo client
enricostano e247110
#send is a word reserved to Ruby... :boom
enricostano 2698747
Fix notifications broadcast
enricostano d94684e
Use Post title as notification title
enricostano fee1aac
Fixed migrations with defaults
mllocs bcb8684
Refactor PushNotificatiosn::Creator with a Base class
mllocs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class SendPushNotificationsJob < ActiveJob::Base | ||
queue_as :cron | ||
|
||
def perform | ||
push_notifications = PushNotification.where(processed_at: nil).limit(100) | ||
|
||
::PushNotifications::Broadcast.new( | ||
push_notifications: push_notifications | ||
).send_notifications | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module PushNotifications | ||
class Broadcast | ||
class PostError < ::StandardError; end | ||
|
||
def initialize(push_notifications:) | ||
@push_notifications = push_notifications | ||
end | ||
|
||
# https://docs.expo.io/versions/latest/guides/push-notifications.html | ||
def send_notifications | ||
return unless push_notifications.any? | ||
|
||
response = client.post( | ||
uri.request_uri, | ||
notifications.to_json, | ||
headers | ||
) | ||
|
||
unless response.is_a? Net::HTTPOK | ||
raise PostError, "HTTP response: #{response.code}, #{response.body}" | ||
end | ||
|
||
now = Time.now.utc | ||
push_notifications.update_all(processed_at: now, updated_at: now) | ||
end | ||
|
||
private | ||
|
||
attr_reader :push_notifications | ||
|
||
def notifications | ||
push_notifications.map do |push_notification| | ||
{ | ||
to: push_notification.token, | ||
title: push_notification.title, | ||
body: push_notification.body, | ||
data: push_notification.data | ||
} | ||
end | ||
end | ||
|
||
def client | ||
https = Net::HTTP.new(uri.host, uri.port) | ||
https.use_ssl = true | ||
https | ||
end | ||
|
||
def uri | ||
URI('https://exp.host/--/api/v2/push/send') | ||
end | ||
|
||
def headers | ||
{ | ||
"Content-Type" => "application/json" | ||
} | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module PushNotifications | ||
module Creator | ||
class Base | ||
# Given an Event it will create as many PushNotification resources | ||
# necessary as the resource associated to the Event will require. | ||
# | ||
# @param [Hash] event: <Event> | ||
def initialize(event:) | ||
@event = event | ||
end | ||
|
||
def create! | ||
event_notifier = EventNotifierFactory.new(event: event).build | ||
event_notifier.device_tokens.each do |device_token| | ||
PushNotification.create!( | ||
event: event, | ||
device_token: device_token, | ||
title: title, | ||
body: body, | ||
data: data | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_accessor :event | ||
|
||
def title | ||
raise 'implement the private method `title`' | ||
end | ||
|
||
def body | ||
raise 'implement the private method `body`' | ||
end | ||
|
||
def data | ||
raise 'implement the private method `data`' | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module PushNotifications | ||
module Creator | ||
class Post < Base | ||
private | ||
|
||
def title | ||
event.post.title | ||
end | ||
|
||
def body | ||
event.post.description&.truncate(20) || 'No description' | ||
end | ||
|
||
def data | ||
if event.post.class.to_s == 'Offer' | ||
{ url: Rails.application.routes.url_helpers.offer_path(event.post) } | ||
elsif event.post.class.to_s == 'Inquiry' | ||
{ url: Rails.application.routes.url_helpers.inquiry_path(event.post) } | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
app/services/push_notifications/post_broadcaster_service.rb
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
send_push_notifications_job: | ||
cron: '*/5 * * * *' | ||
class: 'SendPushNotificationsJob' | ||
queue: cron |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddTitleToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :title, :string, null: false, default: '' | ||
end | ||
|
||
def down | ||
remove_column :push_notifications, :title | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddBodyToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :body, :string, null: false, default: '' | ||
end | ||
|
||
def down | ||
remove_column :push_notifications, :body | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddDataToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :data, :json, null: false, default: '{}' | ||
end | ||
|
||
def down | ||
remove_column :push_notifications, :data | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Fabricator(:device_token) do | ||
token 'token' | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fabricator(:push_notification) do | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SendPushNotificationsJob, type: :job do | ||
describe '#perform' do | ||
let(:user) { Fabricate(:user) } | ||
let(:device_token) { Fabricate(:device_token, user: user) } | ||
let(:post) { Fabricate(:post) } | ||
let(:event_created) { Fabricate(:event, post: post, action: :created) } | ||
let(:event_updated) { Fabricate(:event, post: post, action: :updated) } | ||
let(:push_notification) do | ||
Fabricate( | ||
:push_notification, | ||
event: event_created, | ||
device_token: device_token, | ||
title: 'A new Post hase been created.' | ||
) | ||
end | ||
let(:processed_push_notification) do | ||
Fabricate( | ||
:push_notification, | ||
event: event_updated, | ||
device_token: device_token, | ||
title: 'A new Post hase been created.', | ||
processed_at: Time.zone.now | ||
) | ||
end | ||
|
||
before do | ||
push_notification | ||
processed_push_notification | ||
end | ||
|
||
it 'calls Broadcast to send the notifications' do | ||
broadcast = instance_double(::PushNotifications::Broadcast) | ||
expect(::PushNotifications::Broadcast).to receive(:new) | ||
.with(push_notifications: [push_notification]) | ||
.and_return(broadcast) | ||
expect(broadcast).to receive(:send_notifications) | ||
|
||
described_class.new.perform | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remind me the syntax?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's every 5 minutes, but we still have to take a decision about this.