Skip to content

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 8 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#= require datepicker
#= require give_time
#= require tags
#= require mobile_app_libs

$(document).on 'click', 'a[data-popup]', (event) ->
window.open($(this).attr('href'), 'popup', 'width=600,height=600')
Expand Down
5 changes: 5 additions & 0 deletions app/assets/javascripts/mobile_app_libs.js
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 });
}
19 changes: 19 additions & 0 deletions app/controllers/device_tokens_controller.rb
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
7 changes: 7 additions & 0 deletions app/models/device_token.rb
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
11 changes: 11 additions & 0 deletions app/models/push_notifications/post_notification.rb
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
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class User < ActiveRecord::Base
has_many :posts
has_many :offers
has_many :inquiries
has_many :device_tokens

accepts_nested_attributes_for :members

Expand Down
30 changes: 30 additions & 0 deletions app/services/push_notifications/expo_sender_service.rb
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 app/services/push_notifications/post_broadcaster_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module PushNotifications
class UsersBroadcasterService
Copy link
Collaborator

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.

Copy link
Collaborator Author

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 :)

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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
end

resources :inquiries
resources :device_tokens, only: :create

concern :accountable do
get :give_time, on: :member
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20180221161343_create_device_tokens.rb
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150422162806) do
ActiveRecord::Schema.define(version: 20180221161343) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -54,6 +54,15 @@
t.hstore "name_translations"
end

create_table "device_tokens", force: :cascade do |t|
t.integer "user_id", null: false
t.string "token", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "device_tokens", ["user_id", "token"], name: "index_device_tokens_on_user_id_and_token", unique: true, using: :btree

create_table "documents", force: :cascade do |t|
t.integer "documentable_id"
t.string "documentable_type"
Expand Down
34 changes: 34 additions & 0 deletions spec/controllers/device_tokens_controller_spec.rb
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