2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
2011-05-30 15:53:11 +01:00
|
|
|
* The Pad Manager is a Factory for pad Objects
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-03-26 13:10:41 +00: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.
|
|
|
|
*/
|
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
const CustomError = require('../utils/customError');
|
2021-11-27 17:49:25 -05:00
|
|
|
const Pad = require('../db/Pad');
|
2020-11-23 13:24:19 -05:00
|
|
|
const db = require('./DB');
|
2023-07-03 20:52:49 +02:00
|
|
|
const settings = require('../utils/Settings');
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
/**
|
2013-10-13 21:20:19 +02:00
|
|
|
* A cache of all loaded Pads.
|
|
|
|
*
|
|
|
|
* Provides "get" and "set" functions,
|
2011-09-30 00:41:46 -04:00
|
|
|
* which should be used instead of indexing with brackets. These prepend a
|
|
|
|
* colon to the key, to avoid conflicting with built-in Object methods or with
|
|
|
|
* these functions themselves.
|
|
|
|
*
|
|
|
|
* If this is needed in other places, it would be wise to make this a prototype
|
|
|
|
* that's defined somewhere more sensible.
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2020-11-23 13:24:19 -05:00
|
|
|
const globalPads = {
|
|
|
|
get(name) { return this[`:${name}`]; },
|
|
|
|
set(name, value) {
|
|
|
|
this[`:${name}`] = value;
|
|
|
|
},
|
|
|
|
remove(name) {
|
|
|
|
delete this[`:${name}`];
|
|
|
|
},
|
2011-09-30 00:41:46 -04:00
|
|
|
};
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2013-10-13 21:20:19 +02:00
|
|
|
/**
|
|
|
|
* A cache of the list of all pads.
|
|
|
|
*
|
|
|
|
* Updated without db access as new pads are created/old ones removed.
|
|
|
|
*/
|
2022-01-02 20:40:06 -05:00
|
|
|
const padList = new class {
|
|
|
|
constructor() {
|
|
|
|
this._cachedList = null;
|
|
|
|
this._list = new Set();
|
|
|
|
this._loaded = null;
|
|
|
|
}
|
2019-01-28 16:20:30 +00:00
|
|
|
|
2013-01-11 18:31:53 +01:00
|
|
|
/**
|
|
|
|
* Returns all pads in alphabetical order as array.
|
2023-10-17 12:49:56 +02:00
|
|
|
* @returns {Promise<string[]>} A promise that resolves to an array of pad IDs.
|
2013-01-11 18:31:53 +01:00
|
|
|
*/
|
2020-11-23 13:24:19 -05:00
|
|
|
async getPads() {
|
2022-01-02 20:40:06 -05:00
|
|
|
if (!this._loaded) {
|
|
|
|
this._loaded = (async () => {
|
|
|
|
const dbData = await db.findKeys('pad:*', '*:*:*');
|
|
|
|
if (dbData == null) return;
|
|
|
|
for (const val of dbData) this.addPad(val.replace(/^pad:/, ''));
|
|
|
|
})();
|
2019-01-28 16:20:30 +00:00
|
|
|
}
|
2022-01-02 20:40:06 -05:00
|
|
|
await this._loaded;
|
|
|
|
if (!this._cachedList) this._cachedList = [...this._list].sort();
|
|
|
|
return this._cachedList;
|
|
|
|
}
|
2019-01-28 16:20:30 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
addPad(name) {
|
2022-01-02 20:40:06 -05:00
|
|
|
if (this._list.has(name)) return;
|
|
|
|
this._list.add(name);
|
|
|
|
this._cachedList = null;
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
removePad(name) {
|
2022-01-02 20:40:06 -05:00
|
|
|
if (!this._list.has(name)) return;
|
|
|
|
this._list.delete(name);
|
|
|
|
this._cachedList = null;
|
|
|
|
}
|
|
|
|
}();
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// initialises the all-knowing data structure
|
2013-01-08 20:14:01 +01:00
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
2011-05-30 15:53:11 +01:00
|
|
|
* Returns a Pad Object with the callback
|
2011-03-26 13:10:41 +00:00
|
|
|
* @param id A String with the id of the pad
|
2022-02-17 00:01:07 -05:00
|
|
|
* @param {string} [text] - Optional initial pad text if creating a new pad.
|
|
|
|
* @param {string} [authorId] - Optional author ID of the user that initiated the pad creation (if
|
|
|
|
* applicable).
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2022-02-17 00:01:07 -05:00
|
|
|
exports.getPad = async (id, text, authorId = '') => {
|
2019-02-08 23:20:57 +01:00
|
|
|
// check if this is a valid padId
|
|
|
|
if (!exports.isValidPadId(id)) {
|
2021-01-21 21:06:52 +00:00
|
|
|
throw new CustomError(`${id} is not a valid padId`, 'apierror');
|
2011-08-04 19:20:14 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// check if this is a valid text
|
|
|
|
if (text != null) {
|
|
|
|
// check if text is a string
|
2020-11-23 13:24:19 -05:00
|
|
|
if (typeof text !== 'string') {
|
2021-01-21 21:06:52 +00:00
|
|
|
throw new CustomError('text is not a string', 'apierror');
|
2011-08-08 17:35:40 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// check if text is less than 100k chars
|
|
|
|
if (text.length > 100000) {
|
2021-01-21 21:06:52 +00:00
|
|
|
throw new CustomError('text must be less than 100k chars', 'apierror');
|
2011-08-08 17:35:40 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 11:14:38 +00:00
|
|
|
let pad = globalPads.get(id);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// return pad if it's already loaded
|
|
|
|
if (pad != null) {
|
2019-01-31 11:14:38 +00:00
|
|
|
return pad;
|
2011-05-17 16:33:54 +01:00
|
|
|
}
|
2018-08-29 01:44:13 +02:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// try to load pad
|
2021-11-27 17:49:25 -05:00
|
|
|
pad = new Pad.Pad(id);
|
2013-02-10 19:36:46 +00:00
|
|
|
|
2021-02-03 00:30:07 +01:00
|
|
|
// initialize the pad
|
2022-02-17 00:01:07 -05:00
|
|
|
await pad.init(text, authorId);
|
2019-01-31 11:14:38 +00:00
|
|
|
globalPads.set(id, pad);
|
|
|
|
padList.addPad(id);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 11:14:38 +00:00
|
|
|
return pad;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2011-08-03 19:31:25 +01:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.listAllPads = async () => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const padIDs = await padList.getPads();
|
2019-01-28 16:20:30 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return {padIDs};
|
|
|
|
};
|
2013-01-08 20:14:01 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// checks if a pad exists
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.doesPadExist = async (padId) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const value = await db.get(`pad:${padId}`);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 11:14:38 +00:00
|
|
|
return (value != null && value.atext);
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2011-08-04 16:07:58 +01:00
|
|
|
|
2019-01-25 15:05:12 +00:00
|
|
|
// alias for backwards compatibility
|
|
|
|
exports.doesPadExists = exports.doesPadExist;
|
|
|
|
|
2019-01-25 15:15:16 +00:00
|
|
|
/**
|
|
|
|
* An array of padId transformations. These represent changes in pad name policy over
|
|
|
|
* time, and allow us to "play back" these changes so legacy padIds can be found.
|
|
|
|
*/
|
|
|
|
const padIdTransforms = [
|
|
|
|
[/\s+/g, '_'],
|
2020-11-23 13:24:19 -05:00
|
|
|
[/:+/g, '_'],
|
2019-01-25 15:15:16 +00:00
|
|
|
];
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:15:16 +00:00
|
|
|
// returns a sanitized padId, respecting legacy pad id formats
|
2021-02-21 19:49:41 +00:00
|
|
|
exports.sanitizePadId = async (padId) => {
|
2019-01-25 15:15:16 +00:00
|
|
|
for (let i = 0, n = padIdTransforms.length; i < n; ++i) {
|
2020-11-23 13:24:19 -05:00
|
|
|
const exists = await exports.doesPadExist(padId);
|
2018-08-29 01:46:18 +02:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
if (exists) {
|
2019-01-25 15:15:16 +00:00
|
|
|
return padId;
|
2018-08-29 01:46:18 +02:00
|
|
|
}
|
2018-08-29 01:47:38 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const [from, to] = padIdTransforms[i];
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:15:16 +00:00
|
|
|
padId = padId.replace(from, to);
|
2019-01-21 16:28:05 +00:00
|
|
|
}
|
2019-01-25 15:15:16 +00:00
|
|
|
|
2023-07-03 20:52:49 +02:00
|
|
|
if (settings.lowerCasePadIds) padId = padId.toLowerCase();
|
|
|
|
|
2019-01-25 15:15:16 +00:00
|
|
|
// we're out of possible transformations, so just return it
|
|
|
|
return padId;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2019-01-21 16:28:05 +00:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.isValidPadId = (padId) => /^(g.[a-zA-Z0-9]{16}\$)?[^$]{1,50}$/.test(padId);
|
2011-08-04 16:07:58 +01:00
|
|
|
|
2013-01-08 20:19:10 +01:00
|
|
|
/**
|
|
|
|
* Removes the pad from database and unloads it.
|
|
|
|
*/
|
2020-09-16 21:06:15 -04:00
|
|
|
exports.removePad = async (padId) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const p = db.remove(`pad:${padId}`);
|
2013-01-08 20:14:01 +01:00
|
|
|
exports.unloadPad(padId);
|
|
|
|
padList.removePad(padId);
|
2020-09-16 21:06:15 -04:00
|
|
|
await p;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2013-01-08 20:14:01 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// removes a pad from the cache
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.unloadPad = (padId) => {
|
2013-10-13 21:20:19 +02:00
|
|
|
globalPads.remove(padId);
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|