Skip to content

Commit 44daada

Browse files
committed
bug: fix issue with chat message not showing proper name
1 parent 79d39ab commit 44daada

6 files changed

Lines changed: 49 additions & 4 deletions

File tree

src/auth/auth.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export class AuthController {
4949
public async me(@Req() request: Request) {
5050
const user = request.user;
5151

52+
user.name = await this.cache.get(
53+
HasuraService.PLAYER_NAME_CACHE_KEY(request.user.steam_id),
54+
request.user.name,
55+
);
56+
5257
user.role = await this.cache.get(
5358
HasuraService.PLAYER_ROLE_CACHE_KEY(request.user.steam_id),
5459
);

src/auth/strategies/SteamSerializer.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import { PassportSerializer } from "@nestjs/passport";
22
import { Inject, Injectable, Logger } from "@nestjs/common";
33
import { User } from "../types/User";
4+
import { CacheService } from "../../cache/cache.service";
5+
import { HasuraService } from "../../hasura/hasura.service";
46

57
@Injectable()
68
export class SteamSerializer extends PassportSerializer {
79
@Inject()
810
private readonly logger: Logger;
911

12+
@Inject()
13+
private readonly cache: CacheService;
14+
1015
serializeUser(user: User, done: CallableFunction) {
16+
void this.cache
17+
.forget(HasuraService.PLAYER_NAME_CACHE_KEY(user.steam_id))
18+
.catch((error) => {
19+
this.logger.error("unable to forget player name cache", error);
20+
});
21+
22+
void this.cache
23+
.forget(HasuraService.PLAYER_ROLE_CACHE_KEY(user.steam_id))
24+
.catch((error) => {
25+
this.logger.error("unable to forget player role cache", error);
26+
});
27+
1128
done(null, user);
1229
}
1330

src/cache/cache.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ export class CacheService {
1515
this.connection = redis.getConnection();
1616
}
1717

18-
public async get(key: string) {
18+
public async get(key: string, defaultValue?: any) {
1919
const value = await this.connection.get(key);
2020

2121
if (value !== null && value !== undefined) {
2222
return JSON.parse(value);
2323
}
24+
return defaultValue;
2425
}
2526

2627
public async has(key: string) {

src/chat/chat.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HasuraService } from "../hasura/hasura.service";
66
import { RconService } from "../rcon/rcon.service";
77
import { FiveStackWebSocketClient } from "src/sockets/types/FiveStackWebSocketClient";
88
import { ChatLobbyType } from "./enums/ChatLobbyTypes";
9+
import { e_player_roles_enum } from "generated/schema";
910

1011
@Injectable()
1112
export class ChatService {
@@ -151,13 +152,21 @@ export class ChatService {
151152
}
152153
}
153154

155+
const name = await this.redis.get(
156+
HasuraService.PLAYER_NAME_CACHE_KEY(player.steam_id),
157+
);
158+
159+
const role: e_player_roles_enum = (await this.redis.get(
160+
HasuraService.PLAYER_ROLE_CACHE_KEY(player.steam_id),
161+
)) as unknown as e_player_roles_enum;
162+
154163
const timestamp = new Date();
155164
const message = {
156165
message: _message,
157166
timestamp: timestamp.toISOString(),
158167
from: {
159-
role: player.role,
160-
name: player.name,
168+
role: name ? JSON.parse(role) : player.role,
169+
name: name ? JSON.parse(name) : player.name,
161170
steam_id: player.steam_id,
162171
avatar_url: player.avatar_url,
163172
profile_url: player.profile_url,

src/hasura/hasura.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ export class HasuraService {
3131
this.config = configService.get<HasuraConfig>("hasura");
3232
}
3333

34+
public static PLAYER_NAME_CACHE_KEY(steamId: bigint | string) {
35+
return `user:name:${steamId.toString()}`;
36+
}
37+
3438
public static PLAYER_ROLE_CACHE_KEY(steamId: bigint | string) {
35-
return `user:${steamId.toString()}`;
39+
return `user:role:${steamId.toString()}`;
3640
}
3741

3842
public checkSecret(secret: string) {

src/type-sense/type-sense.controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ export class TypeSenseController {
3939
),
4040
);
4141

42+
if (data.new.name && data.new.name !== data.old.name) {
43+
await this.cache.put(
44+
HasuraService.PLAYER_NAME_CACHE_KEY(
45+
`${data.new.steam_id || data.old.steam_id}`,
46+
),
47+
data.new.name,
48+
);
49+
}
50+
4251
if (data.op === "DELETE") {
4352
await this.typeSense.removePlayer(data.old.steam_id);
4453
return;

0 commit comments

Comments
 (0)