2014-01-22 15:46:44 -05:00
config = require ' ../../server_config '
2014-02-04 17:08:20 -05:00
log = require ' winston '
2016-04-06 13:56:06 -04:00
User = require ' ../models/User '
2014-12-11 23:06:03 -05:00
sendwithus = require ' ../sendwithus '
2014-12-19 20:27:58 -05:00
async = require ' async '
2016-04-06 13:56:06 -04:00
LevelSession = require ' ../models/LevelSession '
2014-12-19 20:27:58 -05:00
moment = require ' moment '
2016-05-25 19:03:06 -04:00
closeIO = require ' ../lib/closeIO '
2014-01-03 13:32:13 -05:00
2014-02-04 16:29:13 -05:00
module.exports.setup = (app) ->
2014-01-03 13:32:13 -05:00
app . post ' /contact ' , (req, res) ->
2014-02-24 23:27:38 -05:00
return res . end ( ) unless req . user
2016-05-25 19:03:06 -04:00
# log.info "Sending mail from #{req.body.email} saying #{req.body.message}"
2014-12-19 21:37:42 -05:00
createMailContext req , (context) ->
2014-12-11 23:06:03 -05:00
sendwithus . api . send context , (err, result) ->
if err
log . error " Error sending contact form email: #{ err . message or err } "
2014-01-03 13:32:13 -05:00
return res . end ( )
2014-12-19 21:37:42 -05:00
createMailContext = (req, done) ->
2015-02-25 20:39:54 -05:00
sender = req . body . sender or req . body . email
2014-12-19 21:37:42 -05:00
message = req . body . message
user = req . user
recipientID = req . body . recipientID
subject = req . body . subject
2015-11-11 18:43:25 -05:00
country = req . body . country
2015-12-28 11:15:48 -05:00
sentFromLevel = levelID: req . body . levelID , courseID: req . body . courseID , courseInstanceID: req . body . courseInstanceID
2014-12-19 21:37:42 -05:00
2014-12-19 01:22:44 -05:00
level = if user ? . get ( ' points ' ) > 0 then Math . floor ( 5 * Math . log ( ( 1 / 100 ) * ( user . get ( ' points ' ) + 100 ) ) ) + 1 else 0
2014-12-18 23:39:15 -05:00
premium = user ? . isPremium ( )
2016-05-12 16:22:25 -04:00
teacher = user ? . isTeacher ( )
2014-12-18 23:39:15 -05:00
content = """
#{message}
2014-12-19 20:27:58 -05:00
- -
2016-05-25 19:03:06 -04:00
http : / / codecombat . com / user / #{user.get('slug') or user.get('_id')}
#{user.get('name') or 'Anonymous'} - Level #{level}#{if teacher then ' - Teacher' else ''}#{if premium then ' - Subscriber' else ''}#{if country then ' - ' + country else ''}
2014-12-18 23:39:15 -05:00
"""
2014-12-19 21:37:42 -05:00
if req . body . browser
content += " \n #{ req . body . browser } - #{ req . body . screenSize } "
2014-12-18 23:39:15 -05:00
2016-05-25 19:03:06 -04:00
toAddress = switch
2016-05-12 16:22:25 -04:00
when premium then config . mail . supportPremium
else config . mail . supportPrimary
2016-05-25 19:03:06 -04:00
fromAddress = sender or user . get ( ' email ' )
2014-12-11 23:06:03 -05:00
context =
email_id: sendwithus . templates . plain_text_email
recipient:
2016-05-25 19:03:06 -04:00
address: toAddress
2014-12-11 23:06:03 -05:00
sender:
address: config . mail . username
2016-05-25 19:03:06 -04:00
reply_to: fromAddress
2014-12-11 23:06:03 -05:00
name: user . get ( ' name ' )
email_data:
2016-05-25 19:03:06 -04:00
subject: " [CodeCombat] #{ subject ? ( ' Feedback - ' + fromAddress ) } "
2014-12-18 23:39:15 -05:00
content: content
2016-05-26 16:34:36 -04:00
contentHTML: content . replace / \ n / g , ' \n <br> '
2016-05-25 19:03:06 -04:00
if recipientID is ' schools@codecombat.com ' or teacher
req . user . update ( { $set: { enrollmentRequestSent: true } } ) . exec ( _ . noop ) if recipientID is ' schools@codecombat.com '
closeIO . getSalesContactEmail fromAddress , (err, salesContactEmail) ->
console . error " Error getting sales contact for #{ sender } : #{ err } " if err
context.recipient.address = salesContactEmail ? config . mail . supportSchools
context.sender.address = fromAddress
done context
2016-05-09 18:16:54 -04:00
else if recipientID and ( user . isAdmin ( ) or ( ' employer ' in ( user . get ( ' permissions ' ) ? [ ] ) ) )
2014-04-10 17:59:32 -04:00
User . findById ( recipientID , ' email ' ) . exec (err, document) ->
if err
log . error " Error looking up recipient to email from #{ recipientID } : #{ err } " if err
else
2014-12-19 20:27:58 -05:00
context.recipient.bcc = [ context . recipient . address , sender ]
context.recipient.address = document . get ( ' email ' )
context.email_data.content = message
2014-12-11 23:06:03 -05:00
done context
2014-04-10 17:59:32 -04:00
else
2014-12-19 20:27:58 -05:00
async . waterfall [
2015-12-28 11:15:48 -05:00
fetchRecentSessions . bind undefined , user , context , sentFromLevel
2014-12-19 20:27:58 -05:00
# Can add other data-grabbing stuff here if we want.
] , (err, results) ->
console . error " Error getting contact message context for #{ sender } : #{ err } " if err
2014-12-19 21:37:42 -05:00
if req . body . screenshotURL
2016-05-26 16:34:36 -04:00
context . email_data . contentHTML += " \n <br><img src= ' #{ req . body . screenshotURL } ' /> "
2014-12-19 20:27:58 -05:00
done context
2015-12-28 11:15:48 -05:00
fetchRecentSessions = (user, context, sentFromLevel, callback) ->
2014-12-19 20:27:58 -05:00
query = creator: user . get ( ' _id ' ) + ' '
projection = levelID: 1 , levelName: 1 , changed: 1 , team: 1 , codeLanguage: 1 , ' state.complete ' : 1 , playtime: 1
sort = changed: - 1
LevelSession . find ( query ) . select ( projection ) . sort ( sort ) . limit ( 3 ) . lean ( ) . exec (err, sessions) ->
return callback err if err
for s in sessions
if s . playtime < 120 then playtime = " #{ s . playtime } s played "
else if s . playtime < 7200 then playtime = " #{ Math . round ( s . playtime / 60 ) } m played "
else playtime = " #{ Math . round ( s . playtime / 3600 ) } h played "
ago = moment ( s . changed ) . fromNow ( )
2015-12-28 11:15:48 -05:00
url = " http://codecombat.com/play/level/ #{ s . levelID } ?session= #{ s . _id } &team= #{ s . team or ' humans ' } &dev=true "
urlName = " #{ s . levelName } #{ if s . team is ' ogres ' then ' ' + s . team else ' ' } "
sessionStatus = " #{ if s . state ? . complete then ' complete ' else ' ' } - #{ s . codeLanguage } , #{ playtime } , #{ ago } "
if sentFromLevel ? . levelID is s . levelID and sentFromLevel ? . courseID
url += " &course= #{ sentFromLevel . courseID } &course-instance= #{ sentFromLevel . courseInstanceID } "
urlName += ' (course) '
2016-05-26 16:34:36 -04:00
context . email_data . contentHTML += " \n <br><a href= ' #{ url } ' > #{ urlName } </a> #{ sessionStatus } "
2014-12-19 20:27:58 -05:00
callback null