+ 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);
+ }
+
+ blerg.API.apiInitialized = true;
+ },
+ signup: function(username, password) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/create',
+ method: 'POST',
+ postBody: {
+ username: username,
+ password: password,
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ if (inResponse.status == 'success') {
+ this.bubble('onSignupSuccess', {username: username});
+ } else {
+ this.bubble('onSignupFailure', {username: username});
+ }
+ });
+ req.error(this, function() {
+ this.bubble('onSignupFailure', {username: username});
+ });
+ req.go();
+ },
+ login: function(username, password) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/login',
+ method: 'POST',
+ postBody: {
+ username: username,
+ password: password
+ }
+ });
+ req.response(this, 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');
+ }
+ });
+ 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',
+ postBody: {
+ username: blerg.API.username
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ blerg.API.loggedIn = false;
+ enyo.setCookie('auth', '', {"Max-Age": 0});
+ this.bubble('onLogoutSuccessful');
+ });
+ 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
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ if (inResponse.status == 'success') {
+ this.bubble('onPasswordChangeSuccessful');
+ } else {
+ this.bubble('onPasswordChangeFailed');
+ }
+ });
+ req.error(this, function() {
+ this.bubble('onPasswordChangeFailed');
+ });
+ req.go();
+ },
+ 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(this, function(inSender, inResponse) {
+ this.bubble('onItemsLoaded', {
+ type: 'user',
+ username: username,
+ from: from,
+ to: to,
+ entries: inResponse
+ });
+ });
+ req.error(this, function(inSender, inResponse) {
+ if (inResponse == 404)
+ this.bubble('onUserNotFound');
+ else
+ this.bubble('onAPIError', {response: inResponse});
+ });
+ 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(this, function(inSender, inResponse) {
+ this.bubble('onItemsLoaded', {
+ type: 'tag',
+ tagType: type,
+ tag: tag,
+ entries: inResponse
+ });
+ });
+ req.error(this, function() {
+ this.bubble('onItemsLoaded', {
+ type: 'tag',
+ tagType: type,
+ tag: tag,
+ entries: []
+ });
+ });
+ 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',
+ postBody: {
+ username: blerg.API.username
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ this.bubble('onFeedInfo', inResponse);
+ });
+ req.go();
+ },
+ 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',
+ postBody: {
+ username: blerg.API.username
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ this.bubble('onItemsLoaded', {
+ type: "feed",
+ entries: inResponse
+ });
+ });
+ req.go();
+ },
+ getSubscriptionStatus: function(username) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/feedinfo/' + username,
+ method: 'POST',
+ postBody: {
+ username: blerg.API.username
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ this.bubble('onSubscriptionStatus', {
+ username: username,
+ subscribed: inResponse.subscribed
+ });
+ });
+ req.go();
+ },
+ subscribe: function(username) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/subscribe/' + 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();
+ },
+ 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();
+ },
+ post: function(data) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/put',
+ method: 'POST',
+ postBody: {
+ username: blerg.API.username,
+ data: data
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ if (inResponse && inResponse.status == 'success') {
+ this.bubble('onPostSuccessful', {
+ username: blerg.API.username,
+ data: data
+ });
+ } else {
+ this.bubble('onPostFailed', {
+ username: blerg.API.username,
+ data: data
+ });
+ }
+ });
+ req.go();
+ }