X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FAPI.js;h=64b1108b0de9b2910b2d0139bae6e925dec010d0;hb=4210d2d20df6635e19c20f5b303d498f4657968b;hp=986ac0963cfe01089017b6671671bc8f6dada3e0;hpb=0085ebfed447d3e7a828eb691a78c5ccc2c21a8c;p=blerg.git diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 986ac09..64b1108 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -21,9 +21,9 @@ enyo.kind({ return; } - if (enyo.getCookie('auth') && enyo.getCookie('username')) { + if (enyo.getCookie('auth')) { blerg.API.loggedIn = true; - blerg.API.username = enyo.getCookie('username'); + blerg.API.username = enyo.getCookie('auth').split('/')[0]; // Defer the signal until everything's initialized setTimeout(function() { this.bubble('onLoginSuccessful', {username: blerg.API.username}); @@ -66,41 +66,44 @@ enyo.kind({ if (inResponse.status == 'success') { blerg.API.loggedIn = true; blerg.API.username = username; - enyo.setCookie('username', username); this.bubble('onLoginSuccessful', {username: username}); } else { - enyo.setCookie('username', '', {"Max-Age": 0}); this.bubble('onLoginFailed'); } }); req.error(this, function() { - enyo.setCookie('username', '', {"Max-Age": 0}); this.bubble('onLoginFailed'); }); req.go(); }, + expireClientAuthentication: function() { + blerg.API.loggedIn = false; + blerg.API.username = ''; + enyo.setCookie('auth', '', {"Max-Age": 0}); + }, logout: function() { var req = new enyo.Ajax({ url: baseURL + '/logout', - method: 'POST', - postBody: { - username: blerg.API.username - } + method: 'POST' }); - req.response(this, function(inSender, inResponse) { - blerg.API.loggedIn = false; - enyo.setCookie('auth', '', {"Max-Age": 0}); + var logout_func = function() { + this.expireClientAuthentication(); this.bubble('onLogoutSuccessful'); - }); + }.bind(this); + req.response(this, logout_func); + req.error(this, logout_func); req.go(); - enyo.setCookie('username', '', {"Max-Age": 0}); + }, + authenticationFailed: function() { + enyo.log("Authentication failed -- logging out"); + this.expireClientAuthentication(); + this.bubble('onAuthFailure'); }, changePassword: function(oldpassword, newpassword) { var req = new enyo.Ajax({ url: baseURL + '/passwd', method: 'POST', postBody: { - username: blerg.API.username, password: oldpassword, new_password: newpassword } @@ -128,7 +131,7 @@ enyo.kind({ var req = new enyo.Ajax({ url: url }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onItemsLoaded', { type: 'user', username: username, @@ -136,13 +139,13 @@ enyo.kind({ to: to, entries: inResponse }); - }.bind(this)); - req.error(function(inSender, inResponse) { + }); + req.error(this, function(inSender, inResponse) { if (inResponse == 404) this.bubble('onUserNotFound'); else this.bubble('onAPIError', {response: inResponse}); - }.bind(this)); + }); req.go(); }, loadTagRecords: function(type, tag) { @@ -181,19 +184,45 @@ enyo.kind({ }); req.go(); }, - getFeedInfo: function() { + getStatus: function() { if (!blerg.API.loggedIn) throw new Error('Cannot request feed status when not logged in'); var req = new enyo.Ajax({ - url: baseURL + '/feedinfo', + url: baseURL + '/status' + }); + req.response(this, function(inSender, inResponse) { + this.bubble('onStatus', inResponse); + }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); + req.go(); + }, + clearStatus: function(type) { + if (!blerg.API.loggedIn) + throw new Error('Cannot request feed status when not logged in'); + + if (!(type == 'feed' || type == 'mentioned')) + throw new Error('Invalid status clear type: ' + type); + + var req = new enyo.Ajax({ + url: baseURL + '/status', method: 'POST', postBody: { - username: blerg.API.username + clear: type } }); req.response(this, function(inSender, inResponse) { - this.bubble('onFeedInfo', inResponse); + inResponse.type = type; + this.bubble('onClearStatus', inResponse); + }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } }); req.go(); }, @@ -202,11 +231,7 @@ enyo.kind({ throw new Error('Cannot request feed status when not logged in'); var req = new enyo.Ajax({ - url: baseURL + '/feed', - method: 'POST', - postBody: { - username: blerg.API.username - } + url: baseURL + '/feed' }); req.response(this, function(inSender, inResponse) { this.bubble('onItemsLoaded', { @@ -214,15 +239,16 @@ enyo.kind({ entries: inResponse }); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, getSubscriptionStatus: function(username) { var req = new enyo.Ajax({ - url: baseURL + '/feedinfo/' + username, - method: 'POST', - postBody: { - username: blerg.API.username - } + url: baseURL + '/status/' + username }); req.response(this, function(inSender, inResponse) { this.bubble('onSubscriptionStatus', { @@ -230,46 +256,46 @@ enyo.kind({ subscribed: inResponse.subscribed }); }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); }, - subscribe: function(username) { + subscription: function(username, v) { + var subv = v ? true : false; var req = new enyo.Ajax({ url: baseURL + '/subscribe/' + username, method: 'POST', postBody: { - username: blerg.API.username + subscribed: subv } }); req.response(this, function(inSender, inResponse) { this.bubble('onSubscriptionStatus', { username: username, - subscribed: inResponse.status == "success" + subscribed: inResponse.status == "success" && subv }); }); - req.go(); - }, - unsubscribe: function(username) { - var req = new enyo.Ajax({ - url: baseURL + '/unsubscribe/' + username, - method: 'POST', - postBody: { - username: blerg.API.username + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); } }); - req.response(this, function(inSender, inResponse) { - this.bubble('onSubscriptionStatus', { - username: username, - subscribed: inResponse.status != "success" - }); - }); req.go(); }, + subscribe: function(username) { + this.subscription(username, true); + }, + unsubscribe: function(username) { + this.subscription(username, false); + }, post: function(data) { var req = new enyo.Ajax({ url: baseURL + '/put', method: 'POST', postBody: { - username: blerg.API.username, data: data } }); @@ -286,6 +312,11 @@ enyo.kind({ }); } }); + req.error(this, function() { + if (req.xhrResponse.status == 403) { + this.authenticationFailed(); + } + }); req.go(); } });