feat: notifications

This commit is contained in:
Zamitto 2024-09-26 14:47:41 -03:00
parent 5c790edb2c
commit c72eefdb77
8 changed files with 81 additions and 17 deletions

View file

@ -1,11 +1,17 @@
import { gameAchievementRepository } from "@main/repository";
import { gameAchievementRepository, gameRepository } from "@main/repository";
import { UnlockedAchievement } from "./types";
import { publishNewAchievementNotification } from "../notifications";
import { GameShop } from "@types";
export const mergeAchievements = async (
objectId: string,
shop: string,
achievements: UnlockedAchievement[]
) => {
const game = await gameRepository.findOne({
where: { objectID: objectId, shop: shop as GameShop },
});
const localGameAchievement = await gameAchievementRepository.findOne({
where: {
objectId,
@ -17,16 +23,30 @@ export const mergeAchievements = async (
localGameAchievement?.unlockedAchievements || "[]"
);
console.log("file achievemets:", achievements);
const newAchievements = achievements.filter((achievement) => {
return !unlockedAchievements.some((localAchievement) => {
return localAchievement.name === achievement.name;
});
});
for (const achievement of newAchievements) {
const completeAchievement = JSON.parse(
localGameAchievement?.achievements || "[]"
).find((steamAchievement) => {
return achievement.name === steamAchievement.name;
});
if (completeAchievement) {
publishNewAchievementNotification(
game?.title || " ",
completeAchievement.displayName,
completeAchievement.icon
);
}
}
const mergedAchievements = unlockedAchievements.concat(newAchievements);
console.log("merged achievemetns", mergedAchievements);
gameAchievementRepository.upsert(
{
objectId,

View file

@ -1,4 +1,8 @@
import { gameAchievementRepository, gameRepository } from "@main/repository";
import {
gameAchievementRepository,
gameRepository,
userPreferencesRepository,
} from "@main/repository";
import { steamFindGameAchievementFiles } from "./steam/steam-find-game-achivement-files";
import { parseAchievementFile } from "./util/parseAchievementFile";
import { HydraApi } from "@main/services";
@ -7,6 +11,10 @@ import { mergeAchievements } from "./merge-achievements";
import { UnlockedAchievement } from "./types";
export const saveAllLocalSteamAchivements = async () => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
const gameAchievementFiles = steamFindGameAchievementFiles();
for (const objectId of Object.keys(gameAchievementFiles)) {
@ -22,11 +30,12 @@ export const saveAllLocalSteamAchivements = async () => {
if (!game) continue;
if (!localAchievements || !localAchievements.achievements) {
HydraApi.get(
await HydraApi.get(
"/games/achievements",
{
shop: "steam",
objectId,
language: userPreferences?.language || "en",
},
{ needsAuth: false }
)
@ -50,11 +59,14 @@ export const saveAllLocalSteamAchivements = async () => {
achievementFile.filePath
);
console.log(achievementFile.filePath);
unlockedAchievements.push(
...checkUnlockedAchievements(achievementFile.type, localAchievementFile)
);
if (localAchievementFile) {
unlockedAchievements.push(
...checkUnlockedAchievements(
achievementFile.type,
localAchievementFile
)
);
}
}
mergeAchievements(objectId, "steam", unlockedAchievements);

View file

@ -1,9 +1,12 @@
import { Notification, nativeImage } from "electron";
import { Notification, app, nativeImage } from "electron";
import { t } from "i18next";
import { parseICO } from "icojs";
import trayIcon from "@resources/tray-icon.png?asset";
import { Game } from "@main/entity";
import { gameRepository, userPreferencesRepository } from "@main/repository";
import axios from "axios";
import fs from "node:fs";
import path from "node:path";
const getGameIconNativeImage = async (gameId: number) => {
try {
@ -82,4 +85,26 @@ export const publishNotificationUpdateReadyToInstall = async (
}).show();
};
export const publishNewAchievementNotification = async (
game: string,
name: string,
icon: string
) => {
const iconName = icon.split("/").pop() || "icon.png";
await axios.get(icon, { responseType: "stream" }).then((response) => {
return response.data.pipe(
fs.createWriteStream(path.join(app.getPath("temp"), iconName))
);
});
new Notification({
title: t("game_achievement_unlocked", {
ns: "notifications",
game,
}),
body: name,
icon: path.join(app.getPath("temp"), iconName),
}).show();
};
export const publishNewFriendRequestNotification = async () => {};