db/SessionManager.js: mostly converted to Promises

This commit is contained in:
Ray Bellis 2019-01-25 14:53:24 +00:00
parent 16c4c33f49
commit a875ca6c30

View file

@ -21,160 +21,106 @@
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var customError = require("../utils/customError"); var customError = require("../utils/customError");
var randomString = require("../utils/randomstring"); var randomString = require("../utils/randomstring");
var db = require("./DB").db; var db = require("./DB");
var async = require("async");
var groupManager = require("./GroupManager"); var groupManager = require("./GroupManager");
var authorManager = require("./AuthorManager"); var authorManager = require("./AuthorManager");
const thenify = require("thenify").withCallback; const thenify = require("thenify").withCallback;
exports.doesSessionExist = thenify(function(sessionID, callback) exports.doesSessionExist = async function(sessionID)
{ {
//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) let session = await db.get("session:" + sessionID);
{ return (session !== null);
if(ERR(err, callback)) return; }
callback(null, session != null);
});
});
/** /**
* Creates a new session between an author and a group * Creates a new session between an author and a group
*/ */
exports.createSession = thenify(function(groupID, authorID, validUntil, callback) exports.createSession = async function(groupID, authorID, validUntil)
{ {
var sessionID; // check if the group exists
let groupExists = await groupManager.doesGroupExist(groupID);
if (!groupExists) {
throw new customError("groupID does not exist", "apierror");
}
async.series([ // check if the author exists
// check if the group exists let authorExists = await authorManager.doesAuthorExist(authorID);
function(callback) if (!authorExists) {
{ throw new customError("authorID does not exist", "apierror");
groupManager.doesGroupExist(groupID, function(err, exists) }
{
if(ERR(err, callback)) return;
// group does not exist // try to parse validUntil if it's not a number
if (exists == false) { if (typeof validUntil !== "number") {
callback(new customError("groupID does not exist", "apierror")); validUntil = parseInt(validUntil);
} else { }
// everything is fine, continue
callback();
}
});
},
// check if the author exists // check it's a valid number
function(callback) if (isNaN(validUntil)) {
{ throw new customError("validUntil is not a number", "apierror");
authorManager.doesAuthorExists(authorID, function(err, exists) }
{
if(ERR(err, callback)) return;
if (exists == false) { // ensure this is not a negative number
// author does not exist if (validUntil < 0) {
callback(new customError("authorID does not exist", "apierror")); throw new customError("validUntil is a negative number", "apierror");
} else { }
// everything is fine, continue
callback();
}
});
},
// check validUntil and create the session db entry // ensure this is not a float value
function(callback) if (!is_int(validUntil)) {
{ throw new customError("validUntil is a float value", "apierror");
// check if rev is a number }
if (typeof validUntil != "number")
{
// try to parse the number
if (isNaN(parseInt(validUntil))) {
callback(new customError("validUntil is not a number", "apierror"));
return;
}
validUntil = parseInt(validUntil); // check if validUntil is in the future
} if (validUntil < Math.floor(Date.now() / 1000)) {
throw new customError("validUntil is in the past", "apierror");
}
// ensure this is not a negative number // generate sessionID
if (validUntil < 0) { let sessionID = "s." + randomString(16);
callback(new customError("validUntil is a negative number", "apierror"));
return;
}
// ensure this is not a float value // set the session into the database
if (!is_int(validUntil)) { await db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
callback(new customError("validUntil is a float value", "apierror"));
return;
}
// check if validUntil is in the future // get the entry
if (Math.floor(Date.now()/1000) > validUntil) { let group2sessions = await db.get("group2sessions:" + groupID);
callback(new customError("validUntil is in the past", "apierror"));
return;
}
// generate sessionID /*
sessionID = "s." + randomString(16); * In some cases, the db layer could return "undefined" as well as "null".
* Thus, it is not possible to perform strict null checks on group2sessions.
* In a previous version of this code, a strict check broke session
* management.
*
* See: https://github.com/ether/etherpad-lite/issues/3567#issuecomment-468613960
*/
if (!group2sessions || !group2sessions.sessionIDs) {
// the entry doesn't exist so far, let's create it
group2sessions = {sessionIDs : {}};
}
// set the session into the database // add the entry for this session
db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil}); group2sessions.sessionIDs[sessionID] = 1;
callback(); // save the new element back
}, await db.set("group2sessions:" + groupID, group2sessions);
// set the group2sessions entry // get the author2sessions entry
function(callback) let author2sessions = await db.get("author2sessions:" + authorID);
{
// get the entry
db.get("group2sessions:" + groupID, function(err, group2sessions)
{
if(ERR(err, callback)) return;
if (group2sessions == null || group2sessions.sessionIDs == null) { if (author2sessions == null || author2sessions.sessionIDs == null) {
// the entry doesn't exist so far, let's create it // the entry doesn't exist so far, let's create it
group2sessions = {sessionIDs : {}}; author2sessions = {sessionIDs : {}};
} }
// add the entry for this session // add the entry for this session
group2sessions.sessionIDs[sessionID] = 1; author2sessions.sessionIDs[sessionID] = 1;
// save the new element back //save the new element back
db.set("group2sessions:" + groupID, group2sessions); await db.set("author2sessions:" + authorID, author2sessions);
callback(); return { sessionID };
}); }
},
// set the author2sessions entry
function(callback)
{
// get the entry
db.get("author2sessions:" + authorID, function(err, author2sessions)
{
if(ERR(err, callback)) return;
if (author2sessions == null || author2sessions.sessionIDs == null) {
// the entry doesn't exist so far, let's create it
author2sessions = {sessionIDs : {}};
}
// add the entry for this session
author2sessions.sessionIDs[sessionID] = 1;
//save the new element back
db.set("author2sessions:" + authorID, author2sessions);
callback();
});
}
], function(err)
{
if(ERR(err, callback)) return;
// return error and sessionID
callback(null, {sessionID: sessionID});
})
});
// @TODO once external dependencies are using Promises
exports.getSessionInfo = thenify(function(sessionID, callback) exports.getSessionInfo = thenify(function(sessionID, callback)
{ {
// check if the database entry of this session exists // check if the database entry of this session exists
@ -195,160 +141,86 @@ exports.getSessionInfo = thenify(function(sessionID, callback)
/** /**
* Deletes a session * Deletes a session
*/ */
exports.deleteSession = thenify(function(sessionID, callback) exports.deleteSession = async function(sessionID)
{ {
var authorID, groupID; // ensure that the session exists
var group2sessions, author2sessions; let session = await db.get("session:" + sessionID);
if (session == null) {
throw new customError("sessionID does not exist", "apierror");
}
async.series([ // everything is fine, use the sessioninfos
function(callback) let groupID = session.groupID;
{ let authorID = session.authorID;
// get the session entry
db.get("session:" + sessionID, function (err, session)
{
if(ERR(err, callback)) return;
if (session == null) { // get the group2sessions and author2sessions entries
// session does not exist let group2sessions = await db.get("group2sessions:" + groupID);
callback(new customError("sessionID does not exist", "apierror")) let author2sessions = await db.get("author2sessions:" + authorID);
} else {
// everything is fine, use the sessioninfos
authorID = session.authorID;
groupID = session.groupID;
callback(); // remove the session
} await db.remove("session:" + sessionID);
});
},
// get the group2sessions entry // remove session from group2sessions
function(callback) if (group2sessions != null) { // Maybe the group was already deleted
{ delete group2sessions.sessionIDs[sessionID];
db.get("group2sessions:" + groupID, function (err, _group2sessions) await db.set("group2sessions:" + groupID, group2sessions);
{ }
if(ERR(err, callback)) return;
group2sessions = _group2sessions;
callback();
});
},
// get the author2sessions entry // remove session from author2sessions
function(callback) if (author2sessions != null) { // Maybe the author was already deleted
{ delete author2sessions.sessionIDs[sessionID];
db.get("author2sessions:" + authorID, function (err, _author2sessions) await db.set("author2sessions:" + authorID, author2sessions);
{ }
if(ERR(err, callback)) return; }
author2sessions = _author2sessions;
callback();
});
},
// remove the values from the database exports.listSessionsOfGroup = async function(groupID)
function(callback)
{
//remove the session
db.remove("session:" + sessionID);
// remove session from group2sessions
if(group2sessions != null) { // Maybe the group was already deleted
delete group2sessions.sessionIDs[sessionID];
db.set("group2sessions:" + groupID, group2sessions);
}
// remove session from author2sessions
if(author2sessions != null) { // Maybe the author was already deleted
delete author2sessions.sessionIDs[sessionID];
db.set("author2sessions:" + authorID, author2sessions);
}
callback();
}
], function(err)
{
if(ERR(err, callback)) return;
callback();
})
});
exports.listSessionsOfGroup = thenify(function(groupID, callback)
{ {
groupManager.doesGroupExist(groupID, function(err, exists) // check that the group exists
{ let exists = await groupManager.doesGroupExist(groupID);
if(ERR(err, callback)) return; if (!exists) {
throw new customError("groupID does not exist", "apierror");
}
if (exists == false) { let sessions = await listSessionsWithDBKey("group2sessions:" + groupID);
// group does not exist return sessions;
callback(new customError("groupID does not exist", "apierror")); }
} else {
// everything is fine, continue
listSessionsWithDBKey("group2sessions:" + groupID, callback);
}
});
});
exports.listSessionsOfAuthor = thenify(function(authorID, callback) exports.listSessionsOfAuthor = async function(authorID)
{ {
authorManager.doesAuthorExists(authorID, function(err, exists) // check that the author exists
{ let exists = await authorManager.doesAuthorExist(authorID)
if(ERR(err, callback)) return; if (!exists) {
throw new customError("authorID does not exist", "apierror");
}
if (exists == false) { let sessions = await listSessionsWithDBKey("author2sessions:" + authorID);
// group does not exist return sessions;
callback(new customError("authorID does not exist", "apierror")); }
} else {
// everything is fine, continue
listSessionsWithDBKey("author2sessions:" + authorID, callback);
}
});
});
// this function is basically the code listSessionsOfAuthor and listSessionsOfGroup has in common // this function is basically the code listSessionsOfAuthor and listSessionsOfGroup has in common
function listSessionsWithDBKey (dbkey, callback) // required to return null rather than an empty object if there are none
async function listSessionsWithDBKey(dbkey, callback)
{ {
var sessions; // get the group2sessions entry
let sessionObject = await db.get(dbkey);
let sessions = sessionObject ? sessionObject.sessionIDs : null;
async.series([ // iterate through the sessions and get the sessioninfos
function(callback) for (let sessionID in sessions) {
{ try {
// get the group2sessions entry let sessionInfo = await exports.getSessionInfo(sessionID);
db.get(dbkey, function(err, sessionObject) sessions[sessionID] = sessionInfo;
{ } catch (err) {
if(ERR(err, callback)) return; if (err == "apierror: sessionID does not exist") {
sessions = sessionObject ? sessionObject.sessionIDs : null; console.warn(`Found bad session ${sessionID} in ${dbkey}`);
callback(); sessions[sessionID] = null;
}); } else {
}, throw err;
function(callback)
{
// collect all sessionIDs in an arrary
var sessionIDs = [];
for (var i in sessions)
{
sessionIDs.push(i);
} }
// iterate through the sessions and get the sessioninfos
async.forEach(sessionIDs, function(sessionID, callback)
{
exports.getSessionInfo(sessionID, function(err, sessionInfo)
{
if (err == "apierror: sessionID does not exist") {
console.warn(`Found bad session ${sessionID} in ${dbkey}`);
} else if(ERR(err, callback)) {
return;
}
sessions[sessionID] = sessionInfo;
callback();
});
}, callback);
} }
], function(err) }
{
if(ERR(err, callback)) return; return sessions;
callback(null, sessions);
});
} }
// checks if a number is an int // checks if a number is an int