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});
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'
+ 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;
- 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
});
+ req.error(this, function() {
+ 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'
});
- req.response(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.go({
- username: blerg.API.username
+ }.bind(this);
+ req.response(this, logout_func);
+ req.error(this, logout_func);
+ req.go();
+ },
+ 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: {
+ password: oldpassword,
+ new_password: newpassword
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ if (inResponse.status == 'success') {
+ this.bubble('onPasswordChangeSuccessful');
+ } else {
+ this.bubble('onPasswordChangeFailed');
+ }
});
- enyo.setCookie('username', '', {"Max-Age": 0});
+ req.error(this, function() {
+ this.bubble('onPasswordChangeFailed');
+ });
+ req.go();
},
loadUserRecords: function(username, from ,to) {
var url;
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,
to: to,
entries: inResponse
});
- }.bind(this));
+ });
+ 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 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();
},
- 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',
- method: 'POST'
+ url: baseURL + '/status'
});
- req.response(function(inSender, inResponse) {
- this.bubble('onFeedInfo', inResponse);
- }.bind(this));
- req.go({
- username: blerg.API.username
+ 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: {
+ clear: type
+ }
+ });
+ req.response(this, function(inSender, inResponse) {
+ inResponse.type = type;
+ this.bubble('onClearStatus', inResponse);
+ });
+ req.error(this, function() {
+ if (req.xhrResponse.status == 403) {
+ this.authenticationFailed();
+ }
+ });
+ 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'
+ url: baseURL + '/feed'
});
- req.response(function(inSender, inResponse) {
+ req.response(this, function(inSender, inResponse) {
this.bubble('onItemsLoaded', {
type: "feed",
entries: inResponse
});
- }.bind(this));
- req.go({
- username: blerg.API.username
});
+ 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'
+ url: baseURL + '/status/' + username
});
- req.response(function(inSender, inResponse) {
+ req.response(this, function(inSender, inResponse) {
this.bubble('onSubscriptionStatus', {
username: username,
subscribed: inResponse.subscribed
});
- }.bind(this));
- req.go({
- username: blerg.API.username
});
+ 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'
+ method: 'POST',
+ postBody: {
+ subscribed: subv
+ }
});
- req.response(function(inSender, inResponse) {
+ req.response(this, function(inSender, inResponse) {
this.bubble('onSubscriptionStatus', {
username: username,
- subscribed: inResponse.status == "success"
+ subscribed: inResponse.status == "success" && subv
});
- }.bind(this));
- req.go({
- username: blerg.API.username
});
+ req.error(this, function() {
+ if (req.xhrResponse.status == 403) {
+ this.authenticationFailed();
+ }
+ });
+ 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 + '/unsubscribe/' + username,
- method: 'POST'
+ url: baseURL + '/put',
+ method: 'POST',
+ postBody: {
+ data: data
+ }
});
- req.response(function(inSender, inResponse) {
- this.bubble('onSubscriptionStatus', {
- username: username,
- subscribed: inResponse.status != "success"
- });
- }.bind(this));
- req.go({
- username: blerg.API.username
+ 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.error(this, function() {
+ if (req.xhrResponse.status == 403) {
+ this.authenticationFailed();
+ }
+ });
+ req.go();
+ }
});