summaryrefslogtreecommitdiff
path: root/node/db/AuthorManager.js
blob: f4f42d112fce4e955cf49ee92668ad4f1327d8c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 * The AuthorManager controlls all information about the Pad authors
 */

/*
 * 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");

/**
 * 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)
  {
    if(ERR(err, callback)) return;
    callback(null, author != null);
  });
}

/**
 * Returns the AuthorID for a token. 
 * @param {String} token The token 
 * @param {Function} callback callback (err, author) 
 */
exports.getAuthor4Token = function (token, callback)
{
  mapAuthorWithDBKey("token2author", token, function(err, author)
  {
    if(ERR(err, callback)) return;
    //return only the sub value authorID
    callback(null, author ? author.authorID : author);
  });
}

/**
 * Returns the AuthorID for a mapper. 
 * @param {String} token The mapper
 * @param {Function} callback callback (err, author) 
 */
exports.createAuthorIfNotExistsFor = function (authorMapper, name, callback)
{
  mapAuthorWithDBKey("mapper2author", authorMapper, function(err, author)
  {
    if(ERR(err, callback)) 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)
{  
  //try to map to an author
  db.get(mapperkey + ":" + mapper, function (err, author)
  {
    if(ERR(err, callback)) return;
  
    //there is no author with this mapper, so create one
    if(author == null)
    {
      exports.createAuthor(null, function(err, author)
      {
        if(ERR(err, callback)) return;
        
        //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});
    }
  });
}

/**
 * Internal function that creates the database entry for an author 
 * @param {String} name The name of the author 
 */
exports.createAuthor = function(name, callback)
{
  //create the new author name
  var author = "a." + randomString(16);
        
  //create the globalAuthors db entry
  var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()};
        
  //set the global author db entry
  db.set("globalAuthor:" + author, authorObj);
  
  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);
}

/**
 * Returns the color Id of the author
 * @param {String} author The id of the author
 * @param {Function} callback callback(err, colorId)
 */
exports.getAuthorColorId = function (author, callback)
{
  db.getSub("globalAuthor:" + author, ["colorId"], callback);
}

/**
 * Sets the color Id of the author
 * @param {String} author The id of the author
 * @param {Function} callback (optional)
 */
exports.setAuthorColorId = function (author, colorId, callback)
{
  db.setSub("globalAuthor:" + author, ["colorId"], colorId, callback);
}

/**
 * Returns the name of the author
 * @param {String} author The id of the author
 * @param {Function} callback callback(err, name)
 */
exports.getAuthorName = function (author, callback)
{
  db.getSub("globalAuthor:" + author, ["name"], callback);
}

/**
 * Sets the name of the author
 * @param {String} author The id of the author
 * @param {Function} callback (optional)
 */
exports.setAuthorName = function (author, name, callback)
{
  db.setSub("globalAuthor:" + author, ["name"], name, callback);
}

/**
 * Generates a random String with the given length. Is needed to generate the Author Ids
 */
function randomString(len) 
{
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var randomstring = '';
  for (var i = 0; i < len; i++)
  {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum, rnum + 1);
  }
  return randomstring;
}