Skip to content

Commit 666df81

Browse files
authored
feature: add chat ttl setting, also add enums for settings we care about (#108)
1 parent 89333d6 commit 666df81

4 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/chat/chat.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class ChatService {
1313
private redis: Redis;
1414
private sessions: Map<string, FiveStackWebSocketClient[]> = new Map();
1515

16+
private expiresIn = 60 * 60;
17+
1618
constructor(
1719
private readonly logger: Logger,
1820
private readonly rcon: RconService,
@@ -22,6 +24,10 @@ export class ChatService {
2224
this.redis = this.redisManager.getConnection();
2325
}
2426

27+
public async updateChatMessageTTL(expiresIn: number) {
28+
this.expiresIn = expiresIn;
29+
}
30+
2531
public async joinMatchLobby(
2632
client: FiveStackWebSocketClient,
2733
type: ChatLobbyType,
@@ -238,7 +244,7 @@ export class ChatService {
238244
await this.redis.sendCommand(
239245
new Redis.Command("HEXPIRE", [
240246
messageKey,
241-
60 * 60,
247+
this.expiresIn,
242248
"FIELDS",
243249
1,
244250
messageField,

src/system/system.controller.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { GameServerNodeService } from "src/game-server-node/game-server-node.ser
1313
import { LoggingService } from "src/k8s/logging/logging.service";
1414
import { isRoleAbove } from "src/utilities/isRoleAbove";
1515
import { PassThrough } from "stream";
16+
import { ChatService } from "src/chat/chat.service";
17+
import { SystemSettingName } from "./enums/SystemSettingName";
1618

1719
@Controller("system")
1820
export class SystemController {
@@ -22,6 +24,7 @@ export class SystemController {
2224
private readonly notifications: NotificationsService,
2325
private readonly gameServerNodeService: GameServerNodeService,
2426
private readonly loggingService: LoggingService,
27+
private readonly chatService: ChatService,
2528
) {}
2629

2730
@Get("healthz")
@@ -262,15 +265,25 @@ export class SystemController {
262265
@HasuraEvent()
263266
public async settings(data: HasuraEventData<settings_set_input>) {
264267
if (
265-
(data.new.name === "demo_network_limiter" ||
266-
data.old.name === "demo_network_limiter") &&
268+
(data.new.name === SystemSettingName.DemoNetworkLimiter ||
269+
data.old.name === SystemSettingName.DemoNetworkLimiter) &&
267270
(data.op === "INSERT" ||
268271
data.op === "DELETE" ||
269272
data.new.value !== data.old.value)
270273
) {
271274
await this.gameServerNodeService.updateDemoNetworkLimiters();
272275
}
273276

277+
if (
278+
(data.new.name === SystemSettingName.ChatMessageTtl ||
279+
data.old.name === SystemSettingName.ChatMessageTtl) &&
280+
(data.op === "INSERT" ||
281+
data.op === "DELETE" ||
282+
data.new.value !== data.old.value)
283+
) {
284+
await this.chatService.updateChatMessageTTL(parseInt(data.new.value));
285+
}
286+
274287
await this.system.updateDefaultOptions();
275288
}
276289
}

src/system/system.module.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { NotificationsModule } from "src/notifications/notifications.module";
1717
import { S3Module } from "src/s3/s3.module";
1818
import { PostgresModule } from "src/postgres/postgres.module";
1919
import { K8sModule } from "src/k8s/k8s.module";
20+
import { ChatModule } from "src/chat/chat.module";
21+
import { SystemSettingName } from "./enums/SystemSettingName";
22+
import { ChatService } from "src/chat/chat.service";
2023

2124
@Module({
2225
imports: [
@@ -27,6 +30,7 @@ import { K8sModule } from "src/k8s/k8s.module";
2730
NotificationsModule,
2831
S3Module,
2932
PostgresModule,
33+
ChatModule,
3034
BullModule.registerQueue({
3135
name: SystemQueues.Version,
3236
}),
@@ -46,7 +50,11 @@ import { K8sModule } from "src/k8s/k8s.module";
4650
controllers: [SystemController],
4751
})
4852
export class SystemModule {
49-
constructor(@InjectQueue(SystemQueues.Version) queue: Queue) {
53+
constructor(
54+
@InjectQueue(SystemQueues.Version) queue: Queue,
55+
private readonly systemService: SystemService,
56+
private readonly chatService: ChatService,
57+
) {
5058
if (process.env.RUN_MIGRATIONS) {
5159
return;
5260
}
@@ -60,5 +68,16 @@ export class SystemModule {
6068
},
6169
},
6270
);
71+
72+
void this.setupSettings();
73+
}
74+
75+
public async setupSettings() {
76+
await this.chatService.updateChatMessageTTL(
77+
await this.systemService.getSetting<number>(
78+
SystemSettingName.ChatMessageTtl,
79+
60 * 60,
80+
),
81+
);
6382
}
6483
}

src/system/system.service.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TailscaleConfig } from "src/configs/types/TailscaleConfig";
1414
import { DiscordConfig } from "src/configs/types/DiscordConfig";
1515
import { SteamConfig } from "src/configs/types/SteamConfig";
1616
import { PostgresService } from "src/postgres/postgres.service";
17+
import { SystemSettingName } from "./enums/SystemSettingName";
1718

1819
@Injectable()
1920
export class SystemService {
@@ -35,6 +36,30 @@ export class SystemService {
3536
this.appsClient = kc.makeApiClient(AppsV1Api);
3637
}
3738

39+
public async getSetting<T extends string | number | boolean>(
40+
name: SystemSettingName,
41+
defaultValue: T,
42+
): Promise<T> {
43+
const [data] = await this.postgres.query<
44+
Array<{
45+
value: string;
46+
}>
47+
>(`SELECT value FROM public.settings WHERE name = $1 LIMIT 1`, [name]);
48+
49+
if (data?.value !== undefined && data?.value !== null) {
50+
// Try to convert the string value to the type of defaultValue
51+
if (typeof defaultValue === "boolean") {
52+
return (data.value === "true") as T;
53+
} else if (typeof defaultValue === "number") {
54+
const num = Number(data.value);
55+
return (isNaN(num) ? defaultValue : num) as T;
56+
} else {
57+
return data.value as T;
58+
}
59+
}
60+
return defaultValue;
61+
}
62+
3863
public async detectFeatures() {
3964
while (this.featuresDetected === false) {
4065
try {
@@ -53,7 +78,7 @@ export class SystemService {
5378
insert_settings_one: {
5479
__args: {
5580
object: {
56-
name: "supports_game_server_nodes",
81+
name: SystemSettingName.SupportsGameServerNodes,
5782
value: supportsGameServerNodes.toString(),
5883
},
5984
on_conflict: {
@@ -80,7 +105,7 @@ export class SystemService {
80105
insert_settings_one: {
81106
__args: {
82107
object: {
83-
name: "public.supports_discord_bot",
108+
name: SystemSettingName.SupportsDiscordBot,
84109
value: supportsDiscordBot.toString(),
85110
},
86111
on_conflict: {
@@ -103,7 +128,7 @@ export class SystemService {
103128
insert_settings_one: {
104129
__args: {
105130
object: {
106-
name: "supports_game_server_version_pinning",
131+
name: SystemSettingName.SupportsGameServerVersionPinning,
107132
value: supportsGameServerNodeVersionPinning.toString(),
108133
},
109134
on_conflict: {
@@ -189,7 +214,7 @@ export class SystemService {
189214
insert_settings_one: {
190215
__args: {
191216
object: {
192-
name: "updates",
217+
name: SystemSettingName.Updates,
193218
value: JSON.stringify(hasUpdates),
194219
},
195220
on_conflict: {
@@ -409,7 +434,7 @@ export class SystemService {
409434

410435
for (const setting of settings) {
411436
switch (setting.name) {
412-
case "public.default_models":
437+
case SystemSettingName.PublicDefaultModels:
413438
await this.postgres.query(
414439
`ALTER TABLE "public"."match_options" ALTER COLUMN "default_models" SET DEFAULT ${setting.value === "true" ? true : false}`,
415440
);

0 commit comments

Comments
 (0)