@@ -46,27 +46,27 @@ function checkError(e, resp, expectedStatus, msg) {
46
46
// expired immediately. In that case we will try to re-login in
47
47
// the backend to give a seamless user experience.
48
48
function requestWithReLogin ( opts , cb ) {
49
- request ( opts , function ( e , resp , body ) {
49
+ var req = request ( opts , function ( e , resp , body ) {
50
50
e = checkError ( e , resp , opts . expectedStatus ) ;
51
51
52
52
// not 403: transparently pass down
53
53
if ( ! config . AUTO_LOGIN || ! e || e . statusCode !== 403 )
54
- return cb ( e , resp , body ) ;
54
+ return cb ( e , resp , body , req ) ;
55
55
56
56
// if 403: try re-login
57
57
log . debug ( 'session expired, auto re-login...' ) ;
58
58
59
59
var core = require ( './core' ) ;
60
60
var user = core . getUser ( ) ;
61
61
core . login ( user , function ( e2 , user ) {
62
- if ( e2 ) return cb ( e , resp , body ) ;
62
+ if ( e2 ) return cb ( e , resp , body , req ) ;
63
63
64
64
log . debug ( 'login successfully, cont\'d...' ) ;
65
65
signOpts ( opts , user ) ;
66
66
67
- request ( opts , function ( e , resp , body ) {
67
+ req = request ( opts , function ( e , resp , body ) {
68
68
e = checkError ( e , resp , opts . expectedStatus ) ;
69
- return cb ( e , resp , body ) ;
69
+ return cb ( e , resp , body , req ) ;
70
70
} ) ;
71
71
} ) ;
72
72
} ) ;
@@ -215,6 +215,19 @@ leetcodeClient.login = function(user, cb) {
215
215
} ) ;
216
216
} ;
217
217
218
+ leetcodeClient . getFavorites = function ( cb ) {
219
+ var opts = makeOpts ( ) ;
220
+ opts . method = 'GET' ;
221
+ opts . url = config . URL_FAVORITES ;
222
+
223
+ requestWithReLogin ( opts , function ( e , resp , body ) {
224
+ if ( e ) return cb ( e ) ;
225
+
226
+ var favorites = JSON . parse ( body ) ;
227
+ return cb ( null , favorites ) ;
228
+ } ) ;
229
+ } ;
230
+
218
231
function verifyResult ( opts , jobs , results , cb ) {
219
232
if ( jobs . length === 0 )
220
233
return cb ( null , results ) ;
@@ -305,18 +318,32 @@ leetcodeClient.submitProblem = function(problem, cb) {
305
318
} ) ;
306
319
} ;
307
320
308
- leetcodeClient . starProblem = function ( problem , starred , cb ) {
309
- var opts = makeOpts ( config . URL_STAR ) ;
310
- opts . method = ( starred ? 'POST' : 'DELETE' ) ;
321
+ leetcodeClient . starProblem = function ( user , problem , starred , cb ) {
322
+ var opts = makeOpts ( null , 204 ) ;
323
+ if ( starred ) {
324
+ opts . url = config . URL_FAVORITES ;
325
+ opts . method = 'POST' ;
326
+ opts . json = true ;
327
+ opts . body = {
328
+ 'favorite_id_hash' : user . hash ,
329
+ 'question_id' : problem . id
330
+ } ;
331
+ } else {
332
+ opts . url = config . URL_FAVORITE_DELETE
333
+ . replace ( '$hash' , user . hash )
334
+ . replace ( '$id' , problem . id ) ;
335
+ opts . method = 'DELETE' ;
336
+ }
311
337
opts . headers . Origin = config . URL_BASE ;
312
338
opts . headers . Referer = problem . link ;
313
- opts . json = true ;
314
- opts . body = { 'qid' : problem . id } ;
315
339
316
- requestWithReLogin ( opts , function ( e , resp , body ) {
317
- if ( e ) return cb ( e ) ;
340
+ requestWithReLogin ( opts , function ( e , resp , body , req ) {
341
+ // FIXME: not sure why we hit HPE_INVALID_CONSTANT error?
342
+ if ( req && req . response && req . response . statusCode === 204 )
343
+ return cb ( null , starred ) ;
318
344
319
- cb ( null , body . is_favor ) ;
345
+ if ( e ) return cb ( e ) ;
346
+ cb ( null , starred ) ;
320
347
} ) ;
321
348
} ;
322
349
0 commit comments