-
Notifications
You must be signed in to change notification settings - Fork 70
Added the device_tokens endpoint #329
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
8 commits
Select commit
Hold shift + click to select a range
89d9a8d
Added device_tokens endpoint
mllocs 749073b
Added JS lib to register a device_token
mllocs 5248f93
Documented and renamed the mobile app js libs
mllocs 38ecee1
Removed unnecessary index
mllocs d3e7bc3
Improved device tokens controller and added specs
mllocs f90d6e4
Refactored the push notifications services
mllocs a8ea1ac
Added uniqueness validation on DeviceToken user/token pair
mllocs 5f22849
Fixed wrong controller parent class for DeviceTokens
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,5 @@ | ||
// Used by the mobile app to register the device token | ||
// https://github.com/coopdevs/timeoverflow-mobile-app | ||
window.TimeOverflowRegisterExpoDeviceToken = function (token) { | ||
$.post('/device_tokens', { token: token }); | ||
} |
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,19 @@ | ||
class DeviceTokensController < ApplicationController | ||
before_filter :authenticate_user! | ||
|
||
def create | ||
@device_token = DeviceToken.new device_token_params.merge! user_id: current_user.id | ||
|
||
if @device_token.save | ||
render nothing: true, status: :created | ||
else | ||
render nothing: true, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def device_token_params | ||
params.permit(:token) | ||
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,7 @@ | ||
class DeviceToken < ActiveRecord::Base | ||
belongs_to :user | ||
|
||
validates :user_id, presence: true | ||
validates :token, presence: true | ||
validates :token, uniqueness: { scope: :user_id } | ||
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,11 @@ | ||
module PushNotifications | ||
class PostNotification | ||
def title | ||
"Notification title" | ||
end | ||
|
||
def body | ||
"Notification body" | ||
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
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,30 @@ | ||
require 'net/http' | ||
|
||
module PushNotifications | ||
class ExpoAdaptorService | ||
def initialize(notification, user) | ||
@notification = notification | ||
@user = user | ||
end | ||
|
||
def run | ||
user.device_tokens.each do |device_token| | ||
# https://docs.expo.io/versions/latest/guides/push-notifications.html | ||
uri = URI('https://exp.host/--/api/v2/push/send') | ||
Net::HTTP.post_form(uri, post_data(device_token.token)) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :notification, :user | ||
|
||
def post_data(token) | ||
{ | ||
"to" => token, | ||
"title" => notification.title, | ||
"body" => notification.body | ||
} | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
app/services/push_notifications/post_broadcaster_service.rb
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,19 @@ | ||
module PushNotifications | ||
class UsersBroadcasterService | ||
def initialize(users) | ||
@users = users | ||
end | ||
|
||
def broadcast | ||
notification = PostNotification.new | ||
|
||
users.each do |user| | ||
ExpoSenderService.new(notification, user).run | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :users | ||
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,11 @@ | ||
class CreateDeviceTokens < ActiveRecord::Migration | ||
def change | ||
create_table :device_tokens do |t| | ||
t.integer :user_id, :null => false | ||
t.string :token, :null => false | ||
|
||
t.timestamps | ||
end | ||
add_index :device_tokens, [:user_id, :token], unique: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 |
||
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,34 @@ | ||
require 'spec_helper' | ||
|
||
describe DeviceTokensController do | ||
let (:organization) { Fabricate(:organization) } | ||
let (:member) { Fabricate(:member, organization: organization) } | ||
|
||
describe 'POST #create' do | ||
context 'without login' do | ||
it 'responds with error' do | ||
expect do | ||
post :create | ||
end.to change(DeviceToken, :count).by(0) | ||
end | ||
end | ||
|
||
context 'with valid params' do | ||
it 'creates a new device_token' do | ||
login(member.user) | ||
|
||
expect do | ||
post :create, token: 'xxx' | ||
end.to change(DeviceToken, :count).by(1) | ||
end | ||
end | ||
|
||
context 'with invalid params' do | ||
it 'responds with error' do | ||
login(member.user) | ||
post :create | ||
expect(response.status).to eq(422) | ||
end | ||
end | ||
end | ||
end |
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.
you're still working on it, right? because I don't see this class used.
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.
this class will be used once we implement the logic behind sending push notifications to users :)