Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Add back user-id
Browse files Browse the repository at this point in the history
  • Loading branch information
samdev-7 committed Jul 1, 2024
1 parent 550a115 commit d111800
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 80 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@
"type": "boolean",
"default": true,
"description": "Remind you to start a session if you're coding without an active session."
},
"arcade-vsc.slackID": {
"type": "string",
"default": "",
"description": "Your Hack Club Slack ID. Get it from #what-is-my-slack-id"
}
}
},
"_comment": {
"arcade-vsc.slackID": {
"type": "string",
"default": "",
"description": "Your Hack Club Slack ID. Get it from #what-is-my-slack-id"
},
"_comment": "This section is used to add views to the sidebar.",
"viewsContainers": {
"activitybar": [
Expand Down
110 changes: 58 additions & 52 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ export type SessionData = {
goal: string;
};

export async function getSession(key: string): Promise<SessionData | null> {
export async function getSession(
key: string,
id: string
): Promise<SessionData | null> {
if (key === "") {
throw new Error("Error while fetching session: API key cannot be empty");
}
if (id === "") {
throw new Error("Error while fetching session: ID cannot be empty");
}

let resp: AxiosResponse<RawSessionData | RawSessionError>;
try {
resp = await axios.get(HH_ENDPOINT + "/api/session/", {
resp = await axios.get(HH_ENDPOINT + "/api/session/" + id, {
headers: {
Authorization: `Bearer ${key}`,
},
Expand Down Expand Up @@ -170,56 +176,56 @@ export type StatsData = {
total: number;
};

export async function getStats(key: string): Promise<StatsData | null> {
if (key === "") {
throw new Error("Error while fetching session: API key cannot be empty");
}

let resp: AxiosResponse<RawStatsData | RawStatsError>;
try {
resp = await axios.get(HH_ENDPOINT + "/api/stats/", {
headers: {
Authorization: `Bearer ${key}`,
},
});
} catch (err) {
if (err instanceof AxiosError) {
if (err.response === undefined) {
throw new Error(`Error while fetching session: ${err}`);
}

resp = err.response;
} else {
throw new Error(`Error while fetching session: ${err}`);
}
}

if (resp.status !== 200 && resp.status !== 404 && resp.status !== 401) {
throw new Error(
`Error while fetching session: Unexpected status code ${resp.status}`
);
}

const data = resp.data;

if (
!data.ok &&
(data.error === "User not found" || data.error === "Unauthorized")
) {
return null;
} else if (!data.ok) {
throw new Error(
`Error while fetching session: Unexpected result of ${JSON.stringify(
data
)}`
);
}

return {
sessions: data.data.sessions,
total: data.data.total,
};
}
// export async function getStats(key: string): Promise<StatsData | null> {
// if (key === "") {
// throw new Error("Error while fetching session: API key cannot be empty");
// }

// let resp: AxiosResponse<RawStatsData | RawStatsError>;
// try {
// resp = await axios.get(HH_ENDPOINT + "/api/stats/", {
// headers: {
// Authorization: `Bearer ${key}`,
// },
// });
// } catch (err) {
// if (err instanceof AxiosError) {
// if (err.response === undefined) {
// throw new Error(`Error while fetching session: ${err}`);
// }

// resp = err.response;
// } else {
// throw new Error(`Error while fetching session: ${err}`);
// }
// }

// if (resp.status !== 200 && resp.status !== 404 && resp.status !== 401) {
// throw new Error(
// `Error while fetching session: Unexpected status code ${resp.status}`
// );
// }

// const data = resp.data;

// if (
// !data.ok &&
// (data.error === "User not found" || data.error === "Unauthorized")
// ) {
// return null;
// } else if (!data.ok) {
// throw new Error(
// `Error while fetching session: Unexpected result of ${JSON.stringify(
// data
// )}`
// );
// }

// return {
// sessions: data.data.sessions,
// total: data.data.total,
// };
// }

type RawStartError = {
ok: false;
Expand Down
34 changes: 17 additions & 17 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import * as vscode from "vscode";

// export async function saveID(id: string): Promise<void> {
// await vscode.workspace
// .getConfiguration()
// .update("arcade-vsc.slackID", id, true);
// }

// export async function getID(): Promise<string> {
// return await vscode.workspace
// .getConfiguration()
// .get("arcade-vsc.slackID", "");
// }

// export async function clearID(): Promise<void> {
// await vscode.workspace
// .getConfiguration()
// .update("arcade-vsc.slackID", undefined, true);
// }
export async function saveID(id: string): Promise<void> {
await vscode.workspace
.getConfiguration()
.update("arcade-vsc.slackID", id, true);
}

export async function getID(): Promise<string> {
return await vscode.workspace
.getConfiguration()
.get("arcade-vsc.slackID", "");
}

export async function clearID(): Promise<void> {
await vscode.workspace
.getConfiguration()
.update("arcade-vsc.slackID", undefined, true);
}

export async function showSessionNotifications(): Promise<boolean> {
return await vscode.workspace
Expand Down
47 changes: 41 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand("arcade-vsc.init", async () => {
const prevID = await config.getID();

let id = await vscode.window.showInputBox({
prompt: "Enter your user ID from the Hack Club Slack",
placeHolder: !!prevID
? `This will overwrite your current ID: (${prevID})`
: "You can get this from #what-is-my-slack-id",
validateInput: (input: string) =>
!!input && /^[A-Z0-9]{5,}$/.test(input)
? ""
: "Please enter a valid ID (not your username or API key)",
});

if (!id) {
vscode.window.showInformationMessage(
"Exited Arcade setup without saving as no ID was provided."
);
return;
}

const prevKey = await config.getApiKey(context);

let key = await vscode.window.showInputBox({
Expand All @@ -48,30 +68,38 @@ export async function activate(context: vscode.ExtensionContext) {
let session: api.SessionData | null = null;

try {
session = await api.retrier(() => api.getSession(key), "getSession");
session = await api.retrier(
() => api.getSession(key, id),
"getSession"
);
} catch (err: unknown) {
vscode.window.showErrorMessage(`Failed to validate API key: ${err}`);
vscode.window.showErrorMessage(
`Failed to validate information key: ${err}`
);
return;
}

if (session === null) {
vscode.window.showErrorMessage(
"Your API KEY is invalid! Please make sure you have at least one arcade session and try again."
"Your saved info is invalid! Please make sure you have at least one arcade session and try again."
);
return;
}

vscode.window.showInformationMessage(
"Your API key has been securely saved locally."
"Your info has been securely saved locally."
);
config.saveApiKey(context, key);
config.saveID(id);
statusBar.setLoading();
loop(context, Infinity, undefined, true);
})
);

context.subscriptions.push(
vscode.commands.registerCommand("arcade-vsc.clear", async () => {
await config.clearApiKey(context);
await config.clearID();
vscode.window.showInformationMessage("Cleared saved data");
})
);
Expand All @@ -94,6 +122,12 @@ export async function activate(context: vscode.ExtensionContext) {
statusBar.setSetup();
return;
}
config.getID().then(async (id) => {
if (id === "" || id === undefined) {
statusBar.setSetup();
return;
}
});

// let session: api.SessionData | null = null;

Expand Down Expand Up @@ -157,8 +191,9 @@ async function loop(
let loopInterval = 1000;

const key = await config.getApiKey(context);
const id = await config.getID();

if (key === "" || key === undefined) {
if (key === "" || key === undefined || id === "" || id === undefined) {
statusBar.setSetup();
loop(context, sinceLastFetch + loopInterval, prevSession);
return;
Expand All @@ -177,7 +212,7 @@ async function loop(
) {
console.log("fetching session");
try {
session = await api.getSession(key);
session = await api.getSession(key, id);
} catch (err: unknown) {
loopInterval = Math.min(
FETCH_RETRY_CAP,
Expand Down

0 comments on commit d111800

Please sign in to comment.