lint: src/node/db/Pad.js

This commit is contained in:
John McLear 2021-01-21 21:06:52 +00:00 committed by Richard Hansen
parent 5ecb3f9f37
commit f0c26c9ba2

View file

@ -1,21 +1,21 @@
'use strict';
/** /**
* The pad object, defined with joose * The pad object, defined with joose
*/ */
const Changeset = require('ep_etherpad-lite/static/js/Changeset'); const Changeset = require('../../static/js/Changeset');
const AttributePool = require('ep_etherpad-lite/static/js/AttributePool'); const AttributePool = require('../../static/js/AttributePool');
const db = require('./DB'); const db = require('./DB');
const settings = require('../utils/Settings'); const settings = require('../utils/Settings');
const authorManager = require('./AuthorManager'); const authorManager = require('./AuthorManager');
const padManager = require('./PadManager'); const padManager = require('./PadManager');
const padMessageHandler = require('../handler/PadMessageHandler'); const padMessageHandler = require('../handler/PadMessageHandler');
const groupManager = require('./GroupManager'); const groupManager = require('./GroupManager');
const customError = require('../utils/customError'); const CustomError = require('../utils/customError');
const readOnlyManager = require('./ReadOnlyManager'); const readOnlyManager = require('./ReadOnlyManager');
const crypto = require('crypto');
const randomString = require('../utils/randomstring'); const randomString = require('../utils/randomstring');
const hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks'); const hooks = require('../../static/js/pluginfw/hooks');
const promises = require('../utils/promises'); const promises = require('../utils/promises');
// serialization/deserialization attributes // serialization/deserialization attributes
@ -23,13 +23,14 @@ const attributeBlackList = ['id'];
const jsonableList = ['pool']; const jsonableList = ['pool'];
/** /**
* Copied from the Etherpad source code. It converts Windows line breaks to Unix line breaks and convert Tabs to spaces * Copied from the Etherpad source code. It converts Windows line breaks to Unix
* line breaks and convert Tabs to spaces
* @param txt * @param txt
*/ */
exports.cleanText = function (txt) { exports.cleanText = (txt) => txt.replace(/\r\n/g, '\n')
return txt.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/\t/g, ' ').replace(/\xa0/g, ' '); .replace(/\r/g, '\n')
}; .replace(/\t/g, ' ')
.replace(/\xa0/g, ' ');
const Pad = function Pad(id) { const Pad = function Pad(id) {
this.atext = Changeset.makeAText('\n'); this.atext = Changeset.makeAText('\n');
@ -56,7 +57,7 @@ Pad.prototype.getSavedRevisionsNumber = function getSavedRevisionsNumber() {
}; };
Pad.prototype.getSavedRevisionsList = function getSavedRevisionsList() { Pad.prototype.getSavedRevisionsList = function getSavedRevisionsList() {
const savedRev = new Array(); const savedRev = [];
for (const rev in this.savedRevisions) { for (const rev in this.savedRevisions) {
savedRev.push(this.savedRevisions[rev].revNum); savedRev.push(this.savedRevisions[rev].revNum);
} }
@ -85,11 +86,11 @@ Pad.prototype.appendRevision = async function appendRevision(aChangeset, author)
newRevData.meta.timestamp = Date.now(); newRevData.meta.timestamp = Date.now();
// ex. getNumForAuthor // ex. getNumForAuthor
if (author != '') { if (author !== '') {
this.pool.putAttrib(['author', author || '']); this.pool.putAttrib(['author', author || '']);
} }
if (newRev % 100 == 0) { if (newRev % 100 === 0) {
newRevData.meta.pool = this.pool; newRevData.meta.pool = this.pool;
newRevData.meta.atext = this.atext; newRevData.meta.atext = this.atext;
} }
@ -104,7 +105,7 @@ Pad.prototype.appendRevision = async function appendRevision(aChangeset, author)
p.push(authorManager.addPad(author, this.id)); p.push(authorManager.addPad(author, this.id));
} }
if (this.head == 0) { if (this.head === 0) {
hooks.callAll('padCreate', {pad: this, author}); hooks.callAll('padCreate', {pad: this, author});
} else { } else {
hooks.callAll('padUpdate', {pad: this, author, revs: newRev, changeset: aChangeset}); hooks.callAll('padUpdate', {pad: this, author, revs: newRev, changeset: aChangeset});
@ -153,7 +154,7 @@ Pad.prototype.getAllAuthors = function getAllAuthors() {
const authors = []; const authors = [];
for (const key in this.pool.numToAttrib) { for (const key in this.pool.numToAttrib) {
if (this.pool.numToAttrib[key][0] == 'author' && this.pool.numToAttrib[key][1] != '') { if (this.pool.numToAttrib[key][0] === 'author' && this.pool.numToAttrib[key][1] !== '') {
authors.push(this.pool.numToAttrib[key][1]); authors.push(this.pool.numToAttrib[key][1]);
} }
} }
@ -177,9 +178,10 @@ Pad.prototype.getInternalRevisionAText = async function getInternalRevisionAText
// get all needed changesets // get all needed changesets
const changesets = []; const changesets = [];
await Promise.all(neededChangesets.map((item) => this.getRevisionChangeset(item).then((changeset) => { await Promise.all(
changesets[item] = changeset; neededChangesets.map((item) => this.getRevisionChangeset(item).then((changeset) => {
}))); changesets[item] = changeset;
})));
// we should have the atext by now // we should have the atext by now
let atext = await p_atext; let atext = await p_atext;
@ -204,10 +206,11 @@ Pad.prototype.getAllAuthorColors = async function getAllAuthorColors() {
const returnTable = {}; const returnTable = {};
const colorPalette = authorManager.getColorPalette(); const colorPalette = authorManager.getColorPalette();
await Promise.all(authors.map((author) => authorManager.getAuthorColorId(author).then((colorId) => { await Promise.all(
// colorId might be a hex color or an number out of the palette authors.map((author) => authorManager.getAuthorColorId(author).then((colorId) => {
returnTable[author] = colorPalette[colorId] || colorId; // colorId might be a hex color or an number out of the palette
}))); returnTable[author] = colorPalette[colorId] || colorId;
})));
return returnTable; return returnTable;
}; };
@ -227,7 +230,7 @@ Pad.prototype.getValidRevisionRange = function getValidRevisionRange(startRev, e
endRev = head; endRev = head;
} }
if (startRev !== null && endRev !== null) { if (startRev != null && endRev != null) {
return {startRev, endRev}; return {startRev, endRev};
} }
return null; return null;
@ -251,7 +254,7 @@ Pad.prototype.setText = async function setText(newText) {
// We want to ensure the pad still ends with a \n, but otherwise keep // We want to ensure the pad still ends with a \n, but otherwise keep
// getText() and setText() consistent. // getText() and setText() consistent.
let changeset; let changeset;
if (newText[newText.length - 1] == '\n') { if (newText[newText.length - 1] === '\n') {
changeset = Changeset.makeSplice(oldText, 0, oldText.length, newText); changeset = Changeset.makeSplice(oldText, 0, oldText.length, newText);
} else { } else {
changeset = Changeset.makeSplice(oldText, 0, oldText.length - 1, newText); changeset = Changeset.makeSplice(oldText, 0, oldText.length - 1, newText);
@ -304,9 +307,10 @@ Pad.prototype.getChatMessages = async function getChatMessages(start, end) {
// get all entries out of the database // get all entries out of the database
const entries = []; const entries = [];
await Promise.all(neededEntries.map((entryObject) => this.getChatMessage(entryObject.entryNum).then((entry) => { await Promise.all(
entries[entryObject.order] = entry; neededEntries.map((entryObject) => this.getChatMessage(entryObject.entryNum).then((entry) => {
}))); entries[entryObject.order] = entry;
})));
// sort out broken chat entries // sort out broken chat entries
// it looks like in happened in the past that the chat head was // it looks like in happened in the past that the chat head was
@ -384,14 +388,16 @@ Pad.prototype.copy = async function copy(destinationID, force) {
// copy all chat messages // copy all chat messages
const chatHead = this.chatHead; const chatHead = this.chatHead;
for (let i = 0; i <= chatHead; ++i) { for (let i = 0; i <= chatHead; ++i) {
const p = db.get(`pad:${sourceID}:chat:${i}`).then((chat) => db.set(`pad:${destinationID}:chat:${i}`, chat)); const p = db.get(`pad:${sourceID}:chat:${i}`)
.then((chat) => db.set(`pad:${destinationID}:chat:${i}`, chat));
promises.push(p); promises.push(p);
} }
// copy all revisions // copy all revisions
const revHead = this.head; const revHead = this.head;
for (let i = 0; i <= revHead; ++i) { for (let i = 0; i <= revHead; ++i) {
const p = db.get(`pad:${sourceID}:revs:${i}`).then((rev) => db.set(`pad:${destinationID}:revs:${i}`, rev)); const p = db.get(`pad:${sourceID}:revs:${i}`)
.then((rev) => db.set(`pad:${destinationID}:revs:${i}`, rev));
promises.push(p); promises.push(p);
} }
@ -426,7 +432,7 @@ Pad.prototype.checkIfGroupExistAndReturnIt = async function checkIfGroupExistAnd
// group does not exist // group does not exist
if (!groupExists) { if (!groupExists) {
throw new customError('groupID does not exist for destinationID', 'apierror'); throw new CustomError('groupID does not exist for destinationID', 'apierror');
} }
} }
return destGroupID; return destGroupID;
@ -446,7 +452,7 @@ Pad.prototype.removePadIfForceIsTrueAndAlreadyExist = async function removePadIf
if (exists) { if (exists) {
if (!force) { if (!force) {
console.error('erroring out without force'); console.error('erroring out without force');
throw new customError('destinationID already exists', 'apierror'); throw new CustomError('destinationID already exists', 'apierror');
} }
// exists and forcing // exists and forcing