Add support for miniMessage serialization in chat logs
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
|
||||
import com.alttd.chat.objects.BatchInsertable;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -15,13 +17,15 @@ public class ChatLog implements BatchInsertable {
|
||||
private final Instant timestamp;
|
||||
private final String server;
|
||||
private final String message;
|
||||
private final Component miniMessage;
|
||||
private final boolean blocked;
|
||||
|
||||
protected ChatLog(UUID uuid, Instant timestamp, String server, String message, boolean blocked) {
|
||||
protected ChatLog(UUID uuid, Instant timestamp, String server, String message, Component miniMessage, boolean blocked) {
|
||||
this.uuid = uuid;
|
||||
this.timestamp = timestamp;
|
||||
this.server = server;
|
||||
this.message = message;
|
||||
this.miniMessage = miniMessage;
|
||||
this.blocked = blocked;
|
||||
}
|
||||
|
||||
@@ -31,7 +35,10 @@ public class ChatLog implements BatchInsertable {
|
||||
preparedStatement.setTimestamp(2, Timestamp.from(timestamp));
|
||||
preparedStatement.setString(3, server);
|
||||
preparedStatement.setString(4, message);
|
||||
preparedStatement.setInt(5, blocked ? 1 : 0);
|
||||
preparedStatement.setString(5,
|
||||
miniMessage == null ? null : GsonComponentSerializer.gson().serialize(miniMessage)
|
||||
);
|
||||
preparedStatement.setInt(6, blocked ? 1 : 0);
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.alttd.chat.objects.chat_log;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.ChatLogQueries;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
@@ -18,8 +20,9 @@ public class ChatLogHandler {
|
||||
private ScheduledExecutorService executorService = null;
|
||||
|
||||
public static ChatLogHandler getInstance(boolean enableLogging) {
|
||||
if (instance == null)
|
||||
if (instance == null) {
|
||||
instance = new ChatLogHandler(enableLogging);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@@ -35,23 +38,30 @@ public class ChatLogHandler {
|
||||
Duration deleteThreshold = Duration.ofDays(Config.CHAT_LOG_DELETE_OLDER_THAN_DAYS);
|
||||
ChatLogQueries.deleteOldMessages(deleteThreshold).thenAccept(success -> {
|
||||
if (success) {
|
||||
ALogger.info(String.format("Deleted all messages older than %s days from chat log database.", deleteThreshold.toDays()));
|
||||
ALogger.info(String.format("Deleted all messages older than %s days from chat log database.",
|
||||
deleteThreshold.toDays()
|
||||
));
|
||||
} else {
|
||||
ALogger.warn(String.format("Failed to delete all messages older than %s days from chat log database.", deleteThreshold.toDays()));
|
||||
ALogger.warn(String.format("Failed to delete all messages older than %s days from chat log database.",
|
||||
deleteThreshold.toDays()
|
||||
));
|
||||
}
|
||||
});
|
||||
executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
executorService.scheduleAtFixedRate(() -> {
|
||||
saveToDatabase(false);
|
||||
ALogger.info(String.format("Running scheduler to save messages with a %d delay", Config.CHAT_LOG_SAVE_DELAY_MINUTES));
|
||||
ALogger.info(String.format("Running scheduler to save messages with a %d delay",
|
||||
Config.CHAT_LOG_SAVE_DELAY_MINUTES
|
||||
));
|
||||
},
|
||||
Config.CHAT_LOG_SAVE_DELAY_MINUTES, Config.CHAT_LOG_SAVE_DELAY_MINUTES, TimeUnit.MINUTES);
|
||||
Config.CHAT_LOG_SAVE_DELAY_MINUTES, Config.CHAT_LOG_SAVE_DELAY_MINUTES, TimeUnit.MINUTES
|
||||
);
|
||||
ALogger.info("Logging has started!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the executor service and saves the chat logs to the database.
|
||||
* Will throw an error if called on a ChatLogHandler that was started without logging
|
||||
* Shuts down the executor service and saves the chat logs to the database. Will throw an error if called on a
|
||||
* ChatLogHandler that was started without logging
|
||||
*/
|
||||
public void shutDown() {
|
||||
executorService.shutdown();
|
||||
@@ -66,7 +76,7 @@ public class ChatLogHandler {
|
||||
return isSaving;
|
||||
}
|
||||
|
||||
public synchronized void addLog(ChatLog chatLog) {
|
||||
private synchronized void addLog(ChatLog chatLog) {
|
||||
if (isBlocked()) {
|
||||
chatLogQueue.add(chatLog);
|
||||
} else {
|
||||
@@ -105,12 +115,15 @@ public class ChatLogHandler {
|
||||
Instant chatTimestamp = resultSet.getTimestamp("time_stamp").toInstant();
|
||||
String server = resultSet.getString("server");
|
||||
String chatMessage = resultSet.getString("chat_message");
|
||||
String stringMiniMessage = resultSet.getString("mini_message");
|
||||
Component miniMessage = stringMiniMessage == null ? null : GsonComponentSerializer.gson().deserialize(
|
||||
stringMiniMessage);
|
||||
boolean chatMessageBlocked = resultSet.getInt("blocked") == 1;
|
||||
return new ChatLog(chatLogUUID, chatTimestamp, server, chatMessage, chatMessageBlocked);
|
||||
return new ChatLog(chatLogUUID, chatTimestamp, server, chatMessage, miniMessage, chatMessageBlocked);
|
||||
}
|
||||
|
||||
public void addChatLog(UUID uuid, String server, String message, boolean blocked) {
|
||||
addLog(new ChatLog(uuid, Instant.now(), server, message, blocked));
|
||||
public void addChatLog(UUID uuid, String server, String message, Component miniMessage, boolean blocked) {
|
||||
addLog(new ChatLog(uuid, Instant.now(), server, message, miniMessage, blocked));
|
||||
}
|
||||
|
||||
public CompletableFuture<List<ChatLog>> retrieveChatLogs(UUID uuid, Duration duration, String server) {
|
||||
|
||||
Reference in New Issue
Block a user