1 2 /** 3 * "constants" for account types 4 */ 5 /** 6 * @constant 7 */ 8 var SPAZCORE_ACCOUNT_TWITTER = 'twitter'; 9 /** 10 * @constant 11 */ 12 var SPAZCORE_ACCOUNT_IDENTICA = 'identi.ca'; 13 /** 14 * @constant 15 */ 16 var SPAZCORE_ACCOUNT_STATUSNET = 'StatusNet'; 17 /** 18 * @constant 19 */ 20 var SPAZCORE_ACCOUNT_FLICKR = 'flickr'; 21 /** 22 * @constant 23 */ 24 var SPAZCORE_ACCOUNT_WORDPRESS = 'wordpress.com'; 25 /** 26 * @constant 27 */ 28 var SPAZCORE_ACCOUNT_WORDPRESS_TWITTER = 'wordpress-twitter'; 29 /** 30 * @constant 31 */ 32 var SPAZCORE_ACCOUNT_TUMBLR = 'tumblr'; 33 /** 34 * @constant 35 */ 36 var SPAZCORE_ACCOUNT_TUMBLR_TWITTER = 'tumblr-twitter'; 37 /** 38 * @constant 39 */ 40 var SPAZCORE_ACCOUNT_FACEBOOK = 'facebook'; 41 /** 42 * @constant 43 */ 44 var SPAZCORE_ACCOUNT_FRIENDFEED = 'friendfeed'; 45 /** 46 * @constant 47 */ 48 var SPAZCORE_ACCOUNT_CUSTOM = 'custom'; 49 50 /** 51 * This creates a new SpazAccounts object, and optionally associates it with an existing preferences object 52 * @constructor 53 * @param (Object) prefsObj An existing SpazPrefs object (optional) 54 */ 55 var SpazAccounts = function(prefsObj) { 56 if (prefsObj) { 57 this.prefs = prefsObj; 58 } else { 59 this.prefs = new SpazPrefs(); 60 this.prefs.load(); 61 } 62 63 /* 64 load existing accounts 65 */ 66 this.load(); 67 68 }; 69 70 /** 71 * the key used inside the prefs object 72 */ 73 SpazAccounts.prototype.prefskey = 'users'; 74 75 /** 76 * loads the accounts array from the prefs object 77 */ 78 SpazAccounts.prototype.load = function() { 79 var accjson = this.prefs.get(this.prefskey); 80 81 sch.debug("accjson:'"+accjson+"'"); 82 83 try { 84 this._accounts = sch.deJSON(this.prefs.get(this.prefskey)); 85 } catch(e) { 86 sch.error(e.message); 87 this._accounts = []; 88 } 89 90 /* 91 sanity check 92 */ 93 if (!sch.isArray(this._accounts)) { 94 this._accounts = []; 95 } 96 97 sch.debug("this._accounts:'"+this._accounts+"'"); 98 99 }; 100 101 /** 102 * saves the accounts array to the prefs obj 103 */ 104 SpazAccounts.prototype.save = function() { 105 this.prefs.set(this.prefskey, sch.enJSON(this._accounts)); 106 sch.debug('saved users to "'+this.prefskey+'" pref'); 107 for (var x in this._accounts) { 108 sch.debug(this._accounts[x].id); 109 }; 110 111 sch.debug('THE ACCOUNTS:'); 112 sch.debug(sch.enJSON(this._accounts)); 113 114 sch.debug('ALL PREFS:'); 115 sch.debug(sch.enJSON(this.prefs._prefs)); 116 117 }; 118 119 /** 120 * returns the array of accounts 121 * @returns {array} the accounts 122 */ 123 SpazAccounts.prototype.getAll = function() { 124 return this._accounts; 125 }; 126 127 /** 128 * Set all users by passing in a hash. overwrites all existing data! 129 * @param {array} accounts_array an array of account objects 130 */ 131 SpazAccounts.prototype.setAll = function(accounts_array) { 132 this._accounts = accounts_array; 133 this.save(); 134 sch.debug("Saved these accounts:"); 135 for (var i=0; i < this._accounts.length; i++) { 136 sch.debug(this._accounts[i].id); 137 }; 138 }; 139 140 /** 141 * @param {string} id the UUID to update 142 * @param {object} acctobj 143 * @param {string} [acctobj.username] a new username 144 * @param {string} [acctobj.password] a new password 145 * @param {string} [acctobj.type] a new account type 146 * @param {object} [acctobj.meta] the hash of metadata; you should probably use SpazAccounts.setMeta() instead 147 */ 148 SpazAccounts.prototype.update = function(id, acctobj) { 149 var orig = this.get(id); 150 if (orig) { 151 var modified = sch.defaults(orig, acctobj); 152 return this.get(id); 153 } else { 154 sch.error('No account with id "'+id+'" exists'); 155 return null; 156 } 157 }; 158 159 160 161 /** 162 * wipes the accounts array and saves it 163 */ 164 SpazAccounts.prototype.initAccounts = function() { 165 this._accounts = []; 166 this.save(); 167 }; 168 169 /** 170 * add a new account 171 * @param {string} username the username of the account 172 * @param {string} auth serialized authentication key, generated by SpazAuth.save() 173 * @param {string} type the type of account 174 * @returns {object} the account object just added 175 */ 176 SpazAccounts.prototype.add = function(username, auth, type) { 177 178 if (!type) { 179 sch.error("Type must be set"); 180 return false; 181 } 182 183 var account = { 184 id: this.generateID(), 185 type: type, 186 auth: auth, 187 username: username, 188 meta: {} 189 }; 190 191 this._accounts.push(account); 192 this.save(); 193 194 return account; 195 }; 196 197 198 /** 199 * @param {string} id the UUID of the account to delete 200 */ 201 SpazAccounts.prototype.remove = function(id) { 202 sch.error("Deleting '"+id+"'…"); 203 204 var index = this._findUserIndex(id); 205 if (index !== false) { 206 var deleted = this._accounts.splice(index, 1); 207 sch.debug("Deleted account '"+deleted[0].id+"'"); 208 this.save(); 209 return deleted[0]; 210 } else { 211 sch.error("Could not find this id to delete: '"+id+"'"); 212 return false; 213 } 214 }; 215 216 217 /** 218 * @param {string} type the type of accounts to retrieve 219 * @returns {array} the array of matching accounts 220 */ 221 SpazAccounts.prototype.getByType = function(type) { 222 var matches = []; 223 224 for (var i=0; i < this._accounts.length; i++) { 225 if (this._accounts[i].type === type) { 226 matches.push(this._accounts[i]); 227 } 228 }; 229 230 return matches; 231 }; 232 233 /** 234 * @param {string} username the username to search for 235 * @returns {array} an array of matching accounts 236 */ 237 SpazAccounts.prototype.getByUsername = function(username) { 238 var matches = []; 239 240 for (var i=0; i < this._accounts.length; i++) { 241 if (this._accounts[i].username === username) { 242 matches.push(this._accounts[i]); 243 } 244 }; 245 246 return matches; 247 }; 248 249 /** 250 * @param {string} username the username to search for 251 * @param {string} type the type to search for 252 * @returns {array} an array of matching accounts 253 */ 254 SpazAccounts.prototype.getByUsernameAndType = function(username, type) { 255 var matches = []; 256 257 for (var i=0; i < this._accounts.length; i++) { 258 if (this._accounts[i].username === username && this._accounts[i].type === type) { 259 matches.push(this._accounts[i]); 260 } 261 }; 262 263 return matches; 264 265 }; 266 267 268 /** 269 * retrives the user object by user and type 270 * @param {string} id the user id UUID 271 * @param {string} type 272 */ 273 SpazAccounts.prototype.get = function(id) { 274 275 var index = this._findUserIndex(id); 276 277 if (index !== false) { 278 return this._accounts[i]; 279 } 280 281 return false; 282 283 }; 284 285 286 /** 287 * a private function to find the user's array index by their UUID 288 * @param {string} id the user's UUID 289 * @returns {number|boolen} returns the array index or false if DNE 290 */ 291 SpazAccounts.prototype._findUserIndex = function(id) { 292 293 for (i=0; i<this._accounts.length; i++) { 294 295 if (this._accounts[i].id === id) { 296 sch.debug('Found matching user record to '+ id); 297 return i; 298 } 299 300 } 301 302 return false; 303 }; 304 305 306 307 /** 308 * @returns {string} returns the generated UUID 309 */ 310 SpazAccounts.prototype.generateID = function() { 311 var id = sc.helpers.UUID(); 312 return id; 313 }; 314 315 316 317 /** 318 * @param {string} id the user's UUID 319 * @param {string} key the key for the metadata entry 320 * @returns {String|Object|Array|Boolean|Number} returns the set value, or null if user ID or meta entry is not found 321 */ 322 SpazAccounts.prototype.getMeta = function(id, key) { 323 324 if ( (user = this.get(id)) ) { 325 if (user.meta && user.meta[key] !== null ) { 326 return user.meta[key]; 327 } 328 } 329 330 return null; 331 332 }; 333 334 /** 335 * @param {string} id the user's UUID 336 * @param {string} key the key for the metadata entry 337 * @param {String|Object|Array|Boolean|Number} value the value of the metadata entry 338 * @returns {String|Object|Array|Boolean|Number} returns the set value, or null if user ID is not found 339 */ 340 SpazAccounts.prototype.setMeta = function(id, key, value) { 341 342 var index = this._findUserIndex(id); 343 344 if (index !== false) { 345 if (!this._accounts[index].meta) { 346 this._accounts[index].meta = {}; 347 } 348 this._accounts[index].meta[key] = value; 349 350 this.save(); 351 352 return this._accounts[index].meta[key]; 353 354 } 355 return null; 356 357 };