Vulcan/packages/telescope-newsletter/lib/server/mailchimp.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

145 lines
3.9 KiB
JavaScript

var htmlToText = Npm.require('html-to-text');
scheduleCampaign = function (campaign, isTest) {
var isTest = typeof isTest === 'undefined' ? false : isTest;
var apiKey = Settings.get('mailChimpAPIKey');
var listId = Settings.get('mailChimpListId');
if(!!apiKey && !!listId){
var wordCount = 15;
var subject = campaign.subject;
while (subject.length >= 150){
subject = Telescope.utils.trimWords(subject, wordCount);
wordCount--;
}
try {
var api = new MailChimp(apiKey);
var text = htmlToText.fromString(campaign.html, {wordwrap: 130});
var defaultEmail = Settings.get('defaultEmail');
var campaignOptions = {
type: 'regular',
options: {
list_id: listId,
subject: subject,
from_email: defaultEmail,
from_name: Settings.get('title')+ ' Top Posts',
},
content: {
html: campaign.html,
text: text
}
};
console.log( '// Creating campaign…');
// create campaign
var mailchimpCampaign = api.call( 'campaigns', 'create', campaignOptions);
console.log( '// Campaign created');
// console.log(campaign)
var scheduledTime = moment().utcOffset(0).add(1, 'hours').format("YYYY-MM-DD HH:mm:ss");
var scheduleOptions = {
cid: mailchimpCampaign.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)
var updated = Posts.update({_id: {$in: campaign.postIds}}, {$set: {scheduledAt: new Date()}}, {multi: true})
// send confirmation email
var confirmationHtml = getEmailTemplate('emailDigestConfirmation')({
time: scheduledTime,
newsletterLink: mailchimpCampaign.archive_url,
subject: subject
});
sendEmail(defaultEmail, 'Newsletter scheduled', buildEmailTemplate(confirmationHtml));
} catch (error) {
console.log(error);
}
return subject;
}
};
addToMailChimpList = function(userOrEmail, confirm, done){
var user, email;
var confirm = (typeof confirm === 'undefined') ? false : confirm; // default to no confirmation
// not sure if it's really necessary that the function take both user and email?
if (typeof userOrEmail === "string") {
user = null;
email = userOrEmail;
} else if (typeof userOrEmail === "object") {
user = userOrEmail;
email = Users.getEmail(user);
if (!email)
throw 'User must have an email address';
}
var apiKey = Settings.get('mailChimpAPIKey');
var listId = Settings.get('mailChimpListId');
// add a user to a MailChimp list.
// called when a new user is created, or when an existing user fills in their email
if(!!apiKey && !!listId){
try {
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)
Users.setUserSetting('subscribedToNewsletter', true, user);
console.log("// User subscribed");
return subscribe;
} catch (error) {
throw new Meteor.Error("subscription-failed", error.message);
}
}
};
Meteor.methods({
addCurrentUserToMailChimpList: function(){
var currentUser = Meteor.users.findOne(this.userId);
try {
return addToMailChimpList(currentUser, false);
} catch (error) {
throw new Meteor.Error(500, error.message);
}
},
addEmailToMailChimpList: function (email) {
try {
return addToMailChimpList(email, true);
} catch (error) {
throw new Meteor.Error(500, error.message);
}
}
});