@@ -46,27 +46,27 @@ function checkError(e, resp, expectedStatus, msg) {
4646// expired immediately. In that case we will try to re-login in
4747// the backend to give a seamless user experience.
4848function requestWithReLogin ( opts , cb ) {
49- request ( opts , function ( e , resp , body ) {
49+ var req = request ( opts , function ( e , resp , body ) {
5050 e = checkError ( e , resp , opts . expectedStatus ) ;
5151
5252 // not 403: transparently pass down
5353 if ( ! config . AUTO_LOGIN || ! e || e . statusCode !== 403 )
54- return cb ( e , resp , body ) ;
54+ return cb ( e , resp , body , req ) ;
5555
5656 // if 403: try re-login
5757 log . debug ( 'session expired, auto re-login...' ) ;
5858
5959 var core = require ( './core' ) ;
6060 var user = core . getUser ( ) ;
6161 core . login ( user , function ( e2 , user ) {
62- if ( e2 ) return cb ( e , resp , body ) ;
62+ if ( e2 ) return cb ( e , resp , body , req ) ;
6363
6464 log . debug ( 'login successfully, cont\'d...' ) ;
6565 signOpts ( opts , user ) ;
6666
67- request ( opts , function ( e , resp , body ) {
67+ req = request ( opts , function ( e , resp , body ) {
6868 e = checkError ( e , resp , opts . expectedStatus ) ;
69- return cb ( e , resp , body ) ;
69+ return cb ( e , resp , body , req ) ;
7070 } ) ;
7171 } ) ;
7272 } ) ;
@@ -215,6 +215,19 @@ leetcodeClient.login = function(user, cb) {
215215 } ) ;
216216} ;
217217
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+
218231function verifyResult ( opts , jobs , results , cb ) {
219232 if ( jobs . length === 0 )
220233 return cb ( null , results ) ;
@@ -305,18 +318,32 @@ leetcodeClient.submitProblem = function(problem, cb) {
305318 } ) ;
306319} ;
307320
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+ }
311337 opts . headers . Origin = config . URL_BASE ;
312338 opts . headers . Referer = problem . link ;
313- opts . json = true ;
314- opts . body = { 'qid' : problem . id } ;
315339
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 ) ;
318344
319- cb ( null , body . is_favor ) ;
345+ if ( e ) return cb ( e ) ;
346+ cb ( null , starred ) ;
320347 } ) ;
321348} ;
322349
0 commit comments