From 4180f5a5de4a47a5af124292e5733e1f14fcdb37 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 23 Aug 2021 09:08:52 +0200 Subject: [PATCH] Added history to chat list, considering how to implement actual chat messages. --- .../java/nl/andrewl/concord_client/gui/MainWindow.java | 2 +- .../gui/{ChatPanel.java => ServerPanel.java} | 10 ++++++++-- .../event/ChatHistoryRequestHandler.java | 7 ++++--- 3 files changed, 13 insertions(+), 6 deletions(-) rename client/src/main/java/nl/andrewl/concord_client/gui/{ChatPanel.java => ServerPanel.java} (88%) diff --git a/client/src/main/java/nl/andrewl/concord_client/gui/MainWindow.java b/client/src/main/java/nl/andrewl/concord_client/gui/MainWindow.java index 91c51d4..7832f07 100644 --- a/client/src/main/java/nl/andrewl/concord_client/gui/MainWindow.java +++ b/client/src/main/java/nl/andrewl/concord_client/gui/MainWindow.java @@ -51,7 +51,7 @@ public class MainWindow extends BasicWindow { try { var client = new ConcordClient(host, port, nickname); - var chatPanel = new ChatPanel(client, this); + var chatPanel = new ServerPanel(client, this); client.addListener(chatPanel); new Thread(client).start(); this.setComponent(chatPanel); diff --git a/client/src/main/java/nl/andrewl/concord_client/gui/ChatPanel.java b/client/src/main/java/nl/andrewl/concord_client/gui/ServerPanel.java similarity index 88% rename from client/src/main/java/nl/andrewl/concord_client/gui/ChatPanel.java rename to client/src/main/java/nl/andrewl/concord_client/gui/ServerPanel.java index b2ce040..3b49dde 100644 --- a/client/src/main/java/nl/andrewl/concord_client/gui/ChatPanel.java +++ b/client/src/main/java/nl/andrewl/concord_client/gui/ServerPanel.java @@ -16,7 +16,7 @@ import java.io.IOException; * meta information in the sidebars which provides the user with a list of all * threads and users in the server. */ -public class ChatPanel extends Panel implements ClientMessageListener { +public class ServerPanel extends Panel implements ClientMessageListener { @Getter private final ChannelChatBox channelChatBox; private final ChannelList channelList; @@ -25,7 +25,7 @@ public class ChatPanel extends Panel implements ClientMessageListener { private final ConcordClient client; private final TextGUIThread guiThread; - public ChatPanel(ConcordClient client, Window window) { + public ServerPanel(ConcordClient client, Window window) { super(new BorderLayout()); this.guiThread = window.getTextGUI().getGUIThread(); this.client = client; @@ -71,6 +71,12 @@ public class ChatPanel extends Panel implements ClientMessageListener { } else if (message instanceof ChatHistoryResponse chatHistoryResponse) { System.out.println("Got chat history response: " + chatHistoryResponse.getSourceId()); System.out.println(chatHistoryResponse.getMessages()); + this.guiThread.invokeLater(() -> { + this.channelChatBox.getChatList().clearItems(); + for (var chat : chatHistoryResponse.getMessages()) { + this.channelChatBox.getChatList().addItem(chat); + } + }); } } } diff --git a/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java b/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java index 8f8fd40..70dc421 100644 --- a/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java +++ b/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java @@ -10,19 +10,20 @@ import org.dizitart.no2.FindOptions; import org.dizitart.no2.SortOrder; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.UUID; public class ChatHistoryRequestHandler implements MessageHandler { @Override - public void handle(ChatHistoryRequest msg, ClientThread client, ConcordServer server) throws Exception { + public void handle(ChatHistoryRequest msg, ClientThread client, ConcordServer server) { var optionalChannel = server.getChannelManager().getChannelById(msg.getSourceId()); if (optionalChannel.isPresent()) { var channel = optionalChannel.get(); System.out.println("Looking for chats in channel-" + channel.getId()); var col = server.getDb().getCollection("channel-" + channel.getId()); var cursor = col.find( - FindOptions.sort("timestamp", SortOrder.Ascending) + FindOptions.sort("timestamp", SortOrder.Descending) .thenLimit(0, 10) ); List chats = new ArrayList<>(10); @@ -35,7 +36,7 @@ public class ChatHistoryRequestHandler implements MessageHandler