2011-08-03 19:31:25 +01:00
|
|
|
/**
|
|
|
|
* The Group Manager provides functions to manage groups in the database
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-08-03 19:31:25 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-02-28 21:19:10 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const customError = require('../utils/customError');
|
|
|
|
const randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
|
|
|
|
const db = require('./DB');
|
|
|
|
const padManager = require('./PadManager');
|
|
|
|
const sessionManager = require('./SessionManager');
|
|
|
|
|
|
|
|
exports.listAllGroups = async function () {
|
|
|
|
let groups = await db.get('groups');
|
2019-01-25 12:56:57 +00:00
|
|
|
groups = groups || {};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const groupIDs = Object.keys(groups);
|
|
|
|
return {groupIDs};
|
|
|
|
};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.deleteGroup = async function (groupID) {
|
|
|
|
const group = await db.get(`group:${groupID}`);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// ensure group exists
|
|
|
|
if (group == null) {
|
|
|
|
// group does not exist
|
2020-11-23 13:24:19 -05:00
|
|
|
throw new customError('groupID does not exist', 'apierror');
|
2019-01-25 12:56:57 +00:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-02-01 00:07:06 +00:00
|
|
|
// iterate through all pads of this group and delete them (in parallel)
|
2020-11-23 13:24:19 -05:00
|
|
|
await Promise.all(Object.keys(group.pads).map((padID) => padManager.getPad(padID).then((pad) => pad.remove())));
|
2019-01-25 12:56:57 +00:00
|
|
|
|
|
|
|
// iterate through group2sessions and delete all sessions
|
2020-11-23 13:24:19 -05:00
|
|
|
const group2sessions = await db.get(`group2sessions:${groupID}`);
|
|
|
|
const sessions = group2sessions ? group2sessions.sessionIDs : {};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-02-01 00:07:06 +00:00
|
|
|
// loop through all sessions and delete them (in parallel)
|
2020-11-23 13:24:19 -05:00
|
|
|
await Promise.all(Object.keys(sessions).map((session) => sessionManager.deleteSession(session)));
|
2019-01-25 12:56:57 +00:00
|
|
|
|
|
|
|
// remove group and group2sessions entry
|
2020-11-23 13:24:19 -05:00
|
|
|
await db.remove(`group2sessions:${groupID}`);
|
|
|
|
await db.remove(`group:${groupID}`);
|
2019-01-25 12:56:57 +00:00
|
|
|
|
|
|
|
// unlist the group
|
|
|
|
let groups = await exports.listAllGroups();
|
|
|
|
groups = groups ? groups.groupIDs : [];
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const index = groups.indexOf(groupID);
|
2019-01-25 12:56:57 +00:00
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
// it's not listed
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove from the list
|
|
|
|
groups.splice(index, 1);
|
|
|
|
|
|
|
|
// regenerate group list
|
2020-11-23 13:24:19 -05:00
|
|
|
const newGroups = {};
|
|
|
|
groups.forEach((group) => newGroups[group] = 1);
|
|
|
|
await db.set('groups', newGroups);
|
|
|
|
};
|
2019-01-25 12:56:57 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.doesGroupExist = async function (groupID) {
|
2019-02-08 23:20:57 +01:00
|
|
|
// try to get the group entry
|
2020-11-23 13:24:19 -05:00
|
|
|
const group = await db.get(`group:${groupID}`);
|
2019-01-31 11:14:38 +00:00
|
|
|
|
|
|
|
return (group != null);
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2011-08-08 16:21:31 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.createGroup = async function () {
|
2019-02-08 23:20:57 +01:00
|
|
|
// search for non existing groupID
|
2020-11-23 13:24:19 -05:00
|
|
|
const groupID = `g.${randomString(16)}`;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// create the group
|
2020-11-23 13:24:19 -05:00
|
|
|
await db.set(`group:${groupID}`, {pads: {}});
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// list the group
|
2019-01-25 12:56:57 +00:00
|
|
|
let groups = await exports.listAllGroups();
|
2020-11-23 13:24:19 -05:00
|
|
|
groups = groups ? groups.groupIDs : [];
|
2019-01-25 12:56:57 +00:00
|
|
|
groups.push(groupID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// regenerate group list
|
2020-11-23 13:24:19 -05:00
|
|
|
const newGroups = {};
|
|
|
|
groups.forEach((group) => newGroups[group] = 1);
|
|
|
|
await db.set('groups', newGroups);
|
2019-01-25 12:56:57 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return {groupID};
|
|
|
|
};
|
2011-08-08 16:21:31 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.createGroupIfNotExistsFor = async function (groupMapper) {
|
2019-02-08 23:20:57 +01:00
|
|
|
// ensure mapper is optional
|
2020-11-23 13:24:19 -05:00
|
|
|
if (typeof groupMapper !== 'string') {
|
|
|
|
throw new customError('groupMapper is not a string', 'apierror');
|
2011-08-08 16:21:31 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// try to get a group for this mapper
|
2020-11-23 13:24:19 -05:00
|
|
|
const groupID = await db.get(`mapper2group:${groupMapper}`);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
if (groupID) {
|
|
|
|
// there is a group for this mapper
|
2020-11-23 13:24:19 -05:00
|
|
|
const exists = await exports.doesGroupExist(groupID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
if (exists) return {groupID};
|
2019-01-25 12:56:57 +00:00
|
|
|
}
|
2018-08-29 02:36:25 +02:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// hah, the returned group doesn't exist, let's create one
|
2020-11-23 13:24:19 -05:00
|
|
|
const result = await exports.createGroup();
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// create the mapper entry for this group
|
2020-11-23 13:24:19 -05:00
|
|
|
await db.set(`mapper2group:${groupMapper}`, result.groupID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
return result;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2018-08-29 02:38:09 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.createGroupPad = async function (groupID, padName, text) {
|
2019-01-25 12:56:57 +00:00
|
|
|
// create the padID
|
2020-11-23 13:24:19 -05:00
|
|
|
const padID = `${groupID}$${padName}`;
|
2018-08-29 02:39:05 +02:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// ensure group exists
|
2020-11-23 13:24:19 -05:00
|
|
|
const groupExists = await exports.doesGroupExist(groupID);
|
2018-08-29 02:39:05 +02:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
if (!groupExists) {
|
2020-11-23 13:24:19 -05:00
|
|
|
throw new customError('groupID does not exist', 'apierror');
|
2019-01-25 12:56:57 +00:00
|
|
|
}
|
2011-08-08 16:21:31 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// ensure pad doesn't exist already
|
2020-11-23 13:24:19 -05:00
|
|
|
const padExists = await padManager.doesPadExists(padID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
if (padExists) {
|
|
|
|
// pad exists already
|
2020-11-23 13:24:19 -05:00
|
|
|
throw new customError('padName does already exist', 'apierror');
|
2019-01-25 12:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the pad
|
|
|
|
await padManager.getPad(padID, text);
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
// create an entry in the group for this pad
|
|
|
|
await db.setSub(`group:${groupID}`, ['pads', padID], 1);
|
2019-01-25 12:56:57 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return {padID};
|
|
|
|
};
|
2011-08-03 19:31:25 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.listPads = async function (groupID) {
|
|
|
|
const exists = await exports.doesGroupExist(groupID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 12:56:57 +00:00
|
|
|
// ensure the group exists
|
|
|
|
if (!exists) {
|
2020-11-23 13:24:19 -05:00
|
|
|
throw new customError('groupID does not exist', 'apierror');
|
2019-01-25 12:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// group exists, let's get the pads
|
2020-11-23 13:24:19 -05:00
|
|
|
const result = await db.getSub(`group:${groupID}`, ['pads']);
|
|
|
|
const padIDs = Object.keys(result);
|
2019-01-25 12:56:57 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return {padIDs};
|
|
|
|
};
|