Add nickname retrieval support: implemented NicknameMapper for database queries, updated ChatService to return player names with optional nicknames, and extended /chat/player/{uuid}/name API endpoint.
This commit is contained in:
@@ -2,6 +2,7 @@ package com.alttd.altitudeweb.controllers.chat;
|
||||
|
||||
import com.alttd.altitudeweb.api.ChatApi;
|
||||
import com.alttd.altitudeweb.model.ChatMessageDto;
|
||||
import com.alttd.altitudeweb.model.PlayerNameDto;
|
||||
import com.alttd.altitudeweb.model.ServerStateDto;
|
||||
import com.alttd.altitudeweb.services.chat.ChatService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -11,6 +12,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -31,4 +33,11 @@ public class ChatController implements ChatApi {
|
||||
//TODO [Stijn] [2026-07-18]: Implement handling server state updates (push to listeners)
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<PlayerNameDto> getName(UUID uuid) {
|
||||
return chatService.getPlayerName(uuid)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,10 @@ import com.alttd.altitudeweb.controllers.chat.ChatMessageMapper;
|
||||
import com.alttd.altitudeweb.controllers.chat.ChatMessageType;
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.chat.ChatLogMapper;
|
||||
import com.alttd.altitudeweb.database.chat.NicknameMapper;
|
||||
import com.alttd.altitudeweb.database.luckperms.UUIDUsernameMapper;
|
||||
import com.alttd.altitudeweb.model.PermissionClaimDto;
|
||||
import com.alttd.altitudeweb.model.PlayerNameDto;
|
||||
import com.alttd.altitudeweb.services.chat.event_publisher.EventPublisher;
|
||||
import com.alttd.altitudeweb.services.chat.event_publisher.EventUser;
|
||||
import com.alttd.altitudeweb.setup.Connection;
|
||||
@@ -142,4 +145,22 @@ public class ChatService {
|
||||
return chatMessages.tailMap(instant, false).values().stream().toList();
|
||||
}
|
||||
|
||||
public Optional<PlayerNameDto> getPlayerName(UUID uuid) {
|
||||
String username = Connection.getConnection(Databases.LUCK_PERMS)
|
||||
.runQueryWithResult(sqlSession -> sqlSession.getMapper(UUIDUsernameMapper.class).getUsernameFromUUID(uuid.toString()));
|
||||
|
||||
if (username == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Optional<String> nickname = Connection.getConnection(Databases.CHAT)
|
||||
.runQueryWithResult(sqlSession -> sqlSession.getMapper(NicknameMapper.class).getNicknameFromUUID(uuid.toString()));
|
||||
|
||||
PlayerNameDto playerNameDto = new PlayerNameDto();
|
||||
playerNameDto.setName(username);
|
||||
playerNameDto.setNickname(nickname.orElse(null));
|
||||
|
||||
return Optional.of(playerNameDto);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class ChatServiceTest {
|
||||
verify(eventPublisher).sendToUsers(anyString(), captor.capture());
|
||||
|
||||
MessageForUser messageForUser = captor.getValue();
|
||||
EventUser user = new EventUser(UUID.randomUUID(), List.of());
|
||||
EventUser user = new EventUser(UUID.randomUUID(), List.of(), null);
|
||||
String result = messageForUser.get(user);
|
||||
|
||||
assertTrue(result.contains("hello"));
|
||||
@@ -73,10 +73,10 @@ class ChatServiceTest {
|
||||
|
||||
MessageForUser messageForUser = captor.getValue();
|
||||
|
||||
EventUser regularUser = new EventUser(UUID.randomUUID(), List.of());
|
||||
EventUser regularUser = new EventUser(UUID.randomUUID(), List.of(), null);
|
||||
assertFalse(messageForUser.get(regularUser).contains("staff chat"), "Regular user should not see GAC message");
|
||||
|
||||
EventUser modUser = new EventUser(UUID.randomUUID(), List.of(PermissionClaimDto.MOD.getValue()));
|
||||
EventUser modUser = new EventUser(UUID.randomUUID(), List.of(PermissionClaimDto.MOD.getValue()), null);
|
||||
assertTrue(messageForUser.get(modUser).contains("staff chat"), "MOD should see GAC message");
|
||||
}
|
||||
|
||||
@@ -101,9 +101,9 @@ class ChatServiceTest {
|
||||
|
||||
MessageForUser messageForUser = captor.getValue();
|
||||
|
||||
EventUser sender = new EventUser(senderUuid, List.of());
|
||||
EventUser receiver = new EventUser(receiverUuid, List.of());
|
||||
EventUser other = new EventUser(UUID.randomUUID(), List.of());
|
||||
EventUser sender = new EventUser(senderUuid, List.of(), null);
|
||||
EventUser receiver = new EventUser(receiverUuid, List.of(), null);
|
||||
EventUser other = new EventUser(UUID.randomUUID(), List.of(), null);
|
||||
|
||||
assertTrue(messageForUser.get(sender).contains("private message"), "Sender should see their own message");
|
||||
assertTrue(messageForUser.get(receiver).contains("private message"), "Receiver should see the message");
|
||||
@@ -127,7 +127,7 @@ class ChatServiceTest {
|
||||
verify(eventPublisher).sendToUsers(anyString(), captor.capture());
|
||||
|
||||
MessageForUser messageForUser = captor.getValue();
|
||||
EventUser user = new EventUser(UUID.randomUUID(), List.of());
|
||||
EventUser user = new EventUser(UUID.randomUUID(), List.of(), null);
|
||||
assertTrue(messageForUser.get(user).contains("global hello"));
|
||||
}
|
||||
|
||||
@@ -148,8 +148,8 @@ class ChatServiceTest {
|
||||
verify(eventPublisher).sendToUsers(anyString(), captor.capture());
|
||||
|
||||
MessageForUser messageForUser = captor.getValue();
|
||||
EventUser regularUser = new EventUser(UUID.randomUUID(), List.of());
|
||||
EventUser headModUser = new EventUser(UUID.randomUUID(), List.of(PermissionClaimDto.HEAD_MOD.getValue()));
|
||||
EventUser regularUser = new EventUser(UUID.randomUUID(), List.of(), null);
|
||||
EventUser headModUser = new EventUser(UUID.randomUUID(), List.of(PermissionClaimDto.HEAD_MOD.getValue()), null);
|
||||
|
||||
assertFalse(messageForUser.get(regularUser).contains("blocked message"));
|
||||
assertTrue(messageForUser.get(headModUser).contains("blocked message"));
|
||||
|
||||
Reference in New Issue
Block a user