Initial commit

This commit is contained in:
2025-07-25 19:00:06 -04:00
commit e604c7a437
52 changed files with 96098 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
import KV from "../persistence/kv.ts";
import { ProfileRole } from "../platforms/base.ts";
import { type ServerBase } from "../server.ts";
import { type SignalRSocketHandler } from "../socket/signalr/socket.ts";
import ProfileManagerBase from "./manager.ts";
import { recNetAccountSchema, SelfAccount, type RecNetAccount } from "./types/profile.ts";
class Profile {
#id: number;
#kv: KV;
#socket: SignalRSocketHandler | null = null;
#server: ServerBase;
#selfAcc: SelfAccount;
constructor(acc: SelfAccount, kv: KV, server: ServerBase) {
this.#id = acc.accountId;
this.#selfAcc = acc;
this.#kv = kv;
this.#server = server;
}
async #saveSelfAcc() {
await this.#kv.getKv().set(this.#constructProfilePropertyKey(), this.#selfAcc);
this.#server.emit('profile.update', { profile: this });
}
#constructProfilePropertyKey(...keys: (string | undefined)[]) {
return [ ProfileManagerBase.profilesKey, this.#id, ...keys.filter(val => typeof val == 'string') ];
}
getUsername() {
return this.#selfAcc.username;
}
async setUsername(username: string) {
this.#kv.getKv().delete([ ProfileManagerBase.profilesKey, this.#selfAcc.username ]);
this.#kv.getKv().set([ ProfileManagerBase.profilesKey, username ], this.getId());
this.#selfAcc.username = username;
await this.#saveSelfAcc();
}
getDisplayName() {
return this.#selfAcc.displayName;
}
async setDisplayName(displayName: string) {
this.#selfAcc.displayName = displayName;
await this.#saveSelfAcc();
}
async getBio(){
const key = this.#constructProfilePropertyKey('bio');
const val = await this.#kv.getKv().get<string>(key);
if (!val.value) return null;
else return val.value;
}
async setBio(bio: string) {
const key = this.#constructProfilePropertyKey('bio');
await this.#kv.getKv().set(key, bio);
}
async getRole(): Promise<ProfileRole> {
const val = (await this.#kv.getKv().get<ProfileRole>(this.#constructProfilePropertyKey('role'))).value;
if (!val) return ProfileRole.User;
else return val;
}
async setRole(role: ProfileRole) {
await this.#kv.getKv().set(this.#constructProfilePropertyKey('role'), role);
this.#server.emit('profile.roleupdate', { profile: this, newRole: role });
}
getId() {
return this.#id;
}
getSocketHandler() {
return this.#socket;
}
setSocketHandler(handler: SignalRSocketHandler | null) {
this.#socket = handler;
}
export(): RecNetAccount | null {
const val = recNetAccountSchema.safeParse(this.#selfAcc);
if (val.success) return val.data;
else return null;
}
selfExport(): SelfAccount {
return this.#selfAcc;
}
}
export default Profile;