Skip to content

Commit

Permalink
notification e-mails send out on changeset creation and application
Browse files Browse the repository at this point in the history
closes #325
closes #326
  • Loading branch information
drewda committed Jan 29, 2016
1 parent c2ae027 commit e8034cb
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 20 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ gem 'database_cleaner', group: :test
gem 'factory_girl_rails', group: [:development, :test]
gem 'ffaker', group: [:development, :test]
gem 'rspec-rails', group: [:development, :test]
gem 'rspec-sidekiq', group: :test
gem 'vcr', group: :test
gem 'webmock', group: :test
gem 'airborne', group: :test
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ GEM
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-sidekiq (2.2.0)
rspec (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.4.1)
rubocop (0.36.0)
parser (>= 2.3.0.0, < 3.0)
Expand Down Expand Up @@ -421,6 +424,7 @@ DEPENDENCIES
redis-rails
rgeo-geojson
rspec-rails
rspec-sidekiq
rubocop
rubocop-rspec
sentry-raven
Expand All @@ -437,4 +441,4 @@ DEPENDENCIES
whenever

BUNDLED WITH
1.10.6
1.11.2
Empty file removed app/mailers/.keep
Empty file.
4 changes: 4 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "transitland@mapzen.com" # TODO: change to a transit.land address
layout 'mailer'
end
13 changes: 13 additions & 0 deletions app/mailers/changeset_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ChangesetMailer < ApplicationMailer
def creation(changeset_id)
@changeset = Changeset.find(changeset_id)
mail to: @changeset.user.email,
subject: "Hello from Transitland. We've received your contribution!"
end

def application(changeset_id)
@changeset = Changeset.find(changeset_id)
mail to: @changeset.user.email,
subject: "Hello from Transitland. We've added your contribution!"
end
end
14 changes: 14 additions & 0 deletions app/models/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def set_user_by_params(user_params)
end

after_initialize :set_default_values
after_create :creation_email

def entities_created_or_updated
# NOTE: this is probably evaluating the SQL queries, rather than merging together ARel relations
Expand Down Expand Up @@ -131,6 +132,11 @@ def apply!
raise Changeset::Error.new(self, $!.message, $!.backtrace)
end
end
unless Figaro.env.send_changeset_emails_to_users.presence == 'false'
if self.user && self.user.email.present? && !self.user.admin
ChangesetMailer.delay.application(self.id)
end
end
# Now that the transaction is complete and has been committed,
# we can do some async tasks like conflate stops with OSM.
if Figaro.env.auto_conflate_stops_with_osm.present? &&
Expand Down Expand Up @@ -167,4 +173,12 @@ def set_default_values
end
end

def creation_email
unless Figaro.env.send_changeset_emails_to_users.presence == 'false'
if self.user && self.user.email.present? && !self.user.admin
ChangesetMailer.delay.creation(self.id)
end
end
end

end
11 changes: 11 additions & 0 deletions app/views/changeset_mailer/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p><strong>Hello <%= @changeset.user.name.presence %></strong></p>

<p>Thank you for submitting a feed to the Transitland Feed Registry!</p>

<p>Your feed is now working its way through the submission process. As soon as the Transitland team reviews your feed, you will see its operators listed in the <a href="https://transit.land/feed-registry/">Feed Registry</a>, and it will be accessible in the <a href="https://transit.land/playground/">Transitland Playground</a> and <a href="https://transit.land/how-it-works/#slide-3">Datastore API</a>.</p>

<p>After that, we will start importing the contents of the feed, which adds the stops, routes, and schedule to the Datastore. In some cases, there are issues in the import process that could delay or make the feed unacceptable for posting.</p>

<p>You will be notified again when your feed has been added to the Feed Registry and the Datastore, or if we encounter any problems along the way.</p>

<p>If you have any questions, you can contact the Transitland team at <a href="mailto:transitland@mapzen.com">transitland@mapzen.com</a>.</p>
11 changes: 11 additions & 0 deletions app/views/changeset_mailer/application.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Hello <%= @changeset.user.name.presence %>

Thank you for submitting a feed to the Transitland Feed Registry!

Your feed is now working its way through the submission process. As soon as the Transitland team reviews your feed, you will see its operators listed in the Feed Registry at https://transit.land/feed-registry/, and it will be accessible in the Transitland Playground and Datastore API.

After that, we will start importing the contents of the feed, which adds the stops, routes, and schedule to the Datastore. In some cases, there are issues in the import process that could delay or make the feed unacceptable for posting.

You will be notified again when your feed has been added to the Feed Registry and the Datastore, or if we encounter any problems along the way.

If you have any questions, you can contact the Transitland team at transitland@mapzen.com
9 changes: 9 additions & 0 deletions app/views/changeset_mailer/creation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p><strong>Hello <%= @changeset.user.name.presence %></strong></p>

<p>Thank you again for submitting a feed to the Transitland Feed Registry! Your feed has been reviewed, and you can now find it in <a href="https://transit.land/feed-registry/">the listings</a>.</p>

<p>We're starting to import the feed into the Transitland Datastore. This will make available the stops, routes, and schedule data that are contained in the feed. This import process may take a few more hours or days. If we run into any problems along the way, we'll be in touch.</p>

<p>Contributions from community members like you help us build Transitland. If you know of other feeds, please submit them, too!</p>

<p>If you have any questions, you can contact the Transitland team at <a href="mailto:transitland@mapzen.com">transitland@mapzen.com</a>.</p>
9 changes: 9 additions & 0 deletions app/views/changeset_mailer/creation.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hello <%= @changeset.user.name.presence %>

