mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-09 15:35:47 +01:00
style
This commit is contained in:
266
src/emailer.js
266
src/emailer.js
@@ -23,146 +23,146 @@ var transports = {
|
|||||||
var app;
|
var app;
|
||||||
var fallbackTransport;
|
var fallbackTransport;
|
||||||
|
|
||||||
(function (Emailer) {
|
var Emailer = module.exports;
|
||||||
Emailer.registerApp = function (expressApp) {
|
|
||||||
app = expressApp;
|
|
||||||
|
|
||||||
// Enable Gmail transport if enabled in ACP
|
|
||||||
if (parseInt(meta.config['email:GmailTransport:enabled'], 10) === 1) {
|
Emailer.registerApp = function (expressApp) {
|
||||||
transports.gmail = nodemailer.createTransport(smtpTransport({
|
app = expressApp;
|
||||||
host: 'smtp.gmail.com',
|
|
||||||
port: 465,
|
// Enable Gmail transport if enabled in ACP
|
||||||
secure: true,
|
if (parseInt(meta.config['email:GmailTransport:enabled'], 10) === 1) {
|
||||||
auth: {
|
transports.gmail = nodemailer.createTransport(smtpTransport({
|
||||||
user: meta.config['email:GmailTransport:user'],
|
host: 'smtp.gmail.com',
|
||||||
pass: meta.config['email:GmailTransport:pass'],
|
port: 465,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: meta.config['email:GmailTransport:user'],
|
||||||
|
pass: meta.config['email:GmailTransport:pass'],
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
fallbackTransport = transports.gmail;
|
||||||
|
} else {
|
||||||
|
fallbackTransport = transports.sendmail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Emailer;
|
||||||
|
};
|
||||||
|
|
||||||
|
Emailer.send = function (template, uid, params, callback) {
|
||||||
|
callback = callback || function () {};
|
||||||
|
if (!app) {
|
||||||
|
winston.warn('[emailer] App not ready!');
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
email: async.apply(User.getUserField, uid, 'email'),
|
||||||
|
settings: async.apply(User.getSettings, uid),
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (results, next) {
|
||||||
|
if (!results.email) {
|
||||||
|
winston.warn('uid : ' + uid + ' has no email, not sending.');
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
params.uid = uid;
|
||||||
|
Emailer.sendToEmail(template, results.email, results.settings.userLang, params, next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Emailer.sendToEmail = function (template, email, language, params, callback) {
|
||||||
|
callback = callback || function () {};
|
||||||
|
|
||||||
|
var lang = language || meta.config.defaultLang || 'en-GB';
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
html: function (next) {
|
||||||
|
renderAndTranslate('emails/' + template, params, lang, next);
|
||||||
},
|
},
|
||||||
}));
|
subject: function (next) {
|
||||||
fallbackTransport = transports.gmail;
|
translator.translate(params.subject, lang, function (translated) {
|
||||||
} else {
|
next(null, translated);
|
||||||
fallbackTransport = transports.sendmail;
|
});
|
||||||
}
|
},
|
||||||
|
}, next);
|
||||||
return Emailer;
|
},
|
||||||
};
|
function (results, next) {
|
||||||
|
var data = {
|
||||||
Emailer.send = function (template, uid, params, callback) {
|
_raw: params,
|
||||||
callback = callback || function () {};
|
to: email,
|
||||||
if (!app) {
|
from: meta.config['email:from'] || 'no-reply@' + getHostname(),
|
||||||
winston.warn('[emailer] App not ready!');
|
from_name: meta.config['email:from_name'] || 'NodeBB',
|
||||||
return callback();
|
subject: results.subject,
|
||||||
}
|
html: results.html,
|
||||||
|
plaintext: htmlToText.fromString(results.html, {
|
||||||
async.waterfall([
|
ignoreImage: true,
|
||||||
function (next) {
|
}),
|
||||||
async.parallel({
|
template: template,
|
||||||
email: async.apply(User.getUserField, uid, 'email'),
|
uid: params.uid,
|
||||||
settings: async.apply(User.getSettings, uid),
|
pid: params.pid,
|
||||||
}, next);
|
fromUid: params.fromUid,
|
||||||
},
|
};
|
||||||
function (results, next) {
|
Plugins.fireHook('filter:email.modify', data, next);
|
||||||
if (!results.email) {
|
},
|
||||||
winston.warn('uid : ' + uid + ' has no email, not sending.');
|
function (data, next) {
|
||||||
return next();
|
if (Plugins.hasListeners('filter:email.send')) {
|
||||||
}
|
Plugins.fireHook('filter:email.send', data, next);
|
||||||
params.uid = uid;
|
|
||||||
Emailer.sendToEmail(template, results.email, results.settings.userLang, params, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Emailer.sendToEmail = function (template, email, language, params, callback) {
|
|
||||||
callback = callback || function () {};
|
|
||||||
|
|
||||||
var lang = language || meta.config.defaultLang || 'en-GB';
|
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
html: function (next) {
|
|
||||||
renderAndTranslate('emails/' + template, params, lang, next);
|
|
||||||
},
|
|
||||||
subject: function (next) {
|
|
||||||
translator.translate(params.subject, lang, function (translated) {
|
|
||||||
next(null, translated);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
var data = {
|
|
||||||
_raw: params,
|
|
||||||
to: email,
|
|
||||||
from: meta.config['email:from'] || 'no-reply@' + getHostname(),
|
|
||||||
from_name: meta.config['email:from_name'] || 'NodeBB',
|
|
||||||
subject: results.subject,
|
|
||||||
html: results.html,
|
|
||||||
plaintext: htmlToText.fromString(results.html, {
|
|
||||||
ignoreImage: true,
|
|
||||||
}),
|
|
||||||
template: template,
|
|
||||||
uid: params.uid,
|
|
||||||
pid: params.pid,
|
|
||||||
fromUid: params.fromUid,
|
|
||||||
};
|
|
||||||
Plugins.fireHook('filter:email.modify', data, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
if (Plugins.hasListeners('filter:email.send')) {
|
|
||||||
Plugins.fireHook('filter:email.send', data, next);
|
|
||||||
} else {
|
|
||||||
Emailer.sendViaFallback(data, next);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
if (err && err.code === 'ENOENT') {
|
|
||||||
callback(new Error('[[error:sendmail-not-found]]'));
|
|
||||||
} else {
|
} else {
|
||||||
callback(err);
|
Emailer.sendViaFallback(data, next);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
};
|
], function (err) {
|
||||||
|
if (err && err.code === 'ENOENT') {
|
||||||
Emailer.sendViaFallback = function (data, callback) {
|
callback(new Error('[[error:sendmail-not-found]]'));
|
||||||
// Some minor alterations to the data to conform to nodemailer standard
|
|
||||||
data.text = data.plaintext;
|
|
||||||
delete data.plaintext;
|
|
||||||
|
|
||||||
// NodeMailer uses a combined "from"
|
|
||||||
data.from = data.from_name + '<' + data.from + '>';
|
|
||||||
delete data.from_name;
|
|
||||||
|
|
||||||
winston.verbose('[emailer] Sending email to uid ' + data.uid + ' (' + data.to + ')');
|
|
||||||
fallbackTransport.sendMail(data, function (err) {
|
|
||||||
if (err) {
|
|
||||||
winston.error(err);
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function render(tpl, params, next) {
|
|
||||||
if (meta.config['email:custom:' + tpl.replace('emails/', '')]) {
|
|
||||||
var text = templates.parse(meta.config['email:custom:' + tpl.replace('emails/', '')], params);
|
|
||||||
next(null, text);
|
|
||||||
} else {
|
} else {
|
||||||
app.render(tpl, params, next);
|
callback(err);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
};
|
||||||
|
|
||||||
function renderAndTranslate(tpl, params, lang, callback) {
|
Emailer.sendViaFallback = function (data, callback) {
|
||||||
render(tpl, params, function (err, html) {
|
// Some minor alterations to the data to conform to nodemailer standard
|
||||||
translator.translate(html, lang, function (translated) {
|
data.text = data.plaintext;
|
||||||
callback(err, translated);
|
delete data.plaintext;
|
||||||
});
|
|
||||||
|
// NodeMailer uses a combined "from"
|
||||||
|
data.from = data.from_name + '<' + data.from + '>';
|
||||||
|
delete data.from_name;
|
||||||
|
|
||||||
|
winston.verbose('[emailer] Sending email to uid ' + data.uid + ' (' + data.to + ')');
|
||||||
|
fallbackTransport.sendMail(data, function (err) {
|
||||||
|
if (err) {
|
||||||
|
winston.error(err);
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function render(tpl, params, next) {
|
||||||
|
if (meta.config['email:custom:' + tpl.replace('emails/', '')]) {
|
||||||
|
var text = templates.parse(meta.config['email:custom:' + tpl.replace('emails/', '')], params);
|
||||||
|
next(null, text);
|
||||||
|
} else {
|
||||||
|
app.render(tpl, params, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderAndTranslate(tpl, params, lang, callback) {
|
||||||
|
render(tpl, params, function (err, html) {
|
||||||
|
translator.translate(html, lang, function (translated) {
|
||||||
|
callback(err, translated);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getHostname() {
|
function getHostname() {
|
||||||
var configUrl = nconf.get('url');
|
var configUrl = nconf.get('url');
|
||||||
var parsed = url.parse(configUrl);
|
var parsed = url.parse(configUrl);
|
||||||
|
|
||||||
return parsed.hostname;
|
|
||||||
}
|
|
||||||
}(module.exports));
|
|
||||||
|
|
||||||
|
return parsed.hostname;
|
||||||
|
}
|
||||||
|
|||||||
211
src/events.js
211
src/events.js
@@ -9,94 +9,95 @@ var batch = require('./batch');
|
|||||||
var user = require('./user');
|
var user = require('./user');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
|
|
||||||
(function (events) {
|
var events = module.exports;
|
||||||
events.log = function (data, callback) {
|
|
||||||
callback = callback || function () {};
|
|
||||||
|
|
||||||
async.waterfall([
|
events.log = function (data, callback) {
|
||||||
function (next) {
|
callback = callback || function () {};
|
||||||
db.incrObjectField('global', 'nextEid', next);
|
|
||||||
},
|
|
||||||
function (eid, next) {
|
|
||||||
data.timestamp = Date.now();
|
|
||||||
data.eid = eid;
|
|
||||||
|
|
||||||
async.parallel([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetAdd('events:time', data.timestamp, eid, next);
|
db.incrObjectField('global', 'nextEid', next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (eid, next) {
|
||||||
db.setObject('event:' + eid, data, next);
|
data.timestamp = Date.now();
|
||||||
},
|
data.eid = eid;
|
||||||
], next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
events.getEvents = function (start, stop, callback) {
|
async.parallel([
|
||||||
async.waterfall([
|
function (next) {
|
||||||
function (next) {
|
db.sortedSetAdd('events:time', data.timestamp, eid, next);
|
||||||
db.getSortedSetRevRange('events:time', start, stop, next);
|
},
|
||||||
},
|
function (next) {
|
||||||
function (eids, next) {
|
db.setObject('event:' + eid, data, next);
|
||||||
var keys = eids.map(function (eid) {
|
},
|
||||||
return 'event:' + eid;
|
], next);
|
||||||
|
},
|
||||||
|
], function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
events.getEvents = function (start, stop, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
db.getSortedSetRevRange('events:time', start, stop, next);
|
||||||
|
},
|
||||||
|
function (eids, next) {
|
||||||
|
var keys = eids.map(function (eid) {
|
||||||
|
return 'event:' + eid;
|
||||||
|
});
|
||||||
|
db.getObjects(keys, next);
|
||||||
|
},
|
||||||
|
function (eventsData, next) {
|
||||||
|
eventsData = eventsData.filter(Boolean);
|
||||||
|
addUserData(eventsData, 'uid', 'user', next);
|
||||||
|
},
|
||||||
|
function (eventsData, next) {
|
||||||
|
addUserData(eventsData, 'targetUid', 'targetUser', next);
|
||||||
|
},
|
||||||
|
function (eventsData, next) {
|
||||||
|
eventsData.forEach(function (event) {
|
||||||
|
Object.keys(event).forEach(function (key) {
|
||||||
|
if (typeof event[key] === 'string') {
|
||||||
|
event[key] = validator.escape(String(event[key] || ''));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
db.getObjects(keys, next);
|
var e = utils.merge(event);
|
||||||
},
|
e.eid = undefined;
|
||||||
function (eventsData, next) {
|
e.uid = undefined;
|
||||||
eventsData = eventsData.filter(Boolean);
|
e.type = undefined;
|
||||||
addUserData(eventsData, 'uid', 'user', next);
|
e.ip = undefined;
|
||||||
},
|
e.user = undefined;
|
||||||
function (eventsData, next) {
|
event.jsonString = JSON.stringify(e, null, 4);
|
||||||
addUserData(eventsData, 'targetUid', 'targetUser', next);
|
event.timestampISO = new Date(parseInt(event.timestamp, 10)).toUTCString();
|
||||||
},
|
});
|
||||||
function (eventsData, next) {
|
next(null, eventsData);
|
||||||
eventsData.forEach(function (event) {
|
},
|
||||||
Object.keys(event).forEach(function (key) {
|
], callback);
|
||||||
if (typeof event[key] === 'string') {
|
};
|
||||||
event[key] = validator.escape(String(event[key] || ''));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
var e = utils.merge(event);
|
|
||||||
e.eid = undefined;
|
|
||||||
e.uid = undefined;
|
|
||||||
e.type = undefined;
|
|
||||||
e.ip = undefined;
|
|
||||||
e.user = undefined;
|
|
||||||
event.jsonString = JSON.stringify(e, null, 4);
|
|
||||||
event.timestampISO = new Date(parseInt(event.timestamp, 10)).toUTCString();
|
|
||||||
});
|
|
||||||
next(null, eventsData);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
function addUserData(eventsData, field, objectName, callback) {
|
function addUserData(eventsData, field, objectName, callback) {
|
||||||
var uids = eventsData.map(function (event) {
|
var uids = eventsData.map(function (event) {
|
||||||
return event && event[field];
|
return event && event[field];
|
||||||
}).filter(function (uid, index, array) {
|
}).filter(function (uid, index, array) {
|
||||||
return uid && array.indexOf(uid) === index;
|
return uid && array.indexOf(uid) === index;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!uids.length) {
|
if (!uids.length) {
|
||||||
return callback(null, eventsData);
|
return callback(null, eventsData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel({
|
|
||||||
isAdmin: function (next) {
|
|
||||||
user.isAdministrator(uids, next);
|
|
||||||
},
|
|
||||||
userData: function (next) {
|
|
||||||
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
|
|
||||||
},
|
|
||||||
}, function (err, results) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
isAdmin: function (next) {
|
||||||
|
user.isAdministrator(uids, next);
|
||||||
|
},
|
||||||
|
userData: function (next) {
|
||||||
|
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
|
||||||
|
},
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (results, next) {
|
||||||
var userData = results.userData;
|
var userData = results.userData;
|
||||||
|
|
||||||
var map = {};
|
var map = {};
|
||||||
@@ -110,30 +111,30 @@ var utils = require('./utils');
|
|||||||
event[objectName] = map[event[field]];
|
event[objectName] = map[event[field]];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
callback(null, eventsData);
|
next(null, eventsData);
|
||||||
});
|
},
|
||||||
}
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
events.deleteEvents = function (eids, callback) {
|
events.deleteEvents = function (eids, callback) {
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
var keys = eids.map(function (eid) {
|
var keys = eids.map(function (eid) {
|
||||||
return 'event:' + eid;
|
return 'event:' + eid;
|
||||||
});
|
});
|
||||||
db.deleteAll(keys, next);
|
db.deleteAll(keys, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetRemove('events:time', eids, next);
|
db.sortedSetRemove('events:time', eids, next);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
events.deleteAll = function (callback) {
|
events.deleteAll = function (callback) {
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
|
|
||||||
batch.processSortedSet('events:time', function (eids, next) {
|
batch.processSortedSet('events:time', function (eids, next) {
|
||||||
events.deleteEvents(eids, next);
|
events.deleteEvents(eids, next);
|
||||||
}, { alwaysStartAt: 0 }, callback);
|
}, { alwaysStartAt: 0 }, callback);
|
||||||
};
|
};
|
||||||
}(module.exports));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user