Implemented GroupMessage reading (sending the right event)
Note: changed a lambda expression in LocalDB and not 100% sure if it is correct
This commit is contained in:
parent
7723db672b
commit
289f2b7032
@ -2,6 +2,7 @@ package envoy.client.data;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -9,6 +10,7 @@ import java.util.Objects;
|
|||||||
import envoy.client.net.WriteProxy;
|
import envoy.client.net.WriteProxy;
|
||||||
import envoy.data.*;
|
import envoy.data.*;
|
||||||
import envoy.data.Message.MessageStatus;
|
import envoy.data.Message.MessageStatus;
|
||||||
|
import envoy.event.GroupMessageStatusChange;
|
||||||
import envoy.event.MessageStatusChange;
|
import envoy.event.MessageStatusChange;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,18 +29,25 @@ import envoy.event.MessageStatusChange;
|
|||||||
public final class Chat implements Serializable {
|
public final class Chat implements Serializable {
|
||||||
|
|
||||||
private final Contact recipient;
|
private final Contact recipient;
|
||||||
|
private final Contact sender;
|
||||||
private final List<Message> messages = new ArrayList<>();
|
private final List<Message> messages = new ArrayList<>();
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides the list of messages that the recipient receives.<p>
|
* Provides the list of messages that the recipient receives.
|
||||||
|
* <p>
|
||||||
* Saves the Messages in the corresponding chat at that Point.
|
* Saves the Messages in the corresponding chat at that Point.
|
||||||
*
|
*
|
||||||
* @param recipient the user who receives the messages
|
* @param recipient the user who receives the messages
|
||||||
|
* @param sender the user who is logged in as sender on this instance of
|
||||||
|
* envoy.client
|
||||||
* @since Envoy Client v0.1-alpha
|
* @since Envoy Client v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public Chat(Contact recipient) { this.recipient = recipient; }
|
public Chat(Contact recipient, Contact sender) {
|
||||||
|
this.recipient = recipient;
|
||||||
|
this.sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); }
|
public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); }
|
||||||
@ -78,6 +87,17 @@ public final class Chat implements Serializable {
|
|||||||
public void read(WriteProxy writeProxy) throws IOException {
|
public void read(WriteProxy writeProxy) throws IOException {
|
||||||
for (int i = messages.size() - 1; i >= 0; --i) {
|
for (int i = messages.size() - 1; i >= 0; --i) {
|
||||||
final Message m = messages.get(i);
|
final Message m = messages.get(i);
|
||||||
|
if(m.getClass().equals(GroupMessage.class)) {
|
||||||
|
GroupMessage g = (GroupMessage) m;
|
||||||
|
if (g.getSenderID() != sender.getID()) {
|
||||||
|
if (g.getMemberStatuses().get(sender.getID()) == MessageStatus.READ) break;
|
||||||
|
else {
|
||||||
|
g.getMemberStatuses().replace(sender.getID(), MessageStatus.READ);
|
||||||
|
writeProxy.writeMessageStatusChange(
|
||||||
|
new GroupMessageStatusChange(g.getID(), MessageStatus.READ, LocalDateTime.now(), sender.getID()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break;
|
if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break;
|
||||||
else {
|
else {
|
||||||
m.setStatus(MessageStatus.READ);
|
m.setStatus(MessageStatus.READ);
|
||||||
@ -85,6 +105,7 @@ public final class Chat implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@code true} if the newest message received in the chat doesn't have
|
* @return {@code true} if the newest message received in the chat doesn't have
|
||||||
|
@ -82,7 +82,7 @@ public abstract class LocalDB {
|
|||||||
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
|
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
|
||||||
|
|
||||||
// Create missing chats
|
// Create missing chats
|
||||||
user.getContacts().stream().filter(u -> !u.equals(user) && getChat(u.getID()).isEmpty()).map(Chat::new).forEach(chats::add);
|
user.getContacts().stream().filter(u -> !u.equals(user) && getChat(u.getID()).isEmpty()).map(u -> new Chat(u, user)).forEach(chats::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,7 +146,7 @@ public final class ChatScene {
|
|||||||
switch (e.getOperationType()) {
|
switch (e.getOperationType()) {
|
||||||
case ADD:
|
case ADD:
|
||||||
localDB.getUsers().put(contact.getName(), contact);
|
localDB.getUsers().put(contact.getName(), contact);
|
||||||
localDB.getChats().add(new Chat(contact));
|
localDB.getChats().add(new Chat(contact, client.getSender()));
|
||||||
Platform.runLater(() -> userList.getItems().add(contact));
|
Platform.runLater(() -> userList.getItems().add(contact));
|
||||||
break;
|
break;
|
||||||
case REMOVE:
|
case REMOVE:
|
||||||
|
Reference in New Issue
Block a user