Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class ChatService {
private redis: Redis;
private sessions: Map<string, FiveStackWebSocketClient[]> = new Map();

private expiresIn = 60 * 60;

constructor(
private readonly logger: Logger,
private readonly rcon: RconService,
Expand All @@ -22,6 +24,10 @@ export class ChatService {
this.redis = this.redisManager.getConnection();
}

public async updateChatMessageTTL(expiresIn: number) {
this.expiresIn = expiresIn;
}

public async joinMatchLobby(
client: FiveStackWebSocketClient,
type: ChatLobbyType,
Expand Down Expand Up @@ -234,7 +240,7 @@ export class ChatService {
await this.redis.sendCommand(
new Redis.Command("HEXPIRE", [
messageKey,
60 * 60,
this.expiresIn,
"FIELDS",
1,
messageField,
Expand Down
9 changes: 9 additions & 0 deletions src/system/enums/SystemSettingName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum SystemSettingName {
Updates = "updates",
ChatMessageTtl = "chat_message_ttl",
DemoNetworkLimiter = "demo_network_limiter",
PublicDefaultModels = "public.default_models",
SupportsDiscordBot = "public.supports_discord_bot",
SupportsGameServerNodes = "supports_game_server_nodes",
SupportsGameServerVersionPinning = "supports_game_server_version_pinning",
}
17 changes: 15 additions & 2 deletions src/system/system.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { GameServerNodeService } from "src/game-server-node/game-server-node.ser
import { LoggingService } from "src/k8s/logging/logging.service";
import { isRoleAbove } from "src/utilities/isRoleAbove";
import { PassThrough } from "stream";
import { ChatService } from "src/chat/chat.service";
import { SystemSettingName } from "./enums/SystemSettingName";

@Controller("system")
export class SystemController {
Expand All @@ -22,6 +24,7 @@ export class SystemController {
private readonly notifications: NotificationsService,
private readonly gameServerNodeService: GameServerNodeService,
private readonly loggingService: LoggingService,
private readonly chatService: ChatService,
) {}

@Get("healthz")
Expand Down Expand Up @@ -262,15 +265,25 @@ export class SystemController {
@HasuraEvent()
public async settings(data: HasuraEventData<settings_set_input>) {
if (
(data.new.name === "demo_network_limiter" ||
data.old.name === "demo_network_limiter") &&
(data.new.name === SystemSettingName.DemoNetworkLimiter ||
data.old.name === SystemSettingName.DemoNetworkLimiter) &&
(data.op === "INSERT" ||
data.op === "DELETE" ||
data.new.value !== data.old.value)
) {
await this.gameServerNodeService.updateDemoNetworkLimiters();
}

if (
(data.new.name === SystemSettingName.ChatMessageTtl ||
data.old.name === SystemSettingName.ChatMessageTtl) &&
(data.op === "INSERT" ||
data.op === "DELETE" ||
data.new.value !== data.old.value)
) {
await this.chatService.updateChatMessageTTL(parseInt(data.new.value));
}

await this.system.updateDefaultOptions();
}
}
21 changes: 20 additions & 1 deletion src/system/system.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { NotificationsModule } from "src/notifications/notifications.module";
import { S3Module } from "src/s3/s3.module";
import { PostgresModule } from "src/postgres/postgres.module";
import { K8sModule } from "src/k8s/k8s.module";
import { ChatModule } from "src/chat/chat.module";
import { SystemSettingName } from "./enums/SystemSettingName";
import { ChatService } from "src/chat/chat.service";

@Module({
imports: [
Expand All @@ -27,6 +30,7 @@ import { K8sModule } from "src/k8s/k8s.module";
NotificationsModule,
S3Module,
PostgresModule,
ChatModule,
BullModule.registerQueue({
name: SystemQueues.Version,
}),
Expand All @@ -46,7 +50,11 @@ import { K8sModule } from "src/k8s/k8s.module";
controllers: [SystemController],
})
export class SystemModule {
constructor(@InjectQueue(SystemQueues.Version) queue: Queue) {
constructor(
@InjectQueue(SystemQueues.Version) queue: Queue,
private readonly systemService: SystemService,
private readonly chatService: ChatService,
) {
if (process.env.RUN_MIGRATIONS) {
return;
}
Expand All @@ -60,5 +68,16 @@ export class SystemModule {
},
},
);

void this.setupSettings();
}

public async setupSettings() {
await this.chatService.updateChatMessageTTL(
await this.systemService.getSetting<number>(
SystemSettingName.ChatMessageTtl,
60 * 60,
),
);
}
}
35 changes: 30 additions & 5 deletions src/system/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TailscaleConfig } from "src/configs/types/TailscaleConfig";
import { DiscordConfig } from "src/configs/types/DiscordConfig";
import { SteamConfig } from "src/configs/types/SteamConfig";
import { PostgresService } from "src/postgres/postgres.service";
import { SystemSettingName } from "./enums/SystemSettingName";

@Injectable()
export class SystemService {
Expand All @@ -35,6 +36,30 @@ export class SystemService {
this.appsClient = kc.makeApiClient(AppsV1Api);
}

public async getSetting<T extends string | number | boolean>(
name: SystemSettingName,
defaultValue: T,
): Promise<T> {
const [data] = await this.postgres.query<
Array<{
value: string;
}>
>(`SELECT value FROM public.settings WHERE name = $1 LIMIT 1`, [name]);

if (data?.value !== undefined && data?.value !== null) {
// Try to convert the string value to the type of defaultValue
if (typeof defaultValue === "boolean") {
return (data.value === "true") as T;
} else if (typeof defaultValue === "number") {
const num = Number(data.value);
return (isNaN(num) ? defaultValue : num) as T;
} else {
return data.value as T;
}
}
return defaultValue;
}

public async detectFeatures() {
while (this.featuresDetected === false) {
try {
Expand All @@ -53,7 +78,7 @@ export class SystemService {
insert_settings_one: {
__args: {
object: {
name: "supports_game_server_nodes",
name: SystemSettingName.SupportsGameServerNodes,
value: supportsGameServerNodes.toString(),
},
on_conflict: {
Expand All @@ -80,7 +105,7 @@ export class SystemService {
insert_settings_one: {
__args: {
object: {
name: "public.supports_discord_bot",
name: SystemSettingName.SupportsDiscordBot,
value: supportsDiscordBot.toString(),
},
on_conflict: {
Expand All @@ -103,7 +128,7 @@ export class SystemService {
insert_settings_one: {
__args: {
object: {
name: "supports_game_server_version_pinning",
name: SystemSettingName.SupportsGameServerVersionPinning,
value: supportsGameServerNodeVersionPinning.toString(),
},
on_conflict: {
Expand Down Expand Up @@ -189,7 +214,7 @@ export class SystemService {
insert_settings_one: {
__args: {
object: {
name: "updates",
name: SystemSettingName.Updates,
value: JSON.stringify(hasUpdates),
},
on_conflict: {
Expand Down Expand Up @@ -409,7 +434,7 @@ export class SystemService {

for (const setting of settings) {
switch (setting.name) {
case "public.default_models":
case SystemSettingName.PublicDefaultModels:
await this.postgres.query(
`ALTER TABLE "public"."match_options" ALTER COLUMN "default_models" SET DEFAULT ${setting.value === "true" ? true : false}`,
);
Expand Down