tiny tidy up and dont email if recipient is NOT on pad
This commit is contained in:
parent
f4d455a751
commit
c1729481da
1 changed files with 39 additions and 52 deletions
91
update.js
91
update.js
|
@ -18,7 +18,7 @@ var emailServer = pluginSettings.emailServer || {host:"127.0.0.1"};
|
||||||
// A timer object we maintain to control how we send emails
|
// A timer object we maintain to control how we send emails
|
||||||
var timers = {};
|
var timers = {};
|
||||||
|
|
||||||
// Connect to the email server
|
// Connect to the email server -- This might not be the ideal place to connect but it stops us having lots of connections
|
||||||
var server = email.server.connect(emailServer);
|
var server = email.server.connect(emailServer);
|
||||||
|
|
||||||
exports.padUpdate = function (hook_name, _pad) {
|
exports.padUpdate = function (hook_name, _pad) {
|
||||||
|
@ -28,7 +28,7 @@ exports.padUpdate = function (hook_name, _pad) {
|
||||||
|
|
||||||
// does an interval not exist for this pad?
|
// does an interval not exist for this pad?
|
||||||
if(!timers[padId]){
|
if(!timers[padId]){
|
||||||
console.warn("Someone started editing "+padId);
|
console.debug("Someone started editing "+padId);
|
||||||
exports.notifyBegin(padId);
|
exports.notifyBegin(padId);
|
||||||
console.debug("Created an interval time check for "+padId);
|
console.debug("Created an interval time check for "+padId);
|
||||||
// if not then create one and write it to the timers object
|
// if not then create one and write it to the timers object
|
||||||
|
@ -39,23 +39,29 @@ exports.padUpdate = function (hook_name, _pad) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.notifyBegin = function(padId){
|
exports.notifyBegin = function(padId){
|
||||||
console.warn("Getting "+padId);
|
console.debug("Getting pad email stuff for "+padId);
|
||||||
db.get("emailSubscription:" + padId, function(err, recipients){ // get everyone we need to email
|
db.get("emailSubscription:" + padId, function(err, recipients){ // get everyone we need to email
|
||||||
if(recipients){
|
if(recipients){
|
||||||
async.forEach(Object.keys(recipients), function(recipient, cb){
|
async.forEach(Object.keys(recipients), function(recipient, cb){
|
||||||
console.warn("Emailing "+recipient +" about a new begin update");
|
// Is this recipient already on the pad?
|
||||||
|
exports.isUserEditingPad(padId, recipients[recipient].authorId, function(err,userIsOnPad){ // is the user already on the pad?
|
||||||
server.send({
|
if(!userIsOnPad){
|
||||||
text: "Your pad at "+urlToPads+padId +" is being edited, we're just emailing you let you know :)",
|
console.debug("Emailing "+recipient +" about a new begin update");
|
||||||
from: fromName+ "<"+fromEmail+">",
|
server.send({
|
||||||
to: recipient,
|
text: "Your pad at "+urlToPads+padId +" is being edited, we're just emailing you let you know :)",
|
||||||
subject: "Someone begin editing "+padId
|
from: fromName+ "<"+fromEmail+">",
|
||||||
}, function(err, message) { console.log(err || message); });
|
to: recipient,
|
||||||
|
subject: "Someone begin editing "+padId
|
||||||
|
}, function(err, message) { console.log(err || message); });
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
console.debug("Didn't send an email because user is already on the pad");
|
||||||
|
}
|
||||||
|
});
|
||||||
cb(); // finish each user
|
cb(); // finish each user
|
||||||
},
|
},
|
||||||
function(err){
|
function(err){
|
||||||
|
// do some error handling..
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -68,29 +74,33 @@ exports.notifyEnd = function(padId){
|
||||||
db.get("emailSubscription:" + padId, function(err, recipients){ // get everyone we need to email
|
db.get("emailSubscription:" + padId, function(err, recipients){ // get everyone we need to email
|
||||||
if(recipients){
|
if(recipients){
|
||||||
async.forEach(Object.keys(recipients), function(recipient, cb){
|
async.forEach(Object.keys(recipients), function(recipient, cb){
|
||||||
console.warn("Emailing "+recipient +" about a pad finish editing");
|
// Is this recipient already on the pad?
|
||||||
|
exports.isUserEditingPad(padId, recipients[recipient].authorId, function(err,userIsOnPad){ // is the user already on the$
|
||||||
server.send({
|
if(!userIsOnPad){
|
||||||
text: "Your pad at "+urlToPads+padId +" has finished being edited, we're just emailing you let you know :) The changes look like this:" + changesToPad,
|
console.debug("Emailing "+recipient +" about a pad finished being updated");
|
||||||
from: fromName+ "<"+fromEmail+">",
|
server.send({
|
||||||
to: recipient,
|
text: "Your pad at "+urlToPads+padId +" has finished being edited, we're just emailing you let you know :) The changes look like this:" + changesToPad,
|
||||||
subject: "Someone finished editing "+padId
|
from: fromName+ "<"+fromEmail+">",
|
||||||
}, function(err, message) { console.log(err || message); });
|
to: recipient,
|
||||||
|
subject: "Someone finished editing "+padId
|
||||||
|
}, function(err, message) { console.log(err || message); });
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
console.debug("Didn't send an email because user is already on the pad");
|
||||||
|
}
|
||||||
|
});
|
||||||
cb(); // finish each user
|
cb(); // finish each user
|
||||||
},
|
},
|
||||||
function(err){
|
function(err){
|
||||||
|
// do some error handling..
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.sendUpdates = function(padId){
|
exports.sendUpdates = function(padId){
|
||||||
|
|
||||||
// check to see if we can delete this interval
|
// check to see if we can delete this interval
|
||||||
API.getLastEdited(padId, function(callback, message){
|
API.getLastEdited(padId, function(callback, message){
|
||||||
|
|
||||||
// we delete an interval if a pad hasn't been edited in X seconds.
|
// we delete an interval if a pad hasn't been edited in X seconds.
|
||||||
var currTS = new Date().getTime();
|
var currTS = new Date().getTime();
|
||||||
if(currTS - message.lastEdited > staleTime){
|
if(currTS - message.lastEdited > staleTime){
|
||||||
|
@ -99,47 +109,26 @@ exports.sendUpdates = function(padId){
|
||||||
var interval = timers[padId];
|
var interval = timers[padId];
|
||||||
clearInterval(timers[padId]); // remove the interval timer
|
clearInterval(timers[padId]); // remove the interval timer
|
||||||
delete timers[padId]; // remove the entry from the padId
|
delete timers[padId]; // remove the entry from the padId
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
console.debug("email timeout not stale so not deleting");
|
console.debug("email timeout not stale so not deleting");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// The status of the users relationship with the pad -- IE if it's subscribed to this pad / if it's already on the pad
|
// The status of the users relationship with the pad -- IE if it's subscribed to this pad / if it's already on the pad
|
||||||
// This comes frmo the database
|
// This comes frmo the database
|
||||||
var userStatus = {};
|
var userStatus = {}; // I'm not even sure we use this value.. I put it here when drunk or something
|
||||||
|
|
||||||
// Temporary user object
|
|
||||||
var user = {
|
|
||||||
name: "John McLear",
|
|
||||||
email: "john@mclear.co.uk",
|
|
||||||
id: "a.n4gEeMLsv1GivNeh"
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug("ep_email_noficiations: padID of pad being edited:"+padId);
|
|
||||||
exports.isUserEditingPad(padId, user, function(err,results){
|
|
||||||
userStatus.userIsEditing = results;
|
|
||||||
console.debug("isUserEditingPad is:", results);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Is the user editing the pad?
|
// Is the user editing the pad?
|
||||||
exports.isUserEditingPad = function(padId, user, cb){
|
exports.isUserEditingPad = function(padId, user, cb){
|
||||||
API.padUsers(padId, function(callback, padUsers){ // get the current users editing the pad
|
API.padUsers(padId, function(callback, padUsers){ // get the current users editing the pad
|
||||||
|
|
||||||
var userIsEditing = false;
|
var userIsEditing = false;
|
||||||
console.debug("Pad Users:"+padUsers);
|
console.debug("Current Pad Users:"+padUsers);
|
||||||
|
|
||||||
// for each user on the pad right now
|
// for each user on the pad right now
|
||||||
async.forEach(padUsers.padUsers,
|
async.forEach(padUsers.padUsers,
|
||||||
|
|
||||||
function(userOnPad, callback){
|
function(userOnPad, callback){
|
||||||
|
if(userOnPad.id == user){
|
||||||
if(userOnPad.id == user.id){
|
console.debug("User is on the pad so don't send any notification");
|
||||||
console.debug("I'm on the pad so don't send any notification");
|
|
||||||
userIsEditing = true; // If the user is editing the pad then return true
|
userIsEditing = true; // If the user is editing the pad then return true
|
||||||
}else{
|
}else{
|
||||||
userIsEditing = false; // If the user isnt on this pad then that'd be okay to contact em
|
userIsEditing = false; // If the user isnt on this pad then that'd be okay to contact em
|
||||||
|
@ -147,11 +136,9 @@ exports.isUserEditingPad = function(padId, user, cb){
|
||||||
callback(userIsEditing);
|
callback(userIsEditing);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
function(err){
|
function(err){
|
||||||
cb(null, userIsEditing);
|
cb(null, userIsEditing);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue