Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: premium questions are blocked #950

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createEnvOption } from "./utils/cpUtils";
import { DialogType, openUrl, promptForOpenOutputChannel } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";
import { getLeetCodeEndpoint } from "./commands/plugin";
import { globalState } from "./globalState";
import { globalState, UserDataType } from "./globalState";
import { queryUserData } from "./request/query-user-data";
import { parseQuery, sleep } from "./utils/toolUtils";

Expand All @@ -31,8 +31,16 @@ class LeetCodeManager extends EventEmitter {
public async getLoginStatus(): Promise<void> {
try {
const result: string = await leetCodeExecutor.getUserInfo();
this.currentUser = this.tryParseUserName(result);
const userInfo = this.tryParseUserInfo(result);
this.userStatus = UserStatus.SignedIn;
this.currentUser = userInfo.username;
const data: UserDataType | undefined = globalState.getUserStatus();
if (data) {
globalState.setUserStatus({
...data,
isPremium: userInfo.isPremium,
} as UserDataType);
}
} catch (error) {
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
Expand Down Expand Up @@ -93,14 +101,16 @@ class LeetCodeManager extends EventEmitter {
return this.currentUser;
}

private tryParseUserName(output: string): string {
const reg: RegExp = /^\s*.\s*(.+?)\s*https:\/\/leetcode/m;
private tryParseUserInfo(output: string): { username: string, isPremium: boolean } {
const reg: RegExp = /^\s*([✔✘]?)\s+(.+?)\s*https:\/\/leetcode/m;
const match: RegExpMatchArray | null = output.match(reg);
if (match && match.length === 2) {
return match[1].trim();
if (match && match.length === 3) {
const isPremium = match[1] === '✔';
const username = match[2].trim();
return { username, isPremium };
}

return "Unknown";
return { username: "Unknown", isPremium: false };
}

public getAuthLoginUrl(): string {
Expand Down