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

105 lines
3.1 KiB
JavaScript

var htmlToText = Npm.require('html-to-text');
// check if server-side template has been customized, and return the correct template
getEmailTemplate = function (template) {
var emailTemplate = Handlebars.templates[template];
if(typeof emailTemplate === 'function'){
return Handlebars.templates[template];
} else {
console.log('Cannot find template '+template+', defaulting to '+template);
return Handlebars.templates[template];
}
};
buildEmailTemplate = function (htmlContent) {
var emailProperties = {
secondaryColor: Settings.get('secondaryColor', '#444444'),
accentColor: Settings.get('accentColor', '#DD3416'),
siteName: Settings.get('title'),
tagline: Settings.get('tagline'),
siteUrl: Telescope.utils.getSiteUrl(),
body: htmlContent,
unsubscribe: '',
accountLink: Telescope.utils.getSiteUrl()+'account',
footer: Settings.get('emailFooter'),
logoUrl: Settings.get('logoUrl'),
logoHeight: Settings.get('logoHeight'),
logoWidth: Settings.get('logoWidth')
};
var emailHTML = Handlebars.templates.emailWrapper(emailProperties);
var inlinedHTML = juice(emailHTML);
var doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
return doctype+inlinedHTML;
};
sendEmail = function(to, subject, html, text){
// TODO: limit who can send emails
// TODO: fix this error: Error: getaddrinfo ENOTFOUND
var from = Settings.get('defaultEmail', 'noreply@example.com');
var siteName = Settings.get('title', 'Telescope');
subject = '['+siteName+'] '+subject;
if (typeof text === 'undefined'){
// Auto-generate text version if it doesn't exist. Has bugs, but should be good enough.
var text = htmlToText.fromString(html, {
wordwrap: 130
});
}
console.log('//////// sending email…');
console.log('from: '+from);
console.log('to: '+to);
console.log('subject: '+subject);
// console.log('html: '+html);
// console.log('text: '+text);
var email = {
from: from,
to: to,
subject: subject,
text: text,
html: html
};
Email.send(email);
return email;
};
buildAndSendEmail = function (to, subject, template, properties) {
var html = buildEmailTemplate(getEmailTemplate(template)(properties));
return sendEmail (to, subject, html);
};
Meteor.methods({
testEmail: function () {
if(Users.is.adminById(this.userId)){
var email = buildAndSendEmail (Settings.get('defaultEmail'), 'Telescope email test', 'emailTest', {date: new Date()});
}
}
});
function adminUserCreationNotification (user) {
// send notifications to admins
var admins = Users.adminUsers();
admins.forEach(function(admin){
if(Users.getUserSetting('notifications.users', false, admin)){
var emailProperties = {
profileUrl: Users.getProfileUrl(user),
username: Users.getUserName(user)
};
var html = getEmailTemplate('emailNewUser')(emailProperties);
sendEmail(Users.getEmail(admin), 'New user account: '+Users.getUserName(user), buildEmailTemplate(html));
}
});
return user;
}
Telescope.callbacks.register("userCreated", adminUserCreationNotification);