mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: getMe on launch
This commit is contained in:
parent
d801bad49e
commit
df5f82d47f
5 changed files with 89 additions and 72 deletions
|
@ -1,80 +1,43 @@
|
||||||
import { registerEvent } from "../register-event";
|
import { registerEvent } from "../register-event";
|
||||||
import * as Sentry from "@sentry/electron/main";
|
import { logger } from "@main/services";
|
||||||
import { HydraApi, logger } from "@main/services";
|
|
||||||
import type { ProfileVisibility, UserDetails } from "@types";
|
import type { ProfileVisibility, UserDetails } from "@types";
|
||||||
import {
|
import { userAuthRepository } from "@main/repository";
|
||||||
userAuthRepository,
|
|
||||||
userSubscriptionRepository,
|
|
||||||
} from "@main/repository";
|
|
||||||
import { UserNotLoggedInError } from "@shared";
|
import { UserNotLoggedInError } from "@shared";
|
||||||
|
import { getUserData } from "@main/services/user/get-user-data";
|
||||||
|
|
||||||
const getMe = async (
|
const getMe = async (
|
||||||
_event: Electron.IpcMainInvokeEvent
|
_event: Electron.IpcMainInvokeEvent
|
||||||
): Promise<UserDetails | null> => {
|
): Promise<UserDetails | null> => {
|
||||||
return HydraApi.get<UserDetails>(`/profile/me`)
|
return getUserData().catch(async (err) => {
|
||||||
.then((me) => {
|
if (err instanceof UserNotLoggedInError) {
|
||||||
userAuthRepository.upsert(
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
displayName: me.displayName,
|
|
||||||
profileImageUrl: me.profileImageUrl,
|
|
||||||
backgroundImageUrl: me.backgroundImageUrl,
|
|
||||||
userId: me.id,
|
|
||||||
},
|
|
||||||
["id"]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (me.subscription) {
|
|
||||||
userSubscriptionRepository.upsert(
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
subscriptionId: me.subscription?.id || "",
|
|
||||||
status: me.subscription?.status || "",
|
|
||||||
planId: me.subscription?.plan.id || "",
|
|
||||||
planName: me.subscription?.plan.name || "",
|
|
||||||
expiresAt: me.subscription?.expiresAt || null,
|
|
||||||
user: { id: 1 },
|
|
||||||
},
|
|
||||||
["id"]
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
userSubscriptionRepository.delete({ id: 1 });
|
|
||||||
}
|
|
||||||
|
|
||||||
Sentry.setUser({ id: me.id, username: me.username });
|
|
||||||
|
|
||||||
return me;
|
|
||||||
})
|
|
||||||
.catch(async (err) => {
|
|
||||||
if (err instanceof UserNotLoggedInError) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
logger.error("Failed to get logged user", err);
|
|
||||||
const loggedUser = await userAuthRepository.findOne({ where: { id: 1 } });
|
|
||||||
|
|
||||||
if (loggedUser) {
|
|
||||||
return {
|
|
||||||
...loggedUser,
|
|
||||||
id: loggedUser.userId,
|
|
||||||
username: "",
|
|
||||||
bio: "",
|
|
||||||
profileVisibility: "PUBLIC" as ProfileVisibility,
|
|
||||||
subscription: loggedUser.subscription
|
|
||||||
? {
|
|
||||||
id: loggedUser.subscription.subscriptionId,
|
|
||||||
status: loggedUser.subscription.status,
|
|
||||||
plan: {
|
|
||||||
id: loggedUser.subscription.planId,
|
|
||||||
name: loggedUser.subscription.planName,
|
|
||||||
},
|
|
||||||
expiresAt: loggedUser.subscription.expiresAt,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
});
|
}
|
||||||
|
logger.error("Failed to get logged user", err);
|
||||||
|
const loggedUser = await userAuthRepository.findOne({ where: { id: 1 } });
|
||||||
|
|
||||||
|
if (loggedUser) {
|
||||||
|
return {
|
||||||
|
...loggedUser,
|
||||||
|
id: loggedUser.userId,
|
||||||
|
username: "",
|
||||||
|
bio: "",
|
||||||
|
profileVisibility: "PUBLIC" as ProfileVisibility,
|
||||||
|
subscription: loggedUser.subscription
|
||||||
|
? {
|
||||||
|
id: loggedUser.subscription.subscriptionId,
|
||||||
|
status: loggedUser.subscription.status,
|
||||||
|
plan: {
|
||||||
|
id: loggedUser.subscription.planId,
|
||||||
|
name: loggedUser.subscription.planName,
|
||||||
|
},
|
||||||
|
expiresAt: loggedUser.subscription.expiresAt,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
registerEvent("getMe", getMe);
|
registerEvent("getMe", getMe);
|
||||||
|
|
|
@ -12,6 +12,7 @@ import { UserPreferences } from "./entity";
|
||||||
import { RealDebridClient } from "./services/real-debrid";
|
import { RealDebridClient } from "./services/real-debrid";
|
||||||
import { HydraApi } from "./services/hydra-api";
|
import { HydraApi } from "./services/hydra-api";
|
||||||
import { uploadGamesBatch } from "./services/library-sync";
|
import { uploadGamesBatch } from "./services/library-sync";
|
||||||
|
import { getUserData } from "./services/user/get-user-data";
|
||||||
|
|
||||||
const loadState = async (userPreferences: UserPreferences | null) => {
|
const loadState = async (userPreferences: UserPreferences | null) => {
|
||||||
import("./events");
|
import("./events");
|
||||||
|
@ -22,7 +23,8 @@ const loadState = async (userPreferences: UserPreferences | null) => {
|
||||||
|
|
||||||
Ludusavi.addManifestToLudusaviConfig();
|
Ludusavi.addManifestToLudusaviConfig();
|
||||||
|
|
||||||
HydraApi.setupApi().then(() => {
|
HydraApi.setupApi().then(async () => {
|
||||||
|
await getUserData().catch(() => {});
|
||||||
uploadGamesBatch();
|
uploadGamesBatch();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { WindowManager } from "../window-manager";
|
||||||
import { HydraApi } from "../hydra-api";
|
import { HydraApi } from "../hydra-api";
|
||||||
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
|
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
|
||||||
import { Game } from "@main/entity";
|
import { Game } from "@main/entity";
|
||||||
|
import { achievementsLogger } from "../logger";
|
||||||
|
|
||||||
const saveAchievementsOnLocal = async (
|
const saveAchievementsOnLocal = async (
|
||||||
objectId: string,
|
objectId: string,
|
||||||
|
@ -117,6 +118,12 @@ export const mergeAchievements = async (
|
||||||
const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);
|
const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);
|
||||||
|
|
||||||
if (game.remoteId) {
|
if (game.remoteId) {
|
||||||
|
achievementsLogger.log(
|
||||||
|
"Syncing achievements with cloud",
|
||||||
|
game.title,
|
||||||
|
game.objectID,
|
||||||
|
game.remoteId
|
||||||
|
);
|
||||||
await HydraApi.put(
|
await HydraApi.put(
|
||||||
"/profile/games/achievements",
|
"/profile/games/achievements",
|
||||||
{
|
{
|
||||||
|
@ -133,7 +140,8 @@ export const mergeAchievements = async (
|
||||||
publishNotification
|
publishNotification
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((err) => {
|
||||||
|
achievementsLogger.error(err);
|
||||||
return saveAchievementsOnLocal(
|
return saveAchievementsOnLocal(
|
||||||
game.objectID,
|
game.objectID,
|
||||||
game.shop,
|
game.shop,
|
||||||
|
|
|
@ -44,7 +44,8 @@ export class HydraApi {
|
||||||
return userSubscriptionRepository
|
return userSubscriptionRepository
|
||||||
.findOne({ where: { id: 1 } })
|
.findOne({ where: { id: 1 } })
|
||||||
.then((userSubscription) => {
|
.then((userSubscription) => {
|
||||||
if (userSubscription?.status !== "active") return false;
|
if (!userSubscription) return false;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
!userSubscription.expiresAt ||
|
!userSubscription.expiresAt ||
|
||||||
userSubscription!.expiresAt > new Date()
|
userSubscription!.expiresAt > new Date()
|
||||||
|
|
43
src/main/services/user/get-user-data.ts
Normal file
43
src/main/services/user/get-user-data.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import type { UserDetails } from "@types";
|
||||||
|
import { HydraApi } from "../hydra-api";
|
||||||
|
import {
|
||||||
|
userAuthRepository,
|
||||||
|
userSubscriptionRepository,
|
||||||
|
} from "@main/repository";
|
||||||
|
import * as Sentry from "@sentry/electron/main";
|
||||||
|
|
||||||
|
export const getUserData = () => {
|
||||||
|
return HydraApi.get<UserDetails>(`/profile/me`).then(async (me) => {
|
||||||
|
userAuthRepository.upsert(
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
displayName: me.displayName,
|
||||||
|
profileImageUrl: me.profileImageUrl,
|
||||||
|
backgroundImageUrl: me.backgroundImageUrl,
|
||||||
|
userId: me.id,
|
||||||
|
},
|
||||||
|
["id"]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (me.subscription) {
|
||||||
|
await userSubscriptionRepository.upsert(
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
subscriptionId: me.subscription?.id || "",
|
||||||
|
status: me.subscription?.status || "",
|
||||||
|
planId: me.subscription?.plan.id || "",
|
||||||
|
planName: me.subscription?.plan.name || "",
|
||||||
|
expiresAt: me.subscription?.expiresAt || null,
|
||||||
|
user: { id: 1 },
|
||||||
|
},
|
||||||
|
["id"]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await userSubscriptionRepository.delete({ id: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
Sentry.setUser({ id: me.id, username: me.username });
|
||||||
|
|
||||||
|
return me;
|
||||||
|
});
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue