From b344a1850a1a9e0b03a5e7f0652cb230d86b32ca Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Mon, 10 Mar 2025 18:57:13 -0300 Subject: [PATCH] feat: auto install default false on linux and friend request notification --- .../autoupdater/restart-and-install-update.ts | 10 ++++-- .../events/profile/sync-friend-requests.ts | 28 +++++++++++++--- src/main/services/notifications/index.ts | 19 +++++++++-- src/main/services/update-manager.ts | 32 +++++++++++++++---- .../src/pages/settings/settings-behavior.tsx | 17 ++++++++++ src/types/level.types.ts | 1 + 6 files changed, 92 insertions(+), 15 deletions(-) diff --git a/src/main/events/autoupdater/restart-and-install-update.ts b/src/main/events/autoupdater/restart-and-install-update.ts index afaade1a..7875f00c 100644 --- a/src/main/events/autoupdater/restart-and-install-update.ts +++ b/src/main/events/autoupdater/restart-and-install-update.ts @@ -4,11 +4,17 @@ import updater from "electron-updater"; const { autoUpdater } = updater; -const restartAndInstallUpdate = async (_event: Electron.IpcMainInvokeEvent) => { +export const restartAndInstallUpdate = async () => { autoUpdater.removeAllListeners(); if (app.isPackaged) { autoUpdater.quitAndInstall(false); } }; -registerEvent("restartAndInstallUpdate", restartAndInstallUpdate); +const restartAndInstallUpdateEvent = async ( + _event: Electron.IpcMainInvokeEvent +) => { + restartAndInstallUpdate(); +}; + +registerEvent("restartAndInstallUpdate", restartAndInstallUpdateEvent); diff --git a/src/main/events/profile/sync-friend-requests.ts b/src/main/events/profile/sync-friend-requests.ts index ffe995e6..ca982c46 100644 --- a/src/main/events/profile/sync-friend-requests.ts +++ b/src/main/events/profile/sync-friend-requests.ts @@ -1,17 +1,35 @@ import { registerEvent } from "../register-event"; import { HydraApi } from "@main/services"; +import { publishNewFriendRequestNotification } from "@main/services/notifications"; import { UserNotLoggedInError } from "@shared"; import type { FriendRequestSync } from "@types"; +interface SyncState { + friendsRequest: number | null; +} + +const syncState: SyncState = { + friendsRequest: null, +}; + const syncFriendRequests = async (_event: Electron.IpcMainInvokeEvent) => { - return HydraApi.get(`/profile/friend-requests/sync`).catch( - (err) => { + return HydraApi.get(`/profile/friend-requests/sync`) + .then((res) => { + if ( + syncState.friendsRequest && + syncState.friendsRequest < res.friendRequestCount + ) { + publishNewFriendRequestNotification(); + } + + syncState.friendsRequest = res.friendRequestCount; + }) + .catch((err) => { if (err instanceof UserNotLoggedInError) { - return { friendRequests: [] }; + return { friendRequestCount: 0 } as FriendRequestSync; } throw err; - } - ); + }); }; registerEvent("syncFriendRequests", syncFriendRequests); diff --git a/src/main/services/notifications/index.ts b/src/main/services/notifications/index.ts index 63c666dc..355bf61d 100644 --- a/src/main/services/notifications/index.ts +++ b/src/main/services/notifications/index.ts @@ -12,6 +12,7 @@ import { logger } from "../logger"; import { WindowManager } from "../window-manager"; import type { Game, UserPreferences } from "@types"; import { db, levelKeys } from "@main/level"; +import { restartAndInstallUpdate } from "@main/events/autoupdater/restart-and-install-update"; async function downloadImage(url: string | null) { if (!url) return undefined; @@ -72,10 +73,24 @@ export const publishNotificationUpdateReadyToInstall = async ( ns: "notifications", }), icon: trayIcon, - }).show(); + }) + .on("click", () => { + restartAndInstallUpdate(); + }) + .show(); }; -export const publishNewFriendRequestNotification = async () => {}; +export const publishNewFriendRequestNotification = async () => { + new Notification({ + title: t("new_friend_request", { + ns: "notifications", + }), + body: t("You have received a new friend request", { + ns: "notifications", + }), + icon: trayIcon, + }); +}; export const publishCombinedNewAchievementNotification = async ( achievementCount, diff --git a/src/main/services/update-manager.ts b/src/main/services/update-manager.ts index 9a277dd7..31817ac9 100644 --- a/src/main/services/update-manager.ts +++ b/src/main/services/update-manager.ts @@ -1,11 +1,9 @@ import updater, { UpdateInfo } from "electron-updater"; import { logger, WindowManager } from "@main/services"; -import { AppUpdaterEvent } from "@types"; +import { AppUpdaterEvent, UserPreferences } from "@types"; import { app } from "electron"; import { publishNotificationUpdateReadyToInstall } from "@main/services/notifications"; - -const isAutoInstallAvailable = - process.platform !== "darwin" && process.env.PORTABLE_EXECUTABLE_FILE == null; +import { db, levelKeys } from "@main/level"; const { autoUpdater } = updater; const sendEventsForDebug = false; @@ -16,7 +14,7 @@ export class UpdateManager { private static checkTick = 0; private static mockValuesForDebug() { - this.sendEvent({ type: "update-available", info: { version: "1.3.0" } }); + this.sendEvent({ type: "update-available", info: { version: "3.3.1" } }); this.sendEvent({ type: "update-downloaded" }); } @@ -24,7 +22,27 @@ export class UpdateManager { WindowManager.mainWindow?.webContents.send("autoUpdaterEvent", event); } - public static checkForUpdates() { + private static async isAutoInstallEnabled() { + if (process.platform === "darwin") return false; + if (process.platform === "win32") { + return process.env.PORTABLE_EXECUTABLE_FILE == null; + } + + if (process.platform === "linux") { + const userPreferences = await db.get( + levelKeys.userPreferences, + { + valueEncoding: "json", + } + ); + + return userPreferences.enableAutoInstall === true; + } + + return false; + } + + public static async checkForUpdates() { autoUpdater .once("update-available", (info: UpdateInfo) => { this.sendEvent({ type: "update-available", info }); @@ -39,6 +57,8 @@ export class UpdateManager { } }); + const isAutoInstallAvailable = await this.isAutoInstallEnabled(); + if (app.isPackaged) { autoUpdater.autoDownload = isAutoInstallAvailable; autoUpdater.checkForUpdates().then((result) => { diff --git a/src/renderer/src/pages/settings/settings-behavior.tsx b/src/renderer/src/pages/settings/settings-behavior.tsx index db8174a5..0a619aa4 100644 --- a/src/renderer/src/pages/settings/settings-behavior.tsx +++ b/src/renderer/src/pages/settings/settings-behavior.tsx @@ -12,6 +12,7 @@ export function SettingsBehavior() { ); const [showRunAtStartup, setShowRunAtStartup] = useState(false); + const [showAutoInstall, setShowAutoInstall] = useState(true); const { updateUserPreferences } = useContext(settingsContext); @@ -20,6 +21,7 @@ export function SettingsBehavior() { runAtStartup: false, startMinimized: false, disableNsfwAlert: false, + enableAutoInstall: false, seedAfterDownloadComplete: false, showHiddenAchievementsDescription: false, }); @@ -34,6 +36,7 @@ export function SettingsBehavior() { runAtStartup: userPreferences.runAtStartup ?? false, startMinimized: userPreferences.startMinimized ?? false, disableNsfwAlert: userPreferences.disableNsfwAlert ?? false, + enableAutoInstall: userPreferences.enableAutoInstall ?? false, seedAfterDownloadComplete: userPreferences.seedAfterDownloadComplete ?? false, showHiddenAchievementsDescription: @@ -46,6 +49,10 @@ export function SettingsBehavior() { window.electron.isPortableVersion().then((isPortableVersion) => { setShowRunAtStartup(!isPortableVersion); }); + + if (window.electron.platform === "linux") { + setShowAutoInstall(true); + } }, []); const handleChange = (values: Partial) => { @@ -99,6 +106,16 @@ export function SettingsBehavior() { )} + {showAutoInstall && ( + { + handleChange({ enableAutoInstall: !form.enableAutoInstall }); + }} + /> + )} +