GET-ify client API
authorChip Black <bytex64@bytex64.net>
Fri, 25 Jul 2014 23:26:52 +0000 (18:26 -0500)
committerChip Black <bytex64@bytex64.net>
Fri, 25 Jul 2014 23:27:16 +0000 (18:27 -0500)
www/jssrc/blerg/API.js

index ecf7cd3..19b6734 100644 (file)
@@ -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,15 +66,12 @@ 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();
@@ -82,25 +79,23 @@ enyo.kind({
     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) {
+        var logout_func = function(inSender, inResponse) {
             blerg.API.loggedIn = false;
+            blerg.API.username = '';
             enyo.setCookie('auth', '', {"Max-Age": 0});
             this.bubble('onLogoutSuccessful');
-        });
+        };
+        req.response(this, logout_func);
+        req.error(this, logout_func);
         req.go();
-        enyo.setCookie('username', '', {"Max-Age": 0});
     },
     changePassword: function(oldpassword, newpassword) {
         var req = new enyo.Ajax({
             url: baseURL + '/passwd',
             method: 'POST',
             postBody: {
-                username: blerg.API.username,
                 password: oldpassword,
                 new_password: newpassword
             }
@@ -186,11 +181,7 @@ enyo.kind({
             throw new Error('Cannot request feed status when not logged in');
 
         var req = new enyo.Ajax({
-            url: baseURL + '/status',
-            method: 'POST',
-            postBody: {
-                username: blerg.API.username
-            }
+            url: baseURL + '/status'
         });
         req.response(this, function(inSender, inResponse) {
             this.bubble('onStatus', inResponse);
@@ -208,7 +199,6 @@ enyo.kind({
             url: baseURL + '/status',
             method: 'POST',
             postBody: {
-                username: blerg.API.username,
                 clear: type
             }
         });
@@ -223,11 +213,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', {
@@ -239,11 +225,7 @@ enyo.kind({
     },
     getSubscriptionStatus: function(username) {
         var req = new enyo.Ajax({
-            url: baseURL + '/status/' + username,
-            method: 'POST',
-            postBody: {
-                username: blerg.API.username
-            }
+            url: baseURL + '/status/' + username
         });
         req.response(this, function(inSender, inResponse) {
             this.bubble('onSubscriptionStatus', {
@@ -253,44 +235,34 @@ enyo.kind({
         });
         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();
     },
+    subscribe: function(username) {
+        this.subscription(username, true);
+    },
     unsubscribe: function(username) {
-        var req = new enyo.Ajax({
-            url: baseURL + '/unsubscribe/' + username,
-            method: 'POST',
-            postBody: {
-                username: blerg.API.username
-            }
-        });
-        req.response(this, function(inSender, inResponse) {
-            this.bubble('onSubscriptionStatus', {
-                username: username,
-                subscribed: inResponse.status != "success"
-            });
-        });
-        req.go();
+        this.subscription(username, false);
     },
     post: function(data) {
         var req = new enyo.Ajax({
             url: baseURL + '/put',
             method: 'POST',
             postBody: {
-                username: blerg.API.username,
                 data: data
             }
         });