Vulcan/packages/telescope-newsletter/lib/newsletter.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

201 lines
4 KiB
JavaScript

var campaignSchema = new SimpleSchema({
_id: {
type: String,
optional: true
},
createdAt: {
type: Date,
optional: true
},
sentAt: {
type: String,
optional: true
},
status: {
type: String,
optional: true
},
posts: {
type: [String],
optional: true
},
webHits: {
type: Number,
optional: true
},
});
Campaigns = new Meteor.Collection("campaigns", {
schema: campaignSchema
});
Posts.registerField(
{
propertyName: 'scheduledAt',
propertySchema: {
type: Date,
optional: true,
autoform: {
omit: true
}
}
}
);
// Settings
var enableNewsletter = {
propertyName: 'enableNewsletter',
propertySchema: {
type: Boolean,
optional: true,
autoform: {
group: 'newsletter',
instructions: 'Enable newsletter (requires restart).'
}
}
};
Settings.registerField(enableNewsletter);
var showBanner = {
propertyName: 'showBanner',
propertySchema: {
type: Boolean,
optional: true,
label: 'Newsletter banner',
autoform: {
group: 'newsletter',
instructions: 'Show newsletter sign-up form on the front page.'
}
}
};
Settings.registerField(showBanner);
var mailChimpAPIKey = {
propertyName: "mailChimpAPIKey",
propertySchema: {
type: String,
optional: true,
private: true,
autoform: {
group: "newsletter",
class: "private-field"
}
}
};
Settings.registerField(mailChimpAPIKey);
var mailChimpListId = {
propertyName: 'mailChimpListId',
propertySchema: {
type: String,
optional: true,
private: true,
autoform: {
group: 'newsletter',
instructions: 'The ID of the list you want to send to.',
class: "private-field"
}
}
};
Settings.registerField(mailChimpListId);
var postsPerNewsletter = {
propertyName: 'postsPerNewsletter',
propertySchema: {
type: Number,
optional: true,
autoform: {
group: 'newsletter'
}
}
};
Settings.registerField(postsPerNewsletter);
var newsletterFrequency = {
propertyName: 'newsletterFrequency',
propertySchema: {
type: Number,
optional: true,
autoform: {
group: 'newsletter',
instructions: 'Defaults to once a week. Changes require restarting your app to take effect.',
options: [
{
value: 1,
label: 'Every Day'
},
{
value: 2,
label: 'Mondays, Wednesdays, Fridays'
},
{
value: 3,
label: 'Mondays & Thursdays'
},
{
value: 7,
label: 'Once a week (Mondays)'
}
]
}
}
};
Settings.registerField(newsletterFrequency);
var newsletterTime = {
propertyName: 'newsletterTime',
propertySchema: {
type: String,
optional: true,
defaultValue: '00:00',
autoform: {
group: 'newsletter',
instructions: 'Defaults to 00:00/12:00 AM. Time to send out newsletter if enabled.',
type: 'time'
}
}
};
Settings.registerField(newsletterTime);
var autoSubscribe = {
propertyName: 'autoSubscribe',
propertySchema: {
type: Boolean,
optional: true,
autoform: {
group: 'newsletter',
instructions: 'Automatically subscribe new users on sign-up.'
}
}
};
Settings.registerField(autoSubscribe);
// create new "campaign" lens for all posts from the past X days that haven't been scheduled yet
Telescope.viewParameters.campaign = function (terms) {
return {
find: {
scheduledAt: {$exists: false},
postedAt: {
$gte: terms.after
}
},
options: {sort: {sticky: -1, score: -1}}
};
};
Telescope.modules.register("hero", {
template: 'newsletterBanner',
order: 10
});
function subscribeUserOnCreation (user) {
if (!!Settings.get('autoSubscribe') && !!Users.getEmail(user)) {
addToMailChimpList(user, false, function (error, result) {
console.log(error);
console.log(result);
});
}
return user;
}
Telescope.callbacks.register("userCreated", subscribeUserOnCreation);