X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FAPI.js;h=a5aa965100ab75fd91bc344cec2329f4a0fe950f;hb=79a8b4304e691959e39d24fe9438d73bcea1badf;hp=1a6b0133816537f82bb6d9eaa3000650f7bd333f;hpb=15917511e7ec895e1caf6464a7deef76ed448f76;p=blerg.git diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 1a6b013..a5aa965 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -3,92 +3,223 @@ var baseURL = ''; // The API state is static so that any instance can use login-dependent API // calls enyo.kind({ - name: "blerg.API", - kind: "Component", - statics: { - apiInitialized: false, - loggedIn: false, - username: "", - }, - create: function() { - this.inherited(arguments); - if (blerg.API.apiInitialized) { - if (blerg.API.loggedIn) { - setTimeout(function() { - this.bubble('onLoginSuccessful', {username: blerg.API.username}); - }.bind(this), 0); - } - return; - } + name: "blerg.API", + kind: "Component", + statics: { + apiInitialized: false, + loggedIn: false, + username: "", + }, + create: function() { + this.inherited(arguments); + if (blerg.API.apiInitialized) { + if (blerg.API.loggedIn) { + setTimeout(function() { + this.bubble('onLoginSuccessful', {username: blerg.API.username}); + }.bind(this), 0); + } + return; + } - if (enyo.getCookie('auth') && enyo.getCookie('username')) { - blerg.API.loggedIn = true; - blerg.API.username = enyo.getCookie('username'); - // Defer the signal until everything's initialized - setTimeout(function() { - this.bubble('onLoginSuccessful', {username: blerg.API.username}); - }.bind(this), 0); - } + if (enyo.getCookie('auth') && enyo.getCookie('username')) { + blerg.API.loggedIn = true; + blerg.API.username = enyo.getCookie('username'); + // Defer the signal until everything's initialized + setTimeout(function() { + this.bubble('onLoginSuccessful', {username: blerg.API.username}); + }.bind(this), 0); + } - blerg.API.apiInitialized = true; - }, - login: function(username, password) { - var req = new enyo.Ajax({ - url: baseURL + '/login', - method: 'POST' - }); - req.response(function(inSender, inResponse) { - 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'); - } - }.bind(this)); - req.go({ - username: username, - password: password - }); - }, - logout: function() { - var req = new enyo.Ajax({ - url: baseURL + '/logout', - method: 'POST' - }); - req.response(function(inSender, inResponse) { - blerg.API.loggedIn = false; - enyo.setCookie('auth', '', {"Max-Age": 0}); - this.bubble('onLogoutSuccessful'); - }.bind(this)); - req.go({ - username: blerg.API.username - }); - enyo.setCookie('username', '', {"Max-Age": 0}); - }, - requestFeedStatus: function() { - if (!blerg.API.loggedIn) - throw new Error('Cannot request feed status when not logged in'); - // TODO - }, - loadUserRecords: function(username, from ,to) { - var url; - if (from != undefined && to != undefined) { - url = baseURL + '/get/' + username + '/' + from + '-' + to; - } else { - url = baseURL + '/get/' + username; - } + blerg.API.apiInitialized = true; + }, + signup: function(username, password) { + var req = new enyo.Ajax({ + url: baseURL + '/create', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + if (inResponse.status == 'success') { + this.bubble('onSignupSuccess', {username: username}); + } else { + this.bubble('onSignupFailure', {username: username}); + } + }.bind(this)); + req.error(function() { + this.bubble('onSignupFailure', {username: username}); + }.bind(this)); + req.go({ + username: username, + password: password, + }); + }, + login: function(username, password) { + var req = new enyo.Ajax({ + url: baseURL + '/login', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + 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'); + } + }.bind(this)); + req.go({ + username: username, + password: password + }); + }, + logout: function() { + var req = new enyo.Ajax({ + url: baseURL + '/logout', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + blerg.API.loggedIn = false; + enyo.setCookie('auth', '', {"Max-Age": 0}); + this.bubble('onLogoutSuccessful'); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + enyo.setCookie('username', '', {"Max-Age": 0}); + }, + loadUserRecords: function(username, from ,to) { + var url; + if (from != undefined && to != undefined) { + url = baseURL + '/get/' + username + '/' + from + '-' + to; + } else { + url = baseURL + '/get/' + username; + } - var req = new enyo.Ajax({ - url: url - }); - req.response(function(inSender, inResponse) { - this.bubble('onItemsLoaded', { - entries: inResponse - }); - }.bind(this)); - req.go(); - } + var req = new enyo.Ajax({ + url: url + }); + req.response(function(inSender, inResponse) { + this.bubble('onItemsLoaded', { + type: 'user', + username: username, + from: from, + to: to, + entries: inResponse + }); + }.bind(this)); + req.error(function(inSender, inResponse) { + if (inResponse == 404) + this.bubble('onUserNotFound'); + else + this.bubble('onAPIError', {response: inResponse}); + }.bind(this)); + req.go(); + }, + loadTagRecords: function(type, tag) { + var url; + switch(type) { + case 'tag': + // Apache eats the hash, even encoded. Probably a security + // feature. + url = baseURL + '/tag/H' + tag; + break; + case 'ref': + url = baseURL + '/tag/%40' + tag; + break; + default: + throw new Error("Invalid tag type: " + type); + return; + } + var req = new enyo.Ajax({ + url: url + }); + req.response(function(inSender, inResponse) { + this.bubble('onItemsLoaded', { + type: 'tag', + tagType: type, + tag: tag, + entries: inResponse + }); + }.bind(this)); + req.go(); + }, + getFeedInfo: function() { + if (!blerg.API.loggedIn) + throw new Error('Cannot request feed status when not logged in'); + + var req = new enyo.Ajax({ + url: baseURL + '/feedinfo', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.bubble('onFeedInfo', inResponse); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + }, + loadFeed: function() { + if (!blerg.API.loggedIn) + throw new Error('Cannot request feed status when not logged in'); + + var req = new enyo.Ajax({ + url: baseURL + '/feed', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.bubble('onItemsLoaded', { + type: "feed", + entries: inResponse + }); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + }, + getSubscriptionStatus: function(username) { + var req = new enyo.Ajax({ + url: baseURL + '/feedinfo/' + username, + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.bubble('onSubscriptionStatus', { + username: username, + subscribed: inResponse.subscribed + }); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + }, + subscribe: function(username) { + var req = new enyo.Ajax({ + url: baseURL + '/subscribe/' + username, + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.bubble('onSubscriptionStatus', { + username: username, + subscribed: inResponse.status == "success" + }); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + }, + unsubscribe: function(username) { + var req = new enyo.Ajax({ + url: baseURL + '/unsubscribe/' + username, + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.bubble('onSubscriptionStatus', { + username: username, + subscribed: inResponse.status != "success" + }); + }.bind(this)); + req.go({ + username: blerg.API.username + }); + }, });