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 getStatus: 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 + '/status',
192 username: blerg.API.username
195 req.response(this, function(inSender, inResponse) {
196 this.bubble('onStatus', inResponse);
200 clearStatus: function(type) {
201 if (!blerg.API.loggedIn)
202 throw new Error('Cannot request feed status when not logged in');
204 if (!(type == 'feed' || type == 'mentioned'))
205 throw new Error('Invalid status clear type: ' + type);
207 var req = new enyo.Ajax({
208 url: baseURL + '/status',
211 username: blerg.API.username,
215 req.response(this, function(inSender, inResponse) {
216 inResponse.type = type;
217 this.bubble('onClearStatus', inResponse);
221 loadFeed: function() {
222 if (!blerg.API.loggedIn)
223 throw new Error('Cannot request feed status when not logged in');
225 var req = new enyo.Ajax({
226 url: baseURL + '/feed',
229 username: blerg.API.username
232 req.response(this, function(inSender, inResponse) {
233 this.bubble('onItemsLoaded', {
240 getSubscriptionStatus: function(username) {
241 var req = new enyo.Ajax({
242 url: baseURL + '/status/' + username,
245 username: blerg.API.username
248 req.response(this, function(inSender, inResponse) {
249 this.bubble('onSubscriptionStatus', {
251 subscribed: inResponse.subscribed
256 subscribe: function(username) {
257 var req = new enyo.Ajax({
258 url: baseURL + '/subscribe/' + username,
261 username: blerg.API.username
264 req.response(this, function(inSender, inResponse) {
265 this.bubble('onSubscriptionStatus', {
267 subscribed: inResponse.status == "success"
272 unsubscribe: function(username) {
273 var req = new enyo.Ajax({
274 url: baseURL + '/unsubscribe/' + username,
277 username: blerg.API.username
280 req.response(this, function(inSender, inResponse) {
281 this.bubble('onSubscriptionStatus', {
283 subscribed: inResponse.status != "success"
288 post: function(data) {
289 var req = new enyo.Ajax({
290 url: baseURL + '/put',
293 username: blerg.API.username,
297 req.response(this, function(inSender, inResponse) {
298 if (inResponse && inResponse.status == 'success') {
299 this.bubble('onPostSuccessful', {
300 username: blerg.API.username,
304 this.bubble('onPostFailed', {
305 username: blerg.API.username,