2014-01-22 15:46:44 -05:00
|
|
|
config = require '../../server_config'
|
2014-02-04 17:08:20 -05:00
|
|
|
log = require 'winston'
|
2014-04-10 17:59:32 -04:00
|
|
|
User = require '../users/User'
|
2014-12-11 23:06:03 -05:00
|
|
|
sendwithus = require '../sendwithus'
|
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
|
2014-11-30 16:19:00 -05:00
|
|
|
#log.info "Sending mail from #{req.body.email} saying #{req.body.message}"
|
2014-12-11 23:06:03 -05:00
|
|
|
createMailContext req.body.email, req.body.message, req.user, req.body.recipientID, req.body.subject, (context) ->
|
|
|
|
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-11 23:06:03 -05:00
|
|
|
createMailContext = (sender, message, user, recipientID, subject, done) ->
|
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()
|
|
|
|
content = """
|
|
|
|
#{message}
|
|
|
|
|
|
|
|
#{user.get('name') or 'Anonymous'} - Level #{level}#{if premium then ' - Subscriber' else ''} - #{user._id}
|
|
|
|
"""
|
|
|
|
|
2014-12-11 23:06:03 -05:00
|
|
|
context =
|
|
|
|
email_id: sendwithus.templates.plain_text_email
|
|
|
|
recipient:
|
2014-12-18 23:39:15 -05:00
|
|
|
address: if premium then config.mail.supportPremium else config.mail.supportPrimary
|
2014-12-11 23:06:03 -05:00
|
|
|
sender:
|
|
|
|
address: config.mail.username
|
|
|
|
reply_to: sender
|
|
|
|
name: user.get('name')
|
|
|
|
email_data:
|
|
|
|
subject: "[CodeCombat] #{subject ? ('Feedback - ' + sender)}"
|
2014-12-18 23:39:15 -05:00
|
|
|
content: content
|
2014-04-10 17:59:32 -04:00
|
|
|
|
2014-06-19 16:42:51 -04:00
|
|
|
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-11 23:06:03 -05:00
|
|
|
context.bcc = [context.to, sender]
|
|
|
|
context.to = document.get('email')
|
|
|
|
done context
|
2014-04-10 17:59:32 -04:00
|
|
|
else
|
2014-12-11 23:06:03 -05:00
|
|
|
done context
|