finally a working subscriber back end but ugly code

This commit is contained in:
John McLear 2013-01-31 00:09:31 +00:00
parent 3ed466930c
commit c3bb3fb22b
4 changed files with 80 additions and 41 deletions

8
README.md Normal file
View file

@ -0,0 +1,8 @@
# TODO
* validation
* a point to unsubscribe
* stop the ui prompting if already subscribed
* send actual emails with content
*

View file

@ -9,7 +9,8 @@
"eejsBlock_editbarMenuRight": "ep_email_notifications/client:eejsBlock_toolbarRight" "eejsBlock_editbarMenuRight": "ep_email_notifications/client:eejsBlock_toolbarRight"
}, },
"client_hooks": { "client_hooks": {
"postAceInit":"ep_email_notifications/static/js/ep_email" "postAceInit":"ep_email_notifications/static/js/ep_email",
"handleClientMessage_emailSubscriptionSuccess":"ep_email_notifications/static/js/ep_email"
} }
} }
] ]

View file

@ -1,6 +1,12 @@
exports.postAceInit = function(){ exports.handleClientMessage_emailSubscriptionSuccess = function(hook, context){ // was subscribing to the email a big win or fail?
if(context.payload == false){
showAlreadyRegistered();
}else{
showRegistrationSuccess();
}
}
exports.postAceInit = function(){
// after 10 seconds if we dont already have an email for this author then prompt them // after 10 seconds if we dont already have an email for this author then prompt them
setTimeout(function(){init()},10000); setTimeout(function(){init()},10000);
} }
@ -13,6 +19,15 @@ function init(){
} }
} }
function showRegistrationSuccess(){ // show a successful registration message
$.gritter.add({
// (string | mandatory) the heading of the notification
title: "Email subscribed",
// (string | mandatory) the text inside the notification
text: "You will recieve email when someone changes this pad. If this is the first time you have requested emails you may need to confirm your email address"
});
}
function showAlreadyRegistered(){ // the client already registered for emails on this pad so notify the UI function showAlreadyRegistered(){ // the client already registered for emails on this pad so notify the UI
$.gritter.add({ $.gritter.add({
// (string | mandatory) the heading of the notification // (string | mandatory) the heading of the notification
@ -35,8 +50,6 @@ function clientHasAlreadyRegistered(){ // Has the client already registered for
message.userInfo = {}; message.userInfo = {};
message.userInfo.userId = userId; message.userInfo.userId = userId;
pad.collabClient.sendMessage(message); pad.collabClient.sendMessage(message);
} }
function askClientToEnterEmail(){ function askClientToEnterEmail(){
@ -53,13 +66,6 @@ function askClientToEnterEmail(){
after_open: function(e){ after_open: function(e){
$('#ep_email_form').submit(function(){ $('#ep_email_form').submit(function(){
$(e).hide(); $(e).hide();
$.gritter.add({
// (string | mandatory) the heading of the notification
title: "Email subscribed",
// (string | mandatory) the text inside the notification
text: "You will recieve email when someone changes this. If this is the first time you have requested emails you will need to confirm your email address"
});
sendEmailToServer(); sendEmailToServer();
return false; return false;
}); });

View file

@ -13,49 +13,60 @@ exports.handleMessage = function(hook_name, context, callback){
if (context.message && context.message.data){ 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.type == 'USERINFO_UPDATE' ) { // if it's a request to update an authors email
if (context.message.data.userInfo){ if (context.message.data.userInfo){
console.debug("userInfo",context.message.data.userInfo);
if(context.message.data.userInfo.email){ // it contains email if(context.message.data.userInfo.email){ // it contains email
// console.warn(context.message.data.userInfo.userId);
console.debug(context.message); console.debug(context.message);
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){ // does email Subscription already exist for this user? // does email Subscription already exist for this email address?
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){
console.debug("UserIds subscribed by email to this pad:", userIds); var alreadyExists = false;
if(userIds){
if(userIds){ // THIS NEEDS TO BE AN OBJECT :: TODO async.forEach(Object.keys(userIds), function(user, cb){
// This user ID is already assigned to this padId so don't do anything except tell the user they are already subscribed somehow.. console.debug("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
context.client.json.send({ type: "COLLABROOM",
data:{ // This user ID is already assigned to this padId so don't do anything except tell the user they are already subscribed somehow..
type: email_subscription_success, alreadyExists = true;
payload: false console.debug("email ", user, "already subscribed to ", context.message.data.padId, " so sending message to client");
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailSubscriptionSuccess",
payload: false
}
});
} }
}); cb();
},
}else{
function(err){
// console.warn ("WRITE MY GOODNESS TO THE DATABASE!",context.message.data.userInfo.email); // There should be something in here!
}); // end async for each
}
if(alreadyExists == false){
console.warn ("Wrote to the database and sent client a positive response ",context.message.data.userInfo.email);
exports.setAuthorEmail( exports.setAuthorEmail(
context.message.data.userInfo.userId, context.message.data.userInfo.userId,
context.message.data.userInfo.email, callback context.message.data.userInfo.email, callback
); );
exports.setAuthorEmailRegistered(
context.message.data.userInfo.userId,
context.message.data.padId, callback
);
exports.setAuthorEmailRegistered(
context.message.data.userInfo.email,
context.message.data.userInfo.userId,
context.message.data.padId
);
context.client.json.send({ type: "COLLABROOM", context.client.json.send({ type: "COLLABROOM",
data:{ data:{
type: email_subscription_success, type: "email_subscription_success",
payload: true payload: true
} }
}); });
} }
}); }); // close db get
callback(null); // don't run onto passing colorId or anything else to the message handler callback(null); // don't run onto passing colorId or anything else to the message handler
@ -171,6 +182,19 @@ exports.setAuthorEmail = function (author, email, callback){
} }
// Write email and padId to the database // Write email and padId to the database
exports.setAuthorEmailRegistered = function(author, padId, callback){ exports.setAuthorEmailRegistered = function(email, authorId, padId){
db.set("emailSubscription:" + padId, author); var timestamp = new Date().getTime();
var registered = {
authorId: authorId,
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
value[email] = registered; // add the registered values to the object
db.set("emailSubscription:" + padId, value); // stick it in the database
});
} }