Skip to content

Commit

Permalink
Add moar logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore committed Jul 18, 2024
1 parent 723a7a1 commit d81e788
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"tailwindcss-animate": "^1.0.7",
"tauri-plugin-websocket-api": "github:tauri-apps/tauri-plugin-websocket#v1",
"tauri-plugin-window-state-api": "github:tauri-apps/tauri-plugin-window-state#v1",
"tauri-plugin-log": "github:tauri-apps/tauri-plugin-log#v1",
"uuid": "^9.0.1",
"zustand": "^4.5.2"
},
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/rpc/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum RPCEvent {
/** non-subscription event sent immediately after connecting, contains server information */
READY = "READY",
/** non-subscription event sent when there is an error, including command responses */

ERROR = "ERROR",
/** sent when a user in a subscribed voice channel speaks */
SPEAKING_START = "SPEAKING_START",
Expand Down
42 changes: 29 additions & 13 deletions apps/desktop/src/rpc/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { VoiceUser } from "@/types";
import { getVersion } from "@tauri-apps/api/app";
import { hash } from "@/utils/crypto";
import { invoke } from "@tauri-apps/api";
import { error, info, debug } from "tauri-plugin-log";

interface TokenResponse {
access_token: string;
Expand Down Expand Up @@ -101,7 +102,7 @@ class SocketManager {
* Setup the websocket connection and listen for messages
*/
async init(navigate: NavigateFunction) {
console.log("Init web socket manager");
info("[WEBSOCKET] Init manager");
this.disconnect();

this._navigate = navigate;
Expand Down Expand Up @@ -135,6 +136,7 @@ class SocketManager {
}

public disconnect() {
debug("[WEBSOCKET] Disconnecting socket");
this.socket?.disconnect();
this.isConnected = false;
}
Expand Down Expand Up @@ -180,6 +182,7 @@ class SocketManager {
// TODO: this has to be a bug in the upstream lib, we should get a proper code
// and not have to check the raw dawg string to see if something is wrong
if (typeof event === "string" && (event as string).includes("Connection reset without closing handshake")) {
error("[WEBSOCKET] Connection reset without closing handshake");
this.navigate("/error");

await this.unpin();
Expand All @@ -199,8 +202,10 @@ class SocketManager {
const acessToken = this.userdataStore.accessToken;

if (acessToken) {
debug("[WEBSOCKET] Found access token and attempting to login with it");
this.login(acessToken);
} else {
debug("[WEBSOCKET] No access token found attempting to login");
this.authenticate();
}
}
Expand Down Expand Up @@ -271,17 +276,26 @@ class SocketManager {

// we got a token back from discord let's fetch an access token
if (payload.cmd === RPCCommand.AUTHORIZE) {
debug("[WEBSOCKET] Got valid code back from discord, fetching access token...");
const { code } = payload.data;
const res = await fetch<TokenResponse>(`${API_URL}/token`, {
method: "POST",
body: Body.json({ code }),
});

// we need send the token to discord
this.userdataStore.setAccessToken(res.data.access_token);
try {
const res = await fetch<TokenResponse>(`${API_URL}/token`, {
method: "POST",
body: Body.json({ code }),
});

debug("[WEBSOCKET] Obtained access token api, attempting to login with it...");

// login with the token
this.login(res.data.access_token);
// we need send the token to discord
this.userdataStore.setAccessToken(res.data.access_token);

// login with the token
this.login(res.data.access_token);
} catch (e) {
error(`[WEBSOCKET] Error fetching token: ${JSON.stringify(e)}`);
this.navigate("/error");
}
}

// GET_SELECTED_VOICE_CHANNEL used to get the current voice channel the client is in
Expand All @@ -297,9 +311,9 @@ class SocketManager {
}
}

// console.log(payload);
// we are ready to do things cause we are fully authed
// we hit an auth error with discord
if (payload.cmd === RPCCommand.AUTHENTICATE && payload.evt === RPCEvent.ERROR) {
error(`[WEBSOCKET] Error authenticating with discord: ${JSON.stringify(payload.data)}`);
// they have a token from the old client id
if (payload.data.code === RPCErrors.INVALID_CLIENTID) {
this.userdataStore.removeAccessToken();
Expand All @@ -318,6 +332,7 @@ class SocketManager {
// track error metric
track(MetricNames.DiscordAuthed, 0);
} else if (payload?.cmd === RPCCommand.AUTHENTICATE) {
info("[WEBSOCKET] Logged into discord successfully");
// track success metric
track(MetricNames.DiscordAuthed, 1);

Expand Down Expand Up @@ -357,6 +372,7 @@ class SocketManager {

// when we move channels we get a new list of users
if (payload.cmd === RPCCommand.GET_CHANNEL) {
debug("[WEBSOCKET] Requesting channel for user");
this.requestUserChannel();
}
}
Expand Down Expand Up @@ -414,12 +430,12 @@ export const useSocket = () => {
return;
}

// if the socket is already initialized return
if (socketRef.current) {
console.log("Socket already initialized");
return;
}

console.log("Initializing websocket...");
info("[WEBSOCKET] Initializing websocket...");
const socketManager = new SocketManager();
socketManager.init(navigate);

Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d81e788

Please sign in to comment.