fixing newsletter sync/async issue

This commit is contained in:
Sacha Greif 2015-01-15 09:41:28 +09:00
parent f01f084525
commit 1fd47b23f0
4 changed files with 88 additions and 346 deletions

View file

@ -39,6 +39,7 @@
display: inline-block; display: inline-block;
float: none; float: none;
vertical-align: middle; vertical-align: middle;
position: relative;
} }
.show-loader{ .show-loader{
pointer-events:none; pointer-events:none;
@ -60,6 +61,7 @@
left: 0px; left: 0px;
right: 0px; right: 0px;
display: block; display: block;
opacity: 0.8;
} }
@media screen and (max-width: 30em) { @media screen and (max-width: 30em) {

View file

@ -81,6 +81,6 @@ scheduleNextCampaign = function (isTest) {
Meteor.methods({ Meteor.methods({
testCampaign: function () { testCampaign: function () {
if(isAdminById(this.userId)) if(isAdminById(this.userId))
scheduleNextCampaign(true); return scheduleNextCampaign(true);
} }
}); });

View file

@ -1,80 +1,70 @@
var htmlToText = Npm.require('html-to-text');
scheduleCampaign = function (campaign, isTest) { scheduleCampaign = function (campaign, isTest) {
var isTest = typeof isTest === 'undefined' ? false : isTest; var isTest = typeof isTest === 'undefined' ? false : isTest;
MailChimpOptions.apiKey = getSetting('mailChimpAPIKey'); var apiKey = getSetting('mailChimpAPIKey');
MailChimpOptions.listId = getSetting('mailChimpListId'); var listId = getSetting('mailChimpListId');
var htmlToText = Npm.require('html-to-text'); if(!!apiKey && !!listId){
var text = htmlToText.fromString(campaign.html, {
wordwrap: 130
});
var defaultEmail = getSetting('defaultEmail');
var result= '';
if(!!MailChimpOptions.apiKey && !!MailChimpOptions.listId){
console.log( 'Creating campaign…');
try { try {
var api = new MailChimp();
} catch ( error ) { var api = new MailChimp(apiKey);
console.log( error.message ); 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));
} catch (error) {
console.log(error);
} }
return campaign.subject;
api.call( 'campaigns', 'create', {
type: 'regular',
options: {
list_id: MailChimpOptions.listId,
subject: campaign.subject,
from_email: getSetting('defaultEmail'),
from_name: getSetting('title')+ ' Top Posts',
},
content: {
html: campaign.html,
text: text
}
}, Meteor.bindEnvironment(function ( error, result ) {
if ( error ) {
console.log( error.message );
result = error.message;
} else {
console.log( 'Campaign created');
// console.log( JSON.stringify( result ) );
var cid = result.id;
var archive_url = result.archive_url;
var scheduledTime = moment().zone(0).add(1, 'hours').format("YYYY-MM-DD HH:mm:ss");
api.call('campaigns', 'schedule', {
cid: cid,
schedule_time: scheduledTime
}, Meteor.bindEnvironment(function ( error, result) {
if (error) {
console.log( error.message );
result = error.message;
}else{
console.log('Campaign scheduled for '+scheduledTime);
console.log(campaign.subject)
// console.log( JSON.stringify( result ) );
// 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: archive_url,
subject: campaign.subject
});
sendEmail(defaultEmail, 'Newsletter scheduled', buildEmailTemplate(confirmationHtml));
result = campaign.subject;
}
}));
}
}));
} }
return result;
} }
addToMailChimpList = function(userOrEmail, confirm, done){ addToMailChimpList = function(userOrEmail, confirm, done){
@ -83,6 +73,7 @@ addToMailChimpList = function(userOrEmail, confirm, done){
var confirm = (typeof confirm === 'undefined') ? false : confirm // default to no confirmation 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") { if (typeof userOrEmail == "string") {
user = null; user = null;
email = userOrEmail; email = userOrEmail;
@ -93,37 +84,37 @@ addToMailChimpList = function(userOrEmail, confirm, done){
throw 'User must have an email address'; throw 'User must have an email address';
} }
MailChimpOptions.apiKey = getSetting('mailChimpAPIKey'); var apiKey = getSetting('mailChimpAPIKey');
MailChimpOptions.listId = getSetting('mailChimpListId'); var listId = getSetting('mailChimpListId');
// add a user to a MailChimp list. // add a user to a MailChimp list.
// called when a new user is created, or when an existing user fills in their email // called when a new user is created, or when an existing user fills in their email
if(!!MailChimpOptions.apiKey && !!MailChimpOptions.listId){ if(!!apiKey && !!listId){
console.log('adding "'+email+'" to MailChimp list…');
try { try {
var api = new MailChimp();
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) { } catch (error) {
console.log( error.message ); console.log( error.message );
} }
api.call('lists', 'subscribe', {
id: MailChimpOptions.listId,
email: {"email": email},
double_optin: confirm
}, Meteor.bindEnvironment(function ( error, result ) {
if (error) {
console.log( error.message );
done(error, null);
} else {
console.log( JSON.stringify( result ) );
if(!!user)
setUserSetting('subscribedToNewsletter', true, user);
done(null, result);
}
}));
} }
}; };
syncAddToMailChimpList = Async.wrap(addToMailChimpList); syncAddToMailChimpList = Async.wrap(addToMailChimpList);
@ -144,4 +135,4 @@ Meteor.methods({
throw new Meteor.Error(500, error.message); throw new Meteor.Error(500, error.message);
} }
} }
}) });

