Add password change functionality
[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         });
40         req.response(function(inSender, inResponse) {
41             if (inResponse.status == 'success') {
42                 this.bubble('onSignupSuccess', {username: username});
43             } else {
44                 this.bubble('onSignupFailure', {username: username});
45             }
46         }.bind(this));
47         req.error(function() {
48             this.bubble('onSignupFailure', {username: username});
49         }.bind(this));
50         req.go({
51             username: username,
52             password: password,
53         });
54     },
55     login: function(username, password) {
56         var req = new enyo.Ajax({
57             url: baseURL + '/login',
58             method: 'POST'
59         });
60         req.response(function(inSender, inResponse) {
61             if (inResponse.status == 'success') {
62                 blerg.API.loggedIn = true;
63                 blerg.API.username = username;
64                 enyo.setCookie('username', username);
65                 this.bubble('onLoginSuccessful', {username: username});
66             } else {
67                 enyo.setCookie('username', '', {"Max-Age": 0});
68                 this.bubble('onLoginFailed');
69             }
70         }.bind(this));
71         req.go({
72             username: username,
73             password: password
74         });
75     },
76     logout: function() {
77         var req = new enyo.Ajax({
78             url: baseURL + '/logout',
79             method: 'POST'
80         });
81         req.response(function(inSender, inResponse) {
82             blerg.API.loggedIn = false;
83             enyo.setCookie('auth', '', {"Max-Age": 0});
84             this.bubble('onLogoutSuccessful');
85         }.bind(this));
86         req.go({
87             username: blerg.API.username
88         });
89         enyo.setCookie('username', '', {"Max-Age": 0});
90     },
91     changePassword: function(oldpassword, newpassword) {
92         var req = new enyo.Ajax({
93             url: baseURL + '/passwd',
94             method: 'POST'
95         });
96         req.response(function(inSender, inResponse) {
97             if (inResponse.status == 'success') {
98                 this.bubble('onPasswordChangeSuccessful');
99             } else {
100                 this.bubble('onPasswordChangeFailed');
101             }
102         }.bind(this));
103         req.go({
104             username: blerg.API.username,
105             password: oldpassword,
106             new_password: newpassword
107         });
108     },
109     loadUserRecords: function(username, from ,to) {
110         var url;
111         if (from != undefined && to != undefined) {
112             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
113         } else {
114             url = baseURL +  '/get/' + username;
115         }
116
117         var req = new enyo.Ajax({
118             url: url
119         });
120         req.response(function(inSender, inResponse) {
121             this.bubble('onItemsLoaded', {
122                 type: 'user',
123                 username: username,
124                 from: from,
125                 to: to,
126                 entries: inResponse
127             });
128         }.bind(this));
129         req.error(function(inSender, inResponse) {
130             if (inResponse == 404)
131                 this.bubble('onUserNotFound');
132             else
133                 this.bubble('onAPIError', {response: inResponse});
134         }.bind(this));
135         req.go();
136     },
137     loadTagRecords: function(type, tag) {
138         var url;
139         switch(type) {
140             case 'tag':
141                 // Apache eats the hash, even encoded.  Probably a security
142                 // feature.
143                 url = baseURL + '/tag/H' + tag;
144                 break;
145             case 'ref':
146                 url = baseURL + '/tag/%40' + tag;
147                 break;
148             default:
149                 throw new Error("Invalid tag type: " + type);
150                 return;
151         }
152         var req = new enyo.Ajax({
153             url: url
154         });
155         req.response(function(inSender, inResponse) {
156             this.bubble('onItemsLoaded', {
157                 type: 'tag',
158                 tagType: type,
159                 tag: tag,
160                 entries: inResponse
161             });
162         }.bind(this));
163         req.go();
164     },
165     getFeedInfo: function() {
166         if (!blerg.API.loggedIn)
167             throw new Error('Cannot request feed status when not logged in');
168
169         var req = new enyo.Ajax({
170             url: baseURL + '/feedinfo',
171             method: 'POST'
172         });
173         req.response(function(inSender, inResponse) {
174             this.bubble('onFeedInfo', inResponse);
175         }.bind(this));
176         req.go({
177             username: blerg.API.username
178         });
179     },
180     loadFeed: function() {
181         if (!blerg.API.loggedIn)
182             throw new Error('Cannot request feed status when not logged in');
183
184         var req = new enyo.Ajax({
185             url: baseURL + '/feed',
186             method: 'POST'
187         });
188         req.response(function(inSender, inResponse) {
189             this.bubble('onItemsLoaded', {
190                 type: "feed",
191                 entries: inResponse
192             });
193         }.bind(this));
194         req.go({
195             username: blerg.API.username
196         });
197     },
198     getSubscriptionStatus: function(username) {
199         var req = new enyo.Ajax({
200             url: baseURL + '/feedinfo/' + username,
201             method: 'POST'
202         });
203         req.response(function(inSender, inResponse) {
204             this.bubble('onSubscriptionStatus', {
205                 username: username,
206                 subscribed: inResponse.subscribed
207             });
208         }.bind(this));
209         req.go({
210             username: blerg.API.username
211         });
212     },
213     subscribe: function(username) {
214         var req = new enyo.Ajax({
215             url: baseURL + '/subscribe/' + username,
216             method: 'POST'
217         });
218         req.response(function(inSender, inResponse) {
219             this.bubble('onSubscriptionStatus', {
220                 username: username,
221                 subscribed: inResponse.status == "success"
222             });
223         }.bind(this));
224         req.go({
225             username: blerg.API.username
226         });
227     },
228     unsubscribe: function(username) {
229         var req = new enyo.Ajax({
230             url: baseURL + '/unsubscribe/' + username,
231             method: 'POST'
232         });
233         req.response(function(inSender, inResponse) {
234             this.bubble('onSubscriptionStatus', {
235                 username: username,
236                 subscribed: inResponse.status != "success"
237             });
238         }.bind(this));
239         req.go({
240             username: blerg.API.username
241         });
242     },
243     post: function(data) {
244         var req = new enyo.Ajax({
245             url: baseURL + '/put',
246             method: 'POST'
247         });
248         req.response(function(inSender, inResponse) {
249             if (inResponse && inResponse.status == 'success') {
250                 this.bubble('onPostSuccessful', {
251                     username: blerg.API.username,
252                     data: data
253                 });
254             } else {
255                 this.bubble('onPostFailed', {
256                     username: blerg.API.username,
257                     data: data
258                 });
259             }
260         }.bind(this));
261         req.go({
262             username: blerg.API.username,
263             data: data
264         });
265     }
266 });