Merge branch 'develop' of bitbucket.org:jamkazam/jam-cloud into develop

This commit is contained in:
Seth Call 2014-04-02 00:43:10 +01:00
commit 90eb0bc081
23 changed files with 421 additions and 13 deletions

View File

@ -65,10 +65,11 @@ gem 'logging-rails', :require => 'logging/rails'
gem 'pg_migrate'
gem 'ruby-protocol-buffers', '1.2.2'
gem 'sendgrid', '1.1.0'
gem 'sendgrid', '1.2.0'
gem 'geokit-rails'
gem 'postgres_ext', '1.0.0'
gem 'resque_mailer'
group :libv8 do
gem 'libv8', "~> 3.11.8"
@ -106,6 +107,7 @@ group :development, :test do
gem 'factory_girl_rails', '4.1.0'
gem 'database_cleaner', '0.7.0'
gem 'launchy'
gem 'faker'
end
group :test do

View File

@ -0,0 +1,106 @@
ActiveAdmin.register JamRuby::EmailBatch, :as => 'Batch Emails' do
menu :label => 'Emails'
config.sort_order = 'updated_at DESC'
config.batch_actions = false
# config.clear_action_items!
config.filters = false
form :partial => 'form'
index do
column 'Subject' do |pp| pp.subject end
column 'Updated' do |pp| pp.updated_at end
column 'From' do |pp| pp.from_email end
column 'Status' do |pp| pp.aasm_state end
column 'Test Emails' do |pp| pp.test_emails end
column 'Email Count' do |pp| pp.qualified_count end
column 'Send Count' do |pp| pp.sent_count end
column 'Started' do |pp| pp.started_at end
column 'Completed' do |pp| pp.completed_at end
column 'Send Test' do |pp|
link_to("Test Batch (#{pp.test_count})",
batch_test_admin_batch_email_path(pp.id),
:confirm => "Run test batch with #{pp.test_count} emails?")
end
column 'Send Live' do |pp|
link_to("Live Batch (#{User.email_opt_in.count})",
batch_send_admin_batch_email_path(pp.id),
:confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?")
end
default_actions
end
action_item :only => :show do
link_to("Send Test Batch (#{resource.test_count})",
batch_test_admin_batch_email_path(resource.id),
:confirm => "Run test batch with #{resource.test_count} emails?")
end
action_item :only => :show do
link_to("Send Live Batch (#{User.email_opt_in.count})",
batch_send_admin_batch_email_path(resource.id),
:confirm => "Run LIVE batch with #{User.email_opt_in.count} emails?")
end
show :title => 'Batch Email' do |obj|
panel 'Email Contents' do
attributes_table_for obj do
row 'From' do |obj| obj.from_email end
row 'Test Emails' do |obj| obj.test_emails end
row 'Subject' do |obj| obj.subject end
row 'Body' do |obj| obj.body end
end
end
columns do
column do
panel 'Sending Parameters' do
attributes_table_for obj do
row 'State' do |obj| obj.aasm_state end
row 'User Count' do |obj|
obj.qualified_count ? obj.qualified_count : User.email_opt_in.count
end
row 'Sent Count' do |obj| obj.sent_count end
row 'Started' do |obj| obj.started_at end
row 'Completed' do |obj| obj.completed_at end
row 'Updated' do |obj| obj.updated_at end
end
end
end
column do
panel 'Send Results' do
end
end
end
end
controller do
def create
batch = EmailBatch.create_with_params(params[:jam_ruby_email_batch])
redirect_to admin_batch_email_path(batch.id)
# redirect_to admin_batch_emails_path
end
def update
resource.update_with_conflict_validation(params[:jam_ruby_email_batch])
redirect_to admin_batch_email_path(resource.id)
end
end
member_action :batch_test, :method => :get do
batch = EmailBatch.find(params[:id])
batch.send_test_batch
redirect_to admin_batch_email_path(batch.id)
end
member_action :batch_send, :method => :get do
batch = EmailBatch.find(params[:id])
batch.deliver_batch
redirect_to admin_batch_email_path(batch.id)
end
end

View File

@ -0,0 +1,9 @@
<%= semantic_form_for([:admin, resource], :url => resource.new_record? ? admin_batch_emails_path : "/admin/batch_emails/#{resource.id}") do |f| %>
<%= f.inputs do %>
<%= f.input(:from_email, :label => "From Email", :input_html => {:maxlength => 64}) %>
<%= f.input(:subject, :label => "Subject", :input_html => {:maxlength => 128}) %>
<%= f.input(:test_emails, :label => "Test Emails", :input_html => {:maxlength => 1024, :size => '3x3'}) %>
<%= f.input(:body, :label => "Body", :input_html => {:maxlength => 3096, :size => '10x20'}) %>
<% end %>
<%= f.actions %>
<% end %>

View File

@ -138,4 +138,5 @@ events_social_description.sql
fix_broken_cities.sql
notifications_with_text.sql
notification_seen_at.sql
order_event_session.sql
order_event_session.sql
emails.sql

38
db/up/emails.sql Normal file
View File

@ -0,0 +1,38 @@
CREATE TABLE email_batches (
id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
subject VARCHAR(256) NOT NULL,
body TEXT NOT NULL,
from_email VARCHAR(64) NOT NULL default 'support@jamkazam.com',
aasm_state VARCHAR(32) NOT NULL default 'pending',
test_emails TEXT NOT NULL default '',
qualified_count INTEGER NOT NULL default 0,
sent_count INTEGER NOT NULL default 0,
lock_version INTEGER,
started_at TIMESTAMP,
completed_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- CREATE TABLE email_batch_results (
-- id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
-- email_batch_id VARCHAR(64) REFERENCES email_batches(id) ON DELETE CASCADE,
-- user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
-- error_type VARCHAR(32),
-- email_address VARCHAR(256),
-- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-- );
-- ALTER TABLE email_batch_results ADD CONSTRAINT email_batch_uniqkey UNIQUE (email_batch_id);
-- ALTER TABLE email_batch_results ADD CONSTRAINT email_user_uniqkey UNIQUE (user_id);
ALTER TABLE users ALTER COLUMN subscribe_email SET DEFAULT true;

View File

@ -27,7 +27,7 @@ gem 'eventmachine', '1.0.3'
gem 'amqp', '1.0.2'
gem 'will_paginate'
gem 'actionmailer', '3.2.13'
gem 'sendgrid'
gem 'sendgrid', '1.2.0'
gem 'aws-sdk', '1.29.1'
gem 'carrierwave'
gem 'aasm', '3.0.16'
@ -40,6 +40,7 @@ gem 'resque'
gem 'resque-retry'
gem 'resque-failed-job-mailer' #, :path => "/Users/seth/workspace/resque_failed_job_mailer"
gem 'resque-lonely_job', '~> 1.0.0'
gem 'resque_mailer'
gem 'oj'
gem 'builder'
gem 'fog'

View File

@ -16,6 +16,7 @@ require "geokit-rails"
require "postgres_ext"
require 'builder'
require 'cgi'
require 'resque_mailer'
require "jam_ruby/constants/limits"
require "jam_ruby/constants/notification_types"
@ -132,6 +133,9 @@ require "jam_ruby/models/playable_play"
require "jam_ruby/models/country"
require "jam_ruby/models/region"
require "jam_ruby/models/city"
require "jam_ruby/models/email_batch"
require "jam_ruby/app/mailers/async_mailer"
require "jam_ruby/app/mailers/batch_mailer"
include Jampb

View File

@ -0,0 +1,8 @@
require 'resque_mailer'
module JamRuby
class AsyncMailer < ActionMailer::Base
include SendGrid
include Resque::Mailer
end
end

View File

@ -0,0 +1,38 @@
module JamRuby
class BatchMailer < JamRuby::AsyncMailer
layout "user_mailer"
sendgrid_category :use_subject_lines
sendgrid_unique_args :env => Environment.mode
def _send_batch(batch, users)
@batch_body = batch.body
emails = users.map(&:email)
sendgrid_recipients(emails)
sendgrid_substitute(EmailBatch::VAR_FIRST_NAME, users.map(&:first_name))
batch.did_send(emails)
mail(:to => emails,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
end
def send_batch_email(batch_id, user_ids)
users = User.find_all_by_id(user_ids)
batch = EmailBatch.where(:id => batch_id).limit(1).first
self._send_batch(batch, users)
end
def send_batch_email_test(batch_id)
batch = EmailBatch.where(:id => batch_id).limit(1).first
users = batch.test_users
self._send_batch(batch, users)
end
end
end

View File

@ -0,0 +1 @@
<%= @body %>

View File

@ -0,0 +1 @@
<%= @body %>

View File

@ -0,0 +1 @@
send_batch_email.html.erb

View File

@ -0,0 +1 @@
send_batch_email.text.erb

View File

@ -24,13 +24,11 @@
<tr>
<td align="left"><h1 style="font-size:22px;font-weight:normal;margin-top:0px"><font color="#F34E1C" face="Arial, Helvetica, sans-serif"><%= yield(:title) %></font></h1>
<p><font size="3" color="#AAAAAA" face="Arial, Helvetica, sans-serif"><%= yield %></font></p>
<p><font size="3" color="#AAAAAA" face="Arial, Helvetica, sans-serif"><%= @batch_body ? @batch_body.html_safe : yield %></font></p>
<br>
</td>
</tr>
<% unless @suppress_user_has_account_footer == true %>
<tr>
<td>
@ -39,8 +37,8 @@
<td align="left">
<!-- CALL OUT BOX -->
<p style="margin-top:0px"><font size="2" color="#7FACBA" face="Arial, Helvetica, sans-serif">This email was sent to you because you have an account at <a style="color: #588C98;" href="http://www.jamkazam.com">JamKazam</a>.&nbsp;&nbsp;Click <a style="color: #588C98;" href="http://www.jamkazam.com/client#/account/profile">here to unsubscribe</a> and update your profile settings.
</font></p>
<p style="margin-top:0px"><font size="2" color="#7FACBA" face="Arial, Helvetica, sans-serif">This email was sent to you because you have an account at <a style="color: #588C98;" href="http://www.jamkazam.com">JamKazam</a>.
</td></tr></table>
</td>

View File

@ -1,8 +1,11 @@
<%= yield %>
<% if @batch_body %>
<%= Nokogiri::HTML(@batch_body).text %>
<% else %>
<%= yield %>
<% end %>
<% unless @suppress_user_has_account_footer == true %>
This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com.
This email was sent to you because you have an account at JamKazam / http://www.jamkazam.com. Visit your profile page to unsubscribe: http://www.jamkazam.com/client#/account/profile.
<% end %>
Copyright <%= Time.now.year %> JamKazam, Inc. All rights reserved.

View File

@ -0,0 +1,142 @@
module JamRuby
class EmailBatch < ActiveRecord::Base
self.table_name = "email_batches"
attr_accessible :from_email, :subject, :test_emails, :body
attr_accessible :lock_version, :qualified_count, :sent_count, :started_at, :completed_at
VAR_FIRST_NAME = '@FIRSTNAME'
VAR_LAST_NAME = '@LASTNAME'
DEFAULT_SENDER = "support@jamkazam.com"
include AASM
aasm do
state :pending, :initial => true
state :testing
state :tested
state :batching
state :batched
state :disabled
event :enable do
transitions :from => :disabled, :to => :pending
end
event :reset do
transitions :from => [:disabled, :testing, :tested, :batching, :batched, :pending], :to => :pending
end
event :do_test_run, :before => :running_tests do
transitions :from => [:pending, :tested, :batched], :to => :testing
end
event :did_test_run, :after => :ran_tests do
transitions :from => :testing, :to => :tested
end
event :do_batch_run, :before => :running_batch do
transitions :from => [:tested, :pending, :batched], :to => :batching
end
event :did_batch_run, :after => :ran_batch do
transitions :from => :batching, :to => :batched
end
event :disable do
transitions :from => [:pending, :tested, :batched], :to => :disabled
end
end
# has_many :email_batch_results, :class_name => 'JamRuby::EmailBatchResult'
def self.create_with_params(params)
obj = self.new
params.each { |kk,vv| vv.strip! }
obj.update_with_conflict_validation(params)
obj
end
def deliver_batch
self.perform_event('do_batch_run!')
User.email_opt_in.find_in_batches(batch_size: 1000) do |users|
BatchMailer.send_batch_email(self.id, users.map(&:id)).deliver
end
end
def test_count
self.test_emails.split(',').count
end
def test_users
self.test_emails.split(',').collect do |ee|
ee.strip!
uu = User.new
uu.email = ee
uu.first_name = ee.match(/^(.*)@/)[1].to_s
uu.last_name = 'Test'
uu
end
end
def send_test_batch
self.perform_event('do_test_run!')
BatchMailer.send_batch_email_test(self.id).deliver
end
def merged_body(user)
body.gsub(VAR_FIRST_NAME, user.first_name).gsub(VAR_LAST_NAME, user.last_name)
end
def did_send(emails)
self.update_with_conflict_validation({ :sent_count => self.sent_count + emails.size })
if self.sent_count >= self.qualified_count
if batching?
self.perform_event('did_batch_run!')
elsif testing?
self.perform_event('did_test_run!')
end
end
end
def perform_event(event_name)
num_try = 0
self.send(event_name)
rescue ActiveRecord::StaleObjectError
num_try += 1
if 5 > num_try
self.reload
retry
end
end
def update_with_conflict_validation(*args)
num_try = 0
update_attributes(*args)
rescue ActiveRecord::StaleObjectError
num_try += 1
if 5 > num_try
self.reload
retry
end
end
def running_batch
self.update_with_conflict_validation({:qualified_count => User.email_opt_in.count,
:sent_count => 0,
:started_at => Time.now
})
end
def running_tests
self.update_with_conflict_validation({:qualified_count => self.test_count,
:sent_count => 0,
:started_at => Time.now
})
end
def ran_tests
self.update_with_conflict_validation({ :completed_at => Time.now })
end
def ran_batch
self.update_with_conflict_validation({ :completed_at => Time.now })
end
end
end

View File

@ -132,6 +132,7 @@ module JamRuby
scope :fans, where(:musician => false)
scope :geocoded_users, where(['lat IS NOT NULL AND lng IS NOT NULL'])
scope :musicians_geocoded, musicians.geocoded_users
scope :email_opt_in, where(:subscribe_email => true)
def user_progression_fields
@user_progression_fields ||= Set.new ["first_downloaded_client_at", "first_ran_client_at", "first_music_session_at", "first_real_music_session_at", "first_good_music_session_at", "first_certified_gear_at", "first_invited_at", "first_friended_at", "first_recording_at", "first_social_promoted_at" ]

View File

@ -429,6 +429,12 @@ FactoryGirl.define do
factory :event_session, :class => JamRuby::EventSession do
end
factory :email_batch, :class => JamRuby::EmailBatch do
subject Faker::Lorem.sentence
body "#{JamRuby::EmailBatch::VAR_FIRST_NAME} " + Faker::Lorem.paragraphs(3).join("\n")
test_emails 4.times.collect { Faker::Internet.safe_email }.join(',')
end
factory :notification, :class => JamRuby::Notification do
factory :notification_text_message do

View File

@ -0,0 +1,18 @@
require 'spec_helper'
describe EmailBatch do
let (:email_batch) { FactoryGirl.create(:email_batch) }
before(:each) do
BatchMailer.deliveries.clear
end
it 'has test emails setup' do
expect(email_batch.test_emails.present?).to be true
expect(email_batch.pending?).to be true
users = email_batch.test_users
expect(email_batch.test_count).to eq(users.count)
end
end

View File

@ -0,0 +1,26 @@
require "spec_helper"
describe BatchMailer do
describe "should send test emails" do
BatchMailer.deliveries.clear
let (:mail) { BatchMailer.deliveries[0] }
batch = FactoryGirl.create(:email_batch)
batch.update_attribute(:test_emails, "jonathan@jamkazam.com")
batch.send_test_batch
it { BatchMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == EmailBatch::DEFAULT_SENDER }
it { mail['to'].to_s.split(',')[0].should == batch.test_emails.split(',')[0] }
it { mail.subject.should == batch.subject }
it { mail.multipart?.should == true } # because we send plain + html
it { mail.text_part.decode_body.should match(/#{Regexp.escape(batch.body)}/) }
it { batch.testing?.should == true }
end
end

View File

@ -45,7 +45,7 @@ gem 'omniauth-twitter'
gem 'omniauth-google-oauth2', '0.2.1'
gem 'twitter'
gem 'fb_graph', '2.5.9'
gem 'sendgrid', '1.1.0'
gem 'sendgrid', '1.2.0'
gem 'recaptcha', '0.3.4'
gem 'filepicker-rails', '0.1.0'
gem 'aws-sdk', '1.29.1'
@ -67,6 +67,8 @@ gem 'resque-retry'
gem 'resque-failed-job-mailer'
gem 'resque-dynamic-queues'
gem 'resque-lonely_job', '~> 1.0.0'
gem 'resque_mailer'
gem 'quiet_assets', :group => :development
gem 'bugsnag'
gem 'multi_json', '1.9.0'

View File

@ -68,8 +68,8 @@ SampleApp::Application.configure do
# it's nice to have even admin accounts (which all the default ones are) generate GA data for testing
config.ga_suppress_admin = false
config.websocket_gateway_connect_time_stale = 12 # 12 matches production
config.websocket_gateway_connect_time_expire = 20 # 20 matches production
config.websocket_gateway_connect_time_stale = 12
config.websocket_gateway_connect_time_expire = 20
config.audiomixer_path = ENV['AUDIOMIXER_PATH'] || audiomixer_workspace_path || "/var/lib/audiomixer/audiomixer/audiomixerapp"

View File

@ -0,0 +1 @@
Resque::Mailer.excluded_environments = [:test, :cucumber]