From dd23358a95651258ea72e0fdd84dad459c519b7c Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:48:52 -0300 Subject: [PATCH 1/4] feat: prevent api calls when user is not logged in --- src/main/events/library/add-game-to-library.ts | 13 +------------ .../events/library/remove-game-from-library.ts | 2 +- src/main/events/profile/get-me.ts | 2 ++ .../events/torrenting/start-game-download.ts | 13 +------------ src/main/events/user/get-user.ts | 2 ++ src/main/services/hydra-api.ts | 6 ++---- src/main/services/library-sync/create-game.ts | 16 +++++++++++++++- .../library-sync/update-game-playtime.ts | 4 +++- src/main/services/process-watcher.ts | 12 ++---------- src/renderer/src/app.tsx | 2 ++ src/renderer/src/hooks/use-user-details.ts | 10 ++++++++-- 11 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/main/events/library/add-game-to-library.ts b/src/main/events/library/add-game-to-library.ts index 8187d41e..1bda0c93 100644 --- a/src/main/events/library/add-game-to-library.ts +++ b/src/main/events/library/add-game-to-library.ts @@ -53,18 +53,7 @@ const addGameToLibrary = async ( const game = await gameRepository.findOne({ where: { objectID } }); - createGame(game!).then((response) => { - const { - id: remoteId, - playTimeInMilliseconds, - lastTimePlayed, - } = response.data; - - gameRepository.update( - { objectID }, - { remoteId, playTimeInMilliseconds, lastTimePlayed } - ); - }); + createGame(game!); }); }; diff --git a/src/main/events/library/remove-game-from-library.ts b/src/main/events/library/remove-game-from-library.ts index d7874b8f..9139696b 100644 --- a/src/main/events/library/remove-game-from-library.ts +++ b/src/main/events/library/remove-game-from-library.ts @@ -19,7 +19,7 @@ const removeGameFromLibrary = async ( const removeRemoveGameFromLibrary = async (gameId: number) => { const game = await gameRepository.findOne({ where: { id: gameId } }); - if (game?.remoteId) { + if (game?.remoteId && HydraApi.isLoggedIn()) { HydraApi.delete(`/games/${game.remoteId}`); } }; diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index 17eb3d38..e8aa72d8 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -8,6 +8,8 @@ import { logger } from "@main/services"; const getMe = async ( _event: Electron.IpcMainInvokeEvent ): Promise => { + if (!HydraApi.isLoggedIn()) return null; + return HydraApi.get(`/profile/me`) .then((response) => { const me = response.data; diff --git a/src/main/events/torrenting/start-game-download.ts b/src/main/events/torrenting/start-game-download.ts index 0aa01fc9..cea41596 100644 --- a/src/main/events/torrenting/start-game-download.ts +++ b/src/main/events/torrenting/start-game-download.ts @@ -95,18 +95,7 @@ const startGameDownload = async ( }, }); - createGame(updatedGame!).then((response) => { - const { - id: remoteId, - playTimeInMilliseconds, - lastTimePlayed, - } = response.data; - - gameRepository.update( - { objectID }, - { remoteId, playTimeInMilliseconds, lastTimePlayed } - ); - }); + createGame(updatedGame!); await downloadQueueRepository.delete({ game: { id: updatedGame!.id } }); await downloadQueueRepository.insert({ game: { id: updatedGame!.id } }); diff --git a/src/main/events/user/get-user.ts b/src/main/events/user/get-user.ts index 596df084..5b4dfae5 100644 --- a/src/main/events/user/get-user.ts +++ b/src/main/events/user/get-user.ts @@ -9,6 +9,8 @@ const getUser = async ( _event: Electron.IpcMainInvokeEvent, userId: string ): Promise => { + if (!HydraApi.isLoggedIn()) return null; + try { const response = await HydraApi.get(`/user/${userId}`); const profile = response.data; diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index fb8b9a28..505ea209 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -127,14 +127,12 @@ export class HydraApi { } private static async revalidateAccessTokenIfExpired() { - if (!this.userAuth.authToken) { - userAuthRepository.delete({ id: 1 }); - logger.error("user is not logged in"); - this.sendSignOutEvent(); + if (!this.isLoggedIn()) { throw new Error("user is not logged in"); } const now = new Date(); + if (this.userAuth.expirationTimestamp < now.getTime()) { try { const response = await this.instance.post(`/auth/refresh`, { diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 6e56a3de..8f935bef 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -1,11 +1,25 @@ import { Game } from "@main/entity"; import { HydraApi } from "../hydra-api"; +import { gameRepository } from "@main/repository"; export const createGame = async (game: Game) => { - return HydraApi.post(`/games`, { + if (!HydraApi.isLoggedIn()) return; + + HydraApi.post(`/games`, { objectId: game.objectID, playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds), shop: game.shop, lastTimePlayed: game.lastTimePlayed, + }).then((response) => { + const { + id: remoteId, + playTimeInMilliseconds, + lastTimePlayed, + } = response.data; + + gameRepository.update( + { objectID: game.objectID }, + { remoteId, playTimeInMilliseconds, lastTimePlayed } + ); }); }; diff --git a/src/main/services/library-sync/update-game-playtime.ts b/src/main/services/library-sync/update-game-playtime.ts index 190d7e7d..3a7cca22 100644 --- a/src/main/services/library-sync/update-game-playtime.ts +++ b/src/main/services/library-sync/update-game-playtime.ts @@ -6,7 +6,9 @@ export const updateGamePlaytime = async ( deltaInMillis: number, lastTimePlayed: Date ) => { - return HydraApi.put(`/games/${game.remoteId}`, { + if (!HydraApi.isLoggedIn()) return; + + HydraApi.put(`/games/${game.remoteId}`, { playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000), lastTimePlayed, }); diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index f20b4128..da7b909d 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -60,12 +60,7 @@ export const watchProcesses = async () => { if (game.remoteId) { updateGamePlaytime(game, 0, new Date()); } else { - createGame({ ...game, lastTimePlayed: new Date() }).then( - (response) => { - const { id: remoteId } = response.data; - gameRepository.update({ objectID: game.objectID }, { remoteId }); - } - ); + createGame({ ...game, lastTimePlayed: new Date() }); } gamesPlaytime.set(game.id, { @@ -84,10 +79,7 @@ export const watchProcesses = async () => { game.lastTimePlayed! ); } else { - createGame(game).then((response) => { - const { id: remoteId } = response.data; - gameRepository.update({ objectID: game.objectID }, { remoteId }); - }); + createGame(game); } } } diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index 0f9d3972..27fa2646 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -98,6 +98,8 @@ export function App() { fetchUserDetails().then((response) => { if (response) updateUserDetails(response); }); + } else { + clearUserDetails(); } }); }, [fetchUserDetails, updateUserDetails, dispatch]); diff --git a/src/renderer/src/hooks/use-user-details.ts b/src/renderer/src/hooks/use-user-details.ts index 1d8257f4..e87f8ff6 100644 --- a/src/renderer/src/hooks/use-user-details.ts +++ b/src/renderer/src/hooks/use-user-details.ts @@ -57,8 +57,14 @@ export function useUserDetails() { ); const fetchUserDetails = useCallback(async () => { - return window.electron.getMe(); - }, []); + return window.electron.getMe().then((userDetails) => { + if (userDetails == null) { + clearUserDetails(); + } + + return userDetails; + }); + }, [clearUserDetails]); const patchUser = useCallback( async (displayName: string, imageProfileUrl: string | null) => { From 9e11d6c098b1e5110359724fee0273dd272d1cde Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:55:38 -0300 Subject: [PATCH 2/4] feat: refactor hydra api --- .../library/remove-game-from-library.ts | 4 +-- src/main/events/misc/is-user-logged-in.ts | 8 ------ src/main/events/profile/get-me.ts | 10 +++---- src/main/events/profile/update-profile.ts | 14 +++++----- src/main/events/user/get-user.ts | 2 -- src/main/main.ts | 4 +-- src/main/services/hydra-api.ts | 17 ++++++++---- src/main/services/library-sync/create-game.ts | 26 +++++++++---------- .../library-sync/merge-with-remote-games.ts | 10 +------ .../library-sync/update-game-playtime.ts | 4 +-- .../library-sync/upload-games-batch.ts | 13 ++-------- src/preload/index.ts | 1 - src/renderer/src/app.tsx | 10 ++----- src/renderer/src/declaration.d.ts | 1 - src/types/index.ts | 7 +++++ 15 files changed, 55 insertions(+), 76 deletions(-) delete mode 100644 src/main/events/misc/is-user-logged-in.ts diff --git a/src/main/events/library/remove-game-from-library.ts b/src/main/events/library/remove-game-from-library.ts index 9139696b..dbb73ffc 100644 --- a/src/main/events/library/remove-game-from-library.ts +++ b/src/main/events/library/remove-game-from-library.ts @@ -19,8 +19,8 @@ const removeGameFromLibrary = async ( const removeRemoveGameFromLibrary = async (gameId: number) => { const game = await gameRepository.findOne({ where: { id: gameId } }); - if (game?.remoteId && HydraApi.isLoggedIn()) { - HydraApi.delete(`/games/${game.remoteId}`); + if (game?.remoteId) { + HydraApi.delete(`/games/${game.remoteId}`).catch(); } }; diff --git a/src/main/events/misc/is-user-logged-in.ts b/src/main/events/misc/is-user-logged-in.ts deleted file mode 100644 index f5f79e15..00000000 --- a/src/main/events/misc/is-user-logged-in.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { registerEvent } from "../register-event"; -import { HydraApi } from "@main/services"; - -const isUserLoggedIn = async (_event: Electron.IpcMainInvokeEvent) => { - return HydraApi.isLoggedIn(); -}; - -registerEvent("isUserLoggedIn", isUserLoggedIn); diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index e8aa72d8..e625ff1e 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -1,15 +1,12 @@ import { registerEvent } from "../register-event"; import * as Sentry from "@sentry/electron/main"; import { HydraApi } from "@main/services"; -import { UserProfile } from "@types"; +import { UserNotLoggedInError, UserProfile } from "@types"; import { userAuthRepository } from "@main/repository"; -import { logger } from "@main/services"; const getMe = async ( _event: Electron.IpcMainInvokeEvent ): Promise => { - if (!HydraApi.isLoggedIn()) return null; - return HydraApi.get(`/profile/me`) .then((response) => { const me = response.data; @@ -29,7 +26,10 @@ const getMe = async ( return me; }) .catch((err) => { - logger.error("getMe", err.message); + if (err instanceof UserNotLoggedInError) { + return null; + } + return userAuthRepository.findOne({ where: { id: 1 } }); }); }; diff --git a/src/main/events/profile/update-profile.ts b/src/main/events/profile/update-profile.ts index fbed0606..fe79d345 100644 --- a/src/main/events/profile/update-profile.ts +++ b/src/main/events/profile/update-profile.ts @@ -26,9 +26,11 @@ const updateProfile = async ( _event: Electron.IpcMainInvokeEvent, displayName: string, newProfileImagePath: string | null -): Promise => { +) => { if (!newProfileImagePath) { - return (await patchUserProfile(displayName)).data; + return patchUserProfile(displayName).then( + (response) => response.data as UserProfile + ); } const stats = fs.statSync(newProfileImagePath); @@ -51,11 +53,11 @@ const updateProfile = async ( }); return profileImageUrl; }) - .catch(() => { - return undefined; - }); + .catch(() => undefined); - return (await patchUserProfile(displayName, profileImageUrl)).data; + return patchUserProfile(displayName, profileImageUrl).then( + (response) => response.data as UserProfile + ); }; registerEvent("updateProfile", updateProfile); diff --git a/src/main/events/user/get-user.ts b/src/main/events/user/get-user.ts index 5b4dfae5..596df084 100644 --- a/src/main/events/user/get-user.ts +++ b/src/main/events/user/get-user.ts @@ -9,8 +9,6 @@ const getUser = async ( _event: Electron.IpcMainInvokeEvent, userId: string ): Promise => { - if (!HydraApi.isLoggedIn()) return null; - try { const response = await HydraApi.get(`/user/${userId}`); const profile = response.data; diff --git a/src/main/main.ts b/src/main/main.ts index 1abd8c2f..4bda8ad8 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -22,8 +22,8 @@ const loadState = async (userPreferences: UserPreferences | null) => { if (userPreferences?.realDebridApiToken) RealDebridClient.authorize(userPreferences?.realDebridApiToken); - HydraApi.setupApi().then(async () => { - if (HydraApi.isLoggedIn()) uploadGamesBatch(); + HydraApi.setupApi().then(() => { + uploadGamesBatch(); }); const [nextQueueItem] = await downloadQueueRepository.find({ diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index 505ea209..5aa6635d 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -5,6 +5,7 @@ import url from "url"; import { uploadGamesBatch } from "./library-sync"; import { clearGamesRemoteIds } from "./library-sync/clear-games-remote-id"; import { logger } from "./logger"; +import { UserNotLoggedInError } from "@types"; export class HydraApi { private static instance: AxiosInstance; @@ -19,7 +20,7 @@ export class HydraApi { expirationTimestamp: 0, }; - static isLoggedIn() { + private static isLoggedIn() { return this.userAuth.authToken !== ""; } @@ -127,10 +128,6 @@ export class HydraApi { } private static async revalidateAccessTokenIfExpired() { - if (!this.isLoggedIn()) { - throw new Error("user is not logged in"); - } - const now = new Date(); if (this.userAuth.expirationTimestamp < now.getTime()) { @@ -188,6 +185,8 @@ export class HydraApi { }; static async get(url: string) { + if (!this.isLoggedIn()) throw new UserNotLoggedInError(); + await this.revalidateAccessTokenIfExpired(); return this.instance .get(url, this.getAxiosConfig()) @@ -195,6 +194,8 @@ export class HydraApi { } static async post(url: string, data?: any) { + if (!this.isLoggedIn()) throw new UserNotLoggedInError(); + await this.revalidateAccessTokenIfExpired(); return this.instance .post(url, data, this.getAxiosConfig()) @@ -202,6 +203,8 @@ export class HydraApi { } static async put(url: string, data?: any) { + if (!this.isLoggedIn()) throw new UserNotLoggedInError(); + await this.revalidateAccessTokenIfExpired(); return this.instance .put(url, data, this.getAxiosConfig()) @@ -209,6 +212,8 @@ export class HydraApi { } static async patch(url: string, data?: any) { + if (!this.isLoggedIn()) throw new UserNotLoggedInError(); + await this.revalidateAccessTokenIfExpired(); return this.instance .patch(url, data, this.getAxiosConfig()) @@ -216,6 +221,8 @@ export class HydraApi { } static async delete(url: string) { + if (!this.isLoggedIn()) throw new UserNotLoggedInError(); + await this.revalidateAccessTokenIfExpired(); return this.instance .delete(url, this.getAxiosConfig()) diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 8f935bef..183b58a7 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -3,23 +3,23 @@ import { HydraApi } from "../hydra-api"; import { gameRepository } from "@main/repository"; export const createGame = async (game: Game) => { - if (!HydraApi.isLoggedIn()) return; - HydraApi.post(`/games`, { objectId: game.objectID, playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds), shop: game.shop, lastTimePlayed: game.lastTimePlayed, - }).then((response) => { - const { - id: remoteId, - playTimeInMilliseconds, - lastTimePlayed, - } = response.data; + }) + .then((response) => { + const { + id: remoteId, + playTimeInMilliseconds, + lastTimePlayed, + } = response.data; - gameRepository.update( - { objectID: game.objectID }, - { remoteId, playTimeInMilliseconds, lastTimePlayed } - ); - }); + gameRepository.update( + { objectID: game.objectID }, + { remoteId, playTimeInMilliseconds, lastTimePlayed } + ); + }) + .catch(); }; diff --git a/src/main/services/library-sync/merge-with-remote-games.ts b/src/main/services/library-sync/merge-with-remote-games.ts index 3a2db640..a0482c6c 100644 --- a/src/main/services/library-sync/merge-with-remote-games.ts +++ b/src/main/services/library-sync/merge-with-remote-games.ts @@ -2,8 +2,6 @@ import { gameRepository } from "@main/repository"; import { HydraApi } from "../hydra-api"; import { steamGamesWorker } from "@main/workers"; import { getSteamAppAsset } from "@main/helpers"; -import { logger } from "../logger"; -import { AxiosError } from "axios"; export const mergeWithRemoteGames = async () => { try { @@ -62,11 +60,5 @@ export const mergeWithRemoteGames = async () => { } } } - } catch (err) { - if (err instanceof AxiosError) { - logger.error("getRemoteGames", err.message); - } else { - logger.error("getRemoteGames", err); - } - } + } catch (err) {} }; diff --git a/src/main/services/library-sync/update-game-playtime.ts b/src/main/services/library-sync/update-game-playtime.ts index 3a7cca22..70d88d50 100644 --- a/src/main/services/library-sync/update-game-playtime.ts +++ b/src/main/services/library-sync/update-game-playtime.ts @@ -6,10 +6,8 @@ export const updateGamePlaytime = async ( deltaInMillis: number, lastTimePlayed: Date ) => { - if (!HydraApi.isLoggedIn()) return; - HydraApi.put(`/games/${game.remoteId}`, { playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000), lastTimePlayed, - }); + }).catch(); }; diff --git a/src/main/services/library-sync/upload-games-batch.ts b/src/main/services/library-sync/upload-games-batch.ts index 63eee82a..2f9f33ed 100644 --- a/src/main/services/library-sync/upload-games-batch.ts +++ b/src/main/services/library-sync/upload-games-batch.ts @@ -2,9 +2,6 @@ import { gameRepository } from "@main/repository"; import { chunk } from "lodash-es"; import { IsNull } from "typeorm"; import { HydraApi } from "../hydra-api"; -import { logger } from "../logger"; -import { AxiosError } from "axios"; - import { mergeWithRemoteGames } from "./merge-with-remote-games"; import { WindowManager } from "../window-manager"; @@ -27,18 +24,12 @@ export const uploadGamesBatch = async () => { lastTimePlayed: game.lastTimePlayed, }; }) - ); + ).catch(); } await mergeWithRemoteGames(); if (WindowManager.mainWindow) WindowManager.mainWindow.webContents.send("on-library-batch-complete"); - } catch (err) { - if (err instanceof AxiosError) { - logger.error("uploadGamesBatch", err.response, err.message); - } else { - logger.error("uploadGamesBatch", err); - } - } + } catch (err) {} }; diff --git a/src/preload/index.ts b/src/preload/index.ts index ecabe9e8..0cadbc03 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -112,7 +112,6 @@ contextBridge.exposeInMainWorld("electron", { getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"), isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"), openExternal: (src: string) => ipcRenderer.invoke("openExternal", src), - isUserLoggedIn: () => ipcRenderer.invoke("isUserLoggedIn"), showOpenDialog: (options: Electron.OpenDialogOptions) => ipcRenderer.invoke("showOpenDialog", options), platform: process.platform, diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index 27fa2646..afce9622 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -93,14 +93,8 @@ export function App() { dispatch(setProfileBackground(profileBackground)); } - window.electron.isUserLoggedIn().then((isLoggedIn) => { - if (isLoggedIn) { - fetchUserDetails().then((response) => { - if (response) updateUserDetails(response); - }); - } else { - clearUserDetails(); - } + fetchUserDetails().then((response) => { + if (response) updateUserDetails(response); }); }, [fetchUserDetails, updateUserDetails, dispatch]); diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index ae89bb8b..48fa7aae 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -100,7 +100,6 @@ declare global { /* Misc */ openExternal: (src: string) => Promise; - isUserLoggedIn: () => Promise; getVersion: () => Promise; ping: () => string; getDefaultDownloadsPath: () => Promise; diff --git a/src/types/index.ts b/src/types/index.ts index 71071620..9027af1e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -290,3 +290,10 @@ export interface DownloadSource { createdAt: Date; updatedAt: Date; } + +export class UserNotLoggedInError extends Error { + constructor() { + super("user not logged in"); + this.name = "UserNotLoggedInError"; + } +} From b8bd786c455bf849bb25c11f6c22c032a213a8fd Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Tue, 2 Jul 2024 15:42:23 -0300 Subject: [PATCH 3/4] feat: refactor --- .../library-sync/merge-with-remote-games.ts | 98 +++++++++---------- .../library-sync/upload-games-batch.ts | 42 ++++---- 2 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/main/services/library-sync/merge-with-remote-games.ts b/src/main/services/library-sync/merge-with-remote-games.ts index a0482c6c..937007dd 100644 --- a/src/main/services/library-sync/merge-with-remote-games.ts +++ b/src/main/services/library-sync/merge-with-remote-games.ts @@ -4,61 +4,61 @@ import { steamGamesWorker } from "@main/workers"; import { getSteamAppAsset } from "@main/helpers"; export const mergeWithRemoteGames = async () => { - try { - const games = await HydraApi.get("/games"); - - for (const game of games.data) { - const localGame = await gameRepository.findOne({ - where: { - objectID: game.objectId, - }, - }); - - if (localGame) { - const updatedLastTimePlayed = - localGame.lastTimePlayed == null || - (game.lastTimePlayed && - new Date(game.lastTimePlayed) > localGame.lastTimePlayed) - ? game.lastTimePlayed - : localGame.lastTimePlayed; - - const updatedPlayTime = - localGame.playTimeInMilliseconds < game.playTimeInMilliseconds - ? game.playTimeInMilliseconds - : localGame.playTimeInMilliseconds; - - gameRepository.update( - { + return HydraApi.get("/games") + .then(async (response) => { + for (const game of response.data) { + const localGame = await gameRepository.findOne({ + where: { objectID: game.objectId, - shop: "steam", }, - { - remoteId: game.id, - lastTimePlayed: updatedLastTimePlayed, - playTimeInMilliseconds: updatedPlayTime, - } - ); - } else { - const steamGame = await steamGamesWorker.run(Number(game.objectId), { - name: "getById", }); - if (steamGame) { - const iconUrl = steamGame?.clientIcon - ? getSteamAppAsset("icon", game.objectId, steamGame.clientIcon) - : null; + if (localGame) { + const updatedLastTimePlayed = + localGame.lastTimePlayed == null || + (game.lastTimePlayed && + new Date(game.lastTimePlayed) > localGame.lastTimePlayed) + ? game.lastTimePlayed + : localGame.lastTimePlayed; - gameRepository.insert({ - objectID: game.objectId, - title: steamGame?.name, - remoteId: game.id, - shop: game.shop, - iconUrl, - lastTimePlayed: game.lastTimePlayed, - playTimeInMilliseconds: game.playTimeInMilliseconds, + const updatedPlayTime = + localGame.playTimeInMilliseconds < game.playTimeInMilliseconds + ? game.playTimeInMilliseconds + : localGame.playTimeInMilliseconds; + + gameRepository.update( + { + objectID: game.objectId, + shop: "steam", + }, + { + remoteId: game.id, + lastTimePlayed: updatedLastTimePlayed, + playTimeInMilliseconds: updatedPlayTime, + } + ); + } else { + const steamGame = await steamGamesWorker.run(Number(game.objectId), { + name: "getById", }); + + if (steamGame) { + const iconUrl = steamGame?.clientIcon + ? getSteamAppAsset("icon", game.objectId, steamGame.clientIcon) + : null; + + gameRepository.insert({ + objectID: game.objectId, + title: steamGame?.name, + remoteId: game.id, + shop: game.shop, + iconUrl, + lastTimePlayed: game.lastTimePlayed, + playTimeInMilliseconds: game.playTimeInMilliseconds, + }); + } } } - } - } catch (err) {} + }) + .catch(); }; diff --git a/src/main/services/library-sync/upload-games-batch.ts b/src/main/services/library-sync/upload-games-batch.ts index 2f9f33ed..a46e0ec7 100644 --- a/src/main/services/library-sync/upload-games-batch.ts +++ b/src/main/services/library-sync/upload-games-batch.ts @@ -6,30 +6,28 @@ import { mergeWithRemoteGames } from "./merge-with-remote-games"; import { WindowManager } from "../window-manager"; export const uploadGamesBatch = async () => { - try { - const games = await gameRepository.find({ - where: { remoteId: IsNull(), isDeleted: false }, - }); + const games = await gameRepository.find({ + where: { remoteId: IsNull(), isDeleted: false }, + }); - const gamesChunks = chunk(games, 200); + const gamesChunks = chunk(games, 200); - for (const chunk of gamesChunks) { - await HydraApi.post( - "/games/batch", - chunk.map((game) => { - return { - objectId: game.objectID, - playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds), - shop: game.shop, - lastTimePlayed: game.lastTimePlayed, - }; - }) - ).catch(); - } + for (const chunk of gamesChunks) { + await HydraApi.post( + "/games/batch", + chunk.map((game) => { + return { + objectId: game.objectID, + playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds), + shop: game.shop, + lastTimePlayed: game.lastTimePlayed, + }; + }) + ).catch(); + } - await mergeWithRemoteGames(); + await mergeWithRemoteGames(); - if (WindowManager.mainWindow) - WindowManager.mainWindow.webContents.send("on-library-batch-complete"); - } catch (err) {} + if (WindowManager.mainWindow) + WindowManager.mainWindow.webContents.send("on-library-batch-complete"); }; From aa253466a3487074051a8b1435233174f39330e5 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Tue, 2 Jul 2024 23:31:07 -0300 Subject: [PATCH 4/4] feat: refactor --- src/main/events/auth/sign-out.ts | 2 +- src/main/events/index.ts | 1 - src/main/events/library/remove-game-from-library.ts | 2 +- src/main/events/profile/get-me.ts | 3 ++- src/main/events/user-preferences/auto-launch.ts | 6 +++--- src/main/services/hydra-api.ts | 2 +- src/main/services/library-sync/create-game.ts | 2 +- src/main/services/library-sync/merge-with-remote-games.ts | 2 +- src/main/services/library-sync/update-game-playtime.ts | 2 +- src/main/services/library-sync/upload-games-batch.ts | 2 +- src/shared/index.ts | 7 +++++++ src/types/index.ts | 7 ------- 12 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/events/auth/sign-out.ts b/src/main/events/auth/sign-out.ts index 9c2ce2e0..af2d5e27 100644 --- a/src/main/events/auth/sign-out.ts +++ b/src/main/events/auth/sign-out.ts @@ -28,7 +28,7 @@ const signOut = async (_event: Electron.IpcMainInvokeEvent) => { await Promise.all([ databaseOperations, - HydraApi.post("/auth/logout").catch(), + HydraApi.post("/auth/logout").catch(() => {}), ]); }; diff --git a/src/main/events/index.ts b/src/main/events/index.ts index d1075e3e..1b500be9 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -22,7 +22,6 @@ import "./library/open-game-installer-path"; import "./library/update-executable-path"; import "./library/remove-game"; import "./library/remove-game-from-library"; -import "./misc/is-user-logged-in"; import "./misc/open-external"; import "./misc/show-open-dialog"; import "./torrenting/cancel-game-download"; diff --git a/src/main/events/library/remove-game-from-library.ts b/src/main/events/library/remove-game-from-library.ts index dbb73ffc..468f5b26 100644 --- a/src/main/events/library/remove-game-from-library.ts +++ b/src/main/events/library/remove-game-from-library.ts @@ -20,7 +20,7 @@ const removeRemoveGameFromLibrary = async (gameId: number) => { const game = await gameRepository.findOne({ where: { id: gameId } }); if (game?.remoteId) { - HydraApi.delete(`/games/${game.remoteId}`).catch(); + HydraApi.delete(`/games/${game.remoteId}`).catch(() => {}); } }; diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index e625ff1e..83463680 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -1,8 +1,9 @@ import { registerEvent } from "../register-event"; import * as Sentry from "@sentry/electron/main"; import { HydraApi } from "@main/services"; -import { UserNotLoggedInError, UserProfile } from "@types"; +import { UserProfile } from "@types"; import { userAuthRepository } from "@main/repository"; +import { UserNotLoggedInError } from "@shared"; const getMe = async ( _event: Electron.IpcMainInvokeEvent diff --git a/src/main/events/user-preferences/auto-launch.ts b/src/main/events/user-preferences/auto-launch.ts index cb40a969..0a3029f2 100644 --- a/src/main/events/user-preferences/auto-launch.ts +++ b/src/main/events/user-preferences/auto-launch.ts @@ -23,14 +23,14 @@ const autoLaunch = async ( fs.copyFileSync(scriptPath, destination); } else { - appLauncher.disable().catch(); + appLauncher.disable().catch(() => {}); fs.rmSync(destination); } } else { if (enabled) { - appLauncher.enable().catch(); + appLauncher.enable().catch(() => {}); } else { - appLauncher.disable().catch(); + appLauncher.disable().catch(() => {}); } } }; diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index 5aa6635d..98b783f3 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -5,7 +5,7 @@ import url from "url"; import { uploadGamesBatch } from "./library-sync"; import { clearGamesRemoteIds } from "./library-sync/clear-games-remote-id"; import { logger } from "./logger"; -import { UserNotLoggedInError } from "@types"; +import { UserNotLoggedInError } from "@shared"; export class HydraApi { private static instance: AxiosInstance; diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 183b58a7..c0e8b1f8 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -21,5 +21,5 @@ export const createGame = async (game: Game) => { { remoteId, playTimeInMilliseconds, lastTimePlayed } ); }) - .catch(); + .catch(() => {}); }; diff --git a/src/main/services/library-sync/merge-with-remote-games.ts b/src/main/services/library-sync/merge-with-remote-games.ts index 937007dd..2162ea58 100644 --- a/src/main/services/library-sync/merge-with-remote-games.ts +++ b/src/main/services/library-sync/merge-with-remote-games.ts @@ -60,5 +60,5 @@ export const mergeWithRemoteGames = async () => { } } }) - .catch(); + .catch(() => {}); }; diff --git a/src/main/services/library-sync/update-game-playtime.ts b/src/main/services/library-sync/update-game-playtime.ts index 70d88d50..39206a12 100644 --- a/src/main/services/library-sync/update-game-playtime.ts +++ b/src/main/services/library-sync/update-game-playtime.ts @@ -9,5 +9,5 @@ export const updateGamePlaytime = async ( HydraApi.put(`/games/${game.remoteId}`, { playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000), lastTimePlayed, - }).catch(); + }).catch(() => {}); }; diff --git a/src/main/services/library-sync/upload-games-batch.ts b/src/main/services/library-sync/upload-games-batch.ts index a46e0ec7..88f02375 100644 --- a/src/main/services/library-sync/upload-games-batch.ts +++ b/src/main/services/library-sync/upload-games-batch.ts @@ -23,7 +23,7 @@ export const uploadGamesBatch = async () => { lastTimePlayed: game.lastTimePlayed, }; }) - ).catch(); + ).catch(() => {}); } await mergeWithRemoteGames(); diff --git a/src/shared/index.ts b/src/shared/index.ts index 7a2933d7..ff556ad2 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -8,6 +8,13 @@ export enum DownloadSourceStatus { Errored, } +export class UserNotLoggedInError extends Error { + constructor() { + super("user not logged in"); + this.name = "UserNotLoggedInError"; + } +} + const FORMAT = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; export const formatBytes = (bytes: number): string => { diff --git a/src/types/index.ts b/src/types/index.ts index 9027af1e..71071620 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -290,10 +290,3 @@ export interface DownloadSource { createdAt: Date; updatedAt: Date; } - -export class UserNotLoggedInError extends Error { - constructor() { - super("user not logged in"); - this.name = "UserNotLoggedInError"; - } -}