pad.libre-service.eu-etherpad/src/node/db/ReadOnlyManager.js

98 lines
2.5 KiB
JavaScript
Raw Normal View History

2011-07-08 18:42:07 +02:00
/**
* The ReadOnlyManager manages the database and rendering releated to read only pads
*/
/*
* 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
var ERR = require("async-stacktrace");
2011-07-27 19:52:23 +02:00
var db = require("./DB").db;
2011-07-08 18:42:07 +02:00
var async = require("async");
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
*/
exports.getReadOnlyId = function (padId, callback)
{
2011-07-08 18:42:07 +02:00
var readOnlyId;
2011-07-08 18:42:07 +02:00
async.waterfall([
// check if there is a pad2readonly entry
function(callback) {
2011-07-08 18:42:07 +02:00
db.get("pad2readonly:" + padId, callback);
},
function(dbReadOnlyId, callback) {
if (dbReadOnlyId == null) {
// there is no readOnly Entry in the database, let's create one
readOnlyId = "r." + randomString(16);
2011-07-08 18:42:07 +02:00
db.set("pad2readonly:" + padId, readOnlyId);
db.set("readonly2pad:" + readOnlyId, padId);
} else {
// there is a readOnly Entry in the database, let's take this one
2011-07-08 18:42:07 +02:00
readOnlyId = dbReadOnlyId;
}
2011-07-08 18:42:07 +02:00
callback();
}
],
function(err) {
if (ERR(err, callback)) return;
// return the results
callback(null, readOnlyId);
2011-07-08 18:42:07 +02:00
})
}
2011-07-08 19:33:01 +02:00
/**
* returns the padId for a read only id
2011-07-08 19:33:01 +02:00
* @param {String} readOnlyId read only id
*/
exports.getPadId = function(readOnlyId, callback)
{
db.get("readonly2pad:" + readOnlyId, callback);
}
2012-04-23 16:18:14 +02: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
*/
exports.getIds = function(id, callback) {
if (id.indexOf("r.") == 0) {
exports.getPadId(id, function (err, value) {
if (ERR(err, callback)) return;
callback(null, {
readOnlyPadId: id,
padId: value, // Might be null, if this is an unknown read-only id
readonly: true
});
});
} else {
exports.getReadOnlyId(id, function (err, value) {
2012-04-23 16:18:14 +02:00
callback(null, {
readOnlyPadId: value,
padId: id,
2012-04-23 16:18:14 +02:00
readonly: false
});
});
}
2012-04-23 16:18:14 +02:00
}