Upgrade to Enyo 2.2
[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(function(inSender, inResponse) {
132             this.bubble('onItemsLoaded', {
133                 type: 'user',
134                 username: username,
135                 from: from,
136                 to: to,
137                 entries: inResponse
138             });
139         }.bind(this));
140         req.error(function(inSender, inResponse) {
141             if (inResponse == 404)
142                 this.bubble('onUserNotFound');
143             else
144                 this.bubble('onAPIError', {response: inResponse});
145         }.bind(this));
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(function(inSender, inResponse) {
167             this.bubble('onItemsLoaded', {
168                 type: 'tag',
169                 tagType: type,
170                 tag: tag,
171                 entries: inResponse
172             });
173         }.bind(this));
174         req.go();
175     },
176     getFeedInfo: function() {
177         if (!blerg.API.loggedIn)
178             throw new Error('Cannot request feed status when not logged in');
179
180         var req = new enyo.Ajax({
181             url: baseURL + '/feedinfo',
182             method: 'POST',
183             postBody: {
184                 username: blerg.API.username
185             }
186         });
187         req.response(this, function(inSender, inResponse) {
188             this.bubble('onFeedInfo', inResponse);
189         });
190         req.go();
191     },
192     loadFeed: function() {
193         if (!blerg.API.loggedIn)
194             throw new Error('Cannot request feed status when not logged in');
195
196         var req = new enyo.Ajax({
197             url: baseURL + '/feed',
198             method: 'POST',
199             postBody: {
200                 username: blerg.API.username
201             }
202         });
203         req.response(this, function(inSender, inResponse) {
204             this.bubble('onItemsLoaded', {
205                 type: "feed",
206                 entries: inResponse
207             });
208         });
209         req.go();
210     },
211     getSubscriptionStatus: function(username) {
212         var req = new enyo.Ajax({
213             url: baseURL + '/feedinfo/' + username,
214             method: 'POST',
215             postBody: {
216                 username: blerg.API.username
217             }
218         });
219         req.response(this, function(inSender, inResponse) {
220             this.bubble('onSubscriptionStatus', {
221                 username: username,
222                 subscribed: inResponse.subscribed
223             });
224         });
225         req.go();
226     },
227     subscribe: function(username) {
228         var req = new enyo.Ajax({
229             url: baseURL + '/subscribe/' + username,
230             method: 'POST',
231             postBody: {
232                 username: blerg.API.username
233             }
234         });
235         req.response(this, function(inSender, inResponse) {
236             this.bubble('onSubscriptionStatus', {
237                 username: username,
238                 subscribed: inResponse.status == "success"
239             });
240         });
241         req.go();
242     },
243     unsubscribe: function(username) {
244         var req = new enyo.Ajax({
245             url: baseURL + '/unsubscribe/' + username,
246             method: 'POST',
247             postBody: {
248                 username: blerg.API.username
249             }
250         });
251         req.response(this, function(inSender, inResponse) {
252             this.bubble('onSubscriptionStatus', {
253                 username: username,
254                 subscribed: inResponse.status != "success"
255             });
256         });
257         req.go();
258     },
259     post: function(data) {
260         var req = new enyo.Ajax({
261             url: baseURL + '/put',
262             method: 'POST',
263             postBody: {
264                 username: blerg.API.username,
265                 data: data
266             }
267         });
268         req.response(this, function(inSender, inResponse) {
269             if (inResponse && inResponse.status == 'success') {
270                 this.bubble('onPostSuccessful', {
271                     username: blerg.API.username,
272                     data: data
273                 });
274             } else {
275                 this.bubble('onPostFailed', {
276                     username: blerg.API.username,
277                     data: data
278                 });
279             }
280         });
281         req.go();
282     }
283 });