feat: adding new friend session notification

This commit is contained in:
Chubby Granny Chaser
2025-05-10 17:43:09 +01:00
parent fee9cfb3e8
commit 216f813771
15 changed files with 286 additions and 414 deletions

View File

@@ -1 +1,3 @@
export * from "./download-manager";
export * from "./real-debrid";
export * from "./torbox";

View File

@@ -11,7 +11,7 @@ import { getUserData } from "./user/get-user-data";
import { db } from "@main/level";
import { levelKeys } from "@main/level/sublevels";
import type { Auth, User } from "@types";
import { WSManager } from "./ws-manager";
import { WSClient } from "./ws/ws-client";
interface HydraApiOptions {
needsAuth?: boolean;
@@ -102,8 +102,8 @@ export class HydraApi {
WindowManager.mainWindow.webContents.send("on-signin");
await clearGamesRemoteIds();
uploadGamesBatch();
WSManager.close();
WSManager.connect();
WSClient.close();
WSClient.connect();
}
}

View File

@@ -12,3 +12,6 @@ export * from "./7zip";
export * from "./game-files-manager";
export * from "./common-redist-manager";
export * from "./aria2";
export * from "./ws";
export * from "./system-path";
export * from "./library-sync";

View File

@@ -10,7 +10,7 @@ import icon from "@resources/icon.png?asset";
import { NotificationOptions, toXmlString } from "./xml";
import { logger } from "../logger";
import { WindowManager } from "../window-manager";
import type { Game, UserPreferences } from "@types";
import type { Game, GameStats, UserPreferences, UserProfile } from "@types";
import { db, levelKeys } from "@main/level";
import { restartAndInstallUpdate } from "@main/events/autoupdater/restart-and-install-update";
import { SystemPath } from "../system-path";
@@ -82,7 +82,7 @@ export const publishNotificationUpdateReadyToInstall = async (
};
export const publishNewFriendRequestNotification = async (
senderProfileImageUrl?: string
user: UserProfile
) => {
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
@@ -99,9 +99,26 @@ export const publishNewFriendRequestNotification = async (
}),
body: t("new_friend_request_description", {
ns: "notifications",
displayName: user.displayName,
}),
icon: senderProfileImageUrl
? await downloadImage(senderProfileImageUrl)
icon: user?.profileImageUrl
? await downloadImage(user.profileImageUrl)
: trayIcon,
}).show();
};
export const publishFriendStartedPlayingGameNotification = async (
friend: UserProfile,
game: GameStats
) => {
new Notification({
title: t("friend_started_playing_game", {
ns: "notifications",
displayName: friend.displayName,
}),
body: game.assets?.title,
icon: friend?.profileImageUrl
? await downloadImage(friend.profileImageUrl)
: trayIcon,
}).show();
};

View File

@@ -1,75 +0,0 @@
import { WebSocket } from "ws";
import { HydraApi } from "./hydra-api";
import { Envelope } from "@main/generated/envelope";
import { logger } from "./logger";
import { WindowManager } from "./window-manager";
export class WSManager {
private static ws: WebSocket | null = null;
private static reconnectInterval = 1000;
private static maxReconnectInterval = 30000;
private static reconnectAttempts = 0;
private static reconnecting = false;
static async connect() {
const { token } = await HydraApi.post<{ token: string }>("/auth/ws");
this.ws = new WebSocket(import.meta.env.MAIN_VITE_WS_URL, {
headers: {
Authorization: `Bearer ${token}`,
},
});
this.ws.on("open", () => {
logger.info("WS connected");
this.reconnectInterval = 1000;
this.reconnecting = false;
});
this.ws.on("message", (message) => {
const envelope = Envelope.fromBinary(
new Uint8Array(Buffer.from(message.toString()))
);
if (envelope.payload.oneofKind === "friendRequest") {
WindowManager.mainWindow?.webContents.send("on-sync-friend-requests", {
friendRequestCount: envelope.payload.friendRequest.friendRequestCount,
});
}
});
this.ws.on("close", () => {
logger.warn("WS closed. Attempting reconnect...");
this.tryReconnect();
});
this.ws.on("error", (err) => {
logger.error("WS error:", err);
this.tryReconnect();
});
}
private static async tryReconnect() {
if (this.reconnecting) return;
this.reconnecting = true;
this.reconnectAttempts++;
const waitTime = Math.min(
this.reconnectInterval * 2 ** this.reconnectAttempts,
this.maxReconnectInterval
);
logger.info(`Reconnecting in ${waitTime / 1000}s...`);
setTimeout(() => {
this.connect();
}, waitTime);
}
public static async close() {
if (this.ws) {
this.ws.close();
this.ws = null;
}
}
}