Introduce chat database integration with message persistence and initialization.

This commit is contained in:
2026-07-18 22:57:55 +02:00
parent 9ebd420874
commit 646bbd4145
8 changed files with 93 additions and 2 deletions
@@ -21,7 +21,6 @@ public class ChatController implements ChatApi {
@Override
public ResponseEntity<Void> sendChatMessages(List<ChatMessageDto> chatMessageDtoList) {
//TODO [Admin] [2026-07-18]: Implement handling chat messages (push to listeners)
chatMessageDtoList.stream().map(ChatMessageMapper::fromDto).forEach(chatService::addChatMessage);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
@@ -1,8 +1,12 @@
package com.alttd.altitudeweb.controllers.chat;
import com.alttd.altitudeweb.database.chat.ChatLogDao;
import com.alttd.altitudeweb.model.ChatMessageDto;
import lombok.experimental.UtilityClass;
import java.time.Instant;
import java.util.UUID;
@UtilityClass
public class ChatMessageMapper {
@@ -16,4 +20,14 @@ public class ChatMessageMapper {
.build();
}
public ChatMessage fromDao(ChatLogDao dao) {
return ChatMessage.builder()
.uuid(UUID.fromString(dao.getUuid()))
.timestamp(Instant.ofEpochMilli(dao.getTimeStamp()))
.server(dao.getServer())
.messageJson(dao.getMiniMessage())
.blocked(dao.isBlocked())
.build();
}
}
@@ -1,7 +1,15 @@
package com.alttd.altitudeweb.services.chat;
import com.alttd.altitudeweb.controllers.chat.ChatMessage;
import com.alttd.altitudeweb.controllers.chat.ChatMessageMapper;
import com.alttd.altitudeweb.database.Databases;
import com.alttd.altitudeweb.database.chat.ChatLogDao;
import com.alttd.altitudeweb.database.chat.ChatLogMapper;
import com.alttd.altitudeweb.database.litebans.HistoryCountMapper;
import com.alttd.altitudeweb.setup.Connection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@@ -27,6 +35,22 @@ public class ChatService {
currentSize - chatMessages.size(), currentSize);
}
@EventListener(ApplicationReadyEvent.class)
public void loadMessagesFromDb() {
Connection.getConnection(Databases.CHAT)
.runQuery(sqlSession -> {
log.debug("Loading uuid search result count");
try {
sqlSession.getMapper(ChatLogMapper.class)
.getChatLogs(Instant.now().minus(Duration.ofHours(1)).toEpochMilli())
.stream().map(ChatMessageMapper::fromDao)
.forEach(this::addChatMessage);
} catch (Exception e) {
log.error("Failed to load history count", e);
}
});
}
public void addChatMessage(ChatMessage chatMessage) {
//TODO [Stijn] [2026-07-18]: remove log info after verifying it works
log.info("Adding chat message: {}", chatMessage);