2014-01-24 14:48:00 -05:00
|
|
|
mail = require '../commons/mail'
|
|
|
|
map = _.invert mail.MAILCHIMP_GROUP_MAP
|
|
|
|
User = require '../users/User.coffee'
|
|
|
|
errors = require '../commons/errors'
|
2014-01-24 20:42:47 -05:00
|
|
|
#request = require 'request'
|
2014-01-24 15:53:41 -05:00
|
|
|
config = require '../../server_config'
|
2014-03-08 21:49:09 -05:00
|
|
|
LevelSession = require '../levels/sessions/LevelSession.coffee'
|
|
|
|
log = require 'winston'
|
|
|
|
sendwithus = require '../sendwithus'
|
2014-01-24 14:48:00 -05:00
|
|
|
|
2014-01-24 20:42:47 -05:00
|
|
|
#badLog = (text) ->
|
|
|
|
# console.log text
|
|
|
|
# request.post 'http://requestb.in/1brdpaz1', { form: {log: text} }
|
2014-03-08 21:49:09 -05:00
|
|
|
|
2014-02-04 16:29:13 -05:00
|
|
|
module.exports.setup = (app) ->
|
2014-03-08 21:49:09 -05:00
|
|
|
app.all config.mail.mailchimpWebhook, handleMailchimpWebHook
|
|
|
|
app.get '/mail/cron/ladder-update', handleLadderUpdate
|
|
|
|
|
|
|
|
handleLadderUpdate = (req, res) ->
|
|
|
|
emailDays = [1, 2, 4, 7, 30]
|
|
|
|
now = new Date()
|
|
|
|
getTimeFromDaysAgo = (daysAgo) ->
|
|
|
|
# 2 hours before the date
|
|
|
|
t = now - (86400 * daysAgo + 2 * 3600) * 1000
|
|
|
|
for daysAgo in emailDays
|
|
|
|
startTime = getTimeFromDaysAgo daysAgo
|
|
|
|
endTime = startTime + 5 * 60 * 1000
|
|
|
|
# Get every session that was submitted in a 5-minute window after the time.
|
|
|
|
findParameters = {submitted: true, submitDate: {$gt: startTime, $lte: endTime}}
|
|
|
|
# TODO: think about putting screenshots in the email
|
|
|
|
selectString = "creator team levelID totalScore matches"
|
|
|
|
query = LevelSession.find(findParameters)
|
|
|
|
.select(selectString)
|
|
|
|
.lean()
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
mongoose.set 'debug', true
|
|
|
|
query.exec (err, results) ->
|
|
|
|
log.info "Yooooo got results: #{results.length}"
|
|
|
|
if err
|
|
|
|
log.error "Couldn't fetch ladder updates for", findParameters, "\nError: ", err
|
|
|
|
return errors.serverError res, "Ladder update email query failed: #{JSON.stringify(err)}"
|
|
|
|
sendLadderUpdateEmail result, daysAgo for result in results
|
|
|
|
res.send('')
|
|
|
|
res.end()
|
|
|
|
|
|
|
|
sendLadderUpdateEmail = (session, daysAgo) ->
|
|
|
|
User.findOne({_id: session.creator}).select("name email firstName lastName emailSubscriptions preferredLanguage").lean().exec (err, user) ->
|
|
|
|
if err
|
|
|
|
log.error "Couldn't find user for", session.creator, "from session", session._id
|
|
|
|
return
|
|
|
|
return unless user.email and 'notification' in user.emailSubscriptions
|
|
|
|
name = if user.firstName and user.lastName then "#{user.firstName} #{user.lastName}" else user.name
|
|
|
|
name = "Wizard" if not name or name is "Anoner"
|
|
|
|
|
|
|
|
sendEmail = (defeatContext, victoryContext) ->
|
|
|
|
# TODO: do something with the preferredLanguage?
|
|
|
|
context =
|
|
|
|
email_id: sendwithus.templates.ladder_update_email
|
|
|
|
#recipient:
|
|
|
|
# address: user.email
|
|
|
|
recipient:
|
|
|
|
address: 'nick@codecombat.com'
|
|
|
|
days_ago: daysAgo
|
|
|
|
name: name
|
|
|
|
wins: session.numberOfWinsAndTies
|
|
|
|
losses: session.numberOfLosses
|
|
|
|
total_score: session.totalScore
|
|
|
|
team: session.team
|
|
|
|
level: id
|
|
|
|
defeat: defeatContext
|
|
|
|
victory: victoryContext
|
|
|
|
sendwithus.api.send context, (err, result) ->
|
|
|
|
log.error "Error sending ladder update email:", err, 'result', result
|
|
|
|
|
|
|
|
defeats = _.filter session.matches, (match) -> match.metrics.rank is 1 and match.opponents[0].metrics.rank is 0
|
|
|
|
victories = _.filter session.matches, (match) -> match.metrics.rank is 0
|
|
|
|
defeat = _.sample defeats
|
|
|
|
victory = _.sample victories
|
|
|
|
urlForMatch = (match) ->
|
|
|
|
"http://codecombat.com/play/ladder/#{session.levelID}?team=#{session.team}&session=#{session._id}&opponent=#{match.opponents[0].sessionID}"
|
|
|
|
|
|
|
|
onFetchedDefeatedOpponent = (err, defeatedOpponent) ->
|
|
|
|
if err
|
|
|
|
log.error "Couldn't find defeateded opponent: #{err}"
|
|
|
|
defeatedOpponent = null
|
|
|
|
victoryContext = {opponent_name: defeatedOpponent?.name ? "Anoner", url: urlForMatch(victory)} if victory
|
|
|
|
|
|
|
|
onFetchedVictoriousOpponent = (err, victoriousOpponent) ->
|
|
|
|
if err
|
|
|
|
log.error "Couldn't find victorious opponent: #{err}"
|
|
|
|
victoriousOpponent = null
|
|
|
|
defeatContext = {opponent_name: victoriousOpponent?.name ? "Anoner", url: urlForMatch(defeat)} if defeat
|
|
|
|
sendEmail defeatContext, victoryContext
|
|
|
|
|
|
|
|
if defeat
|
|
|
|
User.findOne({_id: defeat.opponents[0].userID}).select("name").lean().exec onFetchedVictoriousOpponent
|
|
|
|
else
|
|
|
|
onFetchedVictoriousOpponent null, null
|
|
|
|
|
|
|
|
if victory
|
|
|
|
User.findOne({_id: victory.opponents[0].userID}).select("name").lean().exec onFetchedDefeatedOpponent
|
|
|
|
else
|
|
|
|
onFetchedDefeatedOpponent null, null
|
2014-01-24 16:14:31 -05:00
|
|
|
|
|
|
|
|
2014-03-08 21:49:09 -05:00
|
|
|
handleMailchimpWebHook = (req, res) ->
|
|
|
|
post = req.body
|
|
|
|
#badLog("Got post data: #{JSON.stringify(post, null, '\t')}")
|
|
|
|
|
|
|
|
unless post.type in ['unsubscribe', 'profile']
|
|
|
|
res.send 'Bad post type'
|
|
|
|
return res.end()
|
|
|
|
|
|
|
|
unless post.data.email
|
|
|
|
res.send 'No email provided'
|
|
|
|
return res.end()
|
|
|
|
|
|
|
|
query = {'mailChimp.leid':post.data.web_id}
|
|
|
|
User.findOne query, (err, user) ->
|
|
|
|
return errors.serverError(res) if err
|
|
|
|
if not user
|
|
|
|
return errors.notFound(res)
|
|
|
|
|
|
|
|
handleProfileUpdate(user, post) if post.type is 'profile'
|
|
|
|
handleUnsubscribe(user) if post.type is 'unsubscribe'
|
|
|
|
|
|
|
|
user.updatedMailChimp = true # so as not to echo back to mailchimp
|
|
|
|
user.save (err) ->
|
|
|
|
return errors.serverError(res) if err
|
|
|
|
res.end('Success')
|
2014-01-24 15:23:14 -05:00
|
|
|
|
|
|
|
|
2014-01-24 15:39:05 -05:00
|
|
|
handleProfileUpdate = (user, post) ->
|
|
|
|
groups = post.data.merges.INTERESTS.split(', ')
|
2014-01-24 15:23:14 -05:00
|
|
|
groups = (map[g] for g in groups when map[g])
|
2014-01-24 20:19:20 -05:00
|
|
|
otherSubscriptions = (g for g in user.get('emailSubscriptions') when not mail.MAILCHIMP_GROUP_MAP[g])
|
|
|
|
groups = groups.concat otherSubscriptions
|
2014-01-24 15:23:14 -05:00
|
|
|
user.set 'emailSubscriptions', groups
|
2014-03-08 21:49:09 -05:00
|
|
|
|
2014-01-24 20:19:20 -05:00
|
|
|
fname = post.data.merges.FNAME
|
|
|
|
user.set('firstName', fname) if fname
|
|
|
|
|
|
|
|
lname = post.data.merges.LNAME
|
|
|
|
user.set('lastName', lname) if lname
|
2014-03-08 21:49:09 -05:00
|
|
|
|
2014-01-24 20:35:34 -05:00
|
|
|
user.set 'mailChimp.email', post.data.email
|
|
|
|
user.set 'mailChimp.euid', post.data.id
|
2014-03-08 21:49:09 -05:00
|
|
|
|
2014-01-24 20:42:47 -05:00
|
|
|
# badLog("Updating user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
2014-03-08 21:49:09 -05:00
|
|
|
|
2014-01-24 15:39:05 -05:00
|
|
|
handleUnsubscribe = (user) ->
|
2014-01-24 15:23:14 -05:00
|
|
|
user.set 'emailSubscriptions', []
|
2014-01-24 16:14:31 -05:00
|
|
|
|
2014-03-08 21:49:09 -05:00
|
|
|
# badLog("Unsubscribing user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|