VRFS-1664 new musician weekly email

This commit is contained in:
Jonathan Kolyer 2014-05-16 07:58:06 +00:00
parent 5327be79eb
commit 7428a2f26b
10 changed files with 192 additions and 14 deletions

View File

@ -106,9 +106,12 @@
sendgrid_recipients([user.email])
sendgrid_substitute('@USERID', [user.id])
sendgrid_substitute(EmailBatch::VAR_FIRST_NAME, [user.first_name])
sendgrid_substitute(EmailBatchNewMusician::VAR_MUSICIAN_COUNT, [new_nearby.count.to_s])
sendgrid_unique_args :type => "new_musicians"
mail(:to => user.email, :subject => "JamKazam New Musicians in Your Area") do |format|
mail(:to => user.email, :subject => EmailBatchNewMusician.subject) do |format|
format.text
format.html
end

View File

@ -1,4 +1,8 @@
<% provide(:title, 'New JamKazam Musicians in your Area') %>
Hi <%= @user.first_name %>,
<p>The following new musicians have joined JamKazam within the last week, and have Internet connections with low enough latency to you that you can have a good online session together. We'd suggest that you look through the new musicians listed below to see if any match your musical interests, and if so, click through to their profile page on the JamKazam website to send them a message or a request to connect as a JamKazam friend:
</p>
<% link_style = "background-color:#ED3618; margin:0px 8px 0px 8px; border: solid 1px #F27861; outline: solid 2px #ED3618; padding:3px 10px; font-family:Raleway, Arial, Helvetica, sans-serif; font-size:12px; font-weight:300; cursor:pointer; color:#FC9; text-decoration:none;" %>
<p>
@ -20,3 +24,8 @@
<% end %>
</table>
</p>
<p>There are currently <%= @new_nearby.size%> musicians on JamKazam with low enough latency Internet connections to you to support a good online session. To see ALL the JamKazam musicians with whom you may want to connect and play, view our Musicians page at: <a href="http://www.jamkazam.com/client#/musicians">http://www.jamkazam.com/client#/musicians</a>.
</p>
<p>Best Regards,</p>
Team JamKazam

View File

@ -1,9 +1,17 @@
New JamKazam Musicians in your Area
Hi <%= @user.first_name %>,
The following new musicians have joined JamKazam within the last week, and have Internet connections with low enough latency to you that you can have a good online session together. We'd suggest that you look through the new musicians listed below to see if any match your musical interests, and if so, click through to their profile page on the JamKazam website to send them a message or a request to connect as a JamKazam friend:
<% @new_nearby.each do |user| %>
<%= user.name %> (http://<%= @host %>/client#/profile/<%= user.id %>)
<%= user.location %>
<% user.instruments.collect { |inst| inst.description }.join(', ') %>
<%= user.biography %>
<% end %>
There are currently <%= @new_nearby.size%> musicians on JamKazam with low enough latency Internet connections to you to support a good online session. To see ALL the JamKazam musicians with whom you may want to connect and play, view our Musicians page at: http://www.jamkazam.com/client#/musicians.
Best Regards,
Team JamKazam

View File

@ -78,13 +78,22 @@ FOO
self.test_emails.present? && (self.tested? || self.pending?)
end
def deliver_batch
self.perform_event('do_batch_run!')
def deliver_batch_sets!
User.email_opt_in.find_in_batches(batch_size: BATCH_SIZE) do |users|
self.email_batch_sets << EmailBatchSet.deliver_set(self.id, users.map(&:id))
self.email_batch_sets << (bset = EmailBatchSet.load_set(self, users.map(&:id)))
if 'test' == Rails.env
BatchMailer.send_batch_email(self.id, bset.user_ids).deliver!
else
BatchMailer.send_batch_email(self.id, bset.user_ids).deliver
end
end
end
def deliver_batch
self.perform_event('do_batch_run!')
self.deliver_batch_sets!
end
def test_count
self.test_emails.split(',').count
end
@ -195,5 +204,13 @@ FOO
})
end
def batch_substitutions(users=[])
{}
end
def body_for_users(users=[])
self.body
end
end
end

View File

