Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Push notifications background job for Post #367

Merged
merged 9 commits into from
May 29, 2018

Conversation

enricostano
Copy link
Contributor

@enricostano enricostano commented May 25, 2018

WAT

Whit this PR we add a new step in the PostPersister service while creating or updating a Post: we also enqueue now a CreatePushNotificationsJob.

The background job will retrieve the Event and the Post associated to the event and will create as many PushNotification resources as needed.

In the case of Post it will create a PushNotification for each user in the Post's organization that have a DeviceToken. In future PRs we'll add this feature for Member and Transfer resources.

This PR only creates the PushNotification resources, it doesn't send any push notification to the mobile devices yet. The plan is to add a scheduled background job that will send them in batches.

What to test

The following workflow:

  • a user creates a Post
  • in the DB will be created as many PushNotification records as the people in the organization with a DeviceToken

TODO

  • Setup Redis in Travis CI or just use test engine for ActiveJob

Copy link
Collaborator

@sauloperez sauloperez left a comment

Choose a reason for hiding this comment

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

Looks quite good to me, besides a few small details I'm happy to postpone my only design concern in an upcoming PR when we have a better understanding of the notifications we need to send. I just left it here for the record.


raise 'A valid Event must be provided' unless event

::PushNotifications::Creator.create!(event: event)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need the leading ::? This reference lives in the global namespace so I don't see why PushNotifications::Creator would have any problem finding the constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just used to it, it makes it easier to ruby to find constants. Especially in Rails the constants lookup can be really "kafkian"...

class PushNotification < ActiveRecord::Base
belongs_to :event, foreign_key: 'event_id'

validates_presence_of :event
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 the (latest) validates syntax instead. We don't know how long the old syntax will be maintained.

it { is_expected.to validate_presence_of(:event) }
end

describe 'Relations' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd rather name it association which is the convention from Rails. The way I see it we're are testing it from the AR's perspective.

@@ -0,0 +1,24 @@
module PushNotifications
class EventNotifier
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't follow why we're nesting this PostLogic here? what's the actual reasoning? What confuses me I think is the fact that EventNotifier is both a class and a namespace 🤔

Copy link
Contributor Author

@enricostano enricostano May 28, 2018

Choose a reason for hiding this comment

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

That's correct, I could have put PostLogic class even in the same file where EventNotifier is defined... but I don't like it much. PostLogic basically is a local business domain, only used inside EventNotifier... at least for now...

@@ -0,0 +1,24 @@
module PushNotifications
class EventNotifier
class PostLogic
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 try to find a better name for this. PostLogic is rather vague and as a reader, doesn't tell me much of what to expect from this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't like the name either... but I wanted to move forward... let's think about a better name later.


# Conditions for Post:
# - All the members of the associated organization
# - that have a DeviceToken associated
Copy link
Collaborator

Choose a reason for hiding this comment

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

this second bullet point looks like the continuation of the previous one ⚠️ . It isn't needed, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll rewrite this doc, it's an AND basically...


context 'and the user has a DeviceToken associated' do

before { Fabricate(:device_token, user: user, token: 'aloha') }
Copy link
Collaborator

Choose a reason for hiding this comment

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

the spacing up until here makes it a bit hard to read. I'd simply keep the before's right after their context.

end
end

context 'but the user has not a DeviceToken associated' do
Copy link
Collaborator

@sauloperez sauloperez May 28, 2018

Choose a reason for hiding this comment

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

⚠️ "has no Device Token"


raise 'The resource associated to the Event is not supported'
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

To me, this feels like a Factory. It knows what class to instantiate and but it also delegates #users to the underlying class.

We can avoid this second unnecessary responsibility from EventNotifier by limiting it to return the PostLogic instance. This class is the one that actually implements #users. So something like:

class Creator
  def create!
    event_notifier = EventNotifierFactory.notification_for(event: event)
    event_notifier.users.each do
      PushNotification.create!(event: event)
    end
  end
end

The idea here is that all kinds of event_notifier we come up with conform to the duck-type by implementing #users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we need to play a bit more with the relationship between PostLogic and EventNotifier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Look at PostLogic class as a way to organize things inside EventNotifier business logic. Users outside EventNotifier shouldn't know anything about it, they just ask #users to EventNotifier.

Copy link
Collaborator

@sauloperez sauloperez left a comment

Choose a reason for hiding this comment

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

Looks quite good to me, besides a few small details I'm happy to postpone my only design concern for an upcoming PR when we have a better understanding of the notifications we need to send. I just left it here for the record.

@enricostano enricostano force-pushed the feature/create-push-notifications-job branch 2 times, most recently from 9cff2ec to 4dc7d64 Compare May 28, 2018 08:42
@enricostano enricostano force-pushed the feature/create-push-notifications-job branch 2 times, most recently from b4b4fcc to 4a493c7 Compare May 28, 2018 11:43
@sauloperez
Copy link
Collaborator

Looks very good 👌

@enricostano enricostano force-pushed the feature/create-push-notifications-job branch from ec3dc54 to 0f89488 Compare May 28, 2018 16:25
@enricostano enricostano force-pushed the feature/create-push-notifications-job branch from 0f89488 to 6e64400 Compare May 29, 2018 14:19
@enricostano enricostano force-pushed the feature/create-push-notifications-job branch from b038ab8 to a2d4b8c Compare May 29, 2018 14:52
@enricostano
Copy link
Contributor Author

Tested on staging.

@enricostano enricostano merged commit ec0f47d into develop May 29, 2018
@enricostano enricostano deleted the feature/create-push-notifications-job branch May 29, 2018 16:39
@enricostano enricostano mentioned this pull request Aug 8, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants