Introduce chat database integration with message persistence and initialization.
This commit is contained in:
parent
9ebd420874
commit
646bbd4145
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ public enum Databases {
|
|||
LITE_BANS("litebans"),
|
||||
DISCORD("discordLink"),
|
||||
PROXY_PLAYTIME("proxyplaytime"),
|
||||
VOTING_PLUGIN("votingplugin");
|
||||
VOTING_PLUGIN("votingplugin"),
|
||||
CHAT("chat");
|
||||
|
||||
private final String internalName;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package com.alttd.altitudeweb.database.chat;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class ChatLogDao {
|
||||
|
||||
private final String uuid;
|
||||
private final long timeStamp;
|
||||
private final String server;
|
||||
private final String miniMessage;
|
||||
private final boolean blocked;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.alttd.altitudeweb.database.chat;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ChatLogMapper {
|
||||
|
||||
@Select("""
|
||||
SELECT uuid, time_stamp, server, mini_message, blocked
|
||||
FROM chat_log
|
||||
WHERE mini_message IS NOT NULL
|
||||
AND time_stamp >= #{since}
|
||||
""")
|
||||
List<ChatLogDao> getChatLogs(@Param("since") long since);
|
||||
|
||||
}
|
||||
|
|
@ -40,6 +40,7 @@ public class Connection {
|
|||
InitializeProxyPlaytime.init();
|
||||
InitializeDiscord.init();
|
||||
InitializeVotingPlugin.init();
|
||||
InitializeChat.init();
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package com.alttd.altitudeweb.setup;
|
||||
|
||||
import com.alttd.altitudeweb.database.Databases;
|
||||
import com.alttd.altitudeweb.database.chat.ChatLogMapper;
|
||||
import com.alttd.altitudeweb.database.votingplugin.VotingPluginUsersMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class InitializeChat {
|
||||
|
||||
protected static void init() {
|
||||
log.info("Initializing Chat");
|
||||
Connection.getConnection(Databases.CHAT, (configuration) -> {
|
||||
configuration.addMapper(ChatLogMapper.class);
|
||||
}).join();
|
||||
log.debug("Initialized Chat");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user