From 43981c92720007504a38b27f1c9275f33df59329 Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 28 Sep 2020 23:44:47 +0200 Subject: [PATCH 1/4] Added option to delete messages - for now only for the client locally --- .../src/main/java/envoy/client/data/Chat.java | 9 ++ .../main/java/envoy/client/data/LocalDB.java | 21 ++++- .../client/ui/control/MessageControl.java | 83 ++++++++++++++----- .../envoy/client/ui/controller/ChatScene.java | 9 ++ .../java/envoy/event/MessageDeletion.java | 34 ++++++++ .../src/main/java/envoy/server/Startup.java | 3 +- .../main/java/envoy/server/data/Contact.java | 30 ++++++- .../envoy/server/data/MessageDeletion.java | 81 ++++++++++++++++++ .../envoy/server/data/PersistenceManager.java | 41 ++++++++- .../processors/MessageDeletionProcessor.java | 18 ++++ 10 files changed, 299 insertions(+), 30 deletions(-) create mode 100644 common/src/main/java/envoy/event/MessageDeletion.java create mode 100644 server/src/main/java/envoy/server/data/MessageDeletion.java create mode 100644 server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java diff --git a/client/src/main/java/envoy/client/data/Chat.java b/client/src/main/java/envoy/client/data/Chat.java index 1f88098..75ca5a3 100644 --- a/client/src/main/java/envoy/client/data/Chat.java +++ b/client/src/main/java/envoy/client/data/Chat.java @@ -121,6 +121,15 @@ public class Chat implements Serializable { messages.add(0, message); } + /** + * Removes the message with the given ID + * + * @param messageID the ID of the message to remove + * @return whether any message has been removed + * @since Envoy Client v0.3-beta + */ + public boolean remove(long messageID) { return messages.removeIf(m -> m.getID() == messageID); } + /** * Increments the amount of unread messages. * diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 0e0da55..f99913c 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -7,6 +7,7 @@ import java.time.Instant; import java.util.*; import java.util.logging.*; +import javafx.application.Platform; import javafx.collections.*; import envoy.client.event.*; @@ -235,7 +236,7 @@ public final class LocalDB implements EventListener { @Event(priority = 150) private void onUserStatusChange(UserStatusChange evt) { - this.getChat(evt.getID()).map(Chat::getRecipient).map(User.class::cast).ifPresent(u -> u.setStatus(evt.get())); + getChat(evt.getID()).map(Chat::getRecipient).map(User.class::cast).ifPresent(u -> u.setStatus(evt.get())); } @Event(priority = 150) @@ -273,6 +274,24 @@ public final class LocalDB implements EventListener { cacheMap.clear(); } + /** + * Deletes the message with the given ID, if any is present. + * + * @param message the event that was + * @since Envoy Client v0.3-beta + */ + @Event() + private void onMessageDeletion(MessageDeletion message) { + Platform.runLater(() -> { + + // We suppose that messages have unique IDs, hence the search can be stopped + // once a message was removed + final var messageID = message.get(); + for (final var chat : chats) + if (chat.remove(messageID)) break; + }); + } + /** * @return a {@code Map} of all users stored locally with their * user names as keys diff --git a/client/src/main/java/envoy/client/ui/control/MessageControl.java b/client/src/main/java/envoy/client/ui/control/MessageControl.java index 11f5681..53aa962 100644 --- a/client/src/main/java/envoy/client/ui/control/MessageControl.java +++ b/client/src/main/java/envoy/client/ui/control/MessageControl.java @@ -15,12 +15,16 @@ import javafx.scene.layout.*; import javafx.stage.FileChooser; import envoy.client.data.*; -import envoy.client.ui.*; +import envoy.client.net.Client; +import envoy.client.ui.SceneContext; import envoy.client.util.IconUtil; import envoy.data.*; import envoy.data.Message.MessageStatus; +import envoy.event.MessageDeletion; import envoy.util.EnvoyLog; +import dev.kske.eventbus.EventBus; + /** * This class transforms a single {@link Message} into a UI component. * @@ -32,9 +36,11 @@ public final class MessageControl extends Label { private final boolean ownMessage; - private final LocalDB localDB = Context.getInstance().getLocalDB(); - private final SceneContext sceneContext = Context.getInstance().getSceneContext(); + private final LocalDB localDB = context.getLocalDB(); + private final SceneContext sceneContext = context.getSceneContext(); + private final Client client = context.getClient(); + private static final Context context = Context.getInstance(); private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss") .withZone(ZoneId.systemDefault()); private static final Map statusImages = IconUtil.loadByEnum(MessageStatus.class, 16); @@ -47,6 +53,8 @@ public final class MessageControl extends Label { * @since Envoy Client v0.1-beta */ public MessageControl(Message message) { + ownMessage = message.getSenderID() == localDB.getUser().getID(); + // Creating the underlying VBox and the dateLabel final var hbox = new HBox(); if (message.getSenderID() != localDB.getUser().getID() && message instanceof GroupMessage) { @@ -67,18 +75,40 @@ public final class MessageControl extends Label { final var vbox = new VBox(hbox); // Creating the actions for the MenuItems - final var contextMenu = new ContextMenu(); - final var copyMenuItem = new MenuItem("Copy"); - final var deleteMenuItem = new MenuItem("Delete"); - final var forwardMenuItem = new MenuItem("Forward"); - final var quoteMenuItem = new MenuItem("Quote"); - final var infoMenuItem = new MenuItem("Info"); - copyMenuItem.setOnAction(e -> copyMessage(message)); - deleteMenuItem.setOnAction(e -> deleteMessage(message)); - forwardMenuItem.setOnAction(e -> forwardMessage(message)); - quoteMenuItem.setOnAction(e -> quoteMessage(message)); + final var contextMenu = new ContextMenu(); + final var items = contextMenu.getItems(); + + // Copy message action + final var copyMenuItem = new MenuItem("Copy Text"); + copyMenuItem.setOnAction(e -> copyMessageText(message.getText())); + items.add(copyMenuItem); + + // Delete message - if own message - action + if (ownMessage && client.isOnline()) { + final var deleteMenuItem = new MenuItem("Delete"); + deleteMenuItem.setOnAction(e -> deleteMessage(message)); + items.add(deleteMenuItem); + } + + // As long as these types of messages are not implemented and no caches are + // defined for them, we only want them to appear when being online + if (client.isOnline()) { + + // Forward menu item + final var forwardMenuItem = new MenuItem("Forward"); + forwardMenuItem.setOnAction(e -> forwardMessage(message)); + items.add(forwardMenuItem); + + // Quote menu item + final var quoteMenuItem = new MenuItem("Quote"); + quoteMenuItem.setOnAction(e -> quoteMessage(message)); + items.add(quoteMenuItem); + } + + // Info actions + final var infoMenuItem = new MenuItem("Info"); infoMenuItem.setOnAction(e -> loadMessageInfoScene(message)); - contextMenu.getItems().addAll(copyMenuItem, deleteMenuItem, forwardMenuItem, quoteMenuItem, infoMenuItem); + items.add(infoMenuItem); // Handling message attachment display // TODO: Add missing attachment types @@ -98,7 +128,7 @@ public final class MessageControl extends Label { } final var saveAttachment = new MenuItem("Save attachment"); saveAttachment.setOnAction(e -> saveAttachment(message)); - contextMenu.getItems().add(saveAttachment); + items.add(saveAttachment); } // Creating the textLabel final var textLabel = new Label(message.getText()); @@ -116,12 +146,8 @@ public final class MessageControl extends Label { hBoxBottom.getChildren().add(statusIcon); hBoxBottom.setAlignment(Pos.BOTTOM_RIGHT); getStyleClass().add("own-message"); - ownMessage = true; hbox.setAlignment(Pos.CENTER_RIGHT); - } else { - getStyleClass().add("received-message"); - ownMessage = false; - } + } else getStyleClass().add("received-message"); vbox.getChildren().add(hBoxBottom); // Adjusting height and weight of the cell to the corresponding ListView paddingProperty().setValue(new Insets(5, 20, 5, 20)); @@ -131,11 +157,22 @@ public final class MessageControl extends Label { // Context Menu actions - private void copyMessage(Message message) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(message.getText()), null); + private void copyMessageText(String text) { + logger.log(Level.FINEST, "A copy of message text \"" + text + "\" was requested"); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null); } - private void deleteMessage(Message message) { logger.log(Level.FINEST, "message deletion was requested for " + message); } + private void deleteMessage(Message message) { + final var messageDeletionEvent = new MessageDeletion(message.getID()); + messageDeletionEvent.setOwnEvent(); + + // Removing the message locally + EventBus.getInstance().dispatch(messageDeletionEvent); + + // Removing the message on the server and this chat's recipients + Context.getInstance().getClient().send(messageDeletionEvent); + logger.log(Level.FINEST, "message deletion was requested for " + message); + } private void forwardMessage(Message message) { logger.log(Level.FINEST, "message forwarding was requested for " + message); } diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index 0aa705d..04332f0 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -308,6 +308,15 @@ public final class ChatScene implements EventListener, Restorable { @Event(eventType = Logout.class, priority = 200) private void onLogout() { eventBus.removeListener(this); } + @Event(priority = 200) + private void onMessageDeletion(MessageDeletion message) { + + // Clearing the selection if the own user was the sender of this event + if (message.isOwnEvent()) Platform.runLater(() -> { + messageList.getSelectionModel().clearSelection(); + }); + } + /** * Initializes all {@code SystemCommands} used in {@code ChatScene}. * diff --git a/common/src/main/java/envoy/event/MessageDeletion.java b/common/src/main/java/envoy/event/MessageDeletion.java new file mode 100644 index 0000000..c56a68c --- /dev/null +++ b/common/src/main/java/envoy/event/MessageDeletion.java @@ -0,0 +1,34 @@ +package envoy.event; + +/** + * Conveys the deletion of a message between clients and server. + * + * @author Leon Hofmeister + * @since Envoy Common v0.3-beta + */ +public class MessageDeletion extends Event { + + private static final long serialVersionUID = 1L; + + private transient boolean ownEvent; + + /** + * @param messageID the ID of the deleted message + * @since Envoy Common v0.3-beta + */ + public MessageDeletion(long messageID) { super(messageID); } + + /** + * @return whether the current user was the creator of this event. + * @since Envoy Common v0.3-beta + */ + public boolean isOwnEvent() { return ownEvent; } + + /** + * Marks this event as being sent by this user. Is needed for a bug free + * and efficient selection clearing. + * + * @since Envoy Common v0.3-beta + */ + public void setOwnEvent() { ownEvent = true; } +} diff --git a/server/src/main/java/envoy/server/Startup.java b/server/src/main/java/envoy/server/Startup.java index 6beafee..e6958aa 100755 --- a/server/src/main/java/envoy/server/Startup.java +++ b/server/src/main/java/envoy/server/Startup.java @@ -56,7 +56,8 @@ public final class Startup { new NameChangeProcessor(), new ProfilePicChangeProcessor(), new PasswordChangeRequestProcessor(), - new IssueProposalProcessor()))); + new IssueProposalProcessor(), + new MessageDeletionProcessor()))); // Initialize the current message ID final var persistenceManager = PersistenceManager.getInstance(); diff --git a/server/src/main/java/envoy/server/data/Contact.java b/server/src/main/java/envoy/server/data/Contact.java index 36a302f..b95abf8 100644 --- a/server/src/main/java/envoy/server/data/Contact.java +++ b/server/src/main/java/envoy/server/data/Contact.java @@ -1,7 +1,7 @@ package envoy.server.data; import java.time.Instant; -import java.util.Set; +import java.util.*; import javax.persistence.*; @@ -98,6 +98,34 @@ public abstract class Contact { */ public void setCreationDate(Instant creationDate) { this.creationDate = creationDate; } + /** + * Shortcut to convert a {@code Contact} into a {@code User}. + * + * @param contact the contact to convert + * @return the casted contact + * @throws IllegalStateException if the given contact is not a User + * @since Envoy Server v0.3-beta + */ + public static User toUser(Contact contact) { + if (!(contact instanceof User)) throw new IllegalStateException("Cannot cast a non user to a user"); + return (User) contact; + } + + /** + * Shortcut to convert a set of {@code Contact}s into a set of {@code User}s. + * + * @param contacts the contacts to convert + * @return the casted contacts + * @throws IllegalStateException if one of the given contacts is not a User + * @since Envoy Server v0.3-beta + */ + public static Set toUser(Set contacts) { + final var newSet = new HashSet(); + for (final var contact : contacts) + newSet.add(toUser(contact)); + return newSet; + } + @Override public String toString() { return String.format("%s[id=%d,name=%s,%d contact(s)]", getClass().getSimpleName(), id, name, contacts.size()); } } diff --git a/server/src/main/java/envoy/server/data/MessageDeletion.java b/server/src/main/java/envoy/server/data/MessageDeletion.java new file mode 100644 index 0000000..95c1637 --- /dev/null +++ b/server/src/main/java/envoy/server/data/MessageDeletion.java @@ -0,0 +1,81 @@ +package envoy.server.data; + +import java.util.*; + +import javax.persistence.*; + +/** + * Defines a message that has been deleted. + * + * @author Leon Hofmeister + * @since Envoy Server v0.3-beta + */ +@Entity +@Table(name = "deletionEvents") +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +public final class MessageDeletion { + + @Id + @GeneratedValue + protected long messageID; + + @ManyToOne(targetEntity = User.class) + protected Set recipientsToInform; + + /** + * Creates an instance of {@code DeletionEvent}. + * + * @since Envoy Server v0.3-beta + */ + public MessageDeletion() {} + + /** + * Creates an instance of {@code MessageDeletion}. + * + * @param messageID the ID of the message + * @param recipientsToInform the recipientsToInform of the message
+ * that have not yet been notified of its + * deletion + * @since Envoy Server v0.3-beta + */ + public MessageDeletion(long messageID, Set recipientsToInform) { + this.messageID = messageID; + this.recipientsToInform = recipientsToInform; + } + + /** + * @return the messageID + * @since Envoy Server v0.3-beta + */ + public long getMessageID() { return messageID; } + + /** + * @param messageID the messageID to set + * @since Envoy Server v0.3-beta + */ + public void setMessageID(long messageID) { this.messageID = messageID; } + + /** + * @return the recipients that have yet to be informed + * @since Envoy Server v0.3-beta + */ + public Set getRecipientsToInform() { return recipientsToInform; } + + /** + * @param recipientsToInform the recipients that have yet to be informed + * @since Envoy Server v0.3-beta + */ + public void setRecipientsToInform(Set recipientsToInform) { this.recipientsToInform = recipientsToInform; } + + /** + * @param user the user who has been informed of the message deletion + * @since Envoy Server v0.3-beta + */ + public void recipientInformed(User user) { recipientsToInform.remove(user); } + + /** + * @param users the users that have been informed of the message deletion + * @since Envoy Server v0.3-beta + */ + public void recipientInformed(Collection users) { recipientsToInform.removeAll(users); } +} diff --git a/server/src/main/java/envoy/server/data/PersistenceManager.java b/server/src/main/java/envoy/server/data/PersistenceManager.java index f601fe5..7ae9f1d 100755 --- a/server/src/main/java/envoy/server/data/PersistenceManager.java +++ b/server/src/main/java/envoy/server/data/PersistenceManager.java @@ -1,7 +1,7 @@ package envoy.server.data; import java.time.Instant; -import java.util.List; +import java.util.*; import javax.persistence.*; @@ -100,12 +100,35 @@ public final class PersistenceManager { public void deleteContact(Contact contact) { remove(contact); } /** - * Deletes a {@link Message} in the database. + * Deletes a {@link Message} in the database and creates a new + * {@link MessageDeletion} object for all recipients of the + * message. * * @param message the {@link Message} to delete - * @since Envoy Server Standalone v0.1-alpha + * @return the created {@link MessageDeletion} object + * @since Envoy Server v0.3-beta */ - public void deleteMessage(Message message) { remove(message); } + public MessageDeletion deleteMessage(Message message) { + final var recipient = message.getRecipient(); + return deleteMessage(message, + recipient instanceof Group ? Contact.toUser(getGroupByID(recipient.id).getContacts()) : Set.of(Contact.toUser(recipient))); + } + + /** + * Deletes a {@link Message} in the database and creates a new + * {@link MessageDeletion} object for the given recipients of the message. + * + * @param message the {@link Message} to delete + * @param recipientsYetToInform the (sub)set of all recipients of that message + * @return the created {@link MessageDeletion} object + * @since Envoy Server v0.3-beta + */ + public MessageDeletion deleteMessage(Message message, Set recipientsYetToInform) { + final MessageDeletion deletion = new MessageDeletion(message.id, recipientsYetToInform); + persist(deletion); + remove(message); + return deletion; + } /** * Searches for a {@link User} with a specific ID. @@ -172,6 +195,16 @@ public final class PersistenceManager { */ public ConfigItem getConfigItemByID(String key) { return entityManager.find(ConfigItem.class, key); } + /** + * Searches for a {@link MessageDeletion} with the given message id. + * + * @param id the id of the message to search for + * @return the message deletion object with the specified ID or {@code null} if + * none is found + * @since Envoy Server v0.3-beta + */ + public MessageDeletion getMessageDeletionByID(long id) { return entityManager.find(MessageDeletion.class, id); } + /** * Returns all messages received while being offline or the ones that have * changed. diff --git a/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java b/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java new file mode 100644 index 0000000..d8de0b9 --- /dev/null +++ b/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java @@ -0,0 +1,18 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.MessageDeletion; +import envoy.server.net.ObjectWriteProxy; + +/** + * Listens for and handles incoming {@link MessageDeletion}s. + * + * @author Leon Hofmeister + * @since Envoy Server v0.3-beta + */ +public class MessageDeletionProcessor implements ObjectProcessor { + + @Override + public void process(MessageDeletion message, long socketID, ObjectWriteProxy writeProxy) throws IOException {} +} -- 2.45.2 From 0be5d0e12a3e0d90d827cb4b3430550667e67844 Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 29 Sep 2020 00:28:06 +0200 Subject: [PATCH 2/4] Added CLI options to copy, delete or save attachm. of selected messages --- .../client/ui/control/MessageControl.java | 77 +++---------- .../envoy/client/ui/controller/ChatScene.java | 24 +++- .../java/envoy/client/util/MessageUtil.java | 105 ++++++++++++++++++ 3 files changed, 138 insertions(+), 68 deletions(-) create mode 100644 client/src/main/java/envoy/client/util/MessageUtil.java diff --git a/client/src/main/java/envoy/client/ui/control/MessageControl.java b/client/src/main/java/envoy/client/ui/control/MessageControl.java index 53aa962..15759be 100644 --- a/client/src/main/java/envoy/client/ui/control/MessageControl.java +++ b/client/src/main/java/envoy/client/ui/control/MessageControl.java @@ -1,8 +1,6 @@ package envoy.client.ui.control; -import java.awt.Toolkit; -import java.awt.datatransfer.StringSelection; -import java.io.*; +import java.io.ByteArrayInputStream; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Map; @@ -12,19 +10,14 @@ import javafx.geometry.*; import javafx.scene.control.*; import javafx.scene.image.*; import javafx.scene.layout.*; -import javafx.stage.FileChooser; import envoy.client.data.*; import envoy.client.net.Client; -import envoy.client.ui.SceneContext; -import envoy.client.util.IconUtil; +import envoy.client.util.*; import envoy.data.*; import envoy.data.Message.MessageStatus; -import envoy.event.MessageDeletion; import envoy.util.EnvoyLog; -import dev.kske.eventbus.EventBus; - /** * This class transforms a single {@link Message} into a UI component. * @@ -36,15 +29,13 @@ public final class MessageControl extends Label { private final boolean ownMessage; - private final LocalDB localDB = context.getLocalDB(); - private final SceneContext sceneContext = context.getSceneContext(); - private final Client client = context.getClient(); + private final LocalDB localDB = context.getLocalDB(); + private final Client client = context.getClient(); private static final Context context = Context.getInstance(); private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss") .withZone(ZoneId.systemDefault()); private static final Map statusImages = IconUtil.loadByEnum(MessageStatus.class, 16); - private static final Settings settings = Settings.getInstance(); private static final Logger logger = EnvoyLog.getLogger(MessageControl.class); /** @@ -79,14 +70,16 @@ public final class MessageControl extends Label { final var items = contextMenu.getItems(); // Copy message action - final var copyMenuItem = new MenuItem("Copy Text"); - copyMenuItem.setOnAction(e -> copyMessageText(message.getText())); - items.add(copyMenuItem); + if (!message.getText().isEmpty()) { + final var copyMenuItem = new MenuItem("Copy Text"); + copyMenuItem.setOnAction(e -> MessageUtil.copyMessageText(message)); + items.add(copyMenuItem); + } // Delete message - if own message - action if (ownMessage && client.isOnline()) { final var deleteMenuItem = new MenuItem("Delete"); - deleteMenuItem.setOnAction(e -> deleteMessage(message)); + deleteMenuItem.setOnAction(e -> MessageUtil.deleteMessage(message)); items.add(deleteMenuItem); } @@ -96,12 +89,12 @@ public final class MessageControl extends Label { // Forward menu item final var forwardMenuItem = new MenuItem("Forward"); - forwardMenuItem.setOnAction(e -> forwardMessage(message)); + forwardMenuItem.setOnAction(e -> MessageUtil.forwardMessage(message)); items.add(forwardMenuItem); // Quote menu item final var quoteMenuItem = new MenuItem("Quote"); - quoteMenuItem.setOnAction(e -> quoteMessage(message)); + quoteMenuItem.setOnAction(e -> MessageUtil.quoteMessage(message)); items.add(quoteMenuItem); } @@ -127,7 +120,7 @@ public final class MessageControl extends Label { break; } final var saveAttachment = new MenuItem("Save attachment"); - saveAttachment.setOnAction(e -> saveAttachment(message)); + saveAttachment.setOnAction(e -> MessageUtil.saveAttachment(message)); items.add(saveAttachment); } // Creating the textLabel @@ -155,52 +148,8 @@ public final class MessageControl extends Label { setGraphic(vbox); } - // Context Menu actions - - private void copyMessageText(String text) { - logger.log(Level.FINEST, "A copy of message text \"" + text + "\" was requested"); - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null); - } - - private void deleteMessage(Message message) { - final var messageDeletionEvent = new MessageDeletion(message.getID()); - messageDeletionEvent.setOwnEvent(); - - // Removing the message locally - EventBus.getInstance().dispatch(messageDeletionEvent); - - // Removing the message on the server and this chat's recipients - Context.getInstance().getClient().send(messageDeletionEvent); - logger.log(Level.FINEST, "message deletion was requested for " + message); - } - - private void forwardMessage(Message message) { logger.log(Level.FINEST, "message forwarding was requested for " + message); } - - private void quoteMessage(Message message) { logger.log(Level.FINEST, "message quotation was requested for " + message); } - private void loadMessageInfoScene(Message message) { logger.log(Level.FINEST, "message info scene was requested for " + message); } - private void saveAttachment(Message message) { - File file; - final var fileName = message.getAttachment().getName(); - final var downloadLocation = settings.getDownloadLocation(); - // Show save file dialog, if the user did not opt-out - if (!settings.isDownloadSavedWithoutAsking()) { - final var fileChooser = new FileChooser(); - fileChooser.setInitialFileName(fileName); - fileChooser.setInitialDirectory(downloadLocation); - file = fileChooser.showSaveDialog(sceneContext.getStage()); - } else file = new File(downloadLocation, fileName); - - // A file was selected - if (file != null) try (FileOutputStream fos = new FileOutputStream(file)) { - fos.write(message.getAttachment().getData()); - logger.log(Level.FINE, "Attachment of message was saved at " + file.getAbsolutePath()); - } catch (final IOException e) { - logger.log(Level.WARNING, "Could not save attachment of " + message + ": ", e); - } - } - /** * @return whether the message stored by this {@code MessageControl} has been * sent by this user of Envoy diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index 04332f0..b19a068 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -310,11 +310,9 @@ public final class ChatScene implements EventListener, Restorable { @Event(priority = 200) private void onMessageDeletion(MessageDeletion message) { - + // Clearing the selection if the own user was the sender of this event - if (message.isOwnEvent()) Platform.runLater(() -> { - messageList.getSelectionModel().clearSelection(); - }); + if (message.isOwnEvent()) Platform.runLater(() -> { messageList.getSelectionModel().clearSelection(); }); } /** @@ -345,6 +343,24 @@ public final class ChatScene implements EventListener, Restorable { .setNumberOfArguments(0) .setDescription("Opens the settings screen") .build("settings"); + + // Copy text of selection initialization + builder.setAction(text -> { + final var selectedMessage = messageList.getSelectionModel().getSelectedItem(); + if (selectedMessage != null) MessageUtil.copyMessageText(selectedMessage); + }).setNumberOfArguments(0).setDescription("Copies the text of the currently selected message").build("cp-s"); + + // Delete selection initialization + builder.setAction(text -> { + final var selectedMessage = messageList.getSelectionModel().getSelectedItem(); + if (selectedMessage != null) MessageUtil.deleteMessage(selectedMessage); + }).setNumberOfArguments(0).setDescription("Deletes the currently selected message").build("del-s"); + + // Save attachment of selection initialization + builder.setAction(text -> { + final var selectedMessage = messageList.getSelectionModel().getSelectedItem(); + if (selectedMessage != null && selectedMessage.hasAttachment()) MessageUtil.saveAttachment(selectedMessage); + }).setNumberOfArguments(0).setDescription("Copies the text of the currently selected message").build("save-a-s"); } @Override diff --git a/client/src/main/java/envoy/client/util/MessageUtil.java b/client/src/main/java/envoy/client/util/MessageUtil.java new file mode 100644 index 0000000..8d07d54 --- /dev/null +++ b/client/src/main/java/envoy/client/util/MessageUtil.java @@ -0,0 +1,105 @@ +package envoy.client.util; + +import java.awt.Toolkit; +import java.awt.datatransfer.StringSelection; +import java.io.*; +import java.util.logging.*; + +import javafx.stage.FileChooser; + +import envoy.client.data.*; +import envoy.data.Message; +import envoy.event.MessageDeletion; +import envoy.util.EnvoyLog; + +import dev.kske.eventbus.EventBus; + +/** + * Contains methods that are commonly used for {@link Message}s. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public class MessageUtil { + + private MessageUtil() {} + + private static Logger logger = EnvoyLog.getLogger(MessageUtil.class); + + /** + * Copies the text of the given message to the System Clipboard. + * + * @param message the message whose text to copy + * @since Envoy Client v0.3-beta + */ + public static void copyMessageText(Message message) { + logger.log(Level.FINEST, "A copy of message text \"" + message.getText() + "\" was requested"); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(message.getText()), null); + } + + /** + * Deletes the given message. + * + * @param message the message to delete + * @since Envoy Client v0.3-beta + */ + public static void deleteMessage(Message message) { + final var messageDeletionEvent = new MessageDeletion(message.getID()); + messageDeletionEvent.setOwnEvent(); + + // Removing the message locally + EventBus.getInstance().dispatch(messageDeletionEvent); + + // Removing the message on the server and this chat's recipients + Context.getInstance().getClient().send(messageDeletionEvent); + logger.log(Level.FINEST, "message deletion was requested for " + message); + } + + /** + * Forwards the given message. + * Currently not implemented. + * + * @param message the message to forward + * @since Envoy Client v0.3-beta + */ + public static void forwardMessage(Message message) { logger.log(Level.FINEST, "message forwarding was requested for " + message); } + + /**selected + * Quotes the given message. + * Currently not implemented. + * + * @param message the message to quote + * @since Envoy Client v0.3-beta + */ + public static void quoteMessage(Message message) { logger.log(Level.FINEST, "message quotation was requested for " + message); } + + /** + * Saves the attachment of a message, if present. + * + * @param message the message whose attachment to save + * @throws IllegalStateException if no attachment is present in the message + * @since Envoy Client v0.3-beta + */ + public static void saveAttachment(Message message) { + if (!message.hasAttachment()) throw new IllegalStateException("Cannot save a non-existing attachment"); + File file; + final var fileName = message.getAttachment().getName(); + final var downloadLocation = Settings.getInstance().getDownloadLocation(); + + // Show save file dialog, if the user did not opt-out + if (!Settings.getInstance().isDownloadSavedWithoutAsking()) { + final var fileChooser = new FileChooser(); + fileChooser.setInitialFileName(fileName); + fileChooser.setInitialDirectory(downloadLocation); + file = fileChooser.showSaveDialog(Context.getInstance().getSceneContext().getStage()); + } else file = new File(downloadLocation, fileName); + + // A file was selected + if (file != null) try (FileOutputStream fos = new FileOutputStream(file)) { + fos.write(message.getAttachment().getData()); + logger.log(Level.FINE, "Attachment of message was saved at " + file.getAbsolutePath()); + } catch (final IOException e) { + logger.log(Level.WARNING, "Could not save attachment of " + message + ": ", e); + } + } +} -- 2.45.2 From 57e85f56e9527f14a78e9b75ce73b6551aec9a3f Mon Sep 17 00:00:00 2001 From: delvh Date: Tue, 29 Sep 2020 18:26:28 +0200 Subject: [PATCH 3/4] Removed any server side trace of message deletion --- .../envoy/client/event/MessageDeletion.java | 20 +++++ .../client/ui/control/MessageControl.java | 2 +- .../ui/control/TextInputContextMenu.java | 1 + .../envoy/client/ui/controller/ChatScene.java | 22 ++--- .../java/envoy/client/util/MessageUtil.java | 14 ++-- .../java/envoy/event/MessageDeletion.java | 34 -------- .../src/main/java/envoy/server/Startup.java | 3 +- .../main/java/envoy/server/data/Contact.java | 30 +------ .../envoy/server/data/MessageDeletion.java | 81 ------------------- .../envoy/server/data/PersistenceManager.java | 43 ++-------- .../processors/MessageDeletionProcessor.java | 18 ----- 11 files changed, 48 insertions(+), 220 deletions(-) create mode 100644 client/src/main/java/envoy/client/event/MessageDeletion.java delete mode 100644 common/src/main/java/envoy/event/MessageDeletion.java delete mode 100644 server/src/main/java/envoy/server/data/MessageDeletion.java delete mode 100644 server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java diff --git a/client/src/main/java/envoy/client/event/MessageDeletion.java b/client/src/main/java/envoy/client/event/MessageDeletion.java new file mode 100644 index 0000000..df0bb0b --- /dev/null +++ b/client/src/main/java/envoy/client/event/MessageDeletion.java @@ -0,0 +1,20 @@ +package envoy.client.event; + +import envoy.event.Event; + +/** + * Conveys the deletion of a message between clients and server. + * + * @author Leon Hofmeister + * @since Envoy Common v0.3-beta + */ +public class MessageDeletion extends Event { + + private static final long serialVersionUID = 1L; + + /** + * @param messageID the ID of the deleted message + * @since Envoy Common v0.3-beta + */ + public MessageDeletion(long messageID) { super(messageID); } +} diff --git a/client/src/main/java/envoy/client/ui/control/MessageControl.java b/client/src/main/java/envoy/client/ui/control/MessageControl.java index 15759be..862bb45 100644 --- a/client/src/main/java/envoy/client/ui/control/MessageControl.java +++ b/client/src/main/java/envoy/client/ui/control/MessageControl.java @@ -78,7 +78,7 @@ public final class MessageControl extends Label { // Delete message - if own message - action if (ownMessage && client.isOnline()) { - final var deleteMenuItem = new MenuItem("Delete"); + final var deleteMenuItem = new MenuItem("Delete locally"); deleteMenuItem.setOnAction(e -> MessageUtil.deleteMessage(message)); items.add(deleteMenuItem); } diff --git a/client/src/main/java/envoy/client/ui/control/TextInputContextMenu.java b/client/src/main/java/envoy/client/ui/control/TextInputContextMenu.java index 9925f7d..bafed83 100644 --- a/client/src/main/java/envoy/client/ui/control/TextInputContextMenu.java +++ b/client/src/main/java/envoy/client/ui/control/TextInputContextMenu.java @@ -82,6 +82,7 @@ public class TextInputContextMenu extends ContextMenu { copyMI.disableProperty().bind(control.selectedTextProperty().isEmpty()); deleteMI.disableProperty().bind(control.selectedTextProperty().isEmpty()); clearMI.disableProperty().bind(control.textProperty().isEmpty()); + selectAllMI.disableProperty().bind(control.textProperty().isEmpty()); setOnShowing(e -> pasteMI.setDisable(!Clipboard.getSystemClipboard().hasString())); selectAllMI.getProperties().put("refreshMenu", Boolean.TRUE); diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index b19a068..e3de0fc 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -223,8 +223,8 @@ public final class ChatScene implements EventListener, Restorable { // The sender of the message is the recipient of the chat // Exceptions: this user is the sender (sync) or group message (group is // recipient) - final boolean ownMessage = message.getSenderID() == localDB.getUser().getID(); - final var recipientID = message instanceof GroupMessage || ownMessage ? message.getRecipientID() : message.getSenderID(); + final var ownMessage = message.getSenderID() == localDB.getUser().getID(); + final var recipientID = message instanceof GroupMessage || ownMessage ? message.getRecipientID() : message.getSenderID(); localDB.getChat(recipientID).ifPresent(chat -> { chat.insert(message); @@ -308,13 +308,6 @@ public final class ChatScene implements EventListener, Restorable { @Event(eventType = Logout.class, priority = 200) private void onLogout() { eventBus.removeListener(this); } - @Event(priority = 200) - private void onMessageDeletion(MessageDeletion message) { - - // Clearing the selection if the own user was the sender of this event - if (message.isOwnEvent()) Platform.runLater(() -> { messageList.getSelectionModel().clearSelection(); }); - } - /** * Initializes all {@code SystemCommands} used in {@code ChatScene}. * @@ -412,7 +405,7 @@ public final class ChatScene implements EventListener, Restorable { if (currentChat != null) { topBarContactLabel.setText(currentChat.getRecipient().getName()); if (currentChat.getRecipient() instanceof User) { - final String status = ((User) currentChat.getRecipient()).getStatus().toString(); + final var status = ((User) currentChat.getRecipient()).getStatus().toString(); topBarStatusLabel.setText(status); topBarStatusLabel.getStyleClass().add(status.toLowerCase()); recipientProfilePic.setImage(IconUtil.loadIconThemeSensitive("user_icon", 43)); @@ -420,7 +413,7 @@ public final class ChatScene implements EventListener, Restorable { topBarStatusLabel.setText(currentChat.getRecipient().getContacts().size() + " members"); recipientProfilePic.setImage(IconUtil.loadIconThemeSensitive("group_icon", 43)); } - final Rectangle clip = new Rectangle(); + final var clip = new Rectangle(); clip.setWidth(43); clip.setHeight(43); clip.setArcHeight(43); @@ -778,6 +771,13 @@ public final class ChatScene implements EventListener, Restorable { pendingAttachment = messageAttachment; } + /** + * Clears the current message selection + * + * @since Envoy Client v0.3-beta + */ + public void clearMessageSelection() { messageList.getSelectionModel().clearSelection(); } + @FXML private void searchContacts() { chats.setPredicate(contactSearch.getText().isBlank() ? c -> true diff --git a/client/src/main/java/envoy/client/util/MessageUtil.java b/client/src/main/java/envoy/client/util/MessageUtil.java index 8d07d54..62fb132 100644 --- a/client/src/main/java/envoy/client/util/MessageUtil.java +++ b/client/src/main/java/envoy/client/util/MessageUtil.java @@ -8,8 +8,9 @@ import java.util.logging.*; import javafx.stage.FileChooser; import envoy.client.data.*; +import envoy.client.event.MessageDeletion; +import envoy.client.ui.controller.ChatScene; import envoy.data.Message; -import envoy.event.MessageDeletion; import envoy.util.EnvoyLog; import dev.kske.eventbus.EventBus; @@ -44,14 +45,13 @@ public class MessageUtil { * @since Envoy Client v0.3-beta */ public static void deleteMessage(Message message) { - final var messageDeletionEvent = new MessageDeletion(message.getID()); - messageDeletionEvent.setOwnEvent(); + final var messageDeletionEvent = new MessageDeletion(message.getID()); + final var controller = Context.getInstance().getSceneContext().getController(); + if (controller.getClass().equals(ChatScene.class)) ((ChatScene) controller).clearMessageSelection(); // Removing the message locally EventBus.getInstance().dispatch(messageDeletionEvent); - // Removing the message on the server and this chat's recipients - Context.getInstance().getClient().send(messageDeletionEvent); logger.log(Level.FINEST, "message deletion was requested for " + message); } @@ -64,7 +64,7 @@ public class MessageUtil { */ public static void forwardMessage(Message message) { logger.log(Level.FINEST, "message forwarding was requested for " + message); } - /**selected + /** * Quotes the given message. * Currently not implemented. * @@ -95,7 +95,7 @@ public class MessageUtil { } else file = new File(downloadLocation, fileName); // A file was selected - if (file != null) try (FileOutputStream fos = new FileOutputStream(file)) { + if (file != null) try (var fos = new FileOutputStream(file)) { fos.write(message.getAttachment().getData()); logger.log(Level.FINE, "Attachment of message was saved at " + file.getAbsolutePath()); } catch (final IOException e) { diff --git a/common/src/main/java/envoy/event/MessageDeletion.java b/common/src/main/java/envoy/event/MessageDeletion.java deleted file mode 100644 index c56a68c..0000000 --- a/common/src/main/java/envoy/event/MessageDeletion.java +++ /dev/null @@ -1,34 +0,0 @@ -package envoy.event; - -/** - * Conveys the deletion of a message between clients and server. - * - * @author Leon Hofmeister - * @since Envoy Common v0.3-beta - */ -public class MessageDeletion extends Event { - - private static final long serialVersionUID = 1L; - - private transient boolean ownEvent; - - /** - * @param messageID the ID of the deleted message - * @since Envoy Common v0.3-beta - */ - public MessageDeletion(long messageID) { super(messageID); } - - /** - * @return whether the current user was the creator of this event. - * @since Envoy Common v0.3-beta - */ - public boolean isOwnEvent() { return ownEvent; } - - /** - * Marks this event as being sent by this user. Is needed for a bug free - * and efficient selection clearing. - * - * @since Envoy Common v0.3-beta - */ - public void setOwnEvent() { ownEvent = true; } -} diff --git a/server/src/main/java/envoy/server/Startup.java b/server/src/main/java/envoy/server/Startup.java index e6958aa..6beafee 100755 --- a/server/src/main/java/envoy/server/Startup.java +++ b/server/src/main/java/envoy/server/Startup.java @@ -56,8 +56,7 @@ public final class Startup { new NameChangeProcessor(), new ProfilePicChangeProcessor(), new PasswordChangeRequestProcessor(), - new IssueProposalProcessor(), - new MessageDeletionProcessor()))); + new IssueProposalProcessor()))); // Initialize the current message ID final var persistenceManager = PersistenceManager.getInstance(); diff --git a/server/src/main/java/envoy/server/data/Contact.java b/server/src/main/java/envoy/server/data/Contact.java index b95abf8..36a302f 100644 --- a/server/src/main/java/envoy/server/data/Contact.java +++ b/server/src/main/java/envoy/server/data/Contact.java @@ -1,7 +1,7 @@ package envoy.server.data; import java.time.Instant; -import java.util.*; +import java.util.Set; import javax.persistence.*; @@ -98,34 +98,6 @@ public abstract class Contact { */ public void setCreationDate(Instant creationDate) { this.creationDate = creationDate; } - /** - * Shortcut to convert a {@code Contact} into a {@code User}. - * - * @param contact the contact to convert - * @return the casted contact - * @throws IllegalStateException if the given contact is not a User - * @since Envoy Server v0.3-beta - */ - public static User toUser(Contact contact) { - if (!(contact instanceof User)) throw new IllegalStateException("Cannot cast a non user to a user"); - return (User) contact; - } - - /** - * Shortcut to convert a set of {@code Contact}s into a set of {@code User}s. - * - * @param contacts the contacts to convert - * @return the casted contacts - * @throws IllegalStateException if one of the given contacts is not a User - * @since Envoy Server v0.3-beta - */ - public static Set toUser(Set contacts) { - final var newSet = new HashSet(); - for (final var contact : contacts) - newSet.add(toUser(contact)); - return newSet; - } - @Override public String toString() { return String.format("%s[id=%d,name=%s,%d contact(s)]", getClass().getSimpleName(), id, name, contacts.size()); } } diff --git a/server/src/main/java/envoy/server/data/MessageDeletion.java b/server/src/main/java/envoy/server/data/MessageDeletion.java deleted file mode 100644 index 95c1637..0000000 --- a/server/src/main/java/envoy/server/data/MessageDeletion.java +++ /dev/null @@ -1,81 +0,0 @@ -package envoy.server.data; - -import java.util.*; - -import javax.persistence.*; - -/** - * Defines a message that has been deleted. - * - * @author Leon Hofmeister - * @since Envoy Server v0.3-beta - */ -@Entity -@Table(name = "deletionEvents") -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) -public final class MessageDeletion { - - @Id - @GeneratedValue - protected long messageID; - - @ManyToOne(targetEntity = User.class) - protected Set recipientsToInform; - - /** - * Creates an instance of {@code DeletionEvent}. - * - * @since Envoy Server v0.3-beta - */ - public MessageDeletion() {} - - /** - * Creates an instance of {@code MessageDeletion}. - * - * @param messageID the ID of the message - * @param recipientsToInform the recipientsToInform of the message
- * that have not yet been notified of its - * deletion - * @since Envoy Server v0.3-beta - */ - public MessageDeletion(long messageID, Set recipientsToInform) { - this.messageID = messageID; - this.recipientsToInform = recipientsToInform; - } - - /** - * @return the messageID - * @since Envoy Server v0.3-beta - */ - public long getMessageID() { return messageID; } - - /** - * @param messageID the messageID to set - * @since Envoy Server v0.3-beta - */ - public void setMessageID(long messageID) { this.messageID = messageID; } - - /** - * @return the recipients that have yet to be informed - * @since Envoy Server v0.3-beta - */ - public Set getRecipientsToInform() { return recipientsToInform; } - - /** - * @param recipientsToInform the recipients that have yet to be informed - * @since Envoy Server v0.3-beta - */ - public void setRecipientsToInform(Set recipientsToInform) { this.recipientsToInform = recipientsToInform; } - - /** - * @param user the user who has been informed of the message deletion - * @since Envoy Server v0.3-beta - */ - public void recipientInformed(User user) { recipientsToInform.remove(user); } - - /** - * @param users the users that have been informed of the message deletion - * @since Envoy Server v0.3-beta - */ - public void recipientInformed(Collection users) { recipientsToInform.removeAll(users); } -} diff --git a/server/src/main/java/envoy/server/data/PersistenceManager.java b/server/src/main/java/envoy/server/data/PersistenceManager.java index 7ae9f1d..2946dee 100755 --- a/server/src/main/java/envoy/server/data/PersistenceManager.java +++ b/server/src/main/java/envoy/server/data/PersistenceManager.java @@ -1,7 +1,7 @@ package envoy.server.data; import java.time.Instant; -import java.util.*; +import java.util.List; import javax.persistence.*; @@ -9,6 +9,8 @@ import envoy.data.User.UserStatus; import envoy.server.net.ConnectionManager; /** + * Contains operations used for data retrieval. + * * @author Leon Hofmeister * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha @@ -100,35 +102,12 @@ public final class PersistenceManager { public void deleteContact(Contact contact) { remove(contact); } /** - * Deletes a {@link Message} in the database and creates a new - * {@link MessageDeletion} object for all recipients of the - * message. + * Deletes a {@link Message} in the database. * * @param message the {@link Message} to delete - * @return the created {@link MessageDeletion} object - * @since Envoy Server v0.3-beta + * @since Envoy Server Standalone v0.1-alpha */ - public MessageDeletion deleteMessage(Message message) { - final var recipient = message.getRecipient(); - return deleteMessage(message, - recipient instanceof Group ? Contact.toUser(getGroupByID(recipient.id).getContacts()) : Set.of(Contact.toUser(recipient))); - } - - /** - * Deletes a {@link Message} in the database and creates a new - * {@link MessageDeletion} object for the given recipients of the message. - * - * @param message the {@link Message} to delete - * @param recipientsYetToInform the (sub)set of all recipients of that message - * @return the created {@link MessageDeletion} object - * @since Envoy Server v0.3-beta - */ - public MessageDeletion deleteMessage(Message message, Set recipientsYetToInform) { - final MessageDeletion deletion = new MessageDeletion(message.id, recipientsYetToInform); - persist(deletion); - remove(message); - return deletion; - } + public void deleteMessage(Message message) { remove(message); } /** * Searches for a {@link User} with a specific ID. @@ -195,16 +174,6 @@ public final class PersistenceManager { */ public ConfigItem getConfigItemByID(String key) { return entityManager.find(ConfigItem.class, key); } - /** - * Searches for a {@link MessageDeletion} with the given message id. - * - * @param id the id of the message to search for - * @return the message deletion object with the specified ID or {@code null} if - * none is found - * @since Envoy Server v0.3-beta - */ - public MessageDeletion getMessageDeletionByID(long id) { return entityManager.find(MessageDeletion.class, id); } - /** * Returns all messages received while being offline or the ones that have * changed. diff --git a/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java b/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java deleted file mode 100644 index d8de0b9..0000000 --- a/server/src/main/java/envoy/server/processors/MessageDeletionProcessor.java +++ /dev/null @@ -1,18 +0,0 @@ -package envoy.server.processors; - -import java.io.IOException; - -import envoy.event.MessageDeletion; -import envoy.server.net.ObjectWriteProxy; - -/** - * Listens for and handles incoming {@link MessageDeletion}s. - * - * @author Leon Hofmeister - * @since Envoy Server v0.3-beta - */ -public class MessageDeletionProcessor implements ObjectProcessor { - - @Override - public void process(MessageDeletion message, long socketID, ObjectWriteProxy writeProxy) throws IOException {} -} -- 2.45.2 From 640ecbf41bdc16ce78f85f05c6e9030696fd6d4b Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 30 Sep 2020 13:22:13 +0200 Subject: [PATCH 4/4] Apply suggestions from @kske --- client/src/main/java/envoy/client/data/Chat.java | 8 ++++---- client/src/main/java/envoy/client/data/LocalDB.java | 4 ++-- .../src/main/java/envoy/client/event/MessageDeletion.java | 2 +- .../main/java/envoy/client/ui/controller/ChatScene.java | 2 +- client/src/main/java/envoy/client/util/MessageUtil.java | 8 ++++---- .../main/java/envoy/server/data/PersistenceManager.java | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/client/src/main/java/envoy/client/data/Chat.java b/client/src/main/java/envoy/client/data/Chat.java index 75ca5a3..beabf9c 100644 --- a/client/src/main/java/envoy/client/data/Chat.java +++ b/client/src/main/java/envoy/client/data/Chat.java @@ -74,7 +74,7 @@ public class Chat implements Serializable { public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Chat)) return false; - final Chat other = (Chat) obj; + final var other = (Chat) obj; return Objects.equals(recipient, other.recipient); } @@ -89,7 +89,7 @@ public class Chat implements Serializable { */ public void read(WriteProxy writeProxy) { for (int i = messages.size() - 1; i >= 0; --i) { - final Message m = messages.get(i); + final var m = messages.get(i); if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break; else { m.setStatus(MessageStatus.READ); @@ -122,10 +122,10 @@ public class Chat implements Serializable { } /** - * Removes the message with the given ID + * Removes the message with the given ID. * * @param messageID the ID of the message to remove - * @return whether any message has been removed + * @return whether the message has been found and removed * @since Envoy Client v0.3-beta */ public boolean remove(long messageID) { return messages.removeIf(m -> m.getID() == messageID); } diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index f99913c..51fb2cc 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -275,12 +275,12 @@ public final class LocalDB implements EventListener { } /** - * Deletes the message with the given ID, if any is present. + * Deletes the message with the given ID, if present. * * @param message the event that was * @since Envoy Client v0.3-beta */ - @Event() + @Event private void onMessageDeletion(MessageDeletion message) { Platform.runLater(() -> { diff --git a/client/src/main/java/envoy/client/event/MessageDeletion.java b/client/src/main/java/envoy/client/event/MessageDeletion.java index df0bb0b..e3f5ada 100644 --- a/client/src/main/java/envoy/client/event/MessageDeletion.java +++ b/client/src/main/java/envoy/client/event/MessageDeletion.java @@ -3,7 +3,7 @@ package envoy.client.event; import envoy.event.Event; /** - * Conveys the deletion of a message between clients and server. + * Conveys the deletion of a message. * * @author Leon Hofmeister * @since Envoy Common v0.3-beta diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index e3de0fc..a4ef479 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -772,7 +772,7 @@ public final class ChatScene implements EventListener, Restorable { } /** - * Clears the current message selection + * Clears the current message selection. * * @since Envoy Client v0.3-beta */ diff --git a/client/src/main/java/envoy/client/util/MessageUtil.java b/client/src/main/java/envoy/client/util/MessageUtil.java index 62fb132..468e8dc 100644 --- a/client/src/main/java/envoy/client/util/MessageUtil.java +++ b/client/src/main/java/envoy/client/util/MessageUtil.java @@ -47,7 +47,7 @@ public class MessageUtil { public static void deleteMessage(Message message) { final var messageDeletionEvent = new MessageDeletion(message.getID()); final var controller = Context.getInstance().getSceneContext().getController(); - if (controller.getClass().equals(ChatScene.class)) ((ChatScene) controller).clearMessageSelection(); + if (controller instanceof ChatScene) ((ChatScene) controller).clearMessageSelection(); // Removing the message locally EventBus.getInstance().dispatch(messageDeletionEvent); @@ -62,7 +62,7 @@ public class MessageUtil { * @param message the message to forward * @since Envoy Client v0.3-beta */ - public static void forwardMessage(Message message) { logger.log(Level.FINEST, "message forwarding was requested for " + message); } + public static void forwardMessage(Message message) { logger.log(Level.FINEST, "Message forwarding was requested for " + message); } /** * Quotes the given message. @@ -71,7 +71,7 @@ public class MessageUtil { * @param message the message to quote * @since Envoy Client v0.3-beta */ - public static void quoteMessage(Message message) { logger.log(Level.FINEST, "message quotation was requested for " + message); } + public static void quoteMessage(Message message) { logger.log(Level.FINEST, "Message quotation was requested for " + message); } /** * Saves the attachment of a message, if present. @@ -81,7 +81,7 @@ public class MessageUtil { * @since Envoy Client v0.3-beta */ public static void saveAttachment(Message message) { - if (!message.hasAttachment()) throw new IllegalStateException("Cannot save a non-existing attachment"); + if (!message.hasAttachment()) throw new IllegalArgumentException("Cannot save a non-existing attachment"); File file; final var fileName = message.getAttachment().getName(); final var downloadLocation = Settings.getInstance().getDownloadLocation(); diff --git a/server/src/main/java/envoy/server/data/PersistenceManager.java b/server/src/main/java/envoy/server/data/PersistenceManager.java index 2946dee..67fa4e9 100755 --- a/server/src/main/java/envoy/server/data/PersistenceManager.java +++ b/server/src/main/java/envoy/server/data/PersistenceManager.java @@ -9,7 +9,7 @@ import envoy.data.User.UserStatus; import envoy.server.net.ConnectionManager; /** - * Contains operations used for data retrieval. + * Contains operations used for persistence. * * @author Leon Hofmeister * @author Maximilian Käfer -- 2.45.2