feat: remove notification spam

This commit is contained in:
Zamitto 2024-09-26 21:51:15 -03:00
parent d7c05247c3
commit 753a293cd7
8 changed files with 80 additions and 49 deletions

View File

@ -227,7 +227,8 @@
"repack_count_other": "{{count}} repacks added", "repack_count_other": "{{count}} repacks added",
"new_update_available": "Version {{version}} available", "new_update_available": "Version {{version}} available",
"restart_to_install_update": "Restart Hydra to install the update", "restart_to_install_update": "Restart Hydra to install the update",
"game_achievement_unlocked": "{{game}} achievement unlocked" "notification_achievement_unlocked_title": "Achievement unlocked for {{game}}",
"notification_achievement_unlocked_body": "{{achievement}} and other {{count}} were unlocked"
}, },
"system_tray": { "system_tray": {
"open": "Open Hydra", "open": "Open Hydra",

View File

@ -8,7 +8,7 @@ import { getFileBase64 } from "@main/helpers";
import { steamGamesWorker } from "@main/workers"; import { steamGamesWorker } from "@main/workers";
import { createGame } from "@main/services/library-sync"; import { createGame } from "@main/services/library-sync";
import { steamUrlBuilder } from "@shared"; import { steamUrlBuilder } from "@shared";
import { saveAllLocalSteamAchivements } from "@main/services/achievements/save-all-local-steam-achivements"; import { updateLocalUnlockedAchivements } from "@main/services/achievements/update-local-unlocked-achivements";
const addGameToLibrary = async ( const addGameToLibrary = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
@ -53,9 +53,7 @@ const addGameToLibrary = async (
}); });
} }
// TODO: search for achievements only from this game updateLocalUnlockedAchivements(true, objectID);
console.log("Searching for achievements", title);
saveAllLocalSteamAchivements();
const game = await gameRepository.findOne({ where: { objectID } }); const game = await gameRepository.findOne({ where: { objectID } });

View File

@ -25,7 +25,7 @@ const processAchievementFile = async (game: Game, file: AchievementFile) => {
console.log(unlockedAchievements); console.log(unlockedAchievements);
if (unlockedAchievements.length) { if (unlockedAchievements.length) {
mergeAchievements(game.objectID, game.shop, unlockedAchievements); mergeAchievements(game.objectID, game.shop, unlockedAchievements, true);
} }
} }
}; };

View File

@ -0,0 +1,21 @@
import { userPreferencesRepository } from "@main/repository";
import { HydraApi } from "../hydra-api";
export const getGameAchievementData = async (
objectId: string,
shop: string
) => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
return HydraApi.get(
"/games/achievements",
{
shop,
objectId,
language: userPreferences?.language || "en",
},
{ needsAuth: false }
);
};

View File

