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

Conversation

mllocs
Copy link
Collaborator

@mllocs mllocs commented Feb 23, 2018

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


t.timestamps
end
add_index :device_tokens, [:user_id, :token]
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 should be uniq


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.

👏

@@ -0,0 +1,19 @@
class DeviceTokensController < PostsController
Copy link
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
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
Collaborator Author

Choose a reason for hiding this comment

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

waaaaaaat 👓

@sauloperez
Copy link
Collaborator

sauloperez commented Apr 5, 2018

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
Copy link
Collaborator Author

mllocs commented Apr 9, 2018

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