From 0ed71e63a394e6ac7dd0f6339794cc18766e24f1 Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 19 Oct 2019 15:09:32 +0200 Subject: [PATCH] Requesting unread messages an assigning them to the right chat element. --- src/main/java/envoy/client/Client.java | 14 +++++++++++++- src/main/java/envoy/client/ui/ChatWindow.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 8468c56..ed8ebb1 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -137,7 +137,7 @@ public class Client { private User getUser(String name) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); WebTarget target = client.target(String - .format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort())); + .format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name)); Response response = target.request("application/xml").get(); Users users = response.readEntity(Users.class); System.out.println("Response code: " + response.getStatus()); @@ -146,6 +146,18 @@ public class Client { return users.getUser().get(0); } + public Messages getUnreadMessages(long userId) { + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client.target(String + .format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId)); + Response response = target.request("application/xml").get(); + Messages unreadMessages = response.readEntity(Messages.class); + System.out.println("Response code: " + response.getStatus()); + response.close(); + client.close(); + return unreadMessages; + } + /** * @return the sender object that represents this client * @since Envoy 0.1 diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 9b07a6c..8f11ec2 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -20,11 +20,13 @@ import javax.swing.JTextArea; import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; +import javax.swing.Timer; import javax.swing.border.EmptyBorder; import envoy.client.Chat; import envoy.client.Client; import envoy.schema.Message; +import envoy.schema.Messages; import envoy.schema.User; import envoy.schema.Users; @@ -228,6 +230,19 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); loadUserList(userList); + + new Timer(5000, (evt) -> { + Messages unreadMessages = client.getUnreadMessages(client.getSender().getID()); + System.out.println(unreadMessages.getMessage().get(0)); + for (int i = 0; i < unreadMessages.getMessage().size(); i++) { + for (int j = 0; j < partnerChatList.size(); j++) { + if(partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) { + partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i)); + } + } + } + }).start(); + contentPane.revalidate(); } /**