db/SessionManager: use jshint

This commit is contained in:
booo 2011-12-22 12:02:28 +01:00
parent 8f208ca444
commit 1f41ef3365

View file

@ -17,24 +17,24 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var customError = require("../utils/customError"); var customError = require("../utils/customError");
var db = require("./DB").db; var db = require("./DB").db;
var async = require("async"); var async = require("async");
var groupMangager = require("./GroupManager"); var groupMangager = require("./GroupManager");
var authorMangager = require("./AuthorManager"); var authorMangager = require("./AuthorManager");
exports.doesSessionExist = function(sessionID, callback) exports.doesSessionExist = function(sessionID, callback)
{ {
//check if the database entry of this session exists //check if the database entry of this session exists
db.get("session:" + sessionID, function (err, session) db.get("session:" + sessionID, function (err, session)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(null, session != null); callback(null, session ? true : false);
}); });
} };
/** /**
* Creates a new session between an author and a group * Creates a new session between an author and a group
*/ */
@ -49,9 +49,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
groupMangager.doesGroupExist(groupID, function(err, exists) groupMangager.doesGroupExist(groupID, function(err, exists)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//group does not exist //group does not exist
if(exists == false) if(!exists)
{ {
callback(new customError("groupID does not exist","apierror")); callback(new customError("groupID does not exist","apierror"));
} }
@ -68,9 +68,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
authorMangager.doesAuthorExists(authorID, function(err, exists) authorMangager.doesAuthorExists(authorID, function(err, exists)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//author does not exist //author does not exist
if(exists == false) if(!exists)
{ {
callback(new customError("authorID does not exist","apierror")); callback(new customError("authorID does not exist","apierror"));
} }
@ -88,9 +88,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
if(typeof validUntil != "number") if(typeof validUntil != "number")
{ {
//try to parse the number //try to parse the number
if(!isNaN(parseInt(validUntil))) if(!isNaN(parseInt(validUntil, 10)))
{ {
validUntil = parseInt(validUntil); validUntil = parseInt(validUntil, 10);
} }
else else
{ {
@ -98,34 +98,34 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
return; return;
} }
} }
//ensure this is not a negativ number //ensure this is not a negativ number
if(validUntil < 0) if(validUntil < 0)
{ {
callback(new customError("validUntil is a negativ number","apierror")); callback(new customError("validUntil is a negativ number","apierror"));
return; return;
} }
//ensure this is not a float value //ensure this is not a float value
if(!is_int(validUntil)) if(!is_int(validUntil))
{ {
callback(new customError("validUntil is a float value","apierror")); callback(new customError("validUntil is a float value","apierror"));
return; return;
} }
//check if validUntil is in the future //check if validUntil is in the future
if(Math.floor(new Date().getTime()/1000) > validUntil) if(Math.floor(new Date().getTime()/1000) > validUntil)
{ {
callback(new customError("validUntil is in the past","apierror")); callback(new customError("validUntil is in the past","apierror"));
return; return;
} }
//generate sessionID //generate sessionID
sessionID = "s." + randomString(16); sessionID = "s." + randomString(16);
//set the session into the database //set the session into the database
db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil}); db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
callback(); callback();
}, },
//set the group2sessions entry //set the group2sessions entry
@ -135,19 +135,19 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
db.get("group2sessions:" + groupID, function(err, group2sessions) db.get("group2sessions:" + groupID, function(err, group2sessions)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//the entry doesn't exist so far, let's create it //the entry doesn't exist so far, let's create it
if(group2sessions == null) if(!group2sessions)
{ {
group2sessions = {sessionIDs : {}}; group2sessions = {sessionIDs : {}};
} }
//add the entry for this session //add the entry for this session
group2sessions.sessionIDs[sessionID] = 1; group2sessions.sessionIDs[sessionID] = 1;
//save the new element back //save the new element back
db.set("group2sessions:" + groupID, group2sessions); db.set("group2sessions:" + groupID, group2sessions);
callback(); callback();
}); });
}, },
@ -158,30 +158,30 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
db.get("author2sessions:" + authorID, function(err, author2sessions) db.get("author2sessions:" + authorID, function(err, author2sessions)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//the entry doesn't exist so far, let's create it //the entry doesn't exist so far, let's create it
if(author2sessions == null) if(!author2sessions)
{ {
author2sessions = {sessionIDs : {}}; author2sessions = {sessionIDs : {}};
} }
//add the entry for this session //add the entry for this session
author2sessions.sessionIDs[sessionID] = 1; author2sessions.sessionIDs[sessionID] = 1;
//save the new element back //save the new element back
db.set("author2sessions:" + authorID, author2sessions); db.set("author2sessions:" + authorID, author2sessions);
callback(); callback();
}); });
} }
], function(err) ], function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//return error and sessionID //return error and sessionID
callback(null, {sessionID: sessionID}); callback(null, {sessionID: sessionID});
}) });
} };
exports.getSessionInfo = function(sessionID, callback) exports.getSessionInfo = function(sessionID, callback)
{ {
@ -189,11 +189,11 @@ exports.getSessionInfo = function(sessionID, callback)
db.get("session:" + sessionID, function (err, session) db.get("session:" + sessionID, function (err, session)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//session does not exists //session does not exists
if(session == null) if(session === null)
{ {
callback(new customError("sessionID does not exist","apierror")) callback(new customError("sessionID does not exist","apierror"));
} }
//everything is fine, return the sessioninfos //everything is fine, return the sessioninfos
else else
@ -201,7 +201,7 @@ exports.getSessionInfo = function(sessionID, callback)
callback(null, session); callback(null, session);
} }
}); });
} };
/** /**
* Deletes a session * Deletes a session
@ -218,18 +218,18 @@ exports.deleteSession = function(sessionID, callback)
db.get("session:" + sessionID, function (err, session) db.get("session:" + sessionID, function (err, session)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//session does not exists //session does not exists
if(session == null) if(!session)
{ {
callback(new customError("sessionID does not exist","apierror")) callback(new customError("sessionID does not exist","apierror"));
} }
//everything is fine, return the sessioninfos //everything is fine, return the sessioninfos
else else
{ {
authorID = session.authorID; authorID = session.authorID;
groupID = session.groupID; groupID = session.groupID;
callback(); callback();
} }
}); });
@ -259,32 +259,32 @@ exports.deleteSession = function(sessionID, callback)
{ {
//remove the session //remove the session
db.remove("session:" + sessionID); db.remove("session:" + sessionID);
//remove session from group2sessions //remove session from group2sessions
delete group2sessions.sessionIDs[sessionID]; delete group2sessions.sessionIDs[sessionID];
db.set("group2sessions:" + groupID, group2sessions); db.set("group2sessions:" + groupID, group2sessions);
//remove session from author2sessions //remove session from author2sessions
delete author2sessions.sessionIDs[sessionID]; delete author2sessions.sessionIDs[sessionID];
db.set("author2sessions:" + authorID, author2sessions); db.set("author2sessions:" + authorID, author2sessions);
callback(); callback();
} }
], function(err) ], function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(); callback();
}) });
} };
exports.listSessionsOfGroup = function(groupID, callback) exports.listSessionsOfGroup = function(groupID, callback)
{ {
groupMangager.doesGroupExist(groupID, function(err, exists) groupMangager.doesGroupExist(groupID, function(err, exists)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//group does not exist //group does not exist
if(exists == false) if(!exists)
{ {
callback(new customError("groupID does not exist","apierror")); callback(new customError("groupID does not exist","apierror"));
} }
@ -294,16 +294,16 @@ exports.listSessionsOfGroup = function(groupID, callback)
listSessionsWithDBKey("group2sessions:" + groupID, callback); listSessionsWithDBKey("group2sessions:" + groupID, callback);
} }
}); });
} };
exports.listSessionsOfAuthor = function(authorID, callback) exports.listSessionsOfAuthor = function(authorID, callback)
{ {
authorMangager.doesAuthorExists(authorID, function(err, exists) authorMangager.doesAuthorExists(authorID, function(err, exists)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//group does not exist //group does not exist
if(exists == false) if(!exists)
{ {
callback(new customError("authorID does not exist","apierror")); callback(new customError("authorID does not exist","apierror"));
} }
@ -313,7 +313,7 @@ exports.listSessionsOfAuthor = function(authorID, callback)
listSessionsWithDBKey("author2sessions:" + authorID, callback); listSessionsWithDBKey("author2sessions:" + authorID, callback);
} }
}); });
} };
//this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common //this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common
function listSessionsWithDBKey (dbkey, callback) function listSessionsWithDBKey (dbkey, callback)
@ -332,14 +332,14 @@ function listSessionsWithDBKey (dbkey, callback)
}); });
}, },
function(callback) function(callback)
{ {
//collect all sessionIDs in an arrary //collect all sessionIDs in an arrary
var sessionIDs = []; var sessionIDs = [];
for (var i in sessions) for (var i in sessions)
{ {
sessionIDs.push(i); sessionIDs.push(i);
} }
//foreach trough the sessions and get the sessioninfos //foreach trough the sessions and get the sessioninfos
async.forEach(sessionIDs, function(sessionID, callback) async.forEach(sessionIDs, function(sessionID, callback)
{ {
@ -361,7 +361,7 @@ function listSessionsWithDBKey (dbkey, callback)
/** /**
* Generates a random String with the given length. Is needed to generate the SessionIDs * Generates a random String with the given length. Is needed to generate the SessionIDs
*/ */
function randomString(len) function randomString(len)
{ {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var randomstring = ''; var randomstring = '';
@ -375,6 +375,6 @@ function randomString(len)
//checks if a number is an int //checks if a number is an int
function is_int(value) function is_int(value)
{ {
return (parseFloat(value) == parseInt(value)) && !isNaN(value) return (parseFloat(value) == parseInt(value, 10)) && !isNaN(value);
} }