Thank you again for submitting a feed to the Transitland Feed Registry! Your feed has been reviewed, and you can now find it in the listings at https://transit.land/feed-registry/

We're starting to import the feed into the Transitland Datastore. This will make available the stops, routes, and schedule data that are contained in the feed. This import process may take a few more hours or days. If we run into any problems along the way, we'll be in touch.

Contributions from community members like you help us build Transitland. If you know of other feeds, please submit them, too!

If you have any questions, you can contact the Transitland team at transitland@mapzen.com
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
1 change: 1 addition & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ key | possible values | default | description
`MAX_HOURS_SINCE_LAST_CONFLATE` | Any real number >= 0 | 84 hours | Stops that were last conflated before this number of hours before the re-conflation check time will be re-conflated.
`FEED_EATER_CHANGE_PAYLOAD_MAX_ENTITIES` | Any integer > 0 | 1,000 | Set the number of entities that FeedEaterWorker and FeedEaterScheduleWorker will put into each changeset
`FEED_EATER_STOP_TIMES_MAX_LOAD` | Any integer > 0 | 100,000 | When FeedEaterWorker spawns FeedEaterScheduleWorkers, this is the number of lines from a GTFS feed's `stop_times.txt` that will be sent to each FeedEaterScheduleWorker job
`SEND_CHANGESET_EMAILS_TO_USERS` | `true`, `false` | `true` | By default, e-mail notifications go out to a changeset's author (as long as the user isn't an admin)
3 changes: 3 additions & 0 deletions spec/mailers/changeset_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe ChangesetMailer do
pending "add some examples to (or delete) #{__FILE__}"
end
4 changes: 4 additions & 0 deletions spec/mailers/previews/changeset_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Preview all emails at http://localhost:3000/rails/mailers/changeset_mailer
class ChangesetMailerPreview < ActionMailer::Preview

end
2 changes: 0 additions & 2 deletions spec/models/change_payload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# index_change_payloads_on_changeset_id (changeset_id)
#

# require 'rails_helper'

RSpec.describe ChangePayload, type: :model do
# Changeset/ChangePayload tests are in changeset_spec.rb

Expand Down
80 changes: 63 additions & 17 deletions spec/models/changeset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,50 @@
#

describe Changeset do
it 'can be created' do
changeset = create(:changeset)
expect(Changeset.exists?(changeset.id)).to be true
end
context 'creation' do
it 'is possible' do
changeset = create(:changeset)
expect(Changeset.exists?(changeset.id)).to be true
end

it 'can be created with an initial payload (compat)' do
payload = {
changes: [
{
action: "createUpdate",
stop: {
onestopId: 's-9q8yt4b-1AvHoS',
name: '1st Ave. & Holloway St.'
it 'is possible with an initial payload (compat)' do
payload = {
changes: [
{
action: "createUpdate",
stop: {
onestopId: 's-9q8yt4b-1AvHoS',
name: '1st Ave. & Holloway St.'
}
}
}
]
}
changeset = build(:changeset, payload: payload)
expect(changeset.change_payloads.count).equal?(1)
]
}
changeset = build(:changeset, payload: payload)
expect(changeset.change_payloads.count).equal?(1)
end

context 'creation e-mail' do
it 'sent to normal user' do
allow(Figaro.env).to receive(:send_changeset_emails_to_users) { 'true' }
user = create(:user)
changeset = create(:changeset, user: user)
expect(ChangesetMailer.instance_method :creation).to be_delayed(changeset.id)
end

it 'not sent to admin user' do
allow(Figaro.env).to receive(:send_changeset_emails_to_users) { 'false' }
user = create(:user, admin: true)
changeset = create(:changeset, user: user)
expect(ChangesetMailer.instance_method :creation).to_not be_delayed(changeset.id)
end

it 'not sent when disabled' do
allow(Figaro.env).to receive(:send_changeset_emails_to_users) { 'false' }
user = create(:user)
changeset = create(:changeset, user: user)
expect(ChangesetMailer.instance_method :creation).to_not be_delayed(changeset.id)
end
end
end

it 'sorts payloads by created_at' do
Expand Down Expand Up @@ -190,6 +215,27 @@
expect(OldOperatorServingStop.first.operator).to eq Operator.find_by_onestop_id!('o-9q8y-SFMTA')
expect(OldOperatorServingStop.first.stop).to eq Stop.find_by_onestop_id!('s-9q8yt4b-1AvHoS')
end

context 'application e-mail' do
it 'sent to normal user' do
@changeset1.user = create(:user)
@changeset1.apply!
expect(ChangesetMailer.instance_method :application).to be_delayed(@changeset1.id)
end

it 'not sent to admin user' do
@changeset1.user = create(:user, admin: true)
@changeset1.apply!
expect(ChangesetMailer.instance_method :application).to_not be_delayed(@changeset1.id)
end

it 'not sent when disabled' do
allow(Figaro.env).to receive(:send_changeset_emails_to_users) { 'false' }
@changeset1.user = create(:user)
@changeset1.apply!
expect(ChangesetMailer.instance_method :application).to_not be_delayed(@changeset1.id)
end
end
end

context 'revert' do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
c.configure_rspec_metadata!
end

RSpec::Sidekiq.configure do |config|
config.warn_when_jobs_not_processed_by_sidekiq = false
end

RSpec.configure do |config|
config.mock_with :rspec

Expand Down

0 comments on commit e8034cb

Please sign in to comment.