Added rewrite.

This commit is contained in:
SamTV12345 2024-07-22 14:53:37 +02:00
parent fa2d6d15a9
commit f8175a6433
76 changed files with 3150 additions and 2453 deletions

View file

@ -19,18 +19,18 @@
* limitations under the License.
*/
const CustomError = require('../utils/customError');
import CustomError from '../utils/customError';
const randomString = require('../../static/js/pad_utils').randomString;
const db = require('./DB');
const padManager = require('./PadManager');
const sessionManager = require('./SessionManager');
import {get, getSub, remove, set, setSub} from './DB';
import {doesPadExist, getPad} from './PadManager';
import {deleteSession} from './SessionManager';
/**
* Lists all groups
* @return {Promise<{groupIDs: string[]}>} The ids of all groups
*/
exports.listAllGroups = async () => {
let groups = await db.get('groups');
export const listAllGroups = async (): Promise<{ groupIDs: string[]; }> => {
let groups = await get('groups');
groups = groups || {};
const groupIDs = Object.keys(groups);
@ -42,8 +42,8 @@ exports.listAllGroups = async () => {
* @param {String} groupID The id of the group
* @return {Promise<void>} Resolves when the group is deleted
*/
exports.deleteGroup = async (groupID: string): Promise<void> => {
const group = await db.get(`group:${groupID}`);
export const deleteGroup = async (groupID: string): Promise<void> => {
const group = await get(`group:${groupID}`);
// ensure group exists
if (group == null) {
@ -53,28 +53,28 @@ exports.deleteGroup = async (groupID: string): Promise<void> => {
// iterate through all pads of this group and delete them (in parallel)
await Promise.all(Object.keys(group.pads).map(async (padId) => {
const pad = await padManager.getPad(padId);
const pad = await getPad(padId);
await pad.remove();
}));
// Delete associated sessions in parallel. This should be done before deleting the group2sessions
// record because deleting a session updates the group2sessions record.
const {sessionIDs = {}} = await db.get(`group2sessions:${groupID}`) || {};
const {sessionIDs = {}} = await get(`group2sessions:${groupID}`) || {};
await Promise.all(Object.keys(sessionIDs).map(async (sessionId) => {
await sessionManager.deleteSession(sessionId);
await deleteSession(sessionId);
}));
await Promise.all([
db.remove(`group2sessions:${groupID}`),
remove(`group2sessions:${groupID}`),
// UeberDB's setSub() method atomically reads the record, updates the appropriate property, and
// writes the result. Setting a property to `undefined` deletes that property (JSON.stringify()
// ignores such properties).
db.setSub('groups', [groupID], undefined),
...Object.keys(group.mappings || {}).map(async (m) => await db.remove(`mapper2group:${m}`)),
setSub('groups', [groupID], undefined),
...Object.keys(group.mappings || {}).map(async (m) => await remove(`mapper2group:${m}`)),
]);
// Remove the group record after updating the `groups` record so that the state is consistent.
await db.remove(`group:${groupID}`);
await remove(`group:${groupID}`);
};
/**
@ -82,9 +82,9 @@ exports.deleteGroup = async (groupID: string): Promise<void> => {
* @param {String} groupID the id of the group to delete
* @return {Promise<boolean>} Resolves to true if the group exists
*/
exports.doesGroupExist = async (groupID: string) => {
export const doesGroupExist = async (groupID: string) => {
// try to get the group entry
const group = await db.get(`group:${groupID}`);
const group = await get(`group:${groupID}`);
return (group != null);
};
@ -93,13 +93,13 @@ exports.doesGroupExist = async (groupID: string) => {
* Creates a new group
* @return {Promise<{groupID: string}>} the id of the new group
*/
exports.createGroup = async () => {
export const createGroup = async (): Promise<{ groupID: string; }> => {
const groupID = `g.${randomString(16)}`;
await db.set(`group:${groupID}`, {pads: {}, mappings: {}});
await set(`group:${groupID}`, {pads: {}, mappings: {}});
// Add the group to the `groups` record after the group's individual record is created so that
// the state is consistent. Note: UeberDB's setSub() method atomically reads the record, updates
// the appropriate property, and writes the result.
await db.setSub('groups', [groupID], 1);
await setSub('groups', [groupID], 1);
return {groupID};
};
@ -108,20 +108,20 @@ exports.createGroup = async () => {
* @param groupMapper the mapper of the group
* @return {Promise<{groupID: string}|{groupID: *}>} a promise that resolves to the group ID
*/
exports.createGroupIfNotExistsFor = async (groupMapper: string|object) => {
export const createGroupIfNotExistsFor = async (groupMapper: string|object) => {
if (typeof groupMapper !== 'string') {
throw new CustomError('groupMapper is not a string', 'apierror');
}
const groupID = await db.get(`mapper2group:${groupMapper}`);
if (groupID && await exports.doesGroupExist(groupID)) return {groupID};
const result = await exports.createGroup();
const groupID = await get(`mapper2group:${groupMapper}`) as string;
if (groupID && await doesGroupExist(groupID)) return {groupID};
const result = await createGroup();
await Promise.all([
db.set(`mapper2group:${groupMapper}`, result.groupID),
set(`mapper2group:${groupMapper}`, result.groupID),
// Remember the mapping in the group record so that it can be cleaned up when the group is
// deleted. Although the core Etherpad API does not support multiple mappings for the same
// group, the database record does support multiple mappings in case a plugin decides to extend
// the core Etherpad functionality. (It's also easy to implement it this way.)
db.setSub(`group:${result.groupID}`, ['mappings', groupMapper], 1),
setSub(`group:${result.groupID}`, ['mappings', groupMapper], 1),
]);
return result;
};
@ -134,19 +134,19 @@ exports.createGroupIfNotExistsFor = async (groupMapper: string|object) => {
* @param {String} authorId The id of the author
* @return {Promise<{padID: string}>} a promise that resolves to the id of the new pad
*/
exports.createGroupPad = async (groupID: string, padName: string, text: string, authorId: string = ''): Promise<{ padID: string; }> => {
export const createGroupPad = async (groupID: string, padName: string, text: string, authorId: string = ''): Promise<{ padID: string; }> => {
// create the padID
const padID = `${groupID}$${padName}`;
// ensure group exists
const groupExists = await exports.doesGroupExist(groupID);
const groupExists = await doesGroupExist(groupID);
if (!groupExists) {
throw new CustomError('groupID does not exist', 'apierror');
}
// ensure pad doesn't exist already
const padExists = await padManager.doesPadExists(padID);
const padExists = await doesPadExist(padID);
if (padExists) {
// pad exists already
@ -154,10 +154,10 @@ exports.createGroupPad = async (groupID: string, padName: string, text: string,
}
// create the pad
await padManager.getPad(padID, text, authorId);
await getPad(padID, text, authorId);
// create an entry in the group for this pad
await db.setSub(`group:${groupID}`, ['pads', padID], 1);
await setSub(`group:${groupID}`, ['pads', padID], 1);
return {padID};
};
@ -167,8 +167,8 @@ exports.createGroupPad = async (groupID: string, padName: string, text: string,
* @param {String} groupID The id of the group
* @return {Promise<{padIDs: string[]}>} a promise that resolves to the ids of all pads of the group
*/
exports.listPads = async (groupID: string): Promise<{ padIDs: string[]; }> => {
const exists = await exports.doesGroupExist(groupID);
export const listPads = async (groupID: string): Promise<{ padIDs: string[]; }> => {
const exists = await doesGroupExist(groupID);
// ensure the group exists
if (!exists) {
@ -176,7 +176,7 @@ exports.listPads = async (groupID: string): Promise<{ padIDs: string[]; }> => {
}
// group exists, let's get the pads
const result = await db.getSub(`group:${groupID}`, ['pads']);
const result = await getSub(`group:${groupID}`, ['pads']);
const padIDs = Object.keys(result);
return {padIDs};