summaryrefslogtreecommitdiff
path: root/src/node/db
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2012-09-17 23:03:56 +0200
committerMarcel Klehr <mklehr@gmx.net>2012-09-17 23:03:56 +0200
commitf8f002adc0ec152b15d67bca9b5288d6b46196e1 (patch)
tree9e4c41b8ae2a6f3013fc060bddf3e13bbe8e909b /src/node/db
parent9cfcafb8525bfef1e30c903aca5497b4c41f9a0c (diff)
downloadetherpad-lite-f8f002adc0ec152b15d67bca9b5288d6b46196e1.zip
Add listAllGroups API endpoint
Adds a database key that lists all groups
Diffstat (limited to 'src/node/db')
-rw-r--r--src/node/db/API.js1
-rw-r--r--src/node/db/GroupManager.js70
2 files changed, 70 insertions, 1 deletions
diff --git a/src/node/db/API.js b/src/node/db/API.js
index c5caae0b..4979e8c6 100644
--- a/src/node/db/API.js
+++ b/src/node/db/API.js
@@ -35,6 +35,7 @@ var cleanText = require("./Pad").cleanText;
/**GROUP FUNCTIONS*****/
/**********************/
+exports.listAllGroups = groupManager.listAllGroups;
exports.createGroup = groupManager.createGroup;
exports.createGroupIfNotExistsFor = groupManager.createGroupIfNotExistsFor;
exports.deleteGroup = groupManager.deleteGroup;
diff --git a/src/node/db/GroupManager.js b/src/node/db/GroupManager.js
index bd19507f..81b0cb9e 100644
--- a/src/node/db/GroupManager.js
+++ b/src/node/db/GroupManager.js
@@ -26,6 +26,24 @@ var db = require("./DB").db;
var async = require("async");
var padManager = require("./PadManager");
var sessionManager = require("./SessionManager");
+
+exports.listAllGroups = function(callback) {
+ db.get("groups", function (err, groups) {
+ if(ERR(err, callback)) return;
+
+ // there are no groups
+ if(groups == null) {
+ callback(null, {groupIDs: []});
+ return;
+ }
+
+ var groupIDs = [];
+ for ( var groupID in groups ) {
+ groupIDs.push(groupID);
+ }
+ callback(null, {groupIDs: groupIDs});
+ });
+}
exports.deleteGroup = function(groupID, callback)
{
@@ -105,6 +123,39 @@ exports.deleteGroup = function(groupID, callback)
db.remove("group2sessions:" + groupID);
db.remove("group:" + groupID);
callback();
+ },
+ //unlist the group
+ function(callback)
+ {
+ exports.listAllGroups(function(err, groups) {
+ if(ERR(err, callback)) return;
+ groups = groups? groups.groupIDs : [];
+
+ // it's not listed
+ if(groups.indexOf(groupID) == -1) {
+ callback();
+ return;
+ }
+
+ groups.splice(groups.indexOf(groupID), 1);
+
+ // store empty groupe list
+ if(groups.length == 0) {
+ db.set("groups", {});
+ callback();
+ return;
+ }
+
+ // regenerate group list
+ var newGroups = {};
+ async.forEach(groups, function(group, cb) {
+ newGroups[group] = 1;
+ cb();
+ },function() {
+ db.set("groups", newGroups);
+ callback();
+ });
+ });
}
], function(err)
{
@@ -130,7 +181,24 @@ exports.createGroup = function(callback)
//create the group
db.set("group:" + groupID, {pads: {}});
- callback(null, {groupID: groupID});
+
+ //list the group
+ exports.listAllGroups(function(err, groups) {
+ if(ERR(err, callback)) return;
+ groups = groups? groups.groupIDs : [];
+
+ groups.push(groupID);
+
+ // regenerate group list
+ var newGroups = {};
+ async.forEach(groups, function(group, cb) {
+ newGroups[group] = 1;
+ cb();
+ },function() {
+ db.set("groups", newGroups);
+ callback(null, {groupID: groupID});
+ });
+ });
}
exports.createGroupIfNotExistsFor = function(groupMapper, callback)