Added history to chat list, considering how to implement actual chat messages.

This commit is contained in:
Andrew Lalis 2021-08-23 09:08:52 +02:00
parent fcfea0f70f
commit 4180f5a5de
3 changed files with 13 additions and 6 deletions

View File

@ -51,7 +51,7 @@ public class MainWindow extends BasicWindow {
try { try {
var client = new ConcordClient(host, port, nickname); var client = new ConcordClient(host, port, nickname);
var chatPanel = new ChatPanel(client, this); var chatPanel = new ServerPanel(client, this);
client.addListener(chatPanel); client.addListener(chatPanel);
new Thread(client).start(); new Thread(client).start();
this.setComponent(chatPanel); this.setComponent(chatPanel);

View File

@ -16,7 +16,7 @@ import java.io.IOException;
* meta information in the sidebars which provides the user with a list of all * meta information in the sidebars which provides the user with a list of all
* threads and users in the server. * threads and users in the server.
*/ */
public class ChatPanel extends Panel implements ClientMessageListener { public class ServerPanel extends Panel implements ClientMessageListener {
@Getter @Getter
private final ChannelChatBox channelChatBox; private final ChannelChatBox channelChatBox;
private final ChannelList channelList; private final ChannelList channelList;
@ -25,7 +25,7 @@ public class ChatPanel extends Panel implements ClientMessageListener {
private final ConcordClient client; private final ConcordClient client;
private final TextGUIThread guiThread; private final TextGUIThread guiThread;
public ChatPanel(ConcordClient client, Window window) { public ServerPanel(ConcordClient client, Window window) {
super(new BorderLayout()); super(new BorderLayout());
this.guiThread = window.getTextGUI().getGUIThread(); this.guiThread = window.getTextGUI().getGUIThread();
this.client = client; this.client = client;
@ -71,6 +71,12 @@ public class ChatPanel extends Panel implements ClientMessageListener {
} else if (message instanceof ChatHistoryResponse chatHistoryResponse) { } else if (message instanceof ChatHistoryResponse chatHistoryResponse) {
System.out.println("Got chat history response: " + chatHistoryResponse.getSourceId()); System.out.println("Got chat history response: " + chatHistoryResponse.getSourceId());
System.out.println(chatHistoryResponse.getMessages()); System.out.println(chatHistoryResponse.getMessages());
this.guiThread.invokeLater(() -> {
this.channelChatBox.getChatList().clearItems();
for (var chat : chatHistoryResponse.getMessages()) {
this.channelChatBox.getChatList().addItem(chat);
}
});
} }
} }
} }

View File

@ -10,19 +10,20 @@ import org.dizitart.no2.FindOptions;
import org.dizitart.no2.SortOrder; import org.dizitart.no2.SortOrder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class ChatHistoryRequestHandler implements MessageHandler<ChatHistoryRequest> { public class ChatHistoryRequestHandler implements MessageHandler<ChatHistoryRequest> {
@Override @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()); var optionalChannel = server.getChannelManager().getChannelById(msg.getSourceId());
if (optionalChannel.isPresent()) { if (optionalChannel.isPresent()) {
var channel = optionalChannel.get(); var channel = optionalChannel.get();
System.out.println("Looking for chats in channel-" + channel.getId()); System.out.println("Looking for chats in channel-" + channel.getId());
var col = server.getDb().getCollection("channel-" + channel.getId()); var col = server.getDb().getCollection("channel-" + channel.getId());
var cursor = col.find( var cursor = col.find(
FindOptions.sort("timestamp", SortOrder.Ascending) FindOptions.sort("timestamp", SortOrder.Descending)
.thenLimit(0, 10) .thenLimit(0, 10)
); );
List<Chat> chats = new ArrayList<>(10); List<Chat> chats = new ArrayList<>(10);
@ -35,7 +36,7 @@ public class ChatHistoryRequestHandler implements MessageHandler<ChatHistoryRequ
)); ));
} }
col.close(); col.close();
System.out.println(chats); chats.sort(Comparator.comparingLong(Chat::getTimestamp));
client.sendToClient(new ChatHistoryResponse(msg.getSourceId(), msg.getSourceType(), chats)); client.sendToClient(new ChatHistoryResponse(msg.getSourceId(), msg.getSourceType(), chats));
} }
} }