initial rewrite

This commit is contained in:
booo 2012-02-09 02:09:17 +01:00
parent e6e81135a7
commit c19444f6c9
12 changed files with 777 additions and 697 deletions

View file

@ -17,40 +17,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
var randomString = require("../utils/randomstring");
var db = require("./DB").db;
var async = require("async");
var groupMangager = require("./GroupManager");
var authorMangager = require("./AuthorManager");
exports.doesSessionExist = function(sessionID, callback)
var SessionManager = function SessionManager(db, groupManager, authorManager) {
this.db = db;
this.groupManager = groupManager;
this.authorManager = authorManager;
};
exports.SessionManager = SessionManager;
SessionManager.prototype.doesSessionExist = function(sessionID, callback)
{
//check if the database entry of this session exists
db.get("session:" + sessionID, function (err, session)
this.db.get("session:" + sessionID, function (err, session)
{
if(ERR(err, callback)) return;
callback(null, session != null);
});
}
};
/**
* Creates a new session between an author and a group
*/
exports.createSession = function(groupID, authorID, validUntil, callback)
SessionManager.prototype.createSession = function(groupID, authorID, validUntil, callback)
{
var sessionID;
var that = this;
async.series([
//check if group exists
function(callback)
{
groupMangager.doesGroupExist(groupID, function(err, exists)
that.groupMangager.doesGroupExist(groupID, function(err, exists)
{
if(ERR(err, callback)) return;
//group does not exist
if(exists == false)
{
@ -66,10 +73,10 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
//check if author exists
function(callback)
{
authorMangager.doesAuthorExists(authorID, function(err, exists)
that.authorMangager.doesAuthorExists(authorID, function(err, exists)
{
if(ERR(err, callback)) return;
//author does not exist
if(exists == false)
{
@ -99,56 +106,56 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
return;
}
}
//ensure this is not a negativ number
if(validUntil < 0)
{
callback(new customError("validUntil is a negativ number","apierror"));
return;
}
//ensure this is not a float value
if(!is_int(validUntil))
{
callback(new customError("validUntil is a float value","apierror"));
return;
}
//check if validUntil is in the future
if(Math.floor(new Date().getTime()/1000) > validUntil)
{
callback(new customError("validUntil is in the past","apierror"));
return;
}
//generate sessionID
sessionID = "s." + randomString(16);
//set the session into the database
db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
that.db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
callback();
},
//set the group2sessions entry
function(callback)
{
//get the entry
db.get("group2sessions:" + groupID, function(err, group2sessions)
that.db.get("group2sessions:" + groupID, function(err, group2sessions)
{
if(ERR(err, callback)) return;
//the entry doesn't exist so far, let's create it
if(group2sessions == null)
{
group2sessions = {sessionIDs : {}};
}
//add the entry for this session
group2sessions.sessionIDs[sessionID] = 1;
//save the new element back
db.set("group2sessions:" + groupID, group2sessions);
that.db.set("group2sessions:" + groupID, group2sessions);
callback();
});
},
@ -156,45 +163,45 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
function(callback)
{
//get the entry
db.get("author2sessions:" + authorID, function(err, author2sessions)
that.db.get("author2sessions:" + authorID, function(err, author2sessions)
{
if(ERR(err, callback)) return;
//the entry doesn't exist so far, let's create it
if(author2sessions == null)
{
author2sessions = {sessionIDs : {}};
}
//add the entry for this session
author2sessions.sessionIDs[sessionID] = 1;
//save the new element back
db.set("author2sessions:" + authorID, author2sessions);
that.db.set("author2sessions:" + authorID, author2sessions);
callback();
});
}
], function(err)
{
if(ERR(err, callback)) return;
//return error and sessionID
callback(null, {sessionID: sessionID});
})
}
exports.getSessionInfo = function(sessionID, callback)
SessionManager.prototype.getSessionInfo = function(sessionID, callback)
{
//check if the database entry of this session exists
db.get("session:" + sessionID, function (err, session)
this.db.get("session:" + sessionID, function (err, session)
{
if(ERR(err, callback)) return;
//session does not exists
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
else
@ -202,7 +209,7 @@ exports.getSessionInfo = function(sessionID, callback)
callback(null, session);
}
});
}
};
/**
* Deletes a session
@ -211,15 +218,16 @@ exports.deleteSession = function(sessionID, callback)
{
var authorID, groupID;
var group2sessions, author2sessions;
var that = this;
async.series([
function(callback)
{
//get the session entry
db.get("session:" + sessionID, function (err, session)
that.db.get("session:" + sessionID, function (err, session)
{
if(ERR(err, callback)) return;
//session does not exists
if(session == null)
{
@ -230,7 +238,7 @@ exports.deleteSession = function(sessionID, callback)
{
authorID = session.authorID;
groupID = session.groupID;
callback();
}
});
@ -238,7 +246,7 @@ exports.deleteSession = function(sessionID, callback)
//get the group2sessions entry
function(callback)
{
db.get("group2sessions:" + groupID, function (err, _group2sessions)
that.db.get("group2sessions:" + groupID, function (err, _group2sessions)
{
if(ERR(err, callback)) return;
group2sessions = _group2sessions;
@ -248,7 +256,7 @@ exports.deleteSession = function(sessionID, callback)
//get the author2sessions entry
function(callback)
{
db.get("author2sessions:" + authorID, function (err, _author2sessions)
that.db.get("author2sessions:" + authorID, function (err, _author2sessions)
{
if(ERR(err, callback)) return;
author2sessions = _author2sessions;
@ -259,16 +267,16 @@ exports.deleteSession = function(sessionID, callback)
function(callback)
{
//remove the session
db.remove("session:" + sessionID);
that.db.remove("session:" + sessionID);
//remove session from group2sessions
delete group2sessions.sessionIDs[sessionID];
db.set("group2sessions:" + groupID, group2sessions);
that.db.set("group2sessions:" + groupID, group2sessions);
//remove session from author2sessions
delete author2sessions.sessionIDs[sessionID];
db.set("author2sessions:" + authorID, author2sessions);
that.db.set("author2sessions:" + authorID, author2sessions);
callback();
}
], function(err)
@ -276,14 +284,15 @@ exports.deleteSession = function(sessionID, callback)
if(ERR(err, callback)) return;
callback();
})
}
};
exports.listSessionsOfGroup = function(groupID, callback)
SessionManager.prototype.listSessionsOfGroup = function(groupID, callback)
{
groupMangager.doesGroupExist(groupID, function(err, exists)
var that = this;
this.groupMangager.doesGroupExist(groupID, function(err, exists)
{
if(ERR(err, callback)) return;
//group does not exist
if(exists == false)
{
@ -292,17 +301,18 @@ exports.listSessionsOfGroup = function(groupID, callback)
//everything is fine, continue
else
{
listSessionsWithDBKey("group2sessions:" + groupID, callback);
that.listSessionsWithDBKey("group2sessions:" + groupID, callback);
}
});
}
};
exports.listSessionsOfAuthor = function(authorID, callback)
{
authorMangager.doesAuthorExists(authorID, function(err, exists)
SessionManager.prototype.listSessionsOfAuthor = function(authorID, callback)
{
var that = this;
this.authorMangager.doesAuthorExists(authorID, function(err, exists)
{
if(ERR(err, callback)) return;
//group does not exist
if(exists == false)
{
@ -311,21 +321,21 @@ exports.listSessionsOfAuthor = function(authorID, callback)
//everything is fine, continue
else
{
listSessionsWithDBKey("author2sessions:" + authorID, callback);
that.listSessionsWithDBKey("author2sessions:" + authorID, callback);
}
});
}
};
//this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common
function listSessionsWithDBKey (dbkey, callback)
SessionManager.prototype.listSessionsWithDBKey = function listSessionsWithDBKey (dbkey, callback)
{
var sessions;
var that = this;
async.series([
function(callback)
{
//get the group2sessions entry
db.get(dbkey, function(err, sessionObject)
that.db.get(dbkey, function(err, sessionObject)
{
if(ERR(err, callback)) return;
sessions = sessionObject ? sessionObject.sessionIDs : null;
@ -333,18 +343,18 @@ function listSessionsWithDBKey (dbkey, callback)
});
},
function(callback)
{
{
//collect all sessionIDs in an arrary
var sessionIDs = [];
for (var i in sessions)
{
sessionIDs.push(i);
}
//foreach trough the sessions and get the sessioninfos
async.forEach(sessionIDs, function(sessionID, callback)
{
exports.getSessionInfo(sessionID, function(err, sessionInfo)
that.getSessionInfo(sessionID, function(err, sessionInfo)
{
if(ERR(err, callback)) return;
sessions[sessionID] = sessionInfo;
@ -360,7 +370,6 @@ function listSessionsWithDBKey (dbkey, callback)
}
//checks if a number is an int
function is_int(value)
{
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
function is_int(value) {
return (parseFloat(value) == parseInt(value, 10)) && !isNaN(value);
}