Merge pull request #1014 from marcelklehr/feature/list-all-groups
Add listAllGroups API endpoint
This commit is contained in:
commit
0883043eb9
|
@ -135,6 +135,10 @@ Pads can belong to a group. The padID of grouppads is starting with a groupID li
|
||||||
* `{code: 1, message:"pad does already exist", data: null}`
|
* `{code: 1, message:"pad does already exist", data: null}`
|
||||||
* `{code: 1, message:"groupID does not exist", data: null}`
|
* `{code: 1, message:"groupID does not exist", data: null}`
|
||||||
|
|
||||||
|
* **listAllGroups()** lists all existing groups<br><br>*Example returns:*
|
||||||
|
* `{code: 0, message:"ok", data: {groupIDs: ["g.mKjkmnAbSMtCt8eL", "g.3ADWx6sbGuAiUmCy"]}}`
|
||||||
|
* `{code: 0, message:"ok", data: {groupIDs: []}}`
|
||||||
|
|
||||||
### Author
|
### Author
|
||||||
These authors are bound to the attributes the users choose (color and name).
|
These authors are bound to the attributes the users choose (color and name).
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
## Keys and their values
|
## Keys and their values
|
||||||
|
|
||||||
|
### groups
|
||||||
|
A list of all existing groups (a JSON object with groupIDs as keys and `1` as values).
|
||||||
|
|
||||||
### pad:$PADID
|
### pad:$PADID
|
||||||
Saves all informations about pads
|
Saves all informations about pads
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ var cleanText = require("./Pad").cleanText;
|
||||||
/**GROUP FUNCTIONS*****/
|
/**GROUP FUNCTIONS*****/
|
||||||
/**********************/
|
/**********************/
|
||||||
|
|
||||||
|
exports.listAllGroups = groupManager.listAllGroups;
|
||||||
exports.createGroup = groupManager.createGroup;
|
exports.createGroup = groupManager.createGroup;
|
||||||
exports.createGroupIfNotExistsFor = groupManager.createGroupIfNotExistsFor;
|
exports.createGroupIfNotExistsFor = groupManager.createGroupIfNotExistsFor;
|
||||||
exports.deleteGroup = groupManager.deleteGroup;
|
exports.deleteGroup = groupManager.deleteGroup;
|
||||||
|
|
|
@ -27,6 +27,24 @@ var async = require("async");
|
||||||
var padManager = require("./PadManager");
|
var padManager = require("./PadManager");
|
||||||
var sessionManager = require("./SessionManager");
|
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)
|
exports.deleteGroup = function(groupID, callback)
|
||||||
{
|
{
|
||||||
var group;
|
var group;
|
||||||
|
@ -105,6 +123,39 @@ exports.deleteGroup = function(groupID, callback)
|
||||||
db.remove("group2sessions:" + groupID);
|
db.remove("group2sessions:" + groupID);
|
||||||
db.remove("group:" + groupID);
|
db.remove("group:" + groupID);
|
||||||
callback();
|
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)
|
], function(err)
|
||||||
{
|
{
|
||||||
|
@ -130,7 +181,24 @@ exports.createGroup = function(callback)
|
||||||
|
|
||||||
//create the group
|
//create the group
|
||||||
db.set("group:" + groupID, {pads: {}});
|
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)
|
exports.createGroupIfNotExistsFor = function(groupMapper, callback)
|
||||||
|
|
|
@ -101,6 +101,7 @@ var version =
|
||||||
, "getAuthorName" : ["authorID"]
|
, "getAuthorName" : ["authorID"]
|
||||||
, "padUsers" : ["padID"]
|
, "padUsers" : ["padID"]
|
||||||
, "sendClientsMessage" : ["padID", "msg"]
|
, "sendClientsMessage" : ["padID", "msg"]
|
||||||
|
, "listAllGroups" : []
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue