summaryrefslogtreecommitdiff
path: root/node/db
diff options
context:
space:
mode:
authorPeter 'Pita' Martischka <petermartischka@googlemail.com>2011-08-10 14:24:21 +0100
committerPeter 'Pita' Martischka <petermartischka@googlemail.com>2011-08-10 22:44:08 +0100
commit292c68a0a5be997c59954fa689a62c58be7286d6 (patch)
tree806c31f94cf9f1978571a71eed947590e2a5c6e7 /node/db
parentf6b87daa270fc3e626fc495866cb1a1da20498e9 (diff)
downloadetherpad-lite-292c68a0a5be997c59954fa689a62c58be7286d6.zip
harmonize different ID types, improved the prefixes
Diffstat (limited to 'node/db')
-rw-r--r--node/db/AuthorManager.js17
-rw-r--r--node/db/GroupManager.js60
-rw-r--r--node/db/ReadOnlyManager.js14
-rw-r--r--node/db/SessionManager.js12
4 files changed, 45 insertions, 58 deletions
diff --git a/node/db/AuthorManager.js b/node/db/AuthorManager.js
index f93ebb68..8fa98189 100644
--- a/node/db/AuthorManager.js
+++ b/node/db/AuthorManager.js
@@ -129,7 +129,7 @@ function mapAuthorWithDBKey (mapperkey, mapper, callback)
exports.createAuthor = function(name, callback)
{
//create the new author name
- var author = "g." + _randomString(16);
+ var author = "a." + randomString(16);
//create the globalAuthors db entry
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
*/
-function _randomString(len) {
- // use only numbers and lowercase letters
- var pieces = [];
- for(var i=0;i<len;i++) {
- pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1));
+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 pieces.join('');
+ return randomstring;
}
diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js
index 0d4ed7f1..424d32de 100644
--- a/node/db/GroupManager.js
+++ b/node/db/GroupManager.js
@@ -34,41 +34,11 @@ exports.doesGroupExist = function(groupID, callback)
exports.createGroup = function(callback)
{
//search for non existing groupID
- var groupID;
- var foundNonExistingGroupID = false;
- async.whilst(
- function () { return !foundNonExistingGroupID; },
- function (callback)
- {
- //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});
- }
- );
+ var groupID = "g." + randomString(16);
+
+ //create the group
+ db.set("group:" + groupID, {pads: {}});
+ callback(null, {groupID: groupID});
}
exports.createGroupIfNotExistsFor = function(groupMapper, callback)
@@ -183,11 +153,6 @@ exports.createGroupPad = function(groupID, padName, text, callback)
{
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)
@@ -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;
+}
diff --git a/node/db/ReadOnlyManager.js b/node/db/ReadOnlyManager.js
index 30d726b6..741f9fd5 100644
--- a/node/db/ReadOnlyManager.js
+++ b/node/db/ReadOnlyManager.js
@@ -40,7 +40,7 @@ exports.getReadOnlyId = function (padId, callback)
//there is no readOnly Entry in the database, let's create one
if(dbReadOnlyId == null)
{
- readOnlyId = randomString(10);
+ readOnlyId = "r." + randomString(16);
db.set("pad2readonly:" + padId, readOnlyId);
db.set("readonly2pad:" + readOnlyId, padId);
@@ -74,10 +74,12 @@ exports.getPadId = function(readOnlyId, callback)
*/
function randomString(len)
{
- // use only numbers and lowercase letters
- var pieces = [];
- for(var i=0;i<len;i++) {
- pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1));
+ 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 pieces.join('');
+ return randomstring;
}
diff --git a/node/db/SessionManager.js b/node/db/SessionManager.js
index 05fe3208..797e6cde 100644
--- a/node/db/SessionManager.js
+++ b/node/db/SessionManager.js
@@ -380,12 +380,14 @@ function listSessionsWithDBKey (dbkey, callback)
*/
function randomString(len)
{
- // use only numbers and lowercase letters
- var pieces = [];
- for(var i=0;i<len;i++) {
- pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1));
+ 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 pieces.join('');
+ return randomstring;
}
//checks if a number is an int