Skip to content

Added the device_tokens endpoint - #329

Merged
enricostano merged 8 commits into
developfrom
feature/device-tokens
Apr 11, 2018
Merged

Added the device_tokens endpoint#329
enricostano merged 8 commits into
developfrom
feature/device-tokens

Conversation

@mllocs

@mllocs mllocs commented Feb 23, 2018

Copy link
Copy Markdown
Collaborator

In order to send push notifications we need a place to store the device tokens for each users. In this PR:

  • I'm adding a new table (device_tokens) with a minimal API (just the create action).
  • A couple of services and classes to send push notifications.

The future TimeOverflow mobile app will be implemented in this repo: https://github.com/coopdevs/timeoverflow-mobile-app 👶📱

before_filter :authenticate_user!

def create
@device_token = DeviceToken.all.build device_token_params.merge! user_id: current_user.id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is .all() really needed to instantiate a new DeviceToken?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's enough with a DeviceToken.new(...), isn't it?

@@ -0,0 +1,3 @@
window.TimeOverflowRegisterExpoDeviceToken = function (token) {
$.post('/device_tokens', { token: token });
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe doc the "hack" (:trollface:) somewhere in the wiki and add a link there here?

before_filter :authenticate_user!

def create
@device_token = DeviceToken.all.build device_token_params.merge! user_id: current_user.id

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's enough with a DeviceToken.new(...), isn't it?

{
"to" => token,
"title" => @notification.title,
"body" => @notification.body

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please, use attr_readers for the ivars

def broadcast
notification = PostNotification.new

@post.organization.users.each do |user|

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would decouple this class from the details of the data model by passing the list of users in the constructor instead. This way if we ever change the data model we don't need to touch the push notifications implementation.

t.timestamps
end
add_index :device_tokens, [:user_id, :token]
add_index :device_tokens, [:user_id]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This one is useless due to the left-most thingie. Postgresql can use that one above when only needing user_id

@@ -0,0 +1,19 @@
module PushNotifications
class UsersBroadcasterService

Copy link
Copy Markdown
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
Copy Markdown
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 :)


t.timestamps
end
add_index :device_tokens, [:user_id, :token]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this should be uniq


t.timestamps
end
add_index :device_tokens, [:user_id, :token], unique: true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👏

@@ -0,0 +1,19 @@
class DeviceTokensController < PostsController

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this controller inheriting from PostsController?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

good catch

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

waaaaaaat 👓

@sauloperez

sauloperez commented Apr 5, 2018

Copy link
Copy Markdown
Collaborator

If I understand this correctly we're still missing

A couple of services and classes to send push notifications.

right? In that case I would remove the route from this PR, merge and have an upcoming PR adding the services. It feels like all we can do as a first step for push notifications is done here. Also, it will make it easier to review these services and classes later.

@mllocs

mllocs commented Apr 9, 2018

Copy link
Copy Markdown
Collaborator Author

Yup, I would also merge this, even if we there's nothing that sends notifications. I wouldn't remove the route though, since we could start registering mobile devices.

@enricostano
enricostano merged commit 8ae3a3b into develop Apr 11, 2018
@enricostano
enricostano deleted the feature/device-tokens branch April 11, 2018 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants