pad.pub0.org/node/db/AuthorManager.js

204 lines
5.1 KiB
JavaScript
Raw Normal View History

2011-03-26 13:10:41 +00:00
/**
2011-05-30 14:53:11 +00:00
* The AuthorManager controlls all information about the Pad authors
*/
/*
2011-03-26 13:10:41 +00:00
* 2011 Peter 'Pita' Martischka
*
* 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.
*/
2011-07-27 17:52:23 +00:00
var db = require("./DB").db;
var async = require("async");
2011-03-26 13:10:41 +00:00
2011-08-09 15:45:49 +00: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)
{
callback(err, author != null);
});
}
2011-03-26 13:10:41 +00:00
/**
2011-08-09 13:42:12 +00:00
* Returns the AuthorID for a token.
2011-06-02 12:11:46 +00:00
* @param {String} token The token
* @param {Function} callback callback (err, author)
2011-03-26 13:10:41 +00:00
*/
exports.getAuthor4Token = function (token, callback)
2011-08-09 13:42:12 +00:00
{
mapAuthorWithDBKey("token2author", token, function(err, author)
{
//return only the sub value authorID
callback(err, author ? author.authorID : author);
});
}
/**
* Returns the AuthorID for a mapper.
* @param {String} token The mapper
* @param {Function} callback callback (err, author)
*/
exports.getMappedAuthor4 = function (authorMapper, name, callback)
{
mapAuthorWithDBKey("mapper2author", authorMapper, function(err, author)
{
//error?
if(err)
{
callback(err);
return;
}
//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-08-09 13:42:12 +00:00
//try to map to an author
db.get(mapperkey + ":" + mapper, function (err, author)
{
//error?
if(err)
2011-03-26 13:10:41 +00:00
{
2011-08-09 13:42:12 +00:00
callback(err);
return;
}
//there is no author with this mapper, so create one
if(author == null)
{
2011-08-09 13:42:12 +00:00
exports.createAuthor(null, function(err, author)
{
2011-08-09 13:42:12 +00:00
//error?
if(err)
2011-07-31 14:18:55 +00:00
{
2011-08-09 13:42:12 +00:00
callback(err);
return;
}
2011-08-09 10:55:12 +00:00
2011-08-09 13:42:12 +00: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 13:10:41 +00:00
}
});
2011-03-26 13:10:41 +00:00
}
/**
* Internal function that creates the database entry for an author
2011-08-09 11:09:04 +00:00
* @param {String} name The name of the author
*/
2011-08-09 11:09:04 +00:00
exports.createAuthor = function(name, callback)
{
//create the new author name
2011-07-31 14:18:55 +00:00
var author = "g." + _randomString(16);
2011-07-31 14:18:55 +00:00
//create the globalAuthors db entry
2011-08-09 10:55:12 +00:00
var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()};
2011-07-31 14:18:55 +00:00
2011-08-09 10:55:12 +00:00
//set the global author db entry
db.set("globalAuthor:" + author, authorObj);
2011-08-09 11:09:04 +00:00
callback(null, {authorID: author});
}
/**
* 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 13:10:41 +00:00
/**
* Returns the color Id of the author
2011-06-02 12:11:46 +00:00
* @param {String} author The id of the author
* @param {Function} callback callback(err, colorId)
2011-03-26 13:10:41 +00:00
*/
exports.getAuthorColorId = function (author, callback)
2011-03-26 13:10:41 +00:00
{
db.getSub("globalAuthor:" + author, ["colorId"], callback);
2011-03-26 13:10:41 +00:00
}
/**
* Sets the color Id of the author
2011-06-02 12:11:46 +00:00
* @param {String} author The id of the author
* @param {Function} callback (optional)
2011-03-26 13:10:41 +00:00
*/
exports.setAuthorColorId = function (author, colorId, callback)
2011-03-26 13:10:41 +00:00
{
db.setSub("globalAuthor:" + author, ["colorId"], colorId, callback);
2011-03-26 13:10:41 +00:00
}
/**
* Returns the name of the author
2011-06-02 12:11:46 +00:00
* @param {String} author The id of the author
* @param {Function} callback callback(err, name)
2011-03-26 13:10:41 +00:00
*/
exports.getAuthorName = function (author, callback)
2011-03-26 13:10:41 +00:00
{
db.getSub("globalAuthor:" + author, ["name"], callback);
2011-03-26 13:10:41 +00:00
}
/**
* Sets the name of the author
2011-06-02 12:11:46 +00:00
* @param {String} author The id of the author
* @param {Function} callback (optional)
2011-03-26 13:10:41 +00:00
*/
exports.setAuthorName = function (author, name, callback)
2011-03-26 13:10:41 +00:00
{
db.setSub("globalAuthor:" + author, ["name"], name, callback);
2011-03-26 13:10:41 +00:00
}
/**
* Generates a random String with the given length. Is needed to generate the Author Ids
*/
function _randomString(len) {
// use only numbers and lowercase letters
var pieces = [];
for(var i=0;i<len;i++) {
pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1));
}
return pieces.join('');
}