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

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

View File

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