3 // The API state is static so that any instance can use login-dependent API
14 this.inherited(arguments);
15 if (blerg.API.apiInitialized) {
16 if (blerg.API.loggedIn) {
17 setTimeout(function() {
18 this.bubble('onLoginSuccessful', {username: blerg.API.username});
24 if (enyo.getCookie('auth')) {
25 blerg.API.loggedIn = true;
26 blerg.API.username = enyo.getCookie('auth').split('/')[0];
27 // Defer the signal until everything's initialized
28 setTimeout(function() {
29 this.bubble('onLoginSuccessful', {username: blerg.API.username});
33 blerg.API.apiInitialized = true;
35 signup: function(username, password) {
36 var req = new enyo.Ajax({
37 url: baseURL + '/create',
44 req.response(this, function(inSender, inResponse) {
45 if (inResponse.status == 'success') {
46 this.bubble('onSignupSuccess', {username: username});
48 this.bubble('onSignupFailure', {username: username});
51 req.error(this, function() {
52 this.bubble('onSignupFailure', {username: username});
56 login: function(username, password) {
57 var req = new enyo.Ajax({
58 url: baseURL + '/login',
65 req.response(this, function(inSender, inResponse) {
66 if (inResponse.status == 'success') {
67 blerg.API.loggedIn = true;
68 blerg.API.username = username;
69 this.bubble('onLoginSuccessful', {username: username});
71 this.bubble('onLoginFailed');
74 req.error(this, function() {
75 this.bubble('onLoginFailed');
79 expireClientAuthentication: function() {
80 blerg.API.loggedIn = false;
81 blerg.API.username = '';
82 enyo.setCookie('auth', '', {"Max-Age": 0});
85 var req = new enyo.Ajax({
86 url: baseURL + '/logout',
89 var logout_func = function() {
90 this.expireClientAuthentication();
91 this.bubble('onLogoutSuccessful');
93 req.response(this, logout_func);
94 req.error(this, logout_func);
97 authenticationFailed: function() {
98 enyo.log("Authentication failed -- logging out");
99 this.expireClientAuthentication();
100 this.bubble('onAuthFailure');
102 changePassword: function(oldpassword, newpassword) {
103 var req = new enyo.Ajax({
104 url: baseURL + '/passwd',
107 password: oldpassword,
108 new_password: newpassword
111 req.response(this, function(inSender, inResponse) {
112 if (inResponse.status == 'success') {
113 this.bubble('onPasswordChangeSuccessful');
115 this.bubble('onPasswordChangeFailed');
118 req.error(this, function() {
119 this.bubble('onPasswordChangeFailed');
123 loadUserRecords: function(username, from ,to) {
125 if (from != undefined && to != undefined) {
126 url = baseURL + '/get/' + username + '/' + from + '-' + to;
128 url = baseURL + '/get/' + username;
131 var req = new enyo.Ajax({
134 req.response(this, function(inSender, inResponse) {
135 this.bubble('onItemsLoaded', {
143 req.error(this, function(inSender, inResponse) {
144 if (inResponse == 404)
145 this.bubble('onUserNotFound');
147 this.bubble('onAPIError', {response: inResponse});
151 loadTagRecords: function(type, tag) {
155 // Apache eats the hash, even encoded. Probably a security
157 url = baseURL + '/tag/H' + tag;
160 url = baseURL + '/tag/%40' + tag;
163 throw new Error("Invalid tag type: " + type);
166 var req = new enyo.Ajax({
169 req.response(this, function(inSender, inResponse) {
170 this.bubble('onItemsLoaded', {
177 req.error(this, function() {
178 this.bubble('onItemsLoaded', {
187 getStatus: function() {
188 if (!blerg.API.loggedIn)
189 throw new Error('Cannot request feed status when not logged in');
191 var req = new enyo.Ajax({
192 url: baseURL + '/status'
194 req.response(this, function(inSender, inResponse) {
195 this.bubble('onStatus', inResponse);
197 req.error(this, function() {
198 if (req.xhrResponse.status == 403) {
199 this.authenticationFailed();
204 clearStatus: function(type) {
205 if (!blerg.API.loggedIn)
206 throw new Error('Cannot request feed status when not logged in');
208 if (!(type == 'feed' || type == 'mentioned'))
209 throw new Error('Invalid status clear type: ' + type);
211 var req = new enyo.Ajax({
212 url: baseURL + '/status',
218 req.response(this, function(inSender, inResponse) {
219 inResponse.type = type;
220 this.bubble('onClearStatus', inResponse);
222 req.error(this, function() {
223 if (req.xhrResponse.status == 403) {
224 this.authenticationFailed();
229 loadFeed: function() {
230 if (!blerg.API.loggedIn)
231 throw new Error('Cannot request feed status when not logged in');
233 var req = new enyo.Ajax({
234 url: baseURL + '/feed'
236 req.response(this, function(inSender, inResponse) {
237 this.bubble('onItemsLoaded', {
242 req.error(this, function() {
243 if (req.xhrResponse.status == 403) {
244 this.authenticationFailed();
249 getSubscriptionStatus: function(username) {
250 var req = new enyo.Ajax({
251 url: baseURL + '/status/' + username
253 req.response(this, function(inSender, inResponse) {
254 this.bubble('onSubscriptionStatus', {
256 subscribed: inResponse.subscribed
259 req.error(this, function() {
260 if (req.xhrResponse.status == 403) {
261 this.authenticationFailed();
266 subscription: function(username, v) {
267 var subv = v ? true : false;
268 var req = new enyo.Ajax({
269 url: baseURL + '/subscribe/' + username,
275 req.response(this, function(inSender, inResponse) {
276 this.bubble('onSubscriptionStatus', {
278 subscribed: inResponse.status == "success" && subv
281 req.error(this, function() {
282 if (req.xhrResponse.status == 403) {
283 this.authenticationFailed();
288 subscribe: function(username) {
289 this.subscription(username, true);
291 unsubscribe: function(username) {
292 this.subscription(username, false);
294 post: function(data) {
295 var req = new enyo.Ajax({
296 url: baseURL + '/put',
302 req.response(this, function(inSender, inResponse) {
303 if (inResponse && inResponse.status == 'success') {
304 this.bubble('onPostSuccessful', {
305 username: blerg.API.username,
309 this.bubble('onPostFailed', {
310 username: blerg.API.username,
315 req.error(this, function() {
316 if (req.xhrResponse.status == 403) {
317 this.authenticationFailed();