Update webapp for /status changes
[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') && enyo.getCookie('username')) {
25             blerg.API.loggedIn = true;
26             blerg.API.username = enyo.getCookie('username');
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                 enyo.setCookie('username', username);
70                 this.bubble('onLoginSuccessful', {username: username});
71             } else {
72                 enyo.setCookie('username', '', {"Max-Age": 0});
73                 this.bubble('onLoginFailed');
74             }
75         });
76         req.error(this, function() {
77             enyo.setCookie('username', '', {"Max-Age": 0});
78             this.bubble('onLoginFailed');
79         });
80         req.go();
81     },
82     logout: function() {
83         var req = new enyo.Ajax({
84             url: baseURL + '/logout',
85             method: 'POST',
86             postBody: {
87                 username: blerg.API.username
88             }
89         });
90         req.response(this, function(inSender, inResponse) {
91             blerg.API.loggedIn = false;
92             enyo.setCookie('auth', '', {"Max-Age": 0});
93             this.bubble('onLogoutSuccessful');
94         });
95         req.go();
96         enyo.setCookie('username', '', {"Max-Age": 0});
97     },
98     changePassword: function(oldpassword, newpassword) {
99         var req = new enyo.Ajax({
100             url: baseURL + '/passwd',
101             method: 'POST',
102             postBody: {
103                 username: blerg.API.username,
104                 password: oldpassword,
105                 new_password: newpassword
106             }
107         });
108         req.response(this, function(inSender, inResponse) {
109             if (inResponse.status == 'success') {
110                 this.bubble('onPasswordChangeSuccessful');
111             } else {
112                 this.bubble('onPasswordChangeFailed');
113             }
114         });
115         req.error(this, function() {
116             this.bubble('onPasswordChangeFailed');
117         });
118         req.go();
119     },
120     loadUserRecords: function(username, from ,to) {
121         var url;
122         if (from != undefined && to != undefined) {
123             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
124         } else {
125             url = baseURL +  '/get/' + username;
126         }
127
128         var req = new enyo.Ajax({
129             url: url
130         });
131         req.response(this, function(inSender, inResponse) {
132             this.bubble('onItemsLoaded', {
133                 type: 'user',
134                 username: username,
135                 from: from,
136                 to: to,
137                 entries: inResponse
138             });
139         });
140         req.error(this, function(inSender, inResponse) {
141             if (inResponse == 404)
142                 this.bubble('onUserNotFound');
143             else
144                 this.bubble('onAPIError', {response: inResponse});
145         });
146         req.go();
147     },
148     loadTagRecords: function(type, tag) {
149         var url;
150         switch(type) {
151             case 'tag':
152                 // Apache eats the hash, even encoded.  Probably a security
153                 // feature.
154                 url = baseURL + '/tag/H' + tag;
155                 break;
156             case 'ref':
157                 url = baseURL + '/tag/%40' + tag;
158                 break;
159             default:
160                 throw new Error("Invalid tag type: " + type);
161                 return;
162         }
163         var req = new enyo.Ajax({
164             url: url
165         });
166         req.response(this, function(inSender, inResponse) {
167             this.bubble('onItemsLoaded', {
168                 type: 'tag',
169                 tagType: type,
170                 tag: tag,
171                 entries: inResponse
172             });
173         });
174         req.error(this, function() {
175             this.bubble('onItemsLoaded', {
176                 type: 'tag',
177                 tagType: type,
178                 tag: tag,
179                 entries: []
180             });
181         });
182         req.go();
183     },
184     getStatus: function() {
185         if (!blerg.API.loggedIn)
186             throw new Error('Cannot request feed status when not logged in');
187
188         var req = new enyo.Ajax({
189             url: baseURL + '/status',
190             method: 'POST',
191             postBody: {
192                 username: blerg.API.username
193             }
194         });
195         req.response(this, function(inSender, inResponse) {
196             this.bubble('onStatus', inResponse);
197         });
198         req.go();
199     },
200     clearStatus: function(type) {
201         if (!blerg.API.loggedIn)
202             throw new Error('Cannot request feed status when not logged in');
203
204         if (!(type == 'feed' || type == 'mentioned'))
205             throw new Error('Invalid status clear type: ' + type);
206
207         var req = new enyo.Ajax({
208             url: baseURL + '/status',
209             method: 'POST',
210             postBody: {
211                 username: blerg.API.username,
212                 clear: type
213             }
214         });
215         req.response(this, function(inSender, inResponse) {
216             inResponse.type = type;
217             this.bubble('onClearStatus', inResponse);
218         });
219         req.go();
220     },
221     loadFeed: function() {
222         if (!blerg.API.loggedIn)
223             throw new Error('Cannot request feed status when not logged in');
224
225         var req = new enyo.Ajax({
226             url: baseURL + '/feed',
227             method: 'POST',
228             postBody: {
229                 username: blerg.API.username
230             }
231         });
232         req.response(this, function(inSender, inResponse) {
233             this.bubble('onItemsLoaded', {
234                 type: "feed",
235                 entries: inResponse
236             });
237         });
238         req.go();
239     },
240     getSubscriptionStatus: function(username) {
241         var req = new enyo.Ajax({
242             url: baseURL + '/status/' + username,
243             method: 'POST',
244             postBody: {
245                 username: blerg.API.username
246             }
247         });
248         req.response(this, function(inSender, inResponse) {
249             this.bubble('onSubscriptionStatus', {
250                 username: username,
251                 subscribed: inResponse.subscribed
252             });
253         });
254         req.go();
255     },
256     subscribe: function(username) {
257         var req = new enyo.Ajax({
258             url: baseURL + '/subscribe/' + username,
259             method: 'POST',
260             postBody: {
261                 username: blerg.API.username
262             }
263         });
264         req.response(this, function(inSender, inResponse) {
265             this.bubble('onSubscriptionStatus', {
266                 username: username,
267                 subscribed: inResponse.status == "success"
268             });
269         });
270         req.go();
271     },
272     unsubscribe: function(username) {
273         var req = new enyo.Ajax({
274             url: baseURL + '/unsubscribe/' + username,
275             method: 'POST',
276             postBody: {
277                 username: blerg.API.username
278             }
279         });
280         req.response(this, function(inSender, inResponse) {
281             this.bubble('onSubscriptionStatus', {
282                 username: username,
283                 subscribed: inResponse.status != "success"
284             });
285         });
286         req.go();
287     },
288     post: function(data) {
289         var req = new enyo.Ajax({
290             url: baseURL + '/put',
291             method: 'POST',
292             postBody: {
293                 username: blerg.API.username,
294                 data: data
295             }
296         });
297         req.response(this, function(inSender, inResponse) {
298             if (inResponse && inResponse.status == 'success') {
299                 this.bubble('onPostSuccessful', {
300                     username: blerg.API.username,
301                     data: data
302                 });
303             } else {
304                 this.bubble('onPostFailed', {
305                     username: blerg.API.username,
306                     data: data
307                 });
308             }
309         });
310         req.go();
311     }
312 });