3 // The API state is static so that any instance can use login-dependent API
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});
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});
33 blerg.API.apiInitialized = true;
35 signup: function(username, password) {
36 var req = new enyo.Ajax({
37 url: baseURL + '/create',
44 req.response(this, function(inSender, inResponse) {
45 if (inResponse.status == 'success') {
46 this.bubble('onSignupSuccess', {username: username});
48 this.bubble('onSignupFailure', {username: username});
51 req.error(this, function() {
52 this.bubble('onSignupFailure', {username: username});
56 login: function(username, password) {
57 var req = new enyo.Ajax({
58 url: baseURL + '/login',
65 req.response(this, function(inSender, inResponse) {
66 if (inResponse.status == 'success') {
67 blerg.API.loggedIn = true;
68 blerg.API.username = username;
69 enyo.setCookie('username', username);
70 this.bubble('onLoginSuccessful', {username: username});
72 enyo.setCookie('username', '', {"Max-Age": 0});
73 this.bubble('onLoginFailed');
76 req.error(this, function() {
77 enyo.setCookie('username', '', {"Max-Age": 0});
78 this.bubble('onLoginFailed');
83 var req = new enyo.Ajax({
84 url: baseURL + '/logout',
87 username: blerg.API.username
90 req.response(this, function(inSender, inResponse) {
91 blerg.API.loggedIn = false;
92 enyo.setCookie('auth', '', {"Max-Age": 0});
93 this.bubble('onLogoutSuccessful');
96 enyo.setCookie('username', '', {"Max-Age": 0});
98 changePassword: function(oldpassword, newpassword) {
99 var req = new enyo.Ajax({
100 url: baseURL + '/passwd',
103 username: blerg.API.username,
104 password: oldpassword,
105 new_password: newpassword
108 req.response(this, function(inSender, inResponse) {
109 if (inResponse.status == 'success') {
110 this.bubble('onPasswordChangeSuccessful');
112 this.bubble('onPasswordChangeFailed');
115 req.error(this, function() {
116 this.bubble('onPasswordChangeFailed');
120 loadUserRecords: function(username, from ,to) {
122 if (from != undefined && to != undefined) {
123 url = baseURL + '/get/' + username + '/' + from + '-' + to;
125 url = baseURL + '/get/' + username;
128 var req = new enyo.Ajax({
131 req.response(this, function(inSender, inResponse) {
132 this.bubble('onItemsLoaded', {
140 req.error(this, function(inSender, inResponse) {
141 if (inResponse == 404)
142 this.bubble('onUserNotFound');
144 this.bubble('onAPIError', {response: inResponse});
148 loadTagRecords: function(type, tag) {
152 // Apache eats the hash, even encoded. Probably a security
154 url = baseURL + '/tag/H' + tag;
157 url = baseURL + '/tag/%40' + tag;
160 throw new Error("Invalid tag type: " + type);
163 var req = new enyo.Ajax({
166 req.response(this, function(inSender, inResponse) {
167 this.bubble('onItemsLoaded', {
174 req.error(this, function() {
175 this.bubble('onItemsLoaded', {
184 getFeedInfo: function() {
185 if (!blerg.API.loggedIn)
186 throw new Error('Cannot request feed status when not logged in');
188 var req = new enyo.Ajax({
189 url: baseURL + '/feedinfo',
192 username: blerg.API.username
195 req.response(this, function(inSender, inResponse) {
196 this.bubble('onFeedInfo', inResponse);
200 loadFeed: function() {
201 if (!blerg.API.loggedIn)
202 throw new Error('Cannot request feed status when not logged in');
204 var req = new enyo.Ajax({
205 url: baseURL + '/feed',
208 username: blerg.API.username
211 req.response(this, function(inSender, inResponse) {
212 this.bubble('onItemsLoaded', {
219 getSubscriptionStatus: function(username) {
220 var req = new enyo.Ajax({
221 url: baseURL + '/feedinfo/' + username,
224 username: blerg.API.username
227 req.response(this, function(inSender, inResponse) {
228 this.bubble('onSubscriptionStatus', {
230 subscribed: inResponse.subscribed
235 subscribe: function(username) {
236 var req = new enyo.Ajax({
237 url: baseURL + '/subscribe/' + username,
240 username: blerg.API.username
243 req.response(this, function(inSender, inResponse) {
244 this.bubble('onSubscriptionStatus', {
246 subscribed: inResponse.status == "success"
251 unsubscribe: function(username) {
252 var req = new enyo.Ajax({
253 url: baseURL + '/unsubscribe/' + username,
256 username: blerg.API.username
259 req.response(this, function(inSender, inResponse) {
260 this.bubble('onSubscriptionStatus', {
262 subscribed: inResponse.status != "success"
267 post: function(data) {
268 var req = new enyo.Ajax({
269 url: baseURL + '/put',
272 username: blerg.API.username,
276 req.response(this, function(inSender, inResponse) {
277 if (inResponse && inResponse.status == 'success') {
278 this.bubble('onPostSuccessful', {
279 username: blerg.API.username,
283 this.bubble('onPostFailed', {
284 username: blerg.API.username,