64b1108b0de9b2910b2d0139bae6e925dec010d0
[blerg.git] / www / jssrc / blerg / API.js
1 var baseURL = '';
2
3 // The API state is static so that any instance can use login-dependent API
4 // calls
5 enyo.kind({
6     name: "blerg.API",
7     kind: "Component",
8     statics: {
9         apiInitialized: false,
10         loggedIn: false,
11         username: "",
12     },
13     create: function() {
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});
19                 }.bind(this), 0);
20             }
21             return;
22         }
23
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});
30             }.bind(this), 0);
31         }
32
33         blerg.API.apiInitialized = true;
34     },
35     signup: function(username, password) {
36         var req = new enyo.Ajax({
37             url: baseURL + '/create',
38             method: 'POST',
39             postBody: {
40                 username: username,
41                 password: password,
42             }
43         });
44         req.response(this, function(inSender, inResponse) {
45             if (inResponse.status == 'success') {
46                 this.bubble('onSignupSuccess', {username: username});
47             } else {
48                 this.bubble('onSignupFailure', {username: username});
49             }
50         });
51         req.error(this, function() {
52             this.bubble('onSignupFailure', {username: username});
53         });
54         req.go();
55     },
56     login: function(username, password) {
57         var req = new enyo.Ajax({
58             url: baseURL + '/login',
59             method: 'POST',
60             postBody: {
61                 username: username,
62                 password: password
63             }
64         });
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});
70             } else {
71                 this.bubble('onLoginFailed');
72             }
73         });
74         req.error(this, function() {
75             this.bubble('onLoginFailed');
76         });
77         req.go();
78     },
79     expireClientAuthentication: function() {
80         blerg.API.loggedIn = false;
81         blerg.API.username = '';
82         enyo.setCookie('auth', '', {"Max-Age": 0});
83     },
84     logout: function() {
85         var req = new enyo.Ajax({
86             url: baseURL + '/logout',
87             method: 'POST'
88         });
89         var logout_func = function() {
90             this.expireClientAuthentication();
91             this.bubble('onLogoutSuccessful');
92         }.bind(this);
93         req.response(this, logout_func);
94         req.error(this, logout_func);
95         req.go();
96     },
97     authenticationFailed: function() {
98         enyo.log("Authentication failed -- logging out");
99         this.expireClientAuthentication();
100         this.bubble('onAuthFailure');
101     },
102     changePassword: function(oldpassword, newpassword) {
103         var req = new enyo.Ajax({
104             url: baseURL + '/passwd',
105             method: 'POST',
106             postBody: {
107                 password: oldpassword,
108                 new_password: newpassword
109             }
110         });
111         req.response(this, function(inSender, inResponse) {
112             if (inResponse.status == 'success') {
113                 this.bubble('onPasswordChangeSuccessful');
114             } else {
115                 this.bubble('onPasswordChangeFailed');
116             }
117         });
118         req.error(this, function() {
119             this.bubble('onPasswordChangeFailed');
120         });
121         req.go();
122     },
123     loadUserRecords: function(username, from ,to) {
124         var url;
125         if (from != undefined && to != undefined) {
126             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
127         } else {
128             url = baseURL +  '/get/' + username;
129         }
130
131         var req = new enyo.Ajax({
132             url: url
133         });
134         req.response(this, function(inSender, inResponse) {
135             this.bubble('onItemsLoaded', {
136                 type: 'user',
137                 username: username,
138                 from: from,
139                 to: to,
140                 entries: inResponse
141             });
142         });
143         req.error(this, function(inSender, inResponse) {
144             if (inResponse == 404)
145                 this.bubble('onUserNotFound');
146             else
147                 this.bubble('onAPIError', {response: inResponse});
148         });
149         req.go();
150     },
151     loadTagRecords: function(type, tag) {
152         var url;
153         switch(type) {
154             case 'tag':
155                 // Apache eats the hash, even encoded.  Probably a security
156                 // feature.
157                 url = baseURL + '/tag/H' + tag;
158                 break;
159             case 'ref':
160                 url = baseURL + '/tag/%40' + tag;
161                 break;
162             default:
163                 throw new Error("Invalid tag type: " + type);
164                 return;
165         }
166         var req = new enyo.Ajax({
167             url: url
168         });
169         req.response(this, function(inSender, inResponse) {
170             this.bubble('onItemsLoaded', {
171                 type: 'tag',
172                 tagType: type,
173                 tag: tag,
174                 entries: inResponse
175             });
176         });
177         req.error(this, function() {
178             this.bubble('onItemsLoaded', {
179                 type: 'tag',
180                 tagType: type,
181                 tag: tag,
182                 entries: []
183             });
184         });
185         req.go();
186     },
187     getStatus: function() {
188         if (!blerg.API.loggedIn)
189             throw new Error('Cannot request feed status when not logged in');
190
191         var req = new enyo.Ajax({
192             url: baseURL + '/status'
193         });
194         req.response(this, function(inSender, inResponse) {
195             this.bubble('onStatus', inResponse);
196         });
197         req.error(this, function() {
198             if (req.xhrResponse.status == 403) {
199                 this.authenticationFailed();
200             }
201         });
202         req.go();
203     },
204     clearStatus: function(type) {
205         if (!blerg.API.loggedIn)
206             throw new Error('Cannot request feed status when not logged in');
207
208         if (!(type == 'feed' || type == 'mentioned'))
209             throw new Error('Invalid status clear type: ' + type);
210
211         var req = new enyo.Ajax({
212             url: baseURL + '/status',
213             method: 'POST',
214             postBody: {
215                 clear: type
216             }
217         });
218         req.response(this, function(inSender, inResponse) {
219             inResponse.type = type;
220             this.bubble('onClearStatus', inResponse);
221         });
222         req.error(this, function() {
223             if (req.xhrResponse.status == 403) {
224                 this.authenticationFailed();
225             }
226         });
227         req.go();
228     },
229     loadFeed: function() {
230         if (!blerg.API.loggedIn)
231             throw new Error('Cannot request feed status when not logged in');
232
233         var req = new enyo.Ajax({
234             url: baseURL + '/feed'
235         });
236         req.response(this, function(inSender, inResponse) {
237             this.bubble('onItemsLoaded', {
238                 type: "feed",
239                 entries: inResponse
240             });
241         });
242         req.error(this, function() {
243             if (req.xhrResponse.status == 403) {
244                 this.authenticationFailed();
245             }
246         });
247         req.go();
248     },
249     getSubscriptionStatus: function(username) {
250         var req = new enyo.Ajax({
251             url: baseURL + '/status/' + username
252         });
253         req.response(this, function(inSender, inResponse) {
254             this.bubble('onSubscriptionStatus', {
255                 username: username,
256                 subscribed: inResponse.subscribed
257             });
258         });
259         req.error(this, function() {
260             if (req.xhrResponse.status == 403) {
261                 this.authenticationFailed();
262             }
263         });
264         req.go();
265     },
266     subscription: function(username, v) {
267         var subv = v ? true : false;
268         var req = new enyo.Ajax({
269             url: baseURL + '/subscribe/' + username,
270             method: 'POST',
271             postBody: {
272                 subscribed: subv
273             }
274         });
275         req.response(this, function(inSender, inResponse) {
276             this.bubble('onSubscriptionStatus', {
277                 username: username,
278                 subscribed: inResponse.status == "success" && subv
279             });
280         });
281         req.error(this, function() {
282             if (req.xhrResponse.status == 403) {
283                 this.authenticationFailed();
284             }
285         });
286         req.go();
287     },
288     subscribe: function(username) {
289         this.subscription(username, true);
290     },
291     unsubscribe: function(username) {
292         this.subscription(username, false);
293     },
294     post: function(data) {
295         var req = new enyo.Ajax({
296             url: baseURL + '/put',
297             method: 'POST',
298             postBody: {
299                 data: data
300             }
301         });
302         req.response(this, function(inSender, inResponse) {
303             if (inResponse && inResponse.status == 'success') {
304                 this.bubble('onPostSuccessful', {
305                     username: blerg.API.username,
306                     data: data
307                 });
308             } else {
309                 this.bubble('onPostFailed', {
310                     username: blerg.API.username,
311                     data: data
312                 });
313             }
314         });
315         req.error(this, function() {
316             if (req.xhrResponse.status == 403) {
317                 this.authenticationFailed();
318             }
319         });
320         req.go();
321     }
322 });