2014-02-12 05:23:40 -05:00
|
|
|
async = require('async')
|
|
|
|
logger = require 'logger-sharelatex'
|
|
|
|
Settings = require 'settings-sharelatex'
|
2018-08-04 12:30:24 -04:00
|
|
|
crypto = require('crypto')
|
|
|
|
Mailchimp = require('mailchimp-api-v3')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-08-07 16:38:31 -04:00
|
|
|
if !Settings.mailchimp?.api_key?
|
2018-08-08 09:32:36 -04:00
|
|
|
logger.info "Using newsletter provider: none"
|
2018-08-07 16:38:31 -04:00
|
|
|
mailchimp =
|
|
|
|
request: (opts, cb)-> cb()
|
|
|
|
else
|
2018-08-08 09:32:36 -04:00
|
|
|
logger.info "Using newsletter provider: mailchimp"
|
2018-08-07 16:38:31 -04:00
|
|
|
mailchimp = new Mailchimp(Settings.mailchimp?.api_key)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
module.exports =
|
2018-08-07 16:38:31 -04:00
|
|
|
|
2014-02-13 07:37:23 -05:00
|
|
|
subscribe: (user, callback = () ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
options = buildOptions(user, true)
|
2018-08-28 07:32:20 -04:00
|
|
|
logger.log options:options, user:user, email:user.email, "subscribing user to the mailing list"
|
2018-08-04 12:30:24 -04:00
|
|
|
mailchimp.request options, (err)->
|
|
|
|
if err?
|
2018-08-28 07:32:20 -04:00
|
|
|
logger.err err:err, user:user, "error subscribing person to newsletter"
|
2018-08-04 12:30:24 -04:00
|
|
|
else
|
|
|
|
logger.log user:user, "finished subscribing user to the newsletter"
|
2014-02-13 07:37:23 -05:00
|
|
|
callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-02-13 07:37:23 -05:00
|
|
|
unsubscribe: (user, callback = () ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user:user, email:user.email, "trying to unsubscribe user to the mailing list"
|
|
|
|
options = buildOptions(user, false)
|
2018-08-04 12:30:24 -04:00
|
|
|
mailchimp.request options, (err)->
|
|
|
|
if err?
|
2018-08-28 07:32:20 -04:00
|
|
|
logger.err err:err, user:user, "error unsubscribing person to newsletter"
|
2018-08-04 12:30:24 -04:00
|
|
|
else
|
|
|
|
logger.log user:user, "finished unsubscribing user to the newsletter"
|
2014-02-13 07:37:23 -05:00
|
|
|
callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-08-07 16:38:31 -04:00
|
|
|
changeEmail: (oldEmail, newEmail, callback = ()->)->
|
|
|
|
options = buildOptions({email:oldEmail})
|
|
|
|
delete options.body.status
|
|
|
|
options.body.email_address = newEmail
|
2018-08-28 07:46:49 -04:00
|
|
|
logger.log {oldEmail, newEmail, options}, "changing email in newsletter"
|
2018-08-07 16:38:31 -04:00
|
|
|
mailchimp.request options, (err)->
|
2018-08-28 07:32:20 -04:00
|
|
|
if err? and err?.message?.indexOf("merge fields were invalid") != -1
|
|
|
|
logger.log {oldEmail, newEmail}, "unable to change email in newsletter, user has never subscribed"
|
2018-08-28 05:47:33 -04:00
|
|
|
return callback()
|
2018-08-28 07:32:20 -04:00
|
|
|
else if err? and err?.message?.indexOf("could not be validated") != -1
|
|
|
|
logger.log {oldEmail, newEmail},
|
|
|
|
"unable to change email in newsletter, user has previously unsubscribed or new email already exist on list"
|
2018-08-31 06:04:07 -04:00
|
|
|
return callback()
|
2018-08-28 09:26:51 -04:00
|
|
|
else if err? and err.message.indexOf("is already a list member") != -1
|
|
|
|
logger.log {oldEmail, newEmail},
|
|
|
|
"unable to change email in newsletter, new email is already on mailing list"
|
|
|
|
return callback()
|
2018-08-31 06:04:07 -04:00
|
|
|
else if err? and err?.message?.indexOf("looks fake or invalid") != -1
|
|
|
|
logger.log {oldEmail, newEmail},
|
|
|
|
"unable to change email in newsletter, email looks fake to mailchimp"
|
|
|
|
return callback()
|
2018-08-28 07:32:20 -04:00
|
|
|
else if err?
|
|
|
|
logger.err {err, oldEmail, newEmail}, "error changing email in newsletter"
|
2018-08-07 16:38:31 -04:00
|
|
|
return callback(err)
|
|
|
|
else
|
|
|
|
logger.log "finished changing email in the newsletter"
|
|
|
|
return callback()
|
|
|
|
|
2018-08-04 12:30:24 -04:00
|
|
|
hashEmail = (email)->
|
|
|
|
crypto.createHash('md5').update(email.toLowerCase()).digest("hex")
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
buildOptions = (user, is_subscribed)->
|
2018-08-04 12:30:24 -04:00
|
|
|
subscriber_hash = hashEmail(user.email)
|
2018-08-28 08:34:53 -04:00
|
|
|
status = if is_subscribed then "subscribed" else "unsubscribed"
|
2018-08-04 12:30:24 -04:00
|
|
|
opts =
|
|
|
|
method: "PUT"
|
|
|
|
path: "/lists/#{Settings.mailchimp?.list_id}/members/#{subscriber_hash}"
|
|
|
|
body:
|
|
|
|
email_address:user.email
|
2018-08-28 08:34:53 -04:00
|
|
|
status_if_new: status
|
2018-08-28 07:32:20 -04:00
|
|
|
|
2018-08-28 08:34:53 -04:00
|
|
|
#only set status if we explictly want to set it
|
2018-08-28 07:32:20 -04:00
|
|
|
if is_subscribed?
|
|
|
|
opts.body.status = status
|
|
|
|
|
|
|
|
if user._id?
|
|
|
|
opts.body.merge_fields =
|
|
|
|
FNAME: user.first_name
|
|
|
|
LNAME: user.last_name
|
|
|
|
MONGO_ID:user._id
|
|
|
|
|
2018-08-04 12:30:24 -04:00
|
|
|
return opts
|
|
|
|
|