package envoy.client.data; import java.io.IOException; import java.io.Serializable; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; import envoy.client.net.WriteProxy; import envoy.data.*; import envoy.data.Message.MessageStatus; import envoy.event.GroupMessageStatusChange; import envoy.event.MessageStatusChange; /** * Represents a chat between two {@link User}s * as a list of {@link Message} objects. *
* Project: envoy-client
* Saves the Messages in the corresponding chat at that Point.
*
* @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
*/
public Chat(Contact recipient, Contact sender) {
this.recipient = recipient;
this.sender = sender;
}
@Override
public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); }
/**
* Generates a hash code based on the recipient.
*
* @since Envoy Client v0.1-beta
*/
@Override
public int hashCode() { return Objects.hash(recipient); }
/**
* Tests equality to another object based on the recipient.
*
* @since Envoy Client v0.1-beta
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Chat)) return false;
Chat other = (Chat) obj;
return Objects.equals(recipient, other.recipient);
}
/**
* Sets the status of all chat messages received from the recipient to
* {@code READ} starting from the bottom and stopping once a read message is
* found.
*
* @param writeProxy the write proxy instance used to notify the server about
* the message status changes
* @throws IOException if a {@link MessageStatusChange} could not be
* delivered to the server
* @since Envoy Client v0.3-alpha
*/
public void read(WriteProxy writeProxy) throws IOException {
for (int i = messages.size() - 1; i >= 0; --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;
else {
m.setStatus(MessageStatus.READ);
writeProxy.writeMessageStatusChange(new MessageStatusChange(m));
}
}
}
}
/**
* @return {@code true} if the newest message received in the chat doesn't have
* the status {@code READ}
* @since Envoy Client v0.3-alpha
*/
public boolean isUnread() { return !messages.isEmpty() && messages.get(messages.size() - 1).getStatus() != MessageStatus.READ; }
/**
* Inserts a message at the correct place according to its creation date.
*
* @param message the message to insert
* @since Envoy Client v0.1-beta
*/
public void insert(Message message) {
for (int i = messages.size() - 1; i >= 0; --i)
if (message.getCreationDate().isAfter(messages.get(i).getCreationDate())) {
messages.add(i + 1, message);
return;
}
messages.add(0, message);
}
/**
* @return all messages in the current chat
* @since Envoy Client v0.1-beta
*/
public List
* File: Chat.java
* Created: 19 Oct 2019
*
* @author Maximilian Käfer
* @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-alpha
*/
public final class Chat implements Serializable {
private final Contact recipient;
private final Contact sender;
private final List