View file

@ -1,251 +0,0 @@
{
"dependencies": [
[
"aldeed:simple-schema",
"1.2.0"
],
[
"application-configuration",
"1.0.3"
],
[
"base64",
"1.0.1"
],
[
"binary-heap",
"1.0.1"
],
[
"blaze",
"2.0.3"
],
[
"blaze-tools",
"1.0.1"
],
[
"boilerplate-generator",
"1.0.1"
],
[
"callback-hook",
"1.0.1"
],
[
"check",
"1.0.2"
],
[
"cmather:handlebars-server",
"2.0.0"
],
[
"coffeescript",
"1.0.4"
],
[
"ddp",
"1.0.12"
],
[
"deps",
"1.0.5"
],
[
"ejson",
"1.0.4"
],
[
"follower-livedata",
"1.0.2"
],
[
"geojson-utils",
"1.0.1"
],
[
"handlebars",
"1.0.1"
],
[
"html-tools",
"1.0.2"
],
[
"htmljs",
"1.0.2"
],
[
"id-map",
"1.0.1"
],
[
"iron:controller",
"1.0.3"
],
[
"iron:core",
"1.0.4"
],
[
"iron:dynamic-template",
"1.0.5"
],
[
"iron:layout",
"1.0.5"
],
[
"iron:location",
"1.0.4"
],
[
"iron:middleware-stack",
"1.0.3"
],
[
"iron:router",
"1.0.3"
],
[
"iron:url",
"1.0.4"
],
[
"jquery",
"1.0.1"
],
[
"json",
"1.0.1"
],
[
"logging",
"1.0.5"
],
[
"meteor",
"1.1.3"
],
[
"meteorhacks:async",
"1.0.0"
],
[
"meteorhacks:npm",
"1.2.1"
],
[
"minifiers",
"1.1.2"
],
[
"minimongo",
"1.0.5"
],
[
"mongo",
"1.0.9"
],
[
"mrt:cookies",
"0.3.0"
],
[
"mrt:mailchimp",
"0.4.0"
],
[
"observe-sequence",
"1.0.3"
],
[
"ordered-dict",
"1.0.1"
],
[
"percolatestudio:synced-cron",
"1.1.0"
],
[
"random",
"1.0.1"
],
[
"reactive-dict",
"1.0.4"
],
[
"reactive-var",
"1.0.3"
],
[
"retry",
"1.0.1"
],
[
"routepolicy",
"1.0.2"
],
[
"session",
"1.0.4"
],
[
"spacebars",
"1.0.3"
],
[
"spacebars-compiler",
"1.0.3"
],
[
"tap:http-methods",
"0.0.23"
],
[
"tap:i18n",
"1.2.1"
],
[
"telescope-base",
"0.0.0"
],
[
"telescope-i18n",
"0.0.0"
],
[
"telescope-lib",
"0.2.9"
],
[
"templating",
"1.0.9"
],
[
"tracker",
"1.0.3"
],
[
"ui",
"1.0.4"
],
[
"underscore",
"1.0.1"
],
[
"webapp",
"1.1.4"
],
[
"webapp-hashing",
"1.0.1"
]
],
"pluginDependencies": [],
"toolVersion": "meteor-tool@1.0.36",
"format": "1.0"
}