203706a674da60477844ccca4312302ecb89885d
[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     login: function(username, password) {
36         var req = new enyo.Ajax({
37             url: baseURL + '/login',
38             method: 'POST'
39         });
40         req.response(function(inSender, inResponse) {
41             if (inResponse.status == 'success') {
42                 blerg.API.loggedIn = true;
43                 blerg.API.username = username;
44                 enyo.setCookie('username', username);
45                 this.bubble('onLoginSuccessful', {username: username});
46             } else {
47                 enyo.setCookie('username', '', {"Max-Age": 0});
48                 this.bubble('onLoginFailed');
49             }
50         }.bind(this));
51         req.go({
52             username: username,
53             password: password
54         });
55     },
56     logout: function() {
57         var req = new enyo.Ajax({
58             url: baseURL + '/logout',
59             method: 'POST'
60         });
61         req.response(function(inSender, inResponse) {
62             blerg.API.loggedIn = false;
63             enyo.setCookie('auth', '', {"Max-Age": 0});
64             this.bubble('onLogoutSuccessful');
65         }.bind(this));
66         req.go({
67             username: blerg.API.username
68         });
69         enyo.setCookie('username', '', {"Max-Age": 0});
70     },
71     loadUserRecords: function(username, from ,to) {
72         var url;
73         if (from != undefined && to != undefined) {
74             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
75         } else {
76             url = baseURL +  '/get/' + username;
77         }
78
79         var req = new enyo.Ajax({
80             url: url
81         });
82         req.response(function(inSender, inResponse) {
83             this.bubble('onItemsLoaded', {
84                 type: 'user',
85                 username: username,
86                 from: from,
87                 to: to,
88                 entries: inResponse
89             });
90         }.bind(this));
91         req.go();
92     },
93     loadTagRecords: function(type, tag) {
94         var url;
95         switch(type) {
96             case 'tag':
97                 // Apache eats the hash, even encoded.  Probably a security
98                 // feature.
99                 url = baseURL + '/tag/H' + tag;
100                 break;
101             case 'ref':
102                 url = baseURL + '/tag/%40' + tag;
103                 break;
104             default:
105                 throw new Error("Invalid tag type: " + type);
106                 return;
107         }
108         var req = new enyo.Ajax({
109             url: url
110         });
111         req.response(function(inSender, inResponse) {
112             this.bubble('onItemsLoaded', {
113                 type: 'tag',
114                 tagType: type,
115                 tag: tag,
116                 entries: inResponse
117             });
118         }.bind(this));
119         req.go();
120     },
121     getFeedInfo: function() {
122         if (!blerg.API.loggedIn)
123             throw new Error('Cannot request feed status when not logged in');
124
125         var req = new enyo.Ajax({
126             url: baseURL + '/feedinfo',
127             method: 'POST'
128         });
129         req.response(function(inSender, inResponse) {
130             this.bubble('onFeedInfo', inResponse);
131         }.bind(this));
132         req.go({
133             username: blerg.API.username
134         });
135     },
136     loadFeed: function() {
137         if (!blerg.API.loggedIn)
138             throw new Error('Cannot request feed status when not logged in');
139
140         var req = new enyo.Ajax({
141             url: baseURL + '/feed',
142             method: 'POST'
143         });
144         req.response(function(inSender, inResponse) {
145             this.bubble('onItemsLoaded', {
146                 type: "feed",
147                 entries: inResponse
148             });
149         }.bind(this));
150         req.go({
151             username: blerg.API.username
152         });
153     },
154     getSubscriptionStatus: function(username) {
155         var req = new enyo.Ajax({
156             url: baseURL + '/feedinfo/' + username,
157             method: 'POST'
158         });
159         req.response(function(inSender, inResponse) {
160             this.bubble('onSubscriptionStatus', {
161                 username: username,
162                 subscribed: inResponse.subscribed
163             });
164         }.bind(this));
165         req.go({
166             username: blerg.API.username
167         });
168     },
169     subscribe: function(username) {
170         var req = new enyo.Ajax({
171             url: baseURL + '/subscribe/' + username,
172             method: 'POST'
173         });
174         req.response(function(inSender, inResponse) {
175             this.bubble('onSubscriptionStatus', {
176                 username: username,
177                 subscribed: inResponse.status == "success"
178             });
179         }.bind(this));
180         req.go({
181             username: blerg.API.username
182         });
183     },
184     unsubscribe: function(username) {
185         var req = new enyo.Ajax({
186             url: baseURL + '/unsubscribe/' + username,
187             method: 'POST'
188         });
189         req.response(function(inSender, inResponse) {
190             this.bubble('onSubscriptionStatus', {
191                 username: username,
192                 subscribed: inResponse.status != "success"
193             });
194         }.bind(this));
195         req.go({
196             username: blerg.API.username
197         });
198     },
199 });