This commit is contained in:
Nick Winter 2014-01-24 13:03:09 -08:00
commit bfaf13fc9c
7 changed files with 67 additions and 43 deletions

View file

@ -18,6 +18,7 @@ logging = require './server/commons/logging'
sprites = require './server/routes/sprites'
contact = require './server/routes/contact'
languages = require './server/routes/languages'
mail = require './server/routes/mail'
https = require 'https'
http = require 'http'
@ -82,6 +83,7 @@ contact.setupRoutes(app)
file.setupRoutes(app)
folder.setupRoutes(app)
languages.setupRoutes(app)
mail.setupRoutes(app)
# Some sort of cross-domain communication hack facebook requires
app.get('/channel.html', (req, res) ->

View file

@ -0,0 +1,19 @@
config = require '../../server_config'
module.exports.MAILCHIMP_LIST_ID = 'e9851239eb'
module.exports.MAILCHIMP_GROUP_ID = '4529'
module.exports.MAILCHIMP_GROUP_MAP =
announcement: 'Announcements'
tester: 'Adventurers'
level_creator: 'Artisans'
developer: 'Archmages'
article_editor: 'Scribes'
translator: 'Diplomats'
support: 'Ambassadors'
nodemailer = require 'nodemailer'
module.exports.transport = nodemailer.createTransport "SMTP",
service: config.mail.service
user: config.mail.username
pass: config.mail.password
authMethod: "LOGIN"

View file

@ -4,8 +4,8 @@ LocalStrategy = require('passport-local').Strategy
User = require('../users/User')
UserHandler = require('../users/user_handler')
config = require '../../server_config'
nodemailer = require 'nodemailer'
errors = require '../commons/errors'
mail = require '../commons/mail'
module.exports.setupRoutes = (app) ->
passport.serializeUser((user, done) -> done(null, user._id))
@ -66,9 +66,8 @@ module.exports.setupRoutes = (app) ->
user.save (err) =>
return errors.serverError(res) if err
if config.isProduction
transport = createSMTPTransport()
options = createMailOptions req.body.email, user.get('passwordReset')
transport.sendMail options, (error, response) ->
mail.transport.sendMail options, (error, response) ->
if error
console.error "Error sending mail: #{error.message or error}"
return errors.serverError(res) if err
@ -104,13 +103,4 @@ createMailOptions = (receiver, password) ->
replyTo: config.mail.username
subject: "[CodeCombat] Password Reset"
text: "You can log into your account with: #{password}"
#html: message.replace '\n', '<br>\n'
createSMTPTransport = ->
return smtpTransport if smtpTransport
smtpTransport = nodemailer.createTransport "SMTP",
service: config.mail.service
user: config.mail.username
pass: config.mail.password
authMethod: "LOGIN"
smtpTransport
#

View file

@ -1,14 +1,13 @@
config = require '../../server_config'
winston = require 'winston'
nodemailer = require 'nodemailer'
mail = require '../commons/mail'
module.exports.setupRoutes = (app) ->
app.post '/contact', (req, res) ->
winston.info "Sending mail from #{req.body.email} saying #{req.body.message}"
if config.isProduction
transport = createSMTPTransport()
options = createMailOptions req.body.email, req.body.message, req.user
transport.sendMail options, (error, response) ->
mail.transport.sendMail options, (error, response) ->
if error
winston.error "Error sending mail: #{error.message or error}"
else
@ -17,7 +16,6 @@ module.exports.setupRoutes = (app) ->
createMailOptions = (sender, message, user) ->
# TODO: use email templates here
console.log 'text is now', "#{message}\n\n#{user.get('name')}\nID: #{user._id}"
options =
from: config.mail.username
to: config.mail.username
@ -25,13 +23,3 @@ createMailOptions = (sender, message, user) ->
subject: "[CodeCombat] Feedback - #{sender}"
text: "#{message}\n\nUsername: #{user.get('name') or 'Anonymous'}\nID: #{user._id}"
#html: message.replace '\n', '<br>\n'
smtpTransport = null
createSMTPTransport = ->
return smtpTransport if smtpTransport
smtpTransport = nodemailer.createTransport "SMTP",
service: config.mail.service
user: config.mail.username
pass: config.mail.password
authMethod: "LOGIN"
smtpTransport

34
server/routes/mail.coffee Normal file
View file

@ -0,0 +1,34 @@
mail = require '../commons/mail'
map = _.invert mail.MAILCHIMP_GROUP_MAP
User = require '../users/User.coffee'
errors = require '../commons/errors'
request = require 'request'
config = require '../../server_config'
module.exports.setupRoutes = (app) ->
app.all config.mail.mailchimpWebhook, (req, res) ->
post = req.body
return res.end() unless post.type in ['unsubscribe', 'profile']
return res.end() unless post.data.email
User.findOne {'mailChimp.euid':post.data.id}, (err, user) ->
return errors.serverError(res) if err
return errors.notFound(res) if not user
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()
handleProfileUpdate = (user, post) ->
groups = post.data.merges.INTERESTS.split(', ')
groups = (map[g] for g in groups when map[g])
user.set 'emailSubscriptions', groups
mailChimpInfo = user.get 'mailChimp'
mailChimpInfo.email = post.data.email
user.set 'mailChimp', mailChimpInfo
handleUnsubscribe = (user) ->
user.set 'emailSubscriptions', []

View file

@ -2,6 +2,7 @@ mongoose = require('mongoose')
jsonschema = require('./user_schema')
crypto = require('crypto')
{salt, isProduction} = require('../../server_config')
mail = require '../commons/mail'
sendwithus = require '../sendwithus'
@ -34,16 +35,17 @@ UserSchema.statics.updateMailChimp = (doc, callback) ->
existingProps = doc.get('mailChimp')
emailChanged = (not existingProps) or existingProps?.email isnt doc.get('email')
emailSubs = doc.get('emailSubscriptions')
newGroups = (groupingMap[name] for name in emailSubs when groupingMap[name]?)
gm = mail.MAILCHIMP_GROUP_MAP
newGroups = (gm[name] for name in emailSubs when gm[name]?)
if (not existingProps) and newGroups.length is 0
return callback?() # don't add totally unsubscribed people to the list
subsChanged = doc.currentSubscriptions isnt JSON.stringify(emailSubs)
return callback?() unless emailChanged or subsChanged
params = {}
params.id = MAILCHIMP_LIST_ID
params.id = mail.MAILCHIMP_LIST_ID
params.email = if existingProps then {leid:existingProps.leid} else {email:doc.get('email')}
params.merge_vars = { groupings: [ {id: MAILCHIMP_GROUP_ID, groups: newGroups} ] }
params.merge_vars = { groupings: [ {id: mail.MAILCHIMP_GROUP_ID, groups: newGroups} ] }
params.update_existing = true
params.double_optin = false
@ -79,18 +81,6 @@ UserSchema.pre('save', (next) ->
next()
)
MAILCHIMP_LIST_ID = 'e9851239eb'
MAILCHIMP_GROUP_ID = '4529'
groupingMap =
announcement: 'Announcements'
tester: 'Adventurers'
level_creator: 'Artisans'
developer: 'Archmages'
article_editor: 'Scribes'
translator: 'Diplomats'
support: 'Ambassadors'
UserSchema.post 'save', (doc) ->
UserSchema.statics.updateMailChimp(doc)

View file

@ -27,6 +27,7 @@ config.mail.service = process.env.COCO_MAIL_SERVICE_NAME || "Zoho";
config.mail.username = process.env.COCO_MAIL_SERVICE_USERNAME || "";
config.mail.password = process.env.COCO_MAIL_SERVICE_PASSWORD || "";
config.mail.mailchimpAPIKey = process.env.COCO_MAILCHIMP_API_KEY || '';
config.mail.mailchimpWebhook = process.env.COCO_MAILCHIMP_WEBHOOK || '/mail/webhook';
config.mail.sendwithusAPIKey = process.env.COCO_SENDWITHUS_API_KEY || '';
config.salt = process.env.COCO_SALT || 'pepper';