Add post 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     loadUserRecords: function(username, from ,to) {
92         var url;
93         if (from != undefined && to != undefined) {
94             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
95         } else {
96             url = baseURL +  '/get/' + username;
97         }
98
99         var req = new enyo.Ajax({
100             url: url
101         });
102         req.response(function(inSender, inResponse) {
103             this.bubble('onItemsLoaded', {
104                 type: 'user',
105                 username: username,
106                 from: from,
107                 to: to,
108                 entries: inResponse
109             });
110         }.bind(this));
111         req.error(function(inSender, inResponse) {
112             if (inResponse == 404)
113                 this.bubble('onUserNotFound');
114             else
115                 this.bubble('onAPIError', {response: inResponse});
116         }.bind(this));
117         req.go();
118     },
119     loadTagRecords: function(type, tag) {
120         var url;
121         switch(type) {
122             case 'tag':
123                 // Apache eats the hash, even encoded.  Probably a security
124                 // feature.
125                 url = baseURL + '/tag/H' + tag;
126                 break;
127             case 'ref':
128                 url = baseURL + '/tag/%40' + tag;
129                 break;
130             default:
131                 throw new Error("Invalid tag type: " + type);
132                 return;
133         }
134         var req = new enyo.Ajax({
135             url: url
136         });
137         req.response(function(inSender, inResponse) {
138             this.bubble('onItemsLoaded', {
139                 type: 'tag',
140                 tagType: type,
141                 tag: tag,
142                 entries: inResponse
143             });
144         }.bind(this));
145         req.go();
146     },
147     getFeedInfo: function() {
148         if (!blerg.API.loggedIn)
149             throw new Error('Cannot request feed status when not logged in');
150
151         var req = new enyo.Ajax({
152             url: baseURL + '/feedinfo',
153             method: 'POST'
154         });
155         req.response(function(inSender, inResponse) {
156             this.bubble('onFeedInfo', inResponse);
157         }.bind(this));
158         req.go({
159             username: blerg.API.username
160         });
161     },
162     loadFeed: function() {
163         if (!blerg.API.loggedIn)
164             throw new Error('Cannot request feed status when not logged in');
165
166         var req = new enyo.Ajax({
167             url: baseURL + '/feed',
168             method: 'POST'
169         });
170         req.response(function(inSender, inResponse) {
171             this.bubble('onItemsLoaded', {
172                 type: "feed",
173                 entries: inResponse
174             });
175         }.bind(this));
176         req.go({
177             username: blerg.API.username
178         });
179     },
180     getSubscriptionStatus: function(username) {
181         var req = new enyo.Ajax({
182             url: baseURL + '/feedinfo/' + username,
183             method: 'POST'
184         });
185         req.response(function(inSender, inResponse) {
186             this.bubble('onSubscriptionStatus', {
187                 username: username,
188                 subscribed: inResponse.subscribed
189             });
190         }.bind(this));
191         req.go({
192             username: blerg.API.username
193         });
194     },
195     subscribe: function(username) {
196         var req = new enyo.Ajax({
197             url: baseURL + '/subscribe/' + username,
198             method: 'POST'
199         });
200         req.response(function(inSender, inResponse) {
201             this.bubble('onSubscriptionStatus', {
202                 username: username,
203                 subscribed: inResponse.status == "success"
204             });
205         }.bind(this));
206         req.go({
207             username: blerg.API.username
208         });
209     },
210     unsubscribe: function(username) {
211         var req = new enyo.Ajax({
212             url: baseURL + '/unsubscribe/' + username,
213             method: 'POST'
214         });
215         req.response(function(inSender, inResponse) {
216             this.bubble('onSubscriptionStatus', {
217                 username: username,
218                 subscribed: inResponse.status != "success"
219             });
220         }.bind(this));
221         req.go({
222             username: blerg.API.username
223         });
224     },
225     post: function(data) {
226         var req = new enyo.Ajax({
227             url: baseURL + '/put',
228             method: 'POST'
229         });
230         req.response(function(inSender, inResponse) {
231             if (inResponse && inResponse.status == 'success') {
232                 this.bubble('onPostSuccessful', {
233                     username: blerg.API.username,
234                     data: data
235                 });
236             } else {
237                 this.bubble('onPostFailed', {
238                     username: blerg.API.username,
239                     data: data
240                 });
241             }
242         }.bind(this));
243         req.go({
244             username: blerg.API.username,
245             data: data
246         });
247     }
248 });