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',
40 req.response(function(inSender, inResponse) {
41 if (inResponse.status == 'success') {
42 this.bubble('onSignupSuccess', {username: username});
44 this.bubble('onSignupFailure', {username: username});
47 req.error(function() {
48 this.bubble('onSignupFailure', {username: username});
55 login: function(username, password) {
56 var req = new enyo.Ajax({
57 url: baseURL + '/login',
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});
67 enyo.setCookie('username', '', {"Max-Age": 0});
68 this.bubble('onLoginFailed');
77 var req = new enyo.Ajax({
78 url: baseURL + '/logout',
81 req.response(function(inSender, inResponse) {
82 blerg.API.loggedIn = false;
83 enyo.setCookie('auth', '', {"Max-Age": 0});
84 this.bubble('onLogoutSuccessful');
87 username: blerg.API.username
89 enyo.setCookie('username', '', {"Max-Age": 0});
91 changePassword: function(oldpassword, newpassword) {
92 var req = new enyo.Ajax({
93 url: baseURL + '/passwd',
96 req.response(function(inSender, inResponse) {
97 if (inResponse.status == 'success') {
98 this.bubble('onPasswordChangeSuccessful');
100 this.bubble('onPasswordChangeFailed');
104 username: blerg.API.username,
105 password: oldpassword,
106 new_password: newpassword
109 loadUserRecords: function(username, from ,to) {
111 if (from != undefined && to != undefined) {
112 url = baseURL + '/get/' + username + '/' + from + '-' + to;
114 url = baseURL + '/get/' + username;
117 var req = new enyo.Ajax({
120 req.response(function(inSender, inResponse) {
121 this.bubble('onItemsLoaded', {
129 req.error(function(inSender, inResponse) {
130 if (inResponse == 404)
131 this.bubble('onUserNotFound');
133 this.bubble('onAPIError', {response: inResponse});
137 loadTagRecords: function(type, tag) {
141 // Apache eats the hash, even encoded. Probably a security
143 url = baseURL + '/tag/H' + tag;
146 url = baseURL + '/tag/%40' + tag;
149 throw new Error("Invalid tag type: " + type);
152 var req = new enyo.Ajax({
155 req.response(function(inSender, inResponse) {
156 this.bubble('onItemsLoaded', {
165 getFeedInfo: function() {
166 if (!blerg.API.loggedIn)
167 throw new Error('Cannot request feed status when not logged in');
169 var req = new enyo.Ajax({
170 url: baseURL + '/feedinfo',
173 req.response(function(inSender, inResponse) {
174 this.bubble('onFeedInfo', inResponse);
177 username: blerg.API.username
180 loadFeed: function() {
181 if (!blerg.API.loggedIn)
182 throw new Error('Cannot request feed status when not logged in');
184 var req = new enyo.Ajax({
185 url: baseURL + '/feed',
188 req.response(function(inSender, inResponse) {
189 this.bubble('onItemsLoaded', {
195 username: blerg.API.username
198 getSubscriptionStatus: function(username) {
199 var req = new enyo.Ajax({
200 url: baseURL + '/feedinfo/' + username,
203 req.response(function(inSender, inResponse) {
204 this.bubble('onSubscriptionStatus', {
206 subscribed: inResponse.subscribed
210 username: blerg.API.username
213 subscribe: function(username) {
214 var req = new enyo.Ajax({
215 url: baseURL + '/subscribe/' + username,
218 req.response(function(inSender, inResponse) {
219 this.bubble('onSubscriptionStatus', {
221 subscribed: inResponse.status == "success"
225 username: blerg.API.username
228 unsubscribe: function(username) {
229 var req = new enyo.Ajax({
230 url: baseURL + '/unsubscribe/' + username,
233 req.response(function(inSender, inResponse) {
234 this.bubble('onSubscriptionStatus', {
236 subscribed: inResponse.status != "success"
240 username: blerg.API.username
243 post: function(data) {
244 var req = new enyo.Ajax({
245 url: baseURL + '/put',
248 req.response(function(inSender, inResponse) {
249 if (inResponse && inResponse.status == 'success') {
250 this.bubble('onPostSuccessful', {
251 username: blerg.API.username,
255 this.bubble('onPostFailed', {
256 username: blerg.API.username,
262 username: blerg.API.username,