2020-02-05 20:58:30 +01:00
|
|
|
package envoy.client.data;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-02-05 07:09:25 +01:00
|
|
|
import java.io.IOException;
|
2019-12-21 21:23:19 +01:00
|
|
|
import java.io.Serializable;
|
2020-03-28 15:32:24 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-06-12 10:48:33 +02:00
|
|
|
import java.util.Objects;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-02-06 21:28:02 +01:00
|
|
|
import envoy.client.net.WriteProxy;
|
2020-04-02 09:23:47 +02:00
|
|
|
import envoy.data.*;
|
2020-02-01 10:20:06 +01:00
|
|
|
import envoy.data.Message.MessageStatus;
|
2020-06-20 10:00:38 +02:00
|
|
|
import envoy.event.MessageStatusChange;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-26 23:36:14 +02:00
|
|
|
* Represents a chat between two {@link User}s
|
2019-12-21 21:23:19 +01:00
|
|
|
* as a list of {@link Message} objects.
|
2020-06-26 23:36:14 +02:00
|
|
|
* <p>
|
2019-12-21 21:23:19 +01:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Chat.java</strong><br>
|
|
|
|
* Created: <strong>19 Oct 2019</strong><br>
|
|
|
|
*
|
|
|
|
* @author Maximilian Käfer
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2019-12-21 21:23:19 +01:00
|
|
|
*/
|
2020-03-28 15:32:24 +01:00
|
|
|
public final class Chat implements Serializable {
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-04-02 09:23:47 +02:00
|
|
|
private final Contact recipient;
|
2020-03-28 15:32:24 +01:00
|
|
|
private final List<Message> messages = new ArrayList<>();
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-03-28 15:32:24 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-26 23:36:14 +02:00
|
|
|
* Provides the list of messages that the recipient receives.<p>
|
2019-12-21 21:23:19 +01:00
|
|
|
* Saves the Messages in the corresponding chat at that Point.
|
|
|
|
*
|
|
|
|
* @param recipient the user who receives the messages
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2019-12-21 21:23:19 +01:00
|
|
|
*/
|
2020-04-02 09:23:47 +02:00
|
|
|
public Chat(Contact recipient) { this.recipient = recipient; }
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-03-29 09:16:29 +02:00
|
|
|
@Override
|
|
|
|
public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); }
|
|
|
|
|
2020-06-12 10:48:33 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2019-12-21 21:23:19 +01:00
|
|
|
/**
|
2020-02-03 06:57:19 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2020-02-06 21:28:02 +01:00
|
|
|
* @param writeProxy the write proxy instance used to notify the server about
|
|
|
|
* the message status changes
|
2020-06-20 10:00:38 +02:00
|
|
|
* @throws IOException if a {@link MessageStatusChange} could not be
|
2020-02-05 07:09:25 +01:00
|
|
|
* delivered to the server
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-21 21:23:19 +01:00
|
|
|
*/
|
2020-02-06 21:28:02 +01:00
|
|
|
public void read(WriteProxy writeProxy) throws IOException {
|
2020-03-28 15:32:24 +01:00
|
|
|
for (int i = messages.size() - 1; i >= 0; --i) {
|
|
|
|
final Message m = messages.get(i);
|
2020-03-26 16:06:18 +01:00
|
|
|
if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break;
|
2020-03-14 19:59:37 +01:00
|
|
|
else {
|
|
|
|
m.setStatus(MessageStatus.READ);
|
2020-06-20 10:00:38 +02:00
|
|
|
writeProxy.writeMessageStatusChange(new MessageStatusChange(m));
|
2020-02-03 06:57:19 +01:00
|
|
|
}
|
2020-02-05 07:09:25 +01:00
|
|
|
}
|
2020-02-01 10:20:06 +01:00
|
|
|
}
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-02-03 22:06:56 +01:00
|
|
|
/**
|
|
|
|
* @return {@code true} if the newest message received in the chat doesn't have
|
|
|
|
* the status {@code READ}
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 22:06:56 +01:00
|
|
|
*/
|
2020-03-28 15:32:24 +01:00
|
|
|
public boolean isUnread() { return !messages.isEmpty() && messages.get(messages.size() - 1).getStatus() != MessageStatus.READ; }
|
2020-02-03 22:06:56 +01:00
|
|
|
|
2019-12-21 21:23:19 +01:00
|
|
|
/**
|
|
|
|
* @return all messages in the current chat
|
2020-03-28 15:32:24 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2019-12-21 21:23:19 +01:00
|
|
|
*/
|
2020-03-28 15:32:24 +01:00
|
|
|
public List<Message> getMessages() { return messages; }
|
2020-02-01 10:20:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the recipient of a message
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2020-02-01 10:20:06 +01:00
|
|
|
*/
|
2020-04-02 09:23:47 +02:00
|
|
|
public Contact getRecipient() { return recipient; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return whether this {@link Chat} points at a {@link User}
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public boolean isUserChat() { return recipient instanceof User; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return whether this {@link Chat} points at a {@link Group}
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public boolean isGroupChat() { return recipient instanceof Group; }
|
2020-03-14 19:59:37 +01:00
|
|
|
}
|