2011-03-26 13:10:41 +00:00
|
|
|
/**
|
2011-05-30 15:53:11 +01:00
|
|
|
* The AuthorManager controlls all information about the Pad authors
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
var db = require("./DB");
|
2013-10-03 14:45:23 +01:00
|
|
|
var customError = require("../utils/customError");
|
2012-02-26 17:48:17 +01:00
|
|
|
var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
|
2019-01-21 16:28:05 +00:00
|
|
|
const thenify = require("thenify").withCallback;
|
2012-01-28 13:24:58 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
exports.getColorPalette = function() {
|
|
|
|
return [
|
|
|
|
"#ffc7c7", "#fff1c7", "#e3ffc7", "#c7ffd5", "#c7ffff", "#c7d5ff", "#e3c7ff", "#ffc7f1",
|
|
|
|
"#ffa8a8", "#ffe699", "#cfff9e", "#99ffb3", "#a3ffff", "#99b3ff", "#cc99ff", "#ff99e5",
|
|
|
|
"#e7b1b1", "#e9dcAf", "#cde9af", "#bfedcc", "#b1e7e7", "#c3cdee", "#d2b8ea", "#eec3e6",
|
|
|
|
"#e9cece", "#e7e0ca", "#d3e5c7", "#bce1c5", "#c1e2e2", "#c1c9e2", "#cfc1e2", "#e0bdd9",
|
|
|
|
"#baded3", "#a0f8eb", "#b1e7e0", "#c3c8e4", "#cec5e2", "#b1d5e7", "#cda8f0", "#f0f0a8",
|
|
|
|
"#f2f2a6", "#f5a8eb", "#c5f9a9", "#ececbb", "#e7c4bc", "#daf0b2", "#b0a0fd", "#bce2e7",
|
|
|
|
"#cce2bb", "#ec9afe", "#edabbd", "#aeaeea", "#c4e7b1", "#d722bb", "#f3a5e7", "#ffa8a8",
|
|
|
|
"#d8c0c5", "#eaaedd", "#adc6eb", "#bedad1", "#dee9af", "#e9afc2", "#f8d2a0", "#b3b3e6"
|
|
|
|
];
|
2013-01-27 16:45:09 +00:00
|
|
|
};
|
|
|
|
|
2011-08-09 16:45:49 +01:00
|
|
|
/**
|
|
|
|
* Checks if the author exists
|
|
|
|
*/
|
2019-01-25 15:47:25 +00:00
|
|
|
exports.doesAuthorExist = async function(authorID)
|
2011-08-09 16:45:49 +01:00
|
|
|
{
|
2019-01-25 15:47:25 +00:00
|
|
|
let author = await db.get("globalAuthor:" + authorID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
return author !== null;
|
|
|
|
}
|
2011-08-09 16:45:49 +01:00
|
|
|
|
2019-01-25 13:37:24 +00:00
|
|
|
/* exported for backwards compatibility */
|
|
|
|
exports.doesAuthorExists = exports.doesAuthorExist;
|
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
2017-11-22 16:04:17 +01:00
|
|
|
* Returns the AuthorID for a token.
|
|
|
|
* @param {String} token The token
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
exports.getAuthor4Token = async function(token)
|
2011-08-09 14:42:12 +01:00
|
|
|
{
|
2019-01-28 15:36:36 +00:00
|
|
|
let author = await mapAuthorWithDBKey("token2author", token);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
// return only the sub value authorID
|
|
|
|
return author ? author.authorID : author;
|
|
|
|
}
|
2011-08-09 14:42:12 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-22 16:04:17 +01:00
|
|
|
* Returns the AuthorID for a mapper.
|
2011-08-09 14:42:12 +01:00
|
|
|
* @param {String} token The mapper
|
2012-06-27 18:23:17 +02:00
|
|
|
* @param {String} name The name of the author (optional)
|
2011-08-09 14:42:12 +01:00
|
|
|
*/
|
2019-01-25 15:47:25 +00:00
|
|
|
exports.createAuthorIfNotExistsFor = async function(authorMapper, name)
|
2011-08-09 14:42:12 +01:00
|
|
|
{
|
2019-01-25 15:47:25 +00:00
|
|
|
let author = await mapAuthorWithDBKey("mapper2author", authorMapper);
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
if (name) {
|
|
|
|
// set the name of this author
|
|
|
|
await exports.setAuthorName(author.authorID, name);
|
|
|
|
}
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
return author;
|
|
|
|
};
|
2011-08-09 14:42:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the AuthorID for a mapper. We can map using a mapperkey,
|
|
|
|
* so far this is token2author and mapper2author
|
2017-11-22 16:04:17 +01:00
|
|
|
* @param {String} mapperkey The database key name for this mapper
|
2011-08-09 14:42:12 +01:00
|
|
|
* @param {String} mapper The mapper
|
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
async function mapAuthorWithDBKey (mapperkey, mapper)
|
2017-11-22 16:04:17 +01:00
|
|
|
{
|
2019-02-08 23:20:57 +01:00
|
|
|
// try to map to an author
|
2019-01-28 15:36:36 +00:00
|
|
|
let author = await db.get(mapperkey + ":" + mapper);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
if (author === null) {
|
|
|
|
// there is no author with this mapper, so create one
|
|
|
|
let author = await exports.createAuthor(null);
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
// create the token2author relation
|
|
|
|
await db.set(mapperkey + ":" + mapper, author.authorID);
|
2018-08-29 02:28:35 +02:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
// return the author
|
|
|
|
return author;
|
|
|
|
}
|
2018-08-29 02:28:35 +02:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
// there is an author with this mapper
|
|
|
|
// update the timestamp of this author
|
|
|
|
await db.setSub("globalAuthor:" + author, ["timestamp"], Date.now());
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
// return the author
|
|
|
|
return { authorID: author};
|
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2011-07-30 12:50:29 +01:00
|
|
|
/**
|
2017-11-22 16:04:17 +01:00
|
|
|
* Internal function that creates the database entry for an author
|
|
|
|
* @param {String} name The name of the author
|
2011-07-30 12:50:29 +01:00
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
exports.createAuthor = function(name)
|
2011-07-30 12:50:29 +01:00
|
|
|
{
|
2019-02-08 23:20:57 +01:00
|
|
|
// create the new author name
|
2019-01-28 15:36:36 +00:00
|
|
|
let author = "a." + randomString(16);
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// create the globalAuthors db entry
|
2019-01-28 15:36:36 +00:00
|
|
|
let authorObj = {
|
2019-02-08 23:20:57 +01:00
|
|
|
"colorId": Math.floor(Math.random() * (exports.getColorPalette().length)),
|
|
|
|
"name": name,
|
|
|
|
"timestamp": Date.now()
|
|
|
|
};
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// set the global author db entry
|
2019-01-28 15:36:36 +00:00
|
|
|
// NB: no await, since we're not waiting for the DB set to finish
|
|
|
|
db.set("globalAuthor:" + author, authorObj);
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-28 15:36:36 +00:00
|
|
|
return { authorID: author };
|
|
|
|
}
|
2011-07-30 12:50:29 +01:00
|
|
|
|
2011-06-20 11:44:04 +01:00
|
|
|
/**
|
|
|
|
* Returns the Author Obj of the author
|
|
|
|
* @param {String} author The id of the author
|
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
exports.getAuthor = function(author)
|
2011-06-20 11:44:04 +01:00
|
|
|
{
|
2019-01-28 15:36:36 +00:00
|
|
|
// NB: result is already a Promise
|
|
|
|
return db.get("globalAuthor:" + author);
|
|
|
|
}
|
2011-06-20 11:44:04 +01:00
|
|
|
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the color Id of the author
|
2011-06-02 13:11:46 +01:00
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {Function} callback callback(err, colorId)
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-21 16:28:05 +00:00
|
|
|
exports.getAuthorColorId = thenify(function(author, callback)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2019-01-25 15:47:25 +00:00
|
|
|
db.db.getSub("globalAuthor:" + author, ["colorId"], callback);
|
2019-01-21 16:28:05 +00:00
|
|
|
});
|
2011-03-26 13:10:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the color Id of the author
|
2011-06-02 13:11:46 +01:00
|
|
|
* @param {String} author The id of the author
|
2012-06-27 18:23:17 +02:00
|
|
|
* @param {String} colorId The color id of the author
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
exports.setAuthorColorId = function(author, colorId)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2019-01-28 15:36:36 +00:00
|
|
|
return db.setSub("globalAuthor:" + author, ["colorId"], colorId);
|
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the author
|
2011-06-02 13:11:46 +01:00
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {Function} callback callback(err, name)
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-21 16:28:05 +00:00
|
|
|
exports.getAuthorName = thenify(function(author, callback)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2019-01-25 15:47:25 +00:00
|
|
|
db.db.getSub("globalAuthor:" + author, ["name"], callback);
|
2019-01-21 16:28:05 +00:00
|
|
|
});
|
2011-03-26 13:10:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the author
|
2011-06-02 13:11:46 +01:00
|
|
|
* @param {String} author The id of the author
|
2012-06-27 21:02:41 +02:00
|
|
|
* @param {String} name The name of the author
|
2011-03-26 13:10:41 +00:00
|
|
|
*/
|
2019-01-28 15:36:36 +00:00
|
|
|
exports.setAuthorName = function(author, name)
|
2011-03-26 13:10:41 +00:00
|
|
|
{
|
2019-01-28 15:36:36 +00:00
|
|
|
return db.setSub("globalAuthor:" + author, ["name"], name);
|
|
|
|
}
|
2012-06-27 18:23:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of all pads this author contributed to
|
|
|
|
* @param {String} author The id of the author
|
|
|
|
*/
|
2019-01-25 15:47:25 +00:00
|
|
|
exports.listPadsOfAuthor = async function(authorID)
|
2012-06-27 18:23:17 +02:00
|
|
|
{
|
|
|
|
/* There are two other places where this array is manipulated:
|
|
|
|
* (1) When the author is added to a pad, the author object is also updated
|
|
|
|
* (2) When a pad is deleted, each author of that pad is also updated
|
|
|
|
*/
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// get the globalAuthor
|
2019-01-25 15:47:25 +00:00
|
|
|
let author = await db.get("globalAuthor:" + authorID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
if (author === null) {
|
|
|
|
// author does not exist
|
|
|
|
throw new customError("authorID does not exist", "apierror");
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
// everything is fine, return the pad IDs
|
|
|
|
let padIDs = Object.keys(author.padIDs || {});
|
2018-08-29 02:28:40 +02:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
return { padIDs };
|
|
|
|
}
|
2012-06-27 18:23:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new pad to the list of contributions
|
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {String} padID The id of the pad the author contributes to
|
|
|
|
*/
|
2019-01-25 15:47:25 +00:00
|
|
|
exports.addPad = async function(authorID, padID)
|
2012-06-27 18:23:17 +02:00
|
|
|
{
|
2019-02-08 23:20:57 +01:00
|
|
|
// get the entry
|
2019-01-25 15:47:25 +00:00
|
|
|
let author = await db.get("globalAuthor:" + authorID);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
if (author === null) return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ACHTUNG: padIDs can also be undefined, not just null, so it is not possible
|
|
|
|
* to perform a strict check here
|
|
|
|
*/
|
|
|
|
if (!author.padIDs) {
|
|
|
|
// the entry doesn't exist so far, let's create it
|
|
|
|
author.padIDs = {};
|
|
|
|
}
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
// add the entry for this pad
|
|
|
|
author.padIDs[padID] = 1; // anything, because value is not used
|
2017-11-22 16:04:17 +01:00
|
|
|
|
2019-01-25 15:47:25 +00:00
|
|
|
// save the new element back
|
|
|
|
db.set("globalAuthor:" + authorID, author);
|
2012-06-27 18:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a pad from the list of contributions
|
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {String} padID The id of the pad the author contributes to
|
|
|
|
*/
|
2019-01-25 15:47:25 +00:00
|
|
|
exports.removePad = async function(authorID, padID)
|
2012-06-27 18:23:17 +02:00
|
|
|
{
|
2019-01-25 15:47:25 +00:00
|
|
|
let author = await db.get("globalAuthor:" + authorID);
|
|
|
|
|
|
|
|
if (author === null) return;
|
|
|
|
|
|
|
|
if (author.padIDs !== null) {
|
|
|
|
// remove pad from author
|
|
|
|
delete author.padIDs[padID];
|
|
|
|
db.set("globalAuthor:" + authorID, author);
|
|
|
|
}
|
2013-10-03 14:45:23 +01:00
|
|
|
}
|