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 loadUserRecords: function(username, from ,to) {
93 if (from != undefined && to != undefined) {
94 url = baseURL + '/get/' + username + '/' + from + '-' + to;
96 url = baseURL + '/get/' + username;
99 var req = new enyo.Ajax({
102 req.response(function(inSender, inResponse) {
103 this.bubble('onItemsLoaded', {
111 req.error(function(inSender, inResponse) {
112 if (inResponse == 404)
113 this.bubble('onUserNotFound');
115 this.bubble('onAPIError', {response: inResponse});
119 loadTagRecords: function(type, tag) {
123 // Apache eats the hash, even encoded. Probably a security
125 url = baseURL + '/tag/H' + tag;
128 url = baseURL + '/tag/%40' + tag;
131 throw new Error("Invalid tag type: " + type);
134 var req = new enyo.Ajax({
137 req.response(function(inSender, inResponse) {
138 this.bubble('onItemsLoaded', {
147 getFeedInfo: function() {
148 if (!blerg.API.loggedIn)
149 throw new Error('Cannot request feed status when not logged in');
151 var req = new enyo.Ajax({
152 url: baseURL + '/feedinfo',
155 req.response(function(inSender, inResponse) {
156 this.bubble('onFeedInfo', inResponse);
159 username: blerg.API.username
162 loadFeed: function() {
163 if (!blerg.API.loggedIn)
164 throw new Error('Cannot request feed status when not logged in');
166 var req = new enyo.Ajax({
167 url: baseURL + '/feed',
170 req.response(function(inSender, inResponse) {
171 this.bubble('onItemsLoaded', {
177 username: blerg.API.username
180 getSubscriptionStatus: function(username) {
181 var req = new enyo.Ajax({
182 url: baseURL + '/feedinfo/' + username,
185 req.response(function(inSender, inResponse) {
186 this.bubble('onSubscriptionStatus', {
188 subscribed: inResponse.subscribed
192 username: blerg.API.username
195 subscribe: function(username) {
196 var req = new enyo.Ajax({
197 url: baseURL + '/subscribe/' + username,
200 req.response(function(inSender, inResponse) {
201 this.bubble('onSubscriptionStatus', {
203 subscribed: inResponse.status == "success"
207 username: blerg.API.username
210 unsubscribe: function(username) {
211 var req = new enyo.Ajax({
212 url: baseURL + '/unsubscribe/' + username,
215 req.response(function(inSender, inResponse) {
216 this.bubble('onSubscriptionStatus', {
218 subscribed: inResponse.status != "success"
222 username: blerg.API.username
225 post: function(data) {
226 var req = new enyo.Ajax({
227 url: baseURL + '/put',
230 req.response(function(inSender, inResponse) {
231 if (inResponse && inResponse.status == 'success') {
232 this.bubble('onPostSuccessful', {
233 username: blerg.API.username,
237 this.bubble('onPostFailed', {
238 username: blerg.API.username,
244 username: blerg.API.username,