2013-01-31 01:57:12 +00:00
|
|
|
var db = require('../../src/node/db/DB').db,
|
|
|
|
API = require('../../src/node/db/API.js'),
|
|
|
|
async = require('../../src/node_modules/async'),
|
|
|
|
check = require('validator').check,
|
|
|
|
settings = require('../../src/node/utils/Settings');
|
|
|
|
|
|
|
|
var pluginSettings = settings.ep_email_notifications;
|
|
|
|
|
|
|
|
// When a new message comes in from the client - FML this is ugly
|
|
|
|
exports.handleMessage = function(hook_name, context, callback){
|
|
|
|
if (context.message && context.message.data){
|
|
|
|
if (context.message.data.type == 'USERINFO_UPDATE' ) { // if it's a request to update an authors email
|
|
|
|
if (context.message.data.userInfo){
|
|
|
|
if(context.message.data.userInfo.email){ // it contains email
|
|
|
|
console.debug(context.message);
|
2013-03-26 19:44:34 +00:00
|
|
|
console.info("ep_mail: " + context.message.data.userInfo.email + " / " + context.message.data.userInfo.email_option);
|
|
|
|
console.info(context.message.data.userInfo);
|
2013-01-31 01:57:12 +00:00
|
|
|
|
|
|
|
// does email Subscription already exist for this email address?
|
|
|
|
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){
|
|
|
|
|
|
|
|
var alreadyExists = false;
|
2013-03-26 19:44:34 +00:00
|
|
|
console.info(userIds);
|
2013-01-31 01:57:12 +00:00
|
|
|
if(userIds){
|
|
|
|
async.forEach(Object.keys(userIds), function(user, cb){
|
2013-03-26 19:44:34 +00:00
|
|
|
console.info("UserIds subscribed by email to this pad:", userIds);
|
|
|
|
if(user == context.message.data.userInfo.email){ // If we already have this email registered for this pad
|
2013-01-31 01:57:12 +00:00
|
|
|
// This user ID is already assigned to this padId so don't do anything except tell the user they are already subscribed somehow..
|
|
|
|
alreadyExists = true;
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
|
|
|
|
function(err){
|
|
|
|
// There should be something in here!
|
|
|
|
}); // end async for each
|
|
|
|
}
|
2013-03-26 19:44:34 +00:00
|
|
|
|
|
|
|
if(context.message.data.userInfo.email_option == 'subscribe' && alreadyExists == true){
|
|
|
|
// SUbscription
|
|
|
|
console.info("email ", context.message.data.userInfo.email, "already subscribed to ", context.message.data.padId, " so sending message to client");
|
|
|
|
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
2013-01-31 01:57:12 +00:00
|
|
|
data:{
|
|
|
|
type: "emailSubscriptionSuccess",
|
|
|
|
payload: false
|
2013-03-26 19:44:34 +00:00
|
|
|
}
|
2013-01-31 01:57:12 +00:00
|
|
|
});
|
2013-03-26 19:44:34 +00:00
|
|
|
} else if(context.message.data.userInfo.email_option == 'subscribe' && alreadyExists == false){
|
|
|
|
// SUbscription
|
|
|
|
var validatesAsEmail = check(context.message.data.userInfo.email).isEmail();
|
|
|
|
if(!validatesAsEmail){ // send validation failed if it's malformed.. y'know in general fuck em!
|
|
|
|
console.warn("Dropped email subscription due to malformed email address");
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
|
|
|
type: "emailSubscriptionSuccess",
|
|
|
|
payload: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.info ("Subscription: Wrote to the database and sent client a positive response ",context.message.data.userInfo.email);
|
|
|
|
|
|
|
|
exports.setAuthorEmail(
|
|
|
|
context.message.data.userInfo.userId,
|
|
|
|
context.message.data.userInfo,
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
|
|
|
|
exports.setAuthorEmailRegistered(
|
|
|
|
context.message.data.userInfo,
|
|
|
|
context.message.data.userInfo.userId,
|
|
|
|
context.message.data.padId
|
|
|
|
);
|
|
|
|
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
|
|
|
type: "emailSubscriptionSuccess",
|
|
|
|
payload: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if(context.message.data.userInfo.email_option == 'unsubscribe' && alreadyExists == true) {
|
|
|
|
// Unsubscription
|
|
|
|
console.info ("Unsubscription: Remove from the database and sent client a positive response ",context.message.data.userInfo.email);
|
|
|
|
|
|
|
|
exports.unsetAuthorEmail(
|
2013-01-31 01:57:12 +00:00
|
|
|
context.message.data.userInfo.userId,
|
2013-03-26 19:44:34 +00:00
|
|
|
context.message.data.userInfo,
|
|
|
|
callback
|
2013-01-31 01:57:12 +00:00
|
|
|
);
|
|
|
|
|
2013-03-26 19:44:34 +00:00
|
|
|
exports.unsetAuthorEmailRegistered(
|
|
|
|
context.message.data.userInfo,
|
2013-01-31 01:57:12 +00:00
|
|
|
context.message.data.userInfo.userId,
|
|
|
|
context.message.data.padId
|
|
|
|
);
|
|
|
|
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
2013-03-26 19:44:34 +00:00
|
|
|
type: "emailUnsubscriptionSuccess",
|
2013-01-31 01:57:12 +00:00
|
|
|
payload: true
|
|
|
|
}
|
|
|
|
});
|
2013-03-26 19:44:34 +00:00
|
|
|
} else if(context.message.data.userInfo.email_option == 'unsubscribe' && alreadyExists == false) {
|
|
|
|
// Unsubscription
|
|
|
|
console.info ("Unsubscription: Send client a negative response ",context.message.data.userInfo.email);
|
|
|
|
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
|
|
|
type: "emailUnsubscriptionSuccess",
|
|
|
|
payload: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:57:12 +00:00
|
|
|
}); // close db get
|
|
|
|
|
2013-03-19 22:29:34 +00:00
|
|
|
callback([null]); // don't run onto passing colorId or anything else to the message handler
|
2013-01-31 01:57:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-03-26 19:44:34 +00:00
|
|
|
} else if (context.message.data.type == 'USERINFO_GET' ) { // A request to find datas for a username
|
|
|
|
if (context.message.data.userInfo){
|
|
|
|
if(context.message.data.userInfo.userId){ // it contains the userId
|
|
|
|
console.debug(context.message);
|
|
|
|
|
|
|
|
var userIdFound = false;
|
|
|
|
// does email Subscription already exist for this name and padID?
|
|
|
|
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){
|
|
|
|
if(userIds){
|
|
|
|
async.forEach(Object.keys(userIds), function(user, cb){
|
|
|
|
if(userIds[user].authorId == context.message.data.userInfo.userId){ // if we find the same Id in the Db as the one used by the user
|
|
|
|
console.info("Options for this pad ", userIds[user].authorId, " found in the Db");
|
|
|
|
userIdFound = true;
|
|
|
|
|
|
|
|
// We send back the options set for this user
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
|
|
|
type: "emailNotificationGetUserInfo",
|
|
|
|
payload: {
|
|
|
|
email: user,
|
|
|
|
onStart: userIds[user].onStart && typeof userIds[user].onStart === 'boolean'?userIds[user].onStart:true,
|
|
|
|
onEnd: userIds[user].onEnd && typeof userIds[user].onEnd === 'boolean'?userIds[user].onEnd:false,
|
|
|
|
success:true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
|
|
|
|
function(err){
|
|
|
|
// There should be something in here!
|
|
|
|
}); // end async for each
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!userIdFound) {
|
|
|
|
// We send back the options set for this user
|
|
|
|
context.client.json.send({ type: "COLLABROOM",
|
|
|
|
data:{
|
|
|
|
type: "emailNotificationGetUserInfo",
|
|
|
|
payload: {
|
|
|
|
success:false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:57:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the database with the email record
|
2013-03-26 19:44:34 +00:00
|
|
|
exports.setAuthorEmail = function (author, datas, callback){
|
|
|
|
db.setSub("globalAuthor:" + author, ["email"], datas.email, callback);
|
2013-01-31 01:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write email and padId to the database
|
2013-03-26 19:44:34 +00:00
|
|
|
exports.setAuthorEmailRegistered = function(datas, authorId, padId){
|
2013-01-31 01:57:12 +00:00
|
|
|
var timestamp = new Date().getTime();
|
|
|
|
var registered = {
|
|
|
|
authorId: authorId,
|
2013-03-26 19:44:34 +00:00
|
|
|
onStart: datas.email_onStart,
|
|
|
|
onEnd: datas.email_onEnd,
|
2013-01-31 01:57:12 +00:00
|
|
|
timestamp: timestamp
|
|
|
|
};
|
|
|
|
console.debug("registered", registered, " to ", padId);
|
|
|
|
// Here we have to basically hack a new value into the database, this isn't clean or polite.
|
|
|
|
db.get("emailSubscription:" + padId, function(err, value){ // get the current value
|
|
|
|
if(!value){value = {};} // if an emailSubscription doesnt exist yet for this padId don't panic
|
2013-03-26 19:44:34 +00:00
|
|
|
value[datas.email] = registered; // add the registered values to the object
|
2013-01-31 02:31:59 +00:00
|
|
|
console.warn("written to database");
|
2013-01-31 01:57:12 +00:00
|
|
|
db.set("emailSubscription:" + padId, value); // stick it in the database
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-26 19:44:34 +00:00
|
|
|
// Updates the database by removing the email record for that AuthorId
|
|
|
|
exports.unsetAuthorEmail = function (author, datas, callback){
|
|
|
|
db.get("globalAuthor:" + author, function(err, value){ // get the current value
|
|
|
|
delete value['email'];
|
|
|
|
db.set("globalAuthor:" + author, value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove email and padId from the database
|
|
|
|
exports.unsetAuthorEmailRegistered = function(datas, authorId, padId){
|
|
|
|
console.debug("unregistered", datas.email, " to ", padId);
|
|
|
|
// Here we have to basically hack a new value into the database, this isn't clean or polite.
|
|
|
|
db.get("emailSubscription:" + padId, function(err, value){ // get the current value
|
|
|
|
delete value[datas.email]; // remove the registered values to the object
|
|
|
|
console.warn("written to database");
|
|
|
|
db.set("emailSubscription:" + padId, value); // stick it in the database
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|