diff --git a/node/db/API.js b/node/db/API.js index 09cc95afc..f996f0f0e 100644 --- a/node/db/API.js +++ b/node/db/API.js @@ -35,6 +35,7 @@ var cleanText = require("./Pad").cleanText; /**GROUP FUNCTIONS*****/ /**********************/ +exports.listGroups = groupManager.listGroups; exports.createGroup = groupManager.createGroup; exports.createGroupIfNotExistsFor = groupManager.createGroupIfNotExistsFor; exports.deleteGroup = groupManager.deleteGroup; @@ -463,6 +464,47 @@ exports.isPasswordProtected = function(padID, callback) }); } +/** +listAllPads() returns a array with all pads + +Example returns: + +{code: 0, message:"ok", data: {padIDs: []}} +*/ +exports.listAllPads = function(callback) +{ + allPads = []; + + var defaultGroup = "g.defaultGroupName"; + + //get all groups + groupManager.listGroups(function (err, groups) + { + groups=groups["groupIDs"]; + // if defaultGroup exists add this too, becaus ists not listed in groupManager.listGroups + groupManager.doesGroupExist(defaultGroup, function(err, exists) + { + if(exists) + { + groups.push(defaultGroup); + } + }); + + for(var group in groups) + { + groupManager.listPads(groups[group], function(err, pads) + { + for(var pad in pads["padIDs"]) + { + allPads.push(pads["padIDs"][pad]); + } + }); + } + }); + + callback(null, {padIDs: allPads}); +} + /******************************/ /** INTERNAL HELPER FUNCTIONS */ /******************************/ diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 04c79cfae..4709a5b03 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -102,6 +102,27 @@ exports.deleteGroup = function(groupID, callback) //remove group and group2sessions entry function(callback) { + // remove group from groups entry + db.get("groups", function (err, groups) + { + if(ERR(err, callback)) return; + + existingGroups = []; + + if(groups != undefined) + { + for(var key in groups['groups']) + { + if(groupID != groups['groups'][key]) + { + existingGroups.push(groups['groups'][key]); + } + } + } + + db.set("groups", {groups: existingGroups}); + }); + db.remove("group2sessions:" + groupID); db.remove("group:" + groupID); callback(); @@ -112,6 +133,17 @@ exports.deleteGroup = function(groupID, callback) callback(); }); } + +exports.listGroups = function(callback) +{ + //try to get the groups entry + db.get("groups", function (err, groups) + { + if(groups == null) groups["groups"] = []; + if(ERR(err, callback)) return; + callback(null, {groupIDs: groups["groups"]}); + }); +} exports.doesGroupExist = function(groupID, callback) { @@ -125,9 +157,36 @@ exports.doesGroupExist = function(groupID, callback) exports.createGroup = function(callback) { + // load all existing groups from db + existingGroups = []; + + exports.listGroups(function(err, responseGroups) + { + if(ERR(err, callback)) return; + if(responseGroups != undefined) + { + for(var key in responseGroups['groups']) + { + existingGroups.push(responseGroups['groups'][key]); + } + } + }); + //search for non existing groupID var groupID = "g." + randomString(16); + // check if group already exisits + exports.doesGroupExist(groupID, function(err, groupExist) + { + if(groupExist) return; + }); + + // add the new group to the array + existingGroups.push(groupID); + + // update the entry to the db, which hold all groups + db.set("groups", {groups: existingGroups}); + //create the group db.set("group:" + groupID, {pads: {}}); callback(null, {groupID: groupID}); diff --git a/node/db/Pad.js b/node/db/Pad.js index 40875effb..27f63c8fa 100644 --- a/node/db/Pad.js +++ b/node/db/Pad.js @@ -12,6 +12,7 @@ var async = require("async"); var settings = require('../utils/Settings'); var authorManager = require("./AuthorManager"); var padManager = require("./PadManager"); +var groupManager = require("./GroupManager"); var padMessageHandler = require("../handler/PadMessageHandler"); var readOnlyManager = require("./ReadOnlyManager"); var crypto = require("crypto"); @@ -351,6 +352,20 @@ Pad.prototype.init = function init(text, callback) { { var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(text)); + // if this is a non group pad, add this to the defaultGroup + if(_this.id.indexOf("$")==-1) + { + groupID = "g.defaultGroupName"; + groupManager.doesGroupExist(groupID, function(err, exists) + { + if(!exists) + { + db.set("group:" + groupID, {pads: {}}); + } + }); + db.setSub("group:"+groupID, ["pads", _this.id], 1); + } + _this.appendRevision(firstChangeset, ''); } @@ -391,10 +406,23 @@ Pad.prototype.remove = function remove(callback) { callback(); }); } - //its no group pad, nothing to do here + //its no group pad, so remove this pad from the defaultGroup else { - callback(); + var groupID = "g.defaultGroupName"; + + db.get("group:" + groupID, function (err, group) + { + if(ERR(err, callback)) return; + + //remove the pad entry + delete group.pads[padID]; + + //set the new value + db.set("group:" + groupID, group); + + callback(); + }); } }, //remove the readonly entries diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index a7f66151c..811f47a03 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -39,10 +39,12 @@ catch(e) //a list of all functions var functions = { + "listGroups" : [], "createGroup" : [], "createGroupIfNotExistsFor" : ["groupMapper"], "deleteGroup" : ["groupID"], - "listPads" : ["groupID"], + "listPads" : ["groupID"], + "listAllPads" : [], "createPad" : ["padID", "text"], "createGroupPad" : ["groupID", "padName", "text"], "createAuthor" : ["name"], diff --git a/static/tests.html b/static/tests.html index bd5bc578f..57f3117c2 100644 --- a/static/tests.html +++ b/static/tests.html @@ -3,7 +3,7 @@