feat: adding initial leveldb configuration

This commit is contained in:
Chubby Granny Chaser
2025-01-15 16:58:59 +00:00
parent d4be5b8c66
commit 2c5fb8a037
18 changed files with 202 additions and 196 deletions

View File

@@ -1,7 +1,3 @@
import {
userAuthRepository,
userSubscriptionRepository,
} from "@main/repository";
import axios, { AxiosError, AxiosInstance } from "axios";
import { WindowManager } from "./window-manager";
import url from "url";
@@ -13,6 +9,10 @@ import { omit } from "lodash-es";
import { appVersion } from "@main/constants";
import { getUserData } from "./user/get-user-data";
import { isFuture, isToday } from "date-fns";
import { db } from "@main/level";
import { levelKeys } from "@main/level/sublevels/keys";
import type { Auth, User } from "@types";
import { Crypto } from "./crypto";
interface HydraApiOptions {
needsAuth?: boolean;
@@ -77,14 +77,14 @@ export class HydraApi {
tokenExpirationTimestamp
);
await userAuthRepository.upsert(
db.put<string, Auth>(
levelKeys.auth,
{
id: 1,
accessToken,
accessToken: Crypto.encrypt(accessToken),
refreshToken: Crypto.encrypt(refreshToken),
tokenExpirationTimestamp,
refreshToken,
},
["id"]
{ valueEncoding: "json" }
);
await getUserData().then((userDetails) => {
@@ -186,17 +186,23 @@ export class HydraApi {
);
}
const userAuth = await userAuthRepository.findOne({
where: { id: 1 },
relations: { subscription: true },
const result = await db.getMany<string>([levelKeys.auth, levelKeys.user], {
valueEncoding: "json",
});
const userAuth = result.at(0) as Auth | undefined;
const user = result.at(1) as User | undefined;
this.userAuth = {
authToken: userAuth?.accessToken ?? "",
refreshToken: userAuth?.refreshToken ?? "",
authToken: userAuth?.accessToken
? Crypto.decrypt(userAuth.accessToken)
: "",
refreshToken: userAuth?.refreshToken
? Crypto.decrypt(userAuth.refreshToken)
: "",
expirationTimestamp: userAuth?.tokenExpirationTimestamp ?? 0,
subscription: userAuth?.subscription
? { expiresAt: userAuth.subscription?.expiresAt }
subscription: user?.subscription
? { expiresAt: user.subscription?.expiresAt }
: null,
};
@@ -239,14 +245,19 @@ export class HydraApi {
this.userAuth.expirationTimestamp
);
userAuthRepository.upsert(
{
id: 1,
accessToken,
tokenExpirationTimestamp,
},
["id"]
);
await db
.get<string, Auth>(levelKeys.auth, { valueEncoding: "json" })
.then((auth) => {
return db.put<string, Auth>(
levelKeys.auth,
{
...auth,
accessToken: Crypto.encrypt(accessToken),
tokenExpirationTimestamp,
},
{ valueEncoding: "json" }
);
});
} catch (err) {
this.handleUnauthorizedError(err);
}
@@ -276,8 +287,16 @@ export class HydraApi {
subscription: null,
};
userAuthRepository.delete({ id: 1 });
userSubscriptionRepository.delete({ id: 1 });
db.batch([
{
type: "del",
key: levelKeys.auth,
},
{
type: "del",
key: levelKeys.user,
},
]);
this.sendSignOutEvent();
}

View File

@@ -1,3 +1,4 @@
export * from "./crypto";
export * from "./logger";
export * from "./steam";
export * from "./steam-250";

View File

@@ -1,43 +1,30 @@
import type { ProfileVisibility, UserDetails } from "@types";
import { User, type ProfileVisibility, type UserDetails } from "@types";
import { HydraApi } from "../hydra-api";
import {
userAuthRepository,
userSubscriptionRepository,
} from "@main/repository";
import { UserNotLoggedInError } from "@shared";
import { logger } from "../logger";
import { db } from "@main/level";
import { levelKeys } from "@main/level/sublevels/keys";
export const getUserData = () => {
export const getUserData = async () => {
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"]
db.get<string, User>(levelKeys.user, { valueEncoding: "json" }).then(
(user) => {
return db.put<string, User>(
levelKeys.user,
{
...user,
id: me.id,
displayName: me.displayName,
profileImageUrl: me.profileImageUrl,
backgroundImageUrl: me.backgroundImageUrl,
subscription: me.subscription,
},
{ valueEncoding: "json" }
);
}
);
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 });
}
return me;
})
.catch(async (err) => {
@@ -46,15 +33,14 @@ export const getUserData = () => {
return null;
}
logger.error("Failed to get logged user");
const loggedUser = await userAuthRepository.findOne({
where: { id: 1 },
relations: { subscription: true },
const loggedUser = await db.get<string, User>(levelKeys.user, {
valueEncoding: "json",
});
if (loggedUser) {
return {
...loggedUser,
id: loggedUser.userId,
username: "",
bio: "",
email: null,
@@ -64,11 +50,11 @@ export const getUserData = () => {
},
subscription: loggedUser.subscription
? {
id: loggedUser.subscription.subscriptionId,
id: loggedUser.subscription.id,
status: loggedUser.subscription.status,
plan: {
id: loggedUser.subscription.planId,
name: loggedUser.subscription.planName,
id: loggedUser.subscription.plan.id,
name: loggedUser.subscription.plan.name,
},
expiresAt: loggedUser.subscription.expiresAt,
}