-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #377 from coopdevs/feature/scheduled-jobs
Scheduled job to send push notifications for Post resource
- Loading branch information
Showing
21 changed files
with
322 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.