2011-03-26 14:10:41 +01:00
|
|
|
/**
|
2011-05-30 16:53:11 +02:00
|
|
|
* The AuthorManager controlls all information about the Pad authors
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 16:26:41 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-03-26 14:10:41 +01: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
|
|
|
|
2011-12-04 16:50:02 +01:00
|
|
|
var ERR = require("async-stacktrace");
|
2011-07-27 19:52:23 +02:00
|
|
|
var db = require("./DB").db;
|
2011-05-16 14:10:53 +02:00
|
|
|
var async = require("async");
|
2012-02-26 17:48:17 +01:00
|
|
|
var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
|
2012-01-28 13:24:58 +01:00
|
|
|
|
2011-08-09 17:45:49 +02:00
|
|
|
/**
|
|
|
|
* Checks if the author exists
|
|
|
|
*/
|
|
|
|
exports.doesAuthorExists = function (authorID, callback)
|
|
|
|
{
|
|
|
|
//check if the database entry of this author exists
|
|
|
|
db.get("globalAuthor:" + authorID, function (err, author)
|
|
|
|
{
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
callback(null, author != null);
|
2011-08-09 17:45:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
2011-08-09 15:42:12 +02:00
|
|
|
* Returns the AuthorID for a token.
|
2011-06-02 14:11:46 +02:00
|
|
|
* @param {String} token The token
|
|
|
|
* @param {Function} callback callback (err, author)
|
2011-03-26 14:10:41 +01:00
|
|
|
*/
|
2011-05-16 14:10:53 +02:00
|
|
|
exports.getAuthor4Token = function (token, callback)
|
2011-08-09 15:42:12 +02:00
|
|
|
{
|
|
|
|
mapAuthorWithDBKey("token2author", token, function(err, author)
|
|
|
|
{
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 15:42:12 +02:00
|
|
|
//return only the sub value authorID
|
2011-12-04 16:50:02 +01:00
|
|
|
callback(null, author ? author.authorID : author);
|
2011-08-09 15:42:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the AuthorID for a mapper.
|
|
|
|
* @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 15:42:12 +02:00
|
|
|
* @param {Function} callback callback (err, author)
|
|
|
|
*/
|
2011-08-10 13:38:03 +02:00
|
|
|
exports.createAuthorIfNotExistsFor = function (authorMapper, name, callback)
|
2011-08-09 15:42:12 +02:00
|
|
|
{
|
|
|
|
mapAuthorWithDBKey("mapper2author", authorMapper, function(err, author)
|
|
|
|
{
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 15:42:12 +02:00
|
|
|
|
|
|
|
//set the name of this author
|
|
|
|
if(name)
|
|
|
|
exports.setAuthorName(author.authorID, name);
|
|
|
|
|
|
|
|
//return the authorID
|
|
|
|
callback(null, author);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the AuthorID for a mapper. We can map using a mapperkey,
|
|
|
|
* so far this is token2author and mapper2author
|
|
|
|
* @param {String} mapperkey The database key name for this mapper
|
|
|
|
* @param {String} mapper The mapper
|
|
|
|
* @param {Function} callback callback (err, author)
|
|
|
|
*/
|
|
|
|
function mapAuthorWithDBKey (mapperkey, mapper, callback)
|
2011-05-16 14:10:53 +02:00
|
|
|
{
|
2011-08-09 15:42:12 +02:00
|
|
|
//try to map to an author
|
|
|
|
db.get(mapperkey + ":" + mapper, function (err, author)
|
|
|
|
{
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 15:42:12 +02:00
|
|
|
|
|
|
|
//there is no author with this mapper, so create one
|
|
|
|
if(author == null)
|
2011-05-16 14:10:53 +02:00
|
|
|
{
|
2011-08-09 15:42:12 +02:00
|
|
|
exports.createAuthor(null, function(err, author)
|
2011-05-16 14:10:53 +02:00
|
|
|
{
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-08-09 12:55:12 +02:00
|
|
|
|
2011-08-09 15:42:12 +02:00
|
|
|
//create the token2author relation
|
|
|
|
db.set(mapperkey + ":" + mapper, author.authorID);
|
|
|
|
|
|
|
|
//return the author
|
|
|
|
callback(null, author);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//there is a author with this mapper
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//update the timestamp of this author
|
|
|
|
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
|
|
|
|
|
|
|
|
//return the author
|
|
|
|
callback(null, {authorID: author});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-05-16 14:10:53 +02:00
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-30 13:50:29 +02:00
|
|
|
/**
|
|
|
|
* Internal function that creates the database entry for an author
|
2011-08-09 13:09:04 +02:00
|
|
|
* @param {String} name The name of the author
|
2011-07-30 13:50:29 +02:00
|
|
|
*/
|
2011-08-09 13:09:04 +02:00
|
|
|
exports.createAuthor = function(name, callback)
|
2011-07-30 13:50:29 +02:00
|
|
|
{
|
|
|
|
//create the new author name
|
2011-08-10 15:24:21 +02:00
|
|
|
var author = "a." + randomString(16);
|
2011-07-30 13:50:29 +02:00
|
|
|
|
2011-07-31 16:18:55 +02:00
|
|
|
//create the globalAuthors db entry
|
2011-08-09 12:55:12 +02:00
|
|
|
var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()};
|
2011-07-31 16:18:55 +02:00
|
|
|
|
2011-08-09 12:55:12 +02:00
|
|
|
//set the global author db entry
|
|
|
|
db.set("globalAuthor:" + author, authorObj);
|
|
|
|
|
2011-08-09 13:09:04 +02:00
|
|
|
callback(null, {authorID: author});
|
2011-07-30 13:50:29 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
/**
|
|
|
|
* Returns the Author Obj of the author
|
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {Function} callback callback(err, authorObj)
|
|
|
|
*/
|
|
|
|
exports.getAuthor = function (author, callback)
|
|
|
|
{
|
|
|
|
db.get("globalAuthor:" + author, callback);
|
|
|
|
}
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Returns the color Id of the author
|
2011-06-02 14:11:46 +02:00
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {Function} callback callback(err, colorId)
|
2011-03-26 14:10:41 +01:00
|
|
|
*/
|
2011-05-16 14:10:53 +02:00
|
|
|
exports.getAuthorColorId = function (author, callback)
|
2011-03-26 14:10:41 +01:00
|
|
|
{
|
2011-05-16 14:10:53 +02:00
|
|
|
db.getSub("globalAuthor:" + author, ["colorId"], callback);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the color Id of the author
|
2011-06-02 14:11:46 +02: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-06-02 14:11:46 +02:00
|
|
|
* @param {Function} callback (optional)
|
2011-03-26 14:10:41 +01:00
|
|
|
*/
|
2011-05-16 14:10:53 +02:00
|
|
|
exports.setAuthorColorId = function (author, colorId, callback)
|
2011-03-26 14:10:41 +01:00
|
|
|
{
|
2011-05-16 14:10:53 +02:00
|
|
|
db.setSub("globalAuthor:" + author, ["colorId"], colorId, callback);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the author
|
2011-06-02 14:11:46 +02:00
|
|
|
* @param {String} author The id of the author
|
|
|
|
* @param {Function} callback callback(err, name)
|
2011-03-26 14:10:41 +01:00
|
|
|
*/
|
2011-05-16 14:10:53 +02:00
|
|
|
exports.getAuthorName = function (author, callback)
|
2011-03-26 14:10:41 +01:00
|
|
|
{
|
2011-05-16 14:10:53 +02:00
|
|
|
db.getSub("globalAuthor:" + author, ["name"], callback);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the author
|
2011-06-02 14:11:46 +02: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-06-02 14:11:46 +02:00
|
|
|
* @param {Function} callback (optional)
|
2011-03-26 14:10:41 +01:00
|
|
|
*/
|
2011-05-16 14:10:53 +02:00
|
|
|
exports.setAuthorName = function (author, name, callback)
|
2011-03-26 14:10:41 +01:00
|
|
|
{
|
2011-05-16 14:10:53 +02:00
|
|
|
db.setSub("globalAuthor:" + author, ["name"], name, callback);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
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
|
|
|
|
* @param {Function} callback (optional)
|
|
|
|
*/
|
|
|
|
exports.listPadsOfAuthor = function (authorID, callback)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
//get the globalAuthor
|
|
|
|
db.get("globalAuthor:" + authorID, function(err, author)
|
|
|
|
{
|
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
|
|
|
//author does not exists
|
|
|
|
if(author == null)
|
|
|
|
{
|
|
|
|
callback(new customError("authorID does not exist","apierror"))
|
|
|
|
}
|
|
|
|
//everything is fine, return the pad IDs
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var pads = [];
|
|
|
|
if(author.padIDs != null)
|
|
|
|
{
|
|
|
|
for (var padId in author.padIDs)
|
|
|
|
{
|
|
|
|
pads.push(padId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null, {padIDs: pads});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
exports.addPad = function (authorID, padID)
|
|
|
|
{
|
|
|
|
//get the entry
|
|
|
|
db.get("globalAuthor:" + authorID, function(err, author)
|
|
|
|
{
|
|
|
|
if(ERR(err)) return;
|
|
|
|
if(author == null) return;
|
|
|
|
|
|
|
|
//the entry doesn't exist so far, let's create it
|
|
|
|
if(author.padIDs == null)
|
|
|
|
{
|
2012-06-27 21:02:41 +02:00
|
|
|
author.padIDs = {};
|
2012-06-27 18:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//add the entry for this pad
|
2012-06-27 21:02:41 +02:00
|
|
|
author.padIDs[padID] = 1;// anything, because value is not used
|
2012-06-27 18:23:17 +02:00
|
|
|
|
|
|
|
//save the new element back
|
|
|
|
db.set("globalAuthor:" + authorID, author);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
exports.removePad = function (authorID, padID)
|
|
|
|
{
|
|
|
|
db.get("globalAuthor:" + authorID, function (err, author)
|
|
|
|
{
|
|
|
|
if(ERR(err)) return;
|
|
|
|
if(author == null) return;
|
|
|
|
|
|
|
|
if(author.padIDs != null)
|
|
|
|
{
|
|
|
|
//remove pad from author
|
|
|
|
delete author.padIDs[padID];
|
|
|
|
db.set("globalAuthor:" + authorID, author);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|