pad.libre-service.eu-etherpad/src/node/db/ReadOnlyManager.js
muxator 9497ee734f prepare to async: trivial reformatting
This change is only cosmetic. Its aim is do make it easier to understand the
async changes that are going to be merged later on. It was extracted from the
original work from Ray Bellis.

To verify that nothing has changed, you can run the following command on each
file touched by this commit:
  npm install uglify-es
  diff --unified <(uglify-js --beautify bracketize <BEFORE.js>) <(uglify-js --beautify bracketize <AFTER.js>)



This is a complete script that does the same automatically (works from a
mercurial clone):

```bash
#!/usr/bin/env bash

set -eu

REVISION=<THIS_REVISION>

PARENT_REV=$(hg identify --rev "${REVISION}" --template '{p1rev}')
FILE_LIST=$(hg status --no-status --change ${REVISION})
UGLIFYJS="node_modules/uglify-es/bin/uglifyjs"

for FILE_NAME in ${FILE_LIST[@]}; do
  echo "Checking ${FILE_NAME}"
  diff --unified \
    <("${UGLIFYJS}" --beautify bracketize <(hg cat --rev "${PARENT_REV}" "${FILE_NAME}")) \
    <("${UGLIFYJS}" --beautify bracketize <(hg cat --rev "${REVISION}"   "${FILE_NAME}"))
done
```
2019-02-08 23:20:57 +01:00

98 lines
2.6 KiB
JavaScript

/**
* The ReadOnlyManager manages the database and rendering releated to read only pads
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
*
* 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.
*/
var ERR = require("async-stacktrace");
var db = require("./DB").db;
var async = require("async");
var randomString = require("../utils/randomstring");
const thenify = require("thenify").withCallback;
/**
* returns a read only id for a pad
* @param {String} padId the id of the pad
*/
exports.getReadOnlyId = thenify(function (padId, callback)
{
var readOnlyId;
async.waterfall([
// check if there is a pad2readonly entry
function(callback) {
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);
db.set("pad2readonly:" + padId, readOnlyId);
db.set("readonly2pad:" + readOnlyId, padId);
} else {
// there is a readOnly Entry in the database, let's take this one
readOnlyId = dbReadOnlyId;
}
callback();
}
],
function(err) {
if (ERR(err, callback)) return;
// return the results
callback(null, readOnlyId);
})
});
/**
* returns the padId for a read only id
* @param {String} readOnlyId read only id
*/
exports.getPadId = thenify(function(readOnlyId, callback)
{
db.get("readonly2pad:" + readOnlyId, callback);
});
/**
* returns the padId and readonlyPadId in an object for any id
* @param {String} padIdOrReadonlyPadId read only id or real pad id
*/
exports.getIds = thenify(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) {
callback(null, {
readOnlyPadId: value,
padId: id,
readonly: false
});
});
}
});