072eec53398e9bfd6d1b8d67ac00fcbe152b9e6d
[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                                 entries: inResponse
90                         });
91                 }.bind(this));
92                 req.go();
93         },
94         getSubscriptionStatus: function(username) {
95                 var req = new enyo.Ajax({
96                         url: baseURL + '/feedinfo/' + username,
97                         method: 'POST'
98                 });
99                 req.response(function(inSender, inResponse) {
100                         this.bubble('onSubscriptionStatus', {
101                                 username: username,
102                                 subscribed: inResponse.subscribed
103                         });
104                 }.bind(this));
105                 req.go({
106                         username: blerg.API.username
107                 });
108         },
109         subscribe: function(username) {
110                 var req = new enyo.Ajax({
111                         url: baseURL + '/subscribe/' + username,
112                         method: 'POST'
113                 });
114                 req.response(function(inSender, inResponse) {
115                         this.bubble('onSubscriptionStatus', {
116                                 username: username,
117                                 subscribed: inResponse.status == "success"
118                         });
119                 }.bind(this));
120                 req.go({
121                         username: blerg.API.username
122                 });
123         },
124         unsubscribe: function(username) {
125                 var req = new enyo.Ajax({
126                         url: baseURL + '/unsubscribe/' + username,
127                         method: 'POST'
128                 });
129                 req.response(function(inSender, inResponse) {
130                         this.bubble('onSubscriptionStatus', {
131                                 username: username,
132                                 subscribed: inResponse.status != "success"
133                         });
134                 }.bind(this));
135                 req.go({
136                         username: blerg.API.username
137                 });
138         },
139 });