@ -0,0 +1,71 @@
module JamRuby
class EmailBatchNewMusician < EmailBatchPeriodic
BATCH_SIZE = 100
VAR_MUSICIAN_COUNT = "@MUSICIAN_COUNT"
VAR_MUSICIAN_TABLE = "@MUSICIAN_TABLE"
def self.subject
"New musicians with good Internet connections to you have joined JamKazam!"
end
def time_since_last_batch
if previous = self.class
.where(['created_at < ?', self.created_at])
.order('created_at DESC')
.limit(1)
.first
return previous.created_at
end
Time.now - 2.weeks
end
# def self.recipient_count
# User.geocoded_users
# .email_opt_in
# .where(['created_at >= ?', self.time_since_last_batch])
# .count
# end
# def self.fetch_recipients
# User.geocoded_users
# .email_opt_in
# .where(['created_at >= ?', self.time_since_last_batch])
# .find_in_batches(batch_size: self::BATCH_SIZE) do |users|
# new_musicians = users.inject({}) do |hh, uu|
# if 0 < (nearby = uu.nearest_musicians).count
# hh[uu] = nearby
# end
# hh
# end
# yield(new_musicians)
# end
# end
def deliver_batch_sets!
self.opt_in_count = 0
sent = 0
User.geocoded_users
.email_opt_in
.where(['created_at >= ?', self.time_since_last_batch])
.find_in_batches(batch_size: EmailBatchNewMusician::BATCH_SIZE) do |users|
new_musicians = users.inject({}) do |hh, uu|
if 0 < (nearby = uu.nearest_musicians).count
hh[uu] = nearby
end
hh
end
self.opt_in_count += new_musicians.count
self.email_batch_sets << (bset = EmailBatchSet.load_set(self, new_musicians.keys.map(&:id)))
new_musicians.each do |uu, nearby|
UserMailer.new_musicians(uu, nearby).deliver
sent += 1
end
end
self.sent_count = sent
self.save
end
end
end

View File

@ -0,0 +1,34 @@
module JamRuby
class EmailBatchPeriodic < EmailBatch
self.abstract_class = true
def self.time_since_last_batch
if previous = self.order('created_at DESC').limit(1).first
return Time.now - previous.created_at
end
nil
end
def self.fetch_recipients
yield([])
end
def self.subject
''
end
def self.body
''
end
def self.new(*args)
oo = super
oo.subject = self.subject
oo
end
def deliver_batch_sets!
end
end
end

View File

@ -4,20 +4,15 @@ module JamRuby
belongs_to :email_batch, :class_name => 'JamRuby::EmailBatch'
def self.deliver_set(batch_id, user_ids)
def self.load_set(batch, user_ids)
bset = self.new
bset.email_batch_id = batch_id
bset.email_batch_id = batch.id
bset.user_ids = user_ids.join(',')
bset.started_at = Time.now
bset.batch_count = user_ids.size
bset.save!
if 'test' == Rails.env
BatchMailer.send_batch_email(bset.email_batch_id, user_ids).deliver!
else
BatchMailer.send_batch_email(bset.email_batch_id, user_ids).deliver
end
bset
end
end
end

View File

@ -1168,6 +1168,11 @@ module JamRuby
.limit(3)
end
def nearest_musicians
# FIXME: replace with Scotts scoring query
Search.new_musicians(self, Time.now - 1.week)
end
def self.deliver_new_musician_notifications(since_date=nil)
since_date ||= Time.now-1.week
self.geocoded_users.find_each do |usr|

View File

@ -445,6 +445,10 @@ FactoryGirl.define do
test_emails 4.times.collect { Faker::Internet.safe_email }.join(',')
end
factory :email_batch_new_musician, :class => JamRuby::EmailBatchNewMusician do
end
factory :notification, :class => JamRuby::Notification do
factory :notification_text_message do

View File

@ -2,12 +2,14 @@ require 'spec_helper'
describe EmailBatch do
let (:email_batch) { FactoryGirl.create(:email_batch) }
let (:new_musician_batch) { FactoryGirl.create(:email_batch_new_musician) }
before(:each) do
BatchMailer.deliveries.clear
end
it 'has test emails setup' do
pending
expect(email_batch.test_emails.present?).to be true
expect(email_batch.pending?).to be true
@ -15,4 +17,34 @@ describe EmailBatch do
expect(email_batch.test_count).to eq(users.count)
end
describe 'new musician' do
before(:each) do
@u1 = FactoryGirl.create(:user, :lat => 37.791649, :lng => -122.394395, :email => 'jonathan@jamkazam.com')
@u2 = FactoryGirl.create(:user, :lat => 37.791649, :lng => -122.394395, :subscribe_email => false)
@u3 = FactoryGirl.create(:user, :lat => 37.791649, :lng => -122.394395, :subscribe_email => false)
end
it 'find new musicians with good score' do
pending
EmailBatchNewMusician.fetch_recipients do |new_musicians|
expect(new_musicians.count).to eq(1)
end
end
it 'has correct time since last batch' do
pending
tt = EmailBatchNewMusician.time_since_last_batch
expect(tt.to_i).to be < (Time.now - 1.week).to_i
end
it 'cycles through states properly' do
@u1.update_attributes(created_at: @u1.created_at + 1.minute)
new_musician_batch.deliver_batch
expect(UserMailer.deliveries.length).to eq(1)
new_musician_batch.reload
expect(new_musician_batch.delivering?).to eq(true)
end
end
end