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.
|
|
|
|
*/
|
|
|
|
|
2011-12-10 16:46:47 +01:00
|
|
|
var customError = require("../utils/customError");
|
2012-01-30 15:59:13 +01:00
|
|
|
var Pad = require("../db/Pad").Pad;
|
2019-01-31 11:14:38 +00:00
|
|
|
var db = require("./DB");
|
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
|
|
|
*/
|
2011-12-08 23:32:51 +01:00
|
|
|
var globalPads = {
|
2019-02-08 23:20:57 +01:00
|
|
|
get: function(name) { return this[':'+name]; },
|
|
|
|
set: function(name, value) {
|
2013-01-08 20:14:01 +01:00
|
|
|
this[':'+name] = value;
|
|
|
|
},
|
2019-02-08 23:20:57 +01:00
|
|
|
remove: function(name) {
|
2013-10-13 21:20:19 +02:00
|
|
|
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.
|
|
|
|
*/
|
2019-01-28 16:20:30 +00:00
|
|
|
let padList = {
|
2020-03-24 11:51:15 +01:00
|
|
|
list: new Set(),
|
|
|
|
cachedList: undefined,
|
2013-10-13 21:20:19 +02:00
|
|
|
initiated: false,
|
2019-01-28 16:20:30 +00:00
|
|
|
init: async function() {
|
|
|
|
let dbData = await db.findKeys("pad:*", "*:*:*");
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-28 16:20:30 +00:00
|
|
|
if (dbData != null) {
|
|
|
|
this.initiated = true;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-28 16:20:30 +00:00
|
|
|
for (let val of dbData) {
|
2020-03-24 11:51:15 +01:00
|
|
|
this.addPad(val.replace(/^pad:/,""), false);
|
2013-01-08 20:14:01 +01:00
|
|
|
}
|
2019-01-28 16:20:30 +00:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2013-01-08 20:14:01 +01:00
|
|
|
return this;
|
2019-01-28 16:20:30 +00:00
|
|
|
},
|
|
|
|
load: async function() {
|
|
|
|
if (!this.initiated) {
|
|
|
|
return this.init();
|
2019-02-08 23:20:57 +01:00
|
|
|
}
|
2019-01-28 16:20:30 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
2013-01-11 18:31:53 +01:00
|
|
|
/**
|
|
|
|
* Returns all pads in alphabetical order as array.
|
|
|
|
*/
|
2019-01-28 16:20:30 +00:00
|
|
|
getPads: async function() {
|
|
|
|
await this.load();
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-03-24 11:51:15 +01:00
|
|
|
if (!this.cachedList) {
|
|
|
|
this.cachedList = Array.from(this.list).sort();
|
2019-01-28 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:51:15 +01:00
|
|
|
return this.cachedList;
|
2019-01-28 16:20:30 +00:00
|
|
|
},
|
2019-02-08 23:20:57 +01:00
|
|
|
addPad: function(name) {
|
|
|
|
if (!this.initiated) return;
|
|
|
|
|
2020-03-24 11:51:15 +01:00
|
|
|
if (!this.list.has(name)) {
|
|
|
|
this.list.add(name);
|
|
|
|
this.cachedList = undefined;
|
2013-01-08 20:14:01 +01:00
|
|
|
}
|
|
|
|
},
|
2019-02-08 23:20:57 +01:00
|
|
|
removePad: function(name) {
|
|
|
|
if (!this.initiated) return;
|
|
|
|
|
2020-03-24 11:51:15 +01:00
|
|
|
if (this.list.has(name)) {
|
|
|
|
this.list.delete(name);
|
|
|
|
this.cachedList = undefined;
|
2013-01-08 20:14:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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
|
2019-02-08 23:20:57 +01:00
|
|
|
* @param {Function} callback
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-31 11:14:38 +00:00
|
|
|
exports.getPad = async function(id, text)
|
2019-02-08 23:20:57 +01:00
|
|
|
{
|
|
|
|
// check if this is a valid padId
|
|
|
|
if (!exports.isValidPadId(id)) {
|
2019-01-31 11:14:38 +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
|
|
|
|
if (typeof text != "string") {
|
2019-01-31 11:14:38 +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) {
|
2019-01-31 11:14:38 +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
|
2018-08-29 01:44:13 +02:00
|
|
|
pad = new Pad(id);
|
2013-02-10 19:36:46 +00:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// initalize the pad
|
2019-01-31 11:14:38 +00:00
|
|
|
await pad.init(text);
|
|
|
|
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;
|
|
|
|
}
|
2011-08-03 19:31:25 +01:00
|
|
|
|
2019-01-28 16:20:30 +00:00
|
|
|
exports.listAllPads = async function()
|
2013-01-08 20:14:01 +01:00
|
|
|
{
|
2019-01-28 16:20:30 +00:00
|
|
|
let padIDs = await padList.getPads();
|
|
|
|
|
|
|
|
return { padIDs };
|
|
|
|
}
|
2013-01-08 20:14:01 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// checks if a pad exists
|
2019-01-31 11:14:38 +00:00
|
|
|
exports.doesPadExist = async function(padId)
|
2011-08-03 19:31:25 +01:00
|
|
|
{
|
2019-01-31 11:14:38 +00:00
|
|
|
let 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);
|
|
|
|
}
|
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, '_'],
|
|
|
|
[/:+/g, '_']
|
|
|
|
];
|
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
|
|
|
|
exports.sanitizePadId = async function sanitizePadId(padId) {
|
|
|
|
for (let i = 0, n = padIdTransforms.length; i < n; ++i) {
|
|
|
|
let 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
|
|
|
|
2019-01-25 15:15:16 +00:00
|
|
|
let [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
|
|
|
|
|
|
|
// we're out of possible transformations, so just return it
|
|
|
|
return padId;
|
2019-01-21 16:28:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 16:07:58 +01:00
|
|
|
exports.isValidPadId = function(padId)
|
|
|
|
{
|
2011-08-13 17:48:36 +01:00
|
|
|
return /^(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.
|
|
|
|
*/
|
2019-02-08 23:20:57 +01:00
|
|
|
exports.removePad = function(padId) {
|
|
|
|
db.remove("pad:" + padId);
|
2013-01-08 20:14:01 +01:00
|
|
|
exports.unloadPad(padId);
|
|
|
|
padList.removePad(padId);
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// removes a pad from the cache
|
2011-08-16 20:02:30 +01:00
|
|
|
exports.unloadPad = function(padId)
|
|
|
|
{
|
2013-10-13 21:20:19 +02:00
|
|
|
globalPads.remove(padId);
|
2011-08-16 20:02:30 +01:00
|
|
|
}
|