changed newsletter to use mailchimp

This commit is contained in:
Henry Oswald 2018-08-04 17:30:24 +01:00
parent a828298856
commit 17a63258c6
3 changed files with 37 additions and 21 deletions

View file

@ -1,37 +1,52 @@
async = require('async')
Request = require('request')
logger = require 'logger-sharelatex'
Settings = require 'settings-sharelatex'
crypto = require('crypto')
Mailchimp = require('mailchimp-api-v3')
mailchimp = new Mailchimp(Settings.mailchimp?.api_key) if Settings.mailchimp?
module.exports =
subscribe: (user, callback = () ->)->
if !Settings.markdownmail?
if !Settings.mailchimp?
logger.warn "No newsletter provider configured so not subscribing user"
return callback()
logger.log user:user, email:user.email, "trying to subscribe user to the mailing list"
options = buildOptions(user, true)
Request.post options, (err, response, body)->
logger.log body:body, user:user, "finished attempting to subscribe the user to the news letter"
logger.log options:options, user:user, email:user.email, "trying to subscribe user to the mailing list"
mailchimp.request options, (err)->
if err?
logger.err err:err, "error subscribing person to newsletter"
else
logger.log user:user, "finished subscribing user to the newsletter"
callback(err)
unsubscribe: (user, callback = () ->)->
if !Settings.markdownmail?
if !Settings.mailchimp?
logger.warn "No newsletter provider configured so not unsubscribing user"
return callback()
logger.log user:user, email:user.email, "trying to unsubscribe user to the mailing list"
options = buildOptions(user, false)
Request.post options, (err, response, body)->
logger.log err:err, body:body, email:user.email, "compled newsletter unsubscribe attempt"
mailchimp.request options, (err)->
if err?
logger.err err:err, "error unsubscribing person to newsletter"
else
logger.log user:user, "finished unsubscribing user to the newsletter"
callback(err)
hashEmail = (email)->
crypto.createHash('md5').update(email.toLowerCase()).digest("hex")
buildOptions = (user, is_subscribed)->
options =
json:
secret_token: Settings.markdownmail.secret
name: "#{user.first_name} #{user.last_name}"
email: user.email
subscriber_list_id: Settings.markdownmail.list_id
is_subscribed: is_subscribed
url: "https://www.markdownmail.io/lists/subscribe"
timeout: 30 * 1000
return options
status = if is_subscribed then "subscribed" else "unsubscribed"
subscriber_hash = hashEmail(user.email)
opts =
method: "PUT"
path: "/lists/#{Settings.mailchimp?.list_id}/members/#{subscriber_hash}"
body:
status_if_new: status
status: status
email_address:user.email
merge_fields:
FNAME: user.first_name
LNAME: user.last_name
return opts

View file

@ -277,10 +277,10 @@ module.exports = settings =
# Third party services
# --------------------
#
# ShareLaTeX's regular newsletter is managed by Markdown mail. Add your
# ShareLaTeX's regular newsletter is managed by mailchimp. Add your
# credentials here to integrate with this.
# markdownmail:
# secret: ""
# mailchimp:
# api_key: ""
# list_id: ""
#
# Fill in your unique token from various analytics services to enable

View file

@ -59,6 +59,7 @@
"lodash": "^4.13.1",
"logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master",
"lynx": "0.1.1",
"mailchimp-api-v3": "^1.12.0",
"marked": "^0.3.5",
"method-override": "^2.3.3",
"metrics-sharelatex": "git+https://github.com/sharelatex/metrics-sharelatex.git#v1.7.1",