lint: src/node/db/AuthorManager.js

This commit is contained in:
John McLear 2021-01-21 21:06:52 +00:00 committed by Richard Hansen
parent d9225f326f
commit 8aa729a36f

View file

@ -1,3 +1,4 @@
'use strict';
/** /**
* The AuthorManager controlls all information about the Pad authors * The AuthorManager controlls all information about the Pad authors
*/ */
@ -19,11 +20,10 @@
*/ */
const db = require('./DB'); const db = require('./DB');
const customError = require('../utils/customError'); const CustomError = require('../utils/customError');
const randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString; const randomString = require('../../static/js/pad_utils').randomString;
exports.getColorPalette = function () { exports.getColorPalette = () => [
return [
'#ffc7c7', '#ffc7c7',
'#fff1c7', '#fff1c7',
'#e3ffc7', '#e3ffc7',
@ -88,16 +88,15 @@ exports.getColorPalette = function () {
'#e9afc2', '#e9afc2',
'#f8d2a0', '#f8d2a0',
'#b3b3e6', '#b3b3e6',
]; ];
};
/** /**
* Checks if the author exists * Checks if the author exists
*/ */
exports.doesAuthorExist = async function (authorID) { exports.doesAuthorExist = async (authorID) => {
const author = await db.get(`globalAuthor:${authorID}`); const author = await db.get(`globalAuthor:${authorID}`);
return author !== null; return author != null;
}; };
/* exported for backwards compatibility */ /* exported for backwards compatibility */
@ -107,7 +106,7 @@ exports.doesAuthorExists = exports.doesAuthorExist;
* Returns the AuthorID for a token. * Returns the AuthorID for a token.
* @param {String} token The token * @param {String} token The token
*/ */
exports.getAuthor4Token = async function (token) { exports.getAuthor4Token = async (token) => {
const author = await mapAuthorWithDBKey('token2author', token); const author = await mapAuthorWithDBKey('token2author', token);
// return only the sub value authorID // return only the sub value authorID
@ -119,7 +118,7 @@ exports.getAuthor4Token = async function (token) {
* @param {String} token The mapper * @param {String} token The mapper
* @param {String} name The name of the author (optional) * @param {String} name The name of the author (optional)
*/ */
exports.createAuthorIfNotExistsFor = async function (authorMapper, name) { exports.createAuthorIfNotExistsFor = async (authorMapper, name) => {
const author = await mapAuthorWithDBKey('mapper2author', authorMapper); const author = await mapAuthorWithDBKey('mapper2author', authorMapper);
if (name) { if (name) {
@ -140,7 +139,7 @@ async function mapAuthorWithDBKey(mapperkey, mapper) {
// try to map to an author // try to map to an author
const author = await db.get(`${mapperkey}:${mapper}`); const author = await db.get(`${mapperkey}:${mapper}`);
if (author === null) { if (author == null) {
// there is no author with this mapper, so create one // there is no author with this mapper, so create one
const author = await exports.createAuthor(null); const author = await exports.createAuthor(null);
@ -163,7 +162,7 @@ async function mapAuthorWithDBKey(mapperkey, mapper) {
* Internal function that creates the database entry for an author * Internal function that creates the database entry for an author
* @param {String} name The name of the author * @param {String} name The name of the author
*/ */
exports.createAuthor = function (name) { exports.createAuthor = (name) => {
// create the new author name // create the new author name
const author = `a.${randomString(16)}`; const author = `a.${randomString(16)}`;
@ -185,50 +184,40 @@ exports.createAuthor = function (name) {
* Returns the Author Obj of the author * Returns the Author Obj of the author
* @param {String} author The id of the author * @param {String} author The id of the author
*/ */
exports.getAuthor = function (author) { exports.getAuthor = (author) => db.get(`globalAuthor:${author}`);
// NB: result is already a Promise
return db.get(`globalAuthor:${author}`);
};
/** /**
* Returns the color Id of the author * Returns the color Id of the author
* @param {String} author The id of the author * @param {String} author The id of the author
*/ */
exports.getAuthorColorId = function (author) { exports.getAuthorColorId = (author) => db.getSub(`globalAuthor:${author}`, ['colorId']);
return db.getSub(`globalAuthor:${author}`, ['colorId']);
};
/** /**
* Sets the color Id of the author * Sets the color Id of the author
* @param {String} author The id of the author * @param {String} author The id of the author
* @param {String} colorId The color id of the author * @param {String} colorId The color id of the author
*/ */
exports.setAuthorColorId = function (author, colorId) { exports.setAuthorColorId = (author, colorId) => db.setSub(
return db.setSub(`globalAuthor:${author}`, ['colorId'], colorId); `globalAuthor:${author}`, ['colorId'], colorId);
};
/** /**
* Returns the name of the author * Returns the name of the author
* @param {String} author The id of the author * @param {String} author The id of the author
*/ */
exports.getAuthorName = function (author) { exports.getAuthorName = (author) => db.getSub(`globalAuthor:${author}`, ['name']);
return db.getSub(`globalAuthor:${author}`, ['name']);
};
/** /**
* Sets the name of the author * Sets the name of the author
* @param {String} author The id of the author * @param {String} author The id of the author
* @param {String} name The name of the author * @param {String} name The name of the author
*/ */
exports.setAuthorName = function (author, name) { exports.setAuthorName = (author, name) => db.setSub(`globalAuthor:${author}`, ['name'], name);
return db.setSub(`globalAuthor:${author}`, ['name'], name);
};
/** /**
* Returns an array of all pads this author contributed to * Returns an array of all pads this author contributed to
* @param {String} author The id of the author * @param {String} author The id of the author
*/ */
exports.listPadsOfAuthor = async function (authorID) { exports.listPadsOfAuthor = async (authorID) => {
/* There are two other places where this array is manipulated: /* There are two other places where this array is manipulated:
* (1) When the author is added to a pad, the author object is also updated * (1) When the author is added to a pad, the author object is also updated
* (2) When a pad is deleted, each author of that pad is also updated * (2) When a pad is deleted, each author of that pad is also updated
@ -237,9 +226,9 @@ exports.listPadsOfAuthor = async function (authorID) {
// get the globalAuthor // get the globalAuthor
const author = await db.get(`globalAuthor:${authorID}`); const author = await db.get(`globalAuthor:${authorID}`);
if (author === null) { if (author == null) {
// author does not exist // author does not exist
throw new customError('authorID does not exist', 'apierror'); throw new CustomError('authorID does not exist', 'apierror');
} }
// everything is fine, return the pad IDs // everything is fine, return the pad IDs
@ -253,11 +242,11 @@ exports.listPadsOfAuthor = async function (authorID) {
* @param {String} author The id of the author * @param {String} author The id of the author
* @param {String} padID The id of the pad the author contributes to * @param {String} padID The id of the pad the author contributes to
*/ */
exports.addPad = async function (authorID, padID) { exports.addPad = async (authorID, padID) => {
// get the entry // get the entry
const author = await db.get(`globalAuthor:${authorID}`); const author = await db.get(`globalAuthor:${authorID}`);
if (author === null) return; if (author == null) return;
/* /*
* ACHTUNG: padIDs can also be undefined, not just null, so it is not possible * ACHTUNG: padIDs can also be undefined, not just null, so it is not possible
@ -280,12 +269,12 @@ exports.addPad = async function (authorID, padID) {
* @param {String} author The id of the author * @param {String} author The id of the author
* @param {String} padID The id of the pad the author contributes to * @param {String} padID The id of the pad the author contributes to
*/ */
exports.removePad = async function (authorID, padID) { exports.removePad = async (authorID, padID) => {
const author = await db.get(`globalAuthor:${authorID}`); const author = await db.get(`globalAuthor:${authorID}`);
if (author === null) return; if (author == null) return;
if (author.padIDs !== null) { if (author.padIDs != null) {
// remove pad from author // remove pad from author
delete author.padIDs[padID]; delete author.padIDs[padID];
await db.set(`globalAuthor:${authorID}`, author); await db.set(`globalAuthor:${authorID}`, author);