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