Vulcan/packages/telescope-newsletter/lib/server/mailchimp.js

138 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-01-15 09:41:28 +09:00
var htmlToText = Npm.require('html-to-text');
scheduleCampaign = function (campaign, isTest) {
var isTest = typeof isTest === 'undefined' ? false : isTest;
2015-01-15 09:41:28 +09:00
var apiKey = getSetting('mailChimpAPIKey');
var listId = getSetting('mailChimpListId');
2014-08-29 10:37:35 +09:00
2015-01-15 09:41:28 +09:00
if(!!apiKey && !!listId){
2014-08-29 10:37:35 +09:00
2015-01-15 09:41:28 +09:00
try {
2014-08-29 10:37:35 +09:00
2015-01-15 09:41:28 +09:00
var api = new MailChimp(apiKey);
var text = htmlToText.fromString(campaign.html, {wordwrap: 130});
var defaultEmail = getSetting('defaultEmail');
var campaignOptions = {
type: 'regular',
options: {
list_id: listId,
subject: campaign.subject,
from_email: defaultEmail,
from_name: getSetting('title')+ ' Top Posts',
},
content: {
html: campaign.html,
text: text
}
};
console.log( '// Creating campaign…');
// create campaign
var campaign = api.call( 'campaigns', 'create', campaignOptions);
console.log( '// Campaign created');
// console.log(campaign)
var scheduledTime = moment().zone(0).add(1, 'hours').format("YYYY-MM-DD HH:mm:ss");
var scheduleOptions = {
cid: campaign.id,
schedule_time: scheduledTime
};
// schedule campaign
var schedule = api.call('campaigns', 'schedule', scheduleOptions);
console.log('// Campaign scheduled for '+scheduledTime);
// console.log(schedule)
// if this is not a test, mark posts as sent
if (!isTest)
Posts.update({_id: {$in: campaign.postIds}}, {$set: {scheduledAt: new Date()}}, {multi: true})
// send confirmation email
var confirmationHtml = getEmailTemplate('emailDigestConfirmation')({
time: scheduledTime,
newsletterLink: campaign.archive_url,
subject: campaign.subject
});
sendEmail(defaultEmail, 'Newsletter scheduled', buildEmailTemplate(confirmationHtml));
2014-08-29 10:37:35 +09:00
2015-01-15 09:41:28 +09:00
} catch (error) {
console.log(error);
2014-08-29 10:37:35 +09:00
}
2015-01-15 09:41:28 +09:00
return campaign.subject;
2014-08-29 10:37:35 +09:00
}
}
addToMailChimpList = function(userOrEmail, confirm, done){
2014-08-29 10:37:35 +09:00
var user, email;
var confirm = (typeof confirm === 'undefined') ? false : confirm // default to no confirmation
2015-01-15 09:41:28 +09:00
// not sure if it's really necessary that the function take both user and email?
if (typeof userOrEmail == "string") {
2014-08-29 10:37:35 +09:00
user = null;
email = userOrEmail;
} else if (typeof userOrEmail == "object") {
2014-08-29 10:37:35 +09:00
user = userOrEmail;
email = getEmail(user);
if (!email)
throw 'User must have an email address';
}
2015-01-15 09:41:28 +09:00
var apiKey = getSetting('mailChimpAPIKey');
var listId = getSetting('mailChimpListId');
2014-08-29 10:37:35 +09:00
// add a user to a MailChimp list.
// called when a new user is created, or when an existing user fills in their email
2015-01-15 09:41:28 +09:00
if(!!apiKey && !!listId){
2014-08-29 10:37:35 +09:00
try {
2015-01-15 09:41:28 +09:00
console.log('// Adding "'+email+'" to MailChimp list…');
var api = new MailChimp(apiKey);
var subscribeOptions = {
id: listId,
email: {"email": email},
double_optin: confirm
};
// subscribe user
var subscribe = api.call('lists', 'subscribe', subscribeOptions);
// mark user as subscribed
if(!!user)
setUserSetting('subscribedToNewsletter', true, user);
console.log("// User subscribed");
} catch (error) {
2015-01-15 09:41:28 +09:00
console.log( error.message );
2014-08-29 10:37:35 +09:00
}
}
};
syncAddToMailChimpList = Async.wrap(addToMailChimpList);
Meteor.methods({
addCurrentUserToMailChimpList: function(){
var currentUser = Meteor.users.findOne(this.userId);
try {
return syncAddToMailChimpList(currentUser, false);
} catch (error) {
throw new Meteor.Error(500, error.message);
}
},
addEmailToMailChimpList: function (email) {
try {
return syncAddToMailChimpList(email, true);
} catch (error) {
throw new Meteor.Error(500, error.message);
}
}
2015-01-15 09:41:28 +09:00
});