Release notes for 1.9.1
[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     getFeedInfo: 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 + '/feedinfo',
190             method: 'POST',
191             postBody: {
192                 username: blerg.API.username
193             }
194         });
195         req.response(this, function(inSender, inResponse) {
196             this.bubble('onFeedInfo', inResponse);
197         });
198         req.go();
199     },
200     loadFeed: function() {
201         if (!blerg.API.loggedIn)
202             throw new Error('Cannot request feed status when not logged in');
203
204         var req = new enyo.Ajax({
205             url: baseURL + '/feed',
206             method: 'POST',
207             postBody: {
208                 username: blerg.API.username
209             }
210         });
211         req.response(this, function(inSender, inResponse) {
212             this.bubble('onItemsLoaded', {
213                 type: "feed",
214                 entries: inResponse
215             });
216         });
217         req.go();
218     },
219     getSubscriptionStatus: function(username) {
220         var req = new enyo.Ajax({
221             url: baseURL + '/feedinfo/' + username,
222             method: 'POST',
223             postBody: {
224                 username: blerg.API.username
225             }
226         });
227         req.response(this, function(inSender, inResponse) {
228             this.bubble('onSubscriptionStatus', {
229                 username: username,
230                 subscribed: inResponse.subscribed
231             });
232         });
233         req.go();
234     },
235     subscribe: function(username) {
236         var req = new enyo.Ajax({
237             url: baseURL + '/subscribe/' + username,
238             method: 'POST',
239             postBody: {
240                 username: blerg.API.username
241             }
242         });
243         req.response(this, function(inSender, inResponse) {
244             this.bubble('onSubscriptionStatus', {
245                 username: username,
246                 subscribed: inResponse.status == "success"
247             });
248         });
249         req.go();
250     },
251     unsubscribe: function(username) {
252         var req = new enyo.Ajax({
253             url: baseURL + '/unsubscribe/' + username,
254             method: 'POST',
255             postBody: {
256                 username: blerg.API.username
257             }
258         });
259         req.response(this, function(inSender, inResponse) {
260             this.bubble('onSubscriptionStatus', {
261                 username: username,
262                 subscribed: inResponse.status != "success"
263             });
264         });
265         req.go();
266     },
267     post: function(data) {
268         var req = new enyo.Ajax({
269             url: baseURL + '/put',
270             method: 'POST',
271             postBody: {
272                 username: blerg.API.username,
273                 data: data
274             }
275         });
276         req.response(this, function(inSender, inResponse) {
277             if (inResponse && inResponse.status == 'success') {
278                 this.bubble('onPostSuccessful', {
279                     username: blerg.API.username,
280                     data: data
281                 });
282             } else {
283                 this.bubble('onPostFailed', {
284                     username: blerg.API.username,
285                     data: data
286                 });
287             }
288         });
289         req.go();
290     }
291 });