@ -22,12 +22,15 @@ const saveAchievementsOnLocal = async (
export const mergeAchievements = async ( export const mergeAchievements = async (
objectId: string, objectId: string,
shop: string, shop: string,
achievements: UnlockedAchievement[] achievements: UnlockedAchievement[],
publishNotification: boolean
) => { ) => {
const game = await gameRepository.findOne({ const game = await gameRepository.findOne({
where: { objectID: objectId, shop: shop as GameShop }, where: { objectID: objectId, shop: shop as GameShop },
}); });
if (!game) return;
const localGameAchievement = await gameAchievementRepository.findOne({ const localGameAchievement = await gameAchievementRepository.findOne({
where: { where: {
objectId, objectId,
@ -53,20 +56,20 @@ export const mergeAchievements = async (
); );
} }
for (const achievement of newAchievements.slice(0, 3)) { if (newAchievements.length > 0 && publishNotification) {
const completeAchievement = JSON.parse( const achievement = newAchievements.pop()!;
const achievementInfo = JSON.parse(
localGameAchievement?.achievements || "[]" localGameAchievement?.achievements || "[]"
).find((steamAchievement) => { ).find((steamAchievement) => {
return achievement.name === steamAchievement.name; return achievement.name === steamAchievement.name;
}); });
if (completeAchievement) { publishNewAchievementNotification(
publishNewAchievementNotification( game.title ?? "",
game?.title || " ", achievementInfo.displayName,
completeAchievement.displayName, achievementInfo.icon,
completeAchievement.icon newAchievements.length
); );
}
} }
const mergedLocalAchievements = unlockedAchievements.concat(newAchievements); const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);

View File

@ -1,21 +1,16 @@
import { import { gameAchievementRepository, gameRepository } from "@main/repository";
gameAchievementRepository,
gameRepository,
userPreferencesRepository,
} from "@main/repository";
import { findSteamGameAchievementFiles } from "./find-steam-game-achivement-files"; import { findSteamGameAchievementFiles } from "./find-steam-game-achivement-files";
import { parseAchievementFile } from "./parse-achievement-file"; import { parseAchievementFile } from "./parse-achievement-file";
import { HydraApi } from "@main/services";
import { checkUnlockedAchievements } from "./check-unlocked-achievements"; import { checkUnlockedAchievements } from "./check-unlocked-achievements";
import { mergeAchievements } from "./merge-achievements"; import { mergeAchievements } from "./merge-achievements";
import type { UnlockedAchievement } from "@types"; import type { UnlockedAchievement } from "@types";
import { getGameAchievementData } from "./get-game-achievement-data";
export const saveAllLocalSteamAchivements = async () => { export const updateLocalUnlockedAchivements = async (
const userPreferences = await userPreferencesRepository.findOne({ publishNotification: boolean,
where: { id: 1 }, objectId?: string
}); ) => {
const gameAchievementFiles = findSteamGameAchievementFiles(objectId);
const gameAchievementFiles = findSteamGameAchievementFiles();
for (const objectId of gameAchievementFiles.keys()) { for (const objectId of gameAchievementFiles.keys()) {
const [game, localAchievements] = await Promise.all([ const [game, localAchievements] = await Promise.all([
@ -36,15 +31,7 @@ export const saveAllLocalSteamAchivements = async () => {
); );
if (!localAchievements || !localAchievements.achievements) { if (!localAchievements || !localAchievements.achievements) {
await HydraApi.get( await getGameAchievementData(objectId, "steam")
"/games/achievements",
{
shop: "steam",
objectId,
language: userPreferences?.language || "en",
},
{ needsAuth: false }
)
.then((achievements) => { .then((achievements) => {
return gameAchievementRepository.upsert( return gameAchievementRepository.upsert(
{ {
@ -75,6 +62,11 @@ export const saveAllLocalSteamAchivements = async () => {
} }
} }
mergeAchievements(objectId, "steam", unlockedAchievements); mergeAchievements(
objectId,
"steam",
unlockedAchievements,
publishNotification
);
} }
}; };

View File

@ -4,7 +4,7 @@ import { IsNull } from "typeorm";
import { HydraApi } from "../hydra-api"; import { HydraApi } from "../hydra-api";
import { mergeWithRemoteGames } from "./merge-with-remote-games"; import { mergeWithRemoteGames } from "./merge-with-remote-games";
import { WindowManager } from "../window-manager"; import { WindowManager } from "../window-manager";
import { saveAllLocalSteamAchivements } from "../achievements/save-all-local-steam-achivements"; import { updateLocalUnlockedAchivements } from "../achievements/update-local-unlocked-achivements";
export const uploadGamesBatch = async () => { export const uploadGamesBatch = async () => {
const games = await gameRepository.find({ const games = await gameRepository.find({
@ -29,7 +29,7 @@ export const uploadGamesBatch = async () => {
await mergeWithRemoteGames(); await mergeWithRemoteGames();
await saveAllLocalSteamAchivements(); await updateLocalUnlockedAchivements(false);
if (WindowManager.mainWindow) if (WindowManager.mainWindow)
WindowManager.mainWindow.webContents.send("on-library-batch-complete"); WindowManager.mainWindow.webContents.send("on-library-batch-complete");

View File

@ -99,7 +99,8 @@ const downloadImage = async (url: string, iconPath: string) => {
export const publishNewAchievementNotification = async ( export const publishNewAchievementNotification = async (
game: string, game: string,
name: string, name: string,
iconUrl: string iconUrl: string,
count: number
) => { ) => {
const iconPath = path.join( const iconPath = path.join(
app.getPath("temp"), app.getPath("temp"),
@ -108,14 +109,29 @@ export const publishNewAchievementNotification = async (
await downloadImage(iconUrl, iconPath).catch(() => {}); await downloadImage(iconUrl, iconPath).catch(() => {});
new Notification({ if (count > 1) {
title: t("game_achievement_unlocked", { new Notification({
ns: "notifications", title: t("notification_achievement_unlocked_title", {
game, ns: "notifications",
}), game: game,
body: name, }),
icon: iconPath, body: t("notification_achievement_unlocked_body", {
}).show(); ns: "notifications",
achievement: name,
count,
}),
icon: iconPath,
}).show();
} else {
new Notification({
title: t("notification_achievement_unlocked_title", {
ns: "notifications",
game: game,
}),
body: name,
icon: iconPath,
}).show();
}
}; };
export const publishNewFriendRequestNotification = async () => {}; export const publishNewFriendRequestNotification = async () => {};