Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd5d4ef

Browse files
authored
fix: can not star bug also add test (leetcode-tools#49)
1 parenteded574 commitcd5d4ef

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

‎lib/plugins/leetcode.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -360,31 +360,25 @@ plugin.getSubmission = function(submission, cb) {
360360

361361
plugin.starProblem=function(problem,starred,cb){
362362
log.debug('running leetcode.starProblem');
363-
constopts=plugin.makeOpts();
363+
constuser=session.getUser();
364+
constoperationName=starred ?'addQuestionToFavorite' :'removeQuestionFromFavorite';
365+
constopts=plugin.makeOpts(config.sys.urls.graphql);
364366
opts.headers.Origin=config.sys.urls.base;
365367
opts.headers.Referer=problem.link;
366368

367-
constuser=session.getUser();
368-
if(starred){
369-
opts.url=config.sys.urls.favorites;
370-
opts.method='POST';
371-
opts.json=true;
372-
opts.body={
373-
favorite_id_hash:user.hash,
374-
question_id:problem.id
375-
};
376-
}else{
377-
opts.url=config.sys.urls.favorite_delete
378-
.replace('$hash',user.hash)
379-
.replace('$id',problem.id);
380-
opts.method='DELETE';
381-
}
369+
opts.json=true;
370+
opts.body={
371+
query:`mutation${operationName}($favoriteIdHash: String!, $questionId: String!) {\n${operationName}(favoriteIdHash: $favoriteIdHash, questionId: $questionId) {\n ok\n error\n favoriteIdHash\n questionId\n __typename\n }\n}\n`,
372+
variables:{favoriteIdHash:user.hash,questionId:''+problem.id},
373+
operationName:operationName
374+
};
382375

383-
request(opts,function(e,resp,body){
384-
e=plugin.checkError(e,resp,204);
376+
constspin=h.spin(starred?'star':'unstar'+'problem');
377+
request.post(opts,function(e,resp,body){
378+
spin.stop();
379+
e=plugin.checkError(e,resp,200);
385380
if(e)returncb(e);
386-
387-
cb(null,starred);
381+
returncb(null,starred);
388382
});
389383
};
390384

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":{"addQuestionToFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"AddQuestionToFavorite"}}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":{"removeQuestionFromFavorite":{"ok":true,"error":null,"favoriteIdHash":"","questionId":"389","__typename":"RemoveQuestionFromFavorite"}}}

‎test/plugins/test_leetcode.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,8 @@ describe('plugin:leetcode', function() {
508508
describe('#starProblem',function(){
509509
it('should star ok',function(done){
510510
nock('https://leetcode.com')
511-
.post('/list/api/questions')
512-
.reply(204,'');
511+
.post('/graphql')
512+
.replyWithFile(200,'./test/mock/find-the-difference-star.json.20200821');
513513

514514
plugin.starProblem(PROBLEM,true,function(e,starred){
515515
assert.equal(e,null);
@@ -520,8 +520,8 @@ describe('plugin:leetcode', function() {
520520

521521
it('should unstar ok',function(done){
522522
nock('https://leetcode.com')
523-
.delete('/list/api/questions/abcdef/389')
524-
.reply(204,'');
523+
.post('/graphql')
524+
.replyWithFile(200,'./test/mock/find-the-difference-unstar.json.20200821');
525525

526526
plugin.starProblem(PROBLEM,false,function(e,starred){
527527
assert.equal(e,null);
@@ -532,7 +532,7 @@ describe('plugin:leetcode', function() {
532532

533533
it('should star fail if http error',function(done){
534534
nock('https://leetcode.com')
535-
.post('/list/api/questions')
535+
.post('/graphql')
536536
.replyWithError('unknown error!');
537537

538538
plugin.starProblem(PROBLEM,true,function(e,starred){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp