etherpad-lite/src/node/db/GroupManager.js

175 lines
4.8 KiB
JavaScript
Raw Normal View History

2021-01-21 21:06:52 +00:00
'use strict';
/**
* The Group Manager provides functions to manage groups in the database
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
*
* 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
2021-01-21 21:06:52 +00:00
const CustomError = require('../utils/customError');
const randomString = require('../../static/js/pad_utils').randomString;
2020-11-23 13:24:19 -05:00
const db = require('./DB');
const padManager = require('./PadManager');
const sessionManager = require('./SessionManager');
2021-01-21 21:06:52 +00:00
exports.listAllGroups = async () => {
2020-11-23 13:24:19 -05:00
let groups = await db.get('groups');
groups = groups || {};
2020-11-23 13:24:19 -05:00
const groupIDs = Object.keys(groups);
return {groupIDs};
};
2021-01-21 21:06:52 +00:00
exports.deleteGroup = async (groupID) => {
2020-11-23 13:24:19 -05:00
const group = await db.get(`group:${groupID}`);
// ensure group exists
if (group == null) {
// group does not exist
2021-01-21 21:06:52 +00:00
throw new CustomError('groupID does not exist', 'apierror');
}
// iterate through all pads of this group and delete them (in parallel)
2021-01-21 21:06:52 +00:00
await Promise.all(Object.keys(group.pads)
.map((padID) => padManager.getPad(padID)
.then((pad) => pad.remove())
));
// 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 : {};
// 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)));
// remove group2sessions entry
2020-11-23 13:24:19 -05:00
await db.remove(`group2sessions:${groupID}`);
// 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);
if (index !== -1) {
// remove from the list
groups.splice(index, 1);
// regenerate group list
const newGroups = {};
groups.forEach((group) => newGroups[group] = 1);
await db.set('groups', newGroups);
}
// remove group entry
await db.remove(`group:${groupID}`);
2020-11-23 13:24:19 -05:00
};
2021-01-21 21:06:52 +00:00
exports.doesGroupExist = async (groupID) => {
// try to get the group entry
2020-11-23 13:24:19 -05:00
const group = await db.get(`group:${groupID}`);
return (group != null);
2020-11-23 13:24:19 -05:00
};
2011-08-08 16:21:31 +01:00
2021-01-21 21:06:52 +00:00
exports.createGroup = async () => {
// search for non existing groupID
2020-11-23 13:24:19 -05:00
const groupID = `g.${randomString(16)}`;
// create the group
2020-11-23 13:24:19 -05:00
await db.set(`group:${groupID}`, {pads: {}});
// list the group
let groups = await exports.listAllGroups();
2020-11-23 13:24:19 -05:00
groups = groups ? groups.groupIDs : [];
groups.push(groupID);
// regenerate group list
2020-11-23 13:24:19 -05:00
const newGroups = {};
groups.forEach((group) => newGroups[group] = 1);
await db.set('groups', newGroups);
2020-11-23 13:24:19 -05:00
return {groupID};
};
2011-08-08 16:21:31 +01:00
2021-01-21 21:06:52 +00:00
exports.createGroupIfNotExistsFor = async (groupMapper) => {
// ensure mapper is optional
2020-11-23 13:24:19 -05:00
if (typeof groupMapper !== 'string') {
2021-01-21 21:06:52 +00:00
throw new CustomError('groupMapper is not a string', 'apierror');
2011-08-08 16:21:31 +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}`);
if (groupID) {
// there is a group for this mapper
2020-11-23 13:24:19 -05:00
const exists = await exports.doesGroupExist(groupID);
2020-11-23 13:24:19 -05:00
if (exists) return {groupID};
}
// hah, the returned group doesn't exist, let's create one
2020-11-23 13:24:19 -05:00
const result = await exports.createGroup();
// create the mapper entry for this group
2020-11-23 13:24:19 -05:00
await db.set(`mapper2group:${groupMapper}`, result.groupID);
return result;
2020-11-23 13:24:19 -05:00
};
2021-01-21 21:06:52 +00:00
exports.createGroupPad = async (groupID, padName, text) => {
// create the padID
2020-11-23 13:24:19 -05:00
const padID = `${groupID}$${padName}`;
// ensure group exists
2020-11-23 13:24:19 -05:00
const groupExists = await exports.doesGroupExist(groupID);
if (!groupExists) {
2021-01-21 21:06:52 +00:00
throw new CustomError('groupID does not exist', 'apierror');
}
2011-08-08 16:21:31 +01:00
// ensure pad doesn't exist already
2020-11-23 13:24:19 -05:00
const padExists = await padManager.doesPadExists(padID);
if (padExists) {
// pad exists already
2021-01-21 21:06:52 +00:00
throw new CustomError('padName does already exist', 'apierror');
}
// 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);
2020-11-23 13:24:19 -05:00
return {padID};
};
2021-01-21 21:06:52 +00:00
exports.listPads = async (groupID) => {
2020-11-23 13:24:19 -05:00
const exists = await exports.doesGroupExist(groupID);
// ensure the group exists
if (!exists) {
2021-01-21 21:06:52 +00:00
throw new CustomError('groupID does not exist', 'apierror');
}
// 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);
2020-11-23 13:24:19 -05:00
return {padIDs};
};