Skip to content

Commit

Permalink
basic refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed Dec 28, 2023
1 parent 1ba0f65 commit 3bbe407
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { promisify } from "node:util";

import { ConfigSchema } from "./config.schema";
import { EventModule, MoxyEventEmitter } from "./event";
import { UserNotFoundError } from "./errors";

export type ExpirationDate = "unlimit" | string;

Expand Down Expand Up @@ -96,6 +97,15 @@ export class ConfigService {
return parsed;
}

public async getUser(userKey: string): Promise<UserConfig> {
const config = await this.getConfig();
const userConfig = config.users[userKey];

if (!userConfig) throw new UserNotFoundError(userKey);

return userConfig;
}

public async getConfig(): Promise<Config> {
if (this.cache) return this.cache;

Expand Down
23 changes: 10 additions & 13 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ export class UserFactory {
}

public async get(userKey: string): Promise<User> {
const config = await this.configService.getConfig();
const userConfig = config.users[userKey];
if (!userConfig) throw new Error(`no config found for user ${userKey}`);
const userConfig = await this.configService.getUser(userKey);

return this.getFromUserConfig(userConfig);
}
Expand All @@ -132,8 +130,8 @@ export class UserWatcher {
this.eventEmitter.on("new-user", (user) =>
this.handleNewUser(user).catch((err) => this.logger.error(err))
);
this.eventEmitter.on("update-user", ({ key: userKey }) =>
this.handleUserUpdate(userKey).catch((err) => this.logger.error(err))
this.eventEmitter.on("update-user", (user) =>
this.handleUserUpdate(user).catch((err) => this.logger.error(err))
);
this.eventEmitter.on("delete-user", (userKey) => {
delete this.lastStatus[userKey];
Expand All @@ -154,23 +152,22 @@ export class UserWatcher {

private async handleNewUser(userConfig: UserConfig) {
await this.userStats.assert(userConfig.key);
const user = await this.userFactory.get(userConfig.key);
const user = await this.userFactory.getFromUserConfig(userConfig);
this.lastStatus[userConfig.key] = user.isEnabled();
if (user.isEnabled()) this.eventEmitter.emit("enable-user", userConfig.key);
}

private async handleUserUpdate(userKey: string) {
await this.userStats.assert(userKey);
const user = await this.userFactory.get(userKey);
const wasDisabled = !this.lastStatus[userKey];
private async handleUserUpdate(userConfig: UserConfig) {
const user = await this.userFactory.getFromUserConfig(userConfig);
const wasDisabled = !this.lastStatus[userConfig.key];

if (wasDisabled && user.isEnabled())
this.eventEmitter.emit("enable-user", userKey);
this.eventEmitter.emit("enable-user", userConfig.key);

if (!wasDisabled && !user.isEnabled())
this.eventEmitter.emit("disable-user", userKey);
this.eventEmitter.emit("disable-user", userConfig.key);

this.lastStatus[userKey] = user.isEnabled();
this.lastStatus[userConfig.key] = user.isEnabled();
}

private async handleNewTraffic(
Expand Down

0 comments on commit 3bbe407

Please sign in to comment.