Add Tag handler. Also convert all tabs to spaces.
[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     requestFeedStatus: function() {
72         if (!blerg.API.loggedIn)
73             throw new Error('Cannot request feed status when not logged in');
74         // TODO
75     },
76     loadUserRecords: function(username, from ,to) {
77         var url;
78         if (from != undefined && to != undefined) {
79             url = baseURL +  '/get/' + username + '/' + from + '-' + to;
80         } else {
81             url = baseURL +  '/get/' + username;
82         }
83
84         var req = new enyo.Ajax({
85             url: url
86         });
87         req.response(function(inSender, inResponse) {
88             this.bubble('onItemsLoaded', {
89                 type: 'user',
90                 username: username,
91                 from: from,
92                 to: to,
93                 entries: inResponse
94             });
95         }.bind(this));
96         req.go();
97     },
98     loadTagRecords: function(type, tag) {
99         var url;
100         switch(type) {
101             case 'tag':
102                 // Apache eats the hash, even encoded.  Probably a security
103                 // feature.
104                 url = baseURL + '/tag/H' + tag;
105                 break;
106             case 'ref':
107                 url = baseURL + '/tag/%40' + tag;
108                 break;
109             default:
110                 throw new Error("Invalid tag type: " + type);
111                 return;
112         }
113         var req = new enyo.Ajax({
114             url: url
115         });
116         req.response(function(inSender, inResponse) {
117             this.bubble('onItemsLoaded', {
118                 type: 'tag',
119                 tagType: type,
120                 tag: tag,
121                 entries: inResponse
122             });
123         }.bind(this));
124         req.go();
125     },
126     getSubscriptionStatus: function(username) {
127         var req = new enyo.Ajax({
128             url: baseURL + '/feedinfo/' + username,
129             method: 'POST'
130         });
131         req.response(function(inSender, inResponse) {
132             this.bubble('onSubscriptionStatus', {
133                 username: username,
134                 subscribed: inResponse.subscribed
135             });
136         }.bind(this));
137         req.go({
138             username: blerg.API.username
139         });
140     },
141     subscribe: function(username) {
142         var req = new enyo.Ajax({
143             url: baseURL + '/subscribe/' + username,
144             method: 'POST'
145         });
146         req.response(function(inSender, inResponse) {
147             this.bubble('onSubscriptionStatus', {
148                 username: username,
149                 subscribed: inResponse.status == "success"
150             });
151         }.bind(this));
152         req.go({
153             username: blerg.API.username
154         });
155     },
156     unsubscribe: function(username) {
157         var req = new enyo.Ajax({
158             url: baseURL + '/unsubscribe/' + username,
159             method: 'POST'
160         });
161         req.response(function(inSender, inResponse) {
162             this.bubble('onSubscriptionStatus', {
163                 username: username,
164                 subscribed: inResponse.status != "success"
165             });
166         }.bind(this));
167         req.go({
168             username: blerg.API.username
169         });
170     },
171 });