163 lines
6.3 KiB
Ruby
163 lines
6.3 KiB
Ruby
# -*- coding: utf-8 -*-
|
||
module JamRuby
|
||
class EmailBatchProgression < EmailBatchPeriodic
|
||
|
||
BATCH_SIZE = 3
|
||
SINCE_WEEKS = 2
|
||
|
||
SUBTYPES = [:client_notdl, # Registered Musician Has Not Downloaded Client
|
||
:client_dl_notrun, # Registered Musician Has Downloaded Client But Not Yet Run It
|
||
:client_run_notgear, # Registered Musician Has Run Client But Not Successfully Qualified Audio Gear
|
||
:gear_notsess, # Registered Musician Has Successfully Qualified Audio Gear But Has Not Participated in a ‘Real’ Session
|
||
:sess_notgood, # Registered Musician Has Participated In a "Real" Session But Has Not Had a "Good" Session
|
||
:sess_notrecord, # Registered Musician Has Participated In a "Real" Session But Has Not Made a Recording
|
||
:reg_notinvite, # Registered Musician Has Not Invited Friends to Join JamKazam
|
||
:reg_notconnect, # Registered Musician Has Not Connected with any Friends on JamKazam
|
||
:reg_notlike, # Registered Musician Has Not Liked Jamkazam
|
||
]
|
||
|
||
SUBTYPE_METADATA = {
|
||
:client_notdl => {
|
||
:subject => "Get the free JamKazam app now and start playing with others!",
|
||
:title => "Download the Free JamKazam App"
|
||
},
|
||
:client_dl_notrun => {
|
||
:subject => "Having trouble running the JamKazam application?",
|
||
:title => "Running the JamKazam App"
|
||
},
|
||
:client_run_notgear => {
|
||
:subject => "Having trouble setting up your audio gear for JamKazam?",
|
||
:title => "Setting Up and Qualifying Your Audio Gear on JamKazam"
|
||
},
|
||
:gear_notsess => {
|
||
:subject => "Having trouble getting into a session with other musicians?",
|
||
:title => "Tips on Getting into Sessions with Other Musicians"
|
||
},
|
||
:sess_notgood => {
|
||
:subject => "Have you played in a “good” session on JamKazam yet?",
|
||
:title => "Having a Good Session on JamKazam"
|
||
},
|
||
:reg_notinvite => {
|
||
:subject => "Invite your friends to JamKazam, best way to play online!",
|
||
:title => "Invite Your Friends to Come and Play with You"
|
||
},
|
||
:reg_notconnect => {
|
||
:subject => "Make friends on JamKazam and play more music!",
|
||
:title => "Connecting with Friends on JamKazam"
|
||
},
|
||
:reg_notlike => {
|
||
:subject => "Please give us a LIKE!",
|
||
:title => "Like/Follow JamKazam"
|
||
},
|
||
:sess_notrecord => {
|
||
:subject => "Want to make recordings during your JamKazam sessions?",
|
||
:title => "Making & Sharing Recordings on JamKazam"
|
||
},
|
||
}
|
||
|
||
def self.subject(subtype=nil)
|
||
SUBTYPE_METADATA[subtype][:subject] if subtype
|
||
end
|
||
|
||
def self.title(subtype=nil)
|
||
SUBTYPE_METADATA[subtype][:title] if subtype
|
||
end
|
||
|
||
def self.subtype_trigger_interval(subtype)
|
||
[:reg_notlike, :sess_notrecord].include?(subtype) ? [7,14,21] : [2,5,14]
|
||
end
|
||
|
||
def self.days_past_for_trigger_index(subtype, idx)
|
||
self.subtype_trigger_interval(subtype)[idx]
|
||
end
|
||
|
||
def days_past_for_trigger_index(idx)
|
||
self.class.subtype_trigger_interval(self.sub_type)[idx]
|
||
end
|
||
|
||
def make_set(uu, trigger_idx)
|
||
EmailBatchSet.progress_set(self, uu, trigger_idx)
|
||
end
|
||
|
||
def trigger_date_constraint(trigger_idx)
|
||
intervals = self.class.subtype_trigger_interval(self.sub_type)
|
||
case self.sub_type.to_sym
|
||
when :client_notdl
|
||
return ["users.created_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :client_dl_notrun
|
||
return ["users.first_downloaded_client_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :client_run_notgear
|
||
return ["users.first_ran_client_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :gear_notsess
|
||
return ["users.first_certified_gear_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :sess_notgood
|
||
return ["users.first_real_music_session_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :sess_notrecord
|
||
return ["users.first_real_music_session_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :reg_notconnect
|
||
return ["users.created_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :reg_notinvite
|
||
return ["users.created_at < ?", Time.now - intervals[trigger_idx].days]
|
||
when :reg_notlike
|
||
return ["users.created_at < ?", Time.now - intervals[trigger_idx].days]
|
||
end
|
||
end
|
||
|
||
def progress_column_constraint
|
||
case self.sub_type.to_sym
|
||
when :client_notdl
|
||
return "first_downloaded_client_at IS NULL"
|
||
when :client_dl_notrun
|
||
return "first_downloaded_client_at IS NOT NULL AND first_ran_client_at IS NULL"
|
||
when :client_run_notgear
|
||
return "first_ran_client_at IS NOT NULL AND first_certified_gear_at IS NULL"
|
||
when :gear_notsess
|
||
return "first_certified_gear_at IS NOT NULL AND first_real_music_session_at IS NULL"
|
||
when :sess_notgood
|
||
return "first_real_music_session_at IS NOT NULL AND first_good_music_session_at IS NULL"
|
||
when :sess_notrecord
|
||
return "first_real_music_session_at IS NOT NULL AND first_recording_at IS NULL"
|
||
when :reg_notinvite
|
||
return "first_invited_at IS NOT NULL"
|
||
when :reg_notconnect
|
||
return "first_friended_at IS NOT NULL"
|
||
when :reg_notlike
|
||
return "first_social_promoted_at IS NOT NULL"
|
||
end
|
||
''
|
||
end
|
||
|
||
def fetch_recipients(trigger_idx=0)
|
||
fetched = []
|
||
rel = User
|
||
.email_opt_in
|
||
.musicians
|
||
.where("(SELECT COALESCE(MAX(trigger_index),-1) FROM email_batch_sets ebs WHERE ebs.sub_type = '#{self.sub_type}' AND ebs.user_id = users.id) < #{trigger_idx}")
|
||
.where(self.progress_column_constraint)
|
||
.where(self.trigger_date_constraint(trigger_idx))
|
||
.find_in_batches(batch_size: 500) do |users|
|
||
block_given? ? yield(users) : fetched.concat(users)
|
||
end
|
||
fetched
|
||
end
|
||
|
||
def deliver_batch_sets!
|
||
self.opt_in_count = 0
|
||
sent = 0
|
||
3.times do |trigger_idx|
|
||
self.fetch_recipients(trigger_idx) do |users|
|
||
users.each do |uu|
|
||
self.email_batch_sets << (bset = self.make_set(uu, trigger_idx))
|
||
ProgressMailer.send_reminder(bset).deliver
|
||
sent += 1
|
||
end
|
||
end
|
||
end
|
||
self.sent_count = sent
|
||
self.save
|
||
self.did_batch_run!
|
||
end
|
||
|
||
end
|
||
end
|