Add partially working blocking and deletion (for both client and server)

Additionally had to refactor several classes "a little bit".
(Whenever one bug seemed fixed, another one appeared...)
This commit is contained in:
2020-10-11 23:04:25 +02:00
parent f97d61e58d
commit 7bdcfad08c
20 changed files with 326 additions and 94 deletions

View File

@ -27,6 +27,8 @@ public class Chat implements Serializable {
protected int unreadAmount;
private boolean disabled;
/**
* Stores the last time an {@link envoy.event.IsTyping} event has been sent.
*/
@ -168,4 +170,22 @@ public class Chat implements Serializable {
* @since Envoy Client v0.2-beta
*/
public void lastWritingEventWasNow() { lastWritingEvent = System.currentTimeMillis(); }
/**
* Determines whether messages can be sent in this chat. Should be true i.e. for
* chats whose recipient deleted this client as a contact.
*
* @return whether this {@code Chat} has been disabled
* @since Envoy Client v0.3-beta
*/
public boolean isDisabled() { return disabled; }
/**
* Determines whether messages can be sent in this chat. Should be true i.e. for
* chats whose recipient deleted this client as a contact.
*
* @param disabled the disabled to set
* @since Envoy Client v0.3-beta
*/
public void setDisabled(boolean disabled) { this.disabled = disabled; }
}

View File

@ -25,7 +25,7 @@ public final class GroupChat extends Chat {
* @param recipient the group whose members receive the messages
* @since Envoy Client v0.1-beta
*/
public GroupChat(User sender, Contact recipient) {
public GroupChat(User sender, Group recipient) {
super(recipient);
this.sender = sender;
}

View File

@ -14,6 +14,7 @@ import envoy.client.event.*;
import envoy.data.*;
import envoy.data.Message.MessageStatus;
import envoy.event.*;
import envoy.event.contact.*;
import envoy.exception.EnvoyException;
import envoy.util.*;
@ -34,11 +35,12 @@ public final class LocalDB implements EventListener {
// Data
private User user;
private Map<String, User> users = Collections.synchronizedMap(new HashMap<>());
private ObservableList<Chat> chats = FXCollections.observableArrayList();
private Map<String, User> users = Collections.synchronizedMap(new HashMap<>());
private ObservableList<Chat> chats = FXCollections.observableArrayList();
private IDGenerator idGenerator;
private CacheMap cacheMap = new CacheMap();
private CacheMap cacheMap = new CacheMap();
private String authToken;
private Set<? extends Contact> originalContacts = Set.of();
// Auto save timer
private Timer autoSaver;
@ -163,7 +165,7 @@ public final class LocalDB implements EventListener {
user.getContacts()
.stream()
.filter(c -> !c.equals(user) && getChat(c.getID()).isEmpty())
.map(c -> c instanceof User ? new Chat(c) : new GroupChat(user, c))
.map(c -> c instanceof User ? new Chat(c) : new GroupChat(user, (Group) c))
.forEach(chats::add);
}
@ -197,7 +199,7 @@ public final class LocalDB implements EventListener {
*/
@Event(eventType = EnvoyCloseEvent.class, priority = 1000)
private synchronized void save() {
EnvoyLog.getLogger(LocalDB.class).log(Level.INFO, "Saving local database...");
EnvoyLog.getLogger(LocalDB.class).log(Level.FINER, "Saving local database...");
// Save users
try {
@ -240,6 +242,31 @@ public final class LocalDB implements EventListener {
getChat(evt.getID()).map(Chat::getRecipient).map(User.class::cast).ifPresent(u -> u.setStatus(evt.get()));
}
@Event(priority = 200)
private void onUserOperation(UserOperation operation) {
final var eventUser = operation.get();
switch (operation.getOperationType()) {
case ADD:
getUsers().put(eventUser.getName(), eventUser);
Platform.runLater(() -> chats.add(0, new Chat(eventUser)));
break;
case REMOVE:
getChat(eventUser.getID()).ifPresent(chat -> chat.setDisabled(true));
break;
}
}
@Event
private void onGroupCreationResult(GroupCreationResult evt) {
final var newGroup = evt.get();
// The group creation was not successful
if (newGroup == null) return;
// The group was successfully created
else Platform.runLater(() -> chats.add(new GroupChat(user, newGroup)));
}
@Event(priority = 150)
private void onGroupResize(GroupResize evt) { getChat(evt.getGroupID()).map(Chat::getRecipient).map(Group.class::cast).ifPresent(evt::apply); }
@ -296,6 +323,9 @@ public final class LocalDB implements EventListener {
@Event(priority = 500)
private void onOwnStatusChange(OwnStatusChange statusChange) { user.setStatus(statusChange.get()); }
@Event(eventType = ContactsChangedSinceLastLogin.class, priority = 500)
private void onContactsChangedSinceLastLogin() { if (user != null) originalContacts = user.getContacts(); }
/**
* @return a {@code Map<String, User>} of all users stored locally with their
* user names as keys
@ -323,6 +353,25 @@ public final class LocalDB implements EventListener {
*/
public Optional<Chat> getChat(long recipientID) { return chats.stream().filter(c -> c.getRecipient().getID() == recipientID).findAny(); }
/**
* Sets the given user as base of this {@code LocalDB}.
* Additionally compares his contacts with the ones returned by the server, in
* case they might have been deleted.
*
* @param user the user to set
* @since Envoy Client v0.2-alpha
*/
public void setUserAndMergeContacts(User user) {
this.user = user;
if (originalContacts.isEmpty()) return;
else {
// mark all chats of deleted contacts
originalContacts.removeAll(user.getContacts());
originalContacts.forEach(contact -> getChat(contact.getID()).ifPresent(chat -> chat.setDisabled(true)));
}
}
/**
* @return all saved {@link Chat} objects that list the client user as the
* sender