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')) {
25 blerg.API.loggedIn = true;
26 blerg.API.username = enyo.getCookie('auth').split('/')[0];
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 this.bubble('onLoginSuccessful', {username: username});
71 this.bubble('onLoginFailed');
74 req.error(this, function() {
75 this.bubble('onLoginFailed');
80 var req = new enyo.Ajax({
81 url: baseURL + '/logout',
84 var logout_func = function(inSender, inResponse) {
85 blerg.API.loggedIn = false;
86 blerg.API.username = '';
87 enyo.setCookie('auth', '', {"Max-Age": 0});
88 this.bubble('onLogoutSuccessful');
90 req.response(this, logout_func);
91 req.error(this, logout_func);
94 changePassword: function(oldpassword, newpassword) {
95 var req = new enyo.Ajax({
96 url: baseURL + '/passwd',
99 password: oldpassword,
100 new_password: newpassword
103 req.response(this, function(inSender, inResponse) {
104 if (inResponse.status == 'success') {
105 this.bubble('onPasswordChangeSuccessful');
107 this.bubble('onPasswordChangeFailed');
110 req.error(this, function() {
111 this.bubble('onPasswordChangeFailed');
115 loadUserRecords: function(username, from ,to) {
117 if (from != undefined && to != undefined) {
118 url = baseURL + '/get/' + username + '/' + from + '-' + to;
120 url = baseURL + '/get/' + username;
123 var req = new enyo.Ajax({
126 req.response(this, function(inSender, inResponse) {
127 this.bubble('onItemsLoaded', {
135 req.error(this, function(inSender, inResponse) {
136 if (inResponse == 404)
137 this.bubble('onUserNotFound');
139 this.bubble('onAPIError', {response: inResponse});
143 loadTagRecords: function(type, tag) {
147 // Apache eats the hash, even encoded. Probably a security
149 url = baseURL + '/tag/H' + tag;
152 url = baseURL + '/tag/%40' + tag;
155 throw new Error("Invalid tag type: " + type);
158 var req = new enyo.Ajax({
161 req.response(this, function(inSender, inResponse) {
162 this.bubble('onItemsLoaded', {
169 req.error(this, function() {
170 this.bubble('onItemsLoaded', {
179 getStatus: function() {
180 if (!blerg.API.loggedIn)
181 throw new Error('Cannot request feed status when not logged in');
183 var req = new enyo.Ajax({
184 url: baseURL + '/status'
186 req.response(this, function(inSender, inResponse) {
187 this.bubble('onStatus', inResponse);
191 clearStatus: function(type) {
192 if (!blerg.API.loggedIn)
193 throw new Error('Cannot request feed status when not logged in');
195 if (!(type == 'feed' || type == 'mentioned'))
196 throw new Error('Invalid status clear type: ' + type);
198 var req = new enyo.Ajax({
199 url: baseURL + '/status',
205 req.response(this, function(inSender, inResponse) {
206 inResponse.type = type;
207 this.bubble('onClearStatus', inResponse);
211 loadFeed: function() {
212 if (!blerg.API.loggedIn)
213 throw new Error('Cannot request feed status when not logged in');
215 var req = new enyo.Ajax({
216 url: baseURL + '/feed'
218 req.response(this, function(inSender, inResponse) {
219 this.bubble('onItemsLoaded', {
226 getSubscriptionStatus: function(username) {
227 var req = new enyo.Ajax({
228 url: baseURL + '/status/' + username
230 req.response(this, function(inSender, inResponse) {
231 this.bubble('onSubscriptionStatus', {
233 subscribed: inResponse.subscribed
238 subscription: function(username, v) {
239 var subv = v ? true : false;
240 var req = new enyo.Ajax({
241 url: baseURL + '/subscribe/' + username,
247 req.response(this, function(inSender, inResponse) {
248 this.bubble('onSubscriptionStatus', {
250 subscribed: inResponse.status == "success" && subv
255 subscribe: function(username) {
256 this.subscription(username, true);
258 unsubscribe: function(username) {
259 this.subscription(username, false);
261 post: function(data) {
262 var req = new enyo.Ajax({
263 url: baseURL + '/put',
269 req.response(this, function(inSender, inResponse) {
270 if (inResponse && inResponse.status == 'success') {
271 this.bubble('onPostSuccessful', {
272 username: blerg.API.username,
276 this.bubble('onPostFailed', {
277 username: blerg.API.username,