Update webapp for /status changes
[blerg.git] / www / jssrc / blerg / API.js
index 95cdbcd..ecf7cd3 100644 (file)
@@ -35,29 +35,34 @@ enyo.kind({
     signup: function(username, password) {
         var req = new enyo.Ajax({
             url: baseURL + '/create',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: username,
+                password: password,
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             if (inResponse.status == 'success') {
                 this.bubble('onSignupSuccess', {username: username});
             } else {
                 this.bubble('onSignupFailure', {username: username});
             }
-        }.bind(this));
-        req.error(function() {
+        });
+        req.error(this, function() {
             this.bubble('onSignupFailure', {username: username});
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: username,
-            password: password,
-        }));
+        });
+        req.go();
     },
     login: function(username, password) {
         var req = new enyo.Ajax({
             url: baseURL + '/login',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: username,
+                password: password
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             if (inResponse.status == 'success') {
                 blerg.API.loggedIn = true;
                 blerg.API.username = username;
@@ -67,44 +72,50 @@ enyo.kind({
                 enyo.setCookie('username', '', {"Max-Age": 0});
                 this.bubble('onLoginFailed');
             }
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: username,
-            password: password
-        }));
+        });
+        req.error(this, function() {
+            enyo.setCookie('username', '', {"Max-Age": 0});
+            this.bubble('onLoginFailed');
+        });
+        req.go();
     },
     logout: function() {
         var req = new enyo.Ajax({
             url: baseURL + '/logout',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             blerg.API.loggedIn = false;
             enyo.setCookie('auth', '', {"Max-Age": 0});
             this.bubble('onLogoutSuccessful');
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        });
+        req.go();
         enyo.setCookie('username', '', {"Max-Age": 0});
     },
     changePassword: function(oldpassword, newpassword) {
         var req = new enyo.Ajax({
             url: baseURL + '/passwd',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username,
+                password: oldpassword,
+                new_password: newpassword
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             if (inResponse.status == 'success') {
                 this.bubble('onPasswordChangeSuccessful');
             } else {
                 this.bubble('onPasswordChangeFailed');
             }
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username,
-            password: oldpassword,
-            new_password: newpassword
-        }));
+        });
+        req.error(this, function() {
+            this.bubble('onPasswordChangeFailed');
+        });
+        req.go();
     },
     loadUserRecords: function(username, from ,to) {
         var url;
@@ -117,7 +128,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,
@@ -125,13 +136,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) {
@@ -152,30 +163,60 @@ enyo.kind({
         var req = new enyo.Ajax({
             url: url
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             this.bubble('onItemsLoaded', {
                 type: 'tag',
                 tagType: type,
                 tag: tag,
                 entries: inResponse
             });
-        }.bind(this));
+        });
+        req.error(this, function() {
+            this.bubble('onItemsLoaded', {
+                type: 'tag',
+                tagType: type,
+                tag: tag,
+                entries: []
+            });
+        });
+        req.go();
+    },
+    getStatus: function() {
+        if (!blerg.API.loggedIn)
+            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
+            }
+        });
+        req.response(this, function(inSender, inResponse) {
+            this.bubble('onStatus', inResponse);
+        });
         req.go();
     },
-    getFeedInfo: function() {
+    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 + '/feedinfo',
-            method: 'POST'
+            url: baseURL + '/status',
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username,
+                clear: type
+            }
+        });
+        req.response(this, function(inSender, inResponse) {
+            inResponse.type = type;
+            this.bubble('onClearStatus', inResponse);
         });
-        req.response(function(inSender, inResponse) {
-            this.bubble('onFeedInfo', inResponse);
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        req.go();
     },
     loadFeed: function() {
         if (!blerg.API.loggedIn)
@@ -183,69 +224,77 @@ enyo.kind({
 
         var req = new enyo.Ajax({
             url: baseURL + '/feed',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             this.bubble('onItemsLoaded', {
                 type: "feed",
                 entries: inResponse
             });
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        });
+        req.go();
     },
     getSubscriptionStatus: function(username) {
         var req = new enyo.Ajax({
-            url: baseURL + '/feedinfo/' + username,
-            method: 'POST'
+            url: baseURL + '/status/' + username,
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             this.bubble('onSubscriptionStatus', {
                 username: username,
                 subscribed: inResponse.subscribed
             });
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        });
+        req.go();
     },
     subscribe: function(username) {
         var req = new enyo.Ajax({
             url: baseURL + '/subscribe/' + username,
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             this.bubble('onSubscriptionStatus', {
                 username: username,
                 subscribed: inResponse.status == "success"
             });
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        });
+        req.go();
     },
     unsubscribe: function(username) {
         var req = new enyo.Ajax({
             url: baseURL + '/unsubscribe/' + username,
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             this.bubble('onSubscriptionStatus', {
                 username: username,
                 subscribed: inResponse.status != "success"
             });
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username
-        }));
+        });
+        req.go();
     },
     post: function(data) {
         var req = new enyo.Ajax({
             url: baseURL + '/put',
-            method: 'POST'
+            method: 'POST',
+            postBody: {
+                username: blerg.API.username,
+                data: data
+            }
         });
-        req.response(function(inSender, inResponse) {
+        req.response(this, function(inSender, inResponse) {
             if (inResponse && inResponse.status == 'success') {
                 this.bubble('onPostSuccessful', {
                     username: blerg.API.username,
@@ -257,10 +306,7 @@ enyo.kind({
                     data: data
                 });
             }
-        }.bind(this));
-        req.go(enyo.Ajax.objectToQuery({
-            username: blerg.API.username,
-            data: data
-        }));
+        });
+        req.go();
     }
 });