harmonize different ID types, improved the prefixes

This commit is contained in:
Peter 'Pita' Martischka 2011-08-10 14:24:21 +01:00
parent f6b87daa27
commit 292c68a0a5
5 changed files with 54 additions and 64 deletions

View File

@ -129,7 +129,7 @@ function mapAuthorWithDBKey (mapperkey, mapper, callback)
exports.createAuthor = function(name, callback) exports.createAuthor = function(name, callback)
{ {
//create the new author name //create the new author name
var author = "g." + _randomString(16); var author = "a." + randomString(16);
//create the globalAuthors db entry //create the globalAuthors db entry
var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()}; var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()};
@ -193,11 +193,14 @@ exports.setAuthorName = function (author, name, callback)
/** /**
* Generates a random String with the given length. Is needed to generate the Author Ids * Generates a random String with the given length. Is needed to generate the Author Ids
*/ */
function _randomString(len) { function randomString(len)
// use only numbers and lowercase letters {
var pieces = []; var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(var i=0;i<len;i++) { var randomstring = '';
pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1)); for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
} }
return pieces.join(''); return randomstring;
} }

View File

@ -34,41 +34,11 @@ exports.doesGroupExist = function(groupID, callback)
exports.createGroup = function(callback) exports.createGroup = function(callback)
{ {
//search for non existing groupID //search for non existing groupID
var groupID; var groupID = "g." + randomString(16);
var foundNonExistingGroupID = false;
async.whilst( //create the group
function () { return !foundNonExistingGroupID; }, db.set("group:" + groupID, {pads: {}});
function (callback) callback(null, {groupID: groupID});
{
//generate a random 10 digit groupID
groupID = "";
for(var i=0;i<10;i++)
{
groupID+=Math.floor(Math.random()*10);
}
//check if this groupID already exisits
exports.doesGroupExist(groupID, function(err, exists)
{
foundNonExistingGroupID = !exists;
callback(err);
})
},
//we found a non existing groupID or an error happend
function (err)
{
//check for errors
if(err)
{
callback(err);
return;
}
//create the group
db.set("group:" + groupID, {pads: {}});
callback(null, {groupID: groupID});
}
);
} }
exports.createGroupIfNotExistsFor = function(groupMapper, callback) exports.createGroupIfNotExistsFor = function(groupMapper, callback)
@ -183,11 +153,6 @@ exports.createGroupPad = function(groupID, padName, text, callback)
{ {
callback(err, {padID: padID}); callback(err, {padID: padID});
}); });
//check if groupID exists
//check if pad already exists
//create the pad
//create the subentry in the padobject
} }
exports.listPads = function(groupID, callback) exports.listPads = function(groupID, callback)
@ -214,3 +179,18 @@ exports.listPads = function(groupID, callback)
} }
}); });
} }
/**
* Generates a random String with the given length. Is needed to generate the Author Ids
*/
function randomString(len)
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var randomstring = '';
for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}

View File

@ -40,7 +40,7 @@ exports.getReadOnlyId = function (padId, callback)
//there is no readOnly Entry in the database, let's create one //there is no readOnly Entry in the database, let's create one
if(dbReadOnlyId == null) if(dbReadOnlyId == null)
{ {
readOnlyId = randomString(10); readOnlyId = "r." + randomString(16);
db.set("pad2readonly:" + padId, readOnlyId); db.set("pad2readonly:" + padId, readOnlyId);
db.set("readonly2pad:" + readOnlyId, padId); db.set("readonly2pad:" + readOnlyId, padId);
@ -74,10 +74,12 @@ exports.getPadId = function(readOnlyId, callback)
*/ */
function randomString(len) function randomString(len)
{ {
// use only numbers and lowercase letters var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var pieces = []; var randomstring = '';
for(var i=0;i<len;i++) { for (var i = 0; i < len; i++)
pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1)); {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
} }
return pieces.join(''); return randomstring;
} }

View File

@ -380,12 +380,14 @@ function listSessionsWithDBKey (dbkey, callback)
*/ */
function randomString(len) function randomString(len)
{ {
// use only numbers and lowercase letters var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var pieces = []; var randomstring = '';
for(var i=0;i<len;i++) { for (var i = 0; i < len; i++)
pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1)); {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
} }
return pieces.join(''); return randomstring;
} }
//checks if a number is an int //checks if a number is an int

View File

@ -131,11 +131,14 @@ exports.handle = function(functionName, fields, req, res)
/** /**
* Generates a random String with the given length. Is needed to generate the Author Ids * Generates a random String with the given length. Is needed to generate the Author Ids
*/ */
function randomString(len) { function randomString(len)
// use only numbers and lowercase letters {
var pieces = []; var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(var i=0;i<len;i++) { var randomstring = '';
pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1)); for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
} }
return pieces.join(''); return randomstring;
} }