2011-07-08 18:42:07 +02:00
|
|
|
/**
|
|
|
|
* The ReadOnlyManager manages the database and rendering releated to read only pads
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 16:26:41 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-07-08 18:42:07 +02: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.
|
|
|
|
*/
|
|
|
|
|
2012-02-28 21:19:10 +01:00
|
|
|
|
2019-01-30 17:19:51 +01:00
|
|
|
var db = require("./DB");
|
2014-01-15 20:25:47 +01:00
|
|
|
var randomString = require("../utils/randomstring");
|
2012-01-28 13:24:58 +01:00
|
|
|
|
2011-07-08 18:42:07 +02:00
|
|
|
/**
|
|
|
|
* returns a read only id for a pad
|
|
|
|
* @param {String} padId the id of the pad
|
|
|
|
*/
|
2019-01-30 17:19:51 +01:00
|
|
|
exports.getReadOnlyId = async function (padId)
|
2019-02-08 23:20:57 +01:00
|
|
|
{
|
2019-01-30 17:19:51 +01:00
|
|
|
// check if there is a pad2readonly entry
|
|
|
|
let readOnlyId = await db.get("pad2readonly:" + padId);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 17:19:51 +01:00
|
|
|
// there is no readOnly Entry in the database, let's create one
|
|
|
|
if (readOnlyId == null) {
|
|
|
|
readOnlyId = "r." + randomString(16);
|
|
|
|
db.set("pad2readonly:" + padId, readOnlyId);
|
|
|
|
db.set("readonly2pad:" + readOnlyId, padId);
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 17:19:51 +01:00
|
|
|
return readOnlyId;
|
|
|
|
}
|
2011-07-08 18:42:07 +02:00
|
|
|
|
2011-07-08 19:33:01 +02:00
|
|
|
/**
|
2019-02-08 23:20:57 +01:00
|
|
|
* returns the padId for a read only id
|
2011-07-08 19:33:01 +02:00
|
|
|
* @param {String} readOnlyId read only id
|
|
|
|
*/
|
2019-01-30 17:19:51 +01:00
|
|
|
exports.getPadId = function(readOnlyId)
|
2011-07-08 19:33:01 +02:00
|
|
|
{
|
2019-01-30 17:19:51 +01:00
|
|
|
return db.get("readonly2pad:" + readOnlyId);
|
|
|
|
}
|
2012-04-23 16:18:14 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-08 23:20:57 +01:00
|
|
|
* returns the padId and readonlyPadId in an object for any id
|
2012-04-23 16:18:14 +02:00
|
|
|
* @param {String} padIdOrReadonlyPadId read only id or real pad id
|
|
|
|
*/
|
2019-01-30 17:19:51 +01:00
|
|
|
exports.getIds = async function(id) {
|
|
|
|
let readonly = (id.indexOf("r.") === 0);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 17:19:51 +01:00
|
|
|
// Might be null, if this is an unknown read-only id
|
|
|
|
let readOnlyPadId = readonly ? id : await exports.getReadOnlyId(id);
|
|
|
|
let padId = readonly ? await exports.getPadId(id) : id;
|
|
|
|
|
|
|
|
return { readOnlyPadId, padId, readonly };
|
|
|
|
}
|