Added option to disable attachments and groups on both client and server

This commit is contained in:
delvh 2020-08-22 18:14:26 +02:00
parent eb4e421974
commit c784ebb787
15 changed files with 219 additions and 83 deletions

View File

@ -164,6 +164,13 @@ public final class Client implements Closeable {
// Process ProfilePicChanges
receiver.registerProcessor(ProfilePicChange.class, eventBus::dispatch);
// Process requests to not send anymore attachments as they will not be shown to
// other users
receiver.registerProcessor(NoAttachments.class, eventBus::dispatch);
// Process group creation rejections - they have been disabled on the server
receiver.registerProcessor(GroupCreationResult.class, eventBus::dispatch);
// Send event
eventBus.register(SendEvent.class, evt -> {
try {

View File

@ -69,7 +69,7 @@ public final class Receiver extends Thread {
// Server has stopped sending, i.e. because he went offline
if (len == 0 && bytesRead == -1) {
isAlive = false;
logger.log(Level.WARNING, "Lost connection to the server. Exiting receiver");
logger.log(Level.INFO, "Lost connection to the server. Exiting receiver...");
continue;
}
logger.log(Level.WARNING,

View File

@ -87,6 +87,12 @@ public final class ChatScene implements Restorable {
@FXML
private Button rotateButton;
@FXML
private Button messageSearchButton;
@FXML
private Button newGroupButton;
@FXML
private TextArea messageTextArea;
@ -108,9 +114,6 @@ public final class ChatScene implements Restorable {
@FXML
private Label topBarStatusLabel;
@FXML
private Button messageSearchButton;
@FXML
private ImageView clientProfilePic;
@ -129,7 +132,8 @@ public final class ChatScene implements Restorable {
private AudioRecorder recorder;
private boolean recording;
private Attachment pendingAttachment;
private boolean postingPermanentlyDisabled;
private boolean postingPermanentlyDisabled;
private final SystemCommandsMap messageTextAreaCommands = new SystemCommandsMap();
@ -237,6 +241,21 @@ public final class ChatScene implements Restorable {
break;
}
});
// Disable attachment button if server says attachments will be filtered out
eventBus.register(NoAttachments.class, e -> {
Platform.runLater(() -> {
attachmentButton.setDisable(true);
voiceButton.setDisable(true);
final var alert = new Alert(AlertType.ERROR);
alert.setTitle("No attachments possible");
alert.setHeaderText("Your current server does not support attachments.");
alert.setContentText("If this is unplanned, please contact your server administrator.");
alert.showAndWait();
});
});
eventBus.register(GroupCreationResult.class, e -> Platform.runLater(() -> { newGroupButton.setDisable(!e.get()); }));
}
/**

View File

@ -6,10 +6,8 @@ import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import envoy.client.data.LocalDB;
import envoy.client.event.SendEvent;
@ -20,6 +18,7 @@ import envoy.client.ui.listcell.ListCellFactory;
import envoy.data.User;
import envoy.event.ElementOperation;
import envoy.event.EventBus;
import envoy.event.GroupCreationResult;
import envoy.event.contact.ContactOperation;
import envoy.event.contact.UserSearchRequest;
import envoy.event.contact.UserSearchResult;
@ -50,6 +49,9 @@ public final class ContactSearchScene {
@FXML
private ListView<User> userList;
@FXML
private Button newGroupButton;
private SceneContext sceneContext;
private LocalDB localDB;
@ -86,6 +88,7 @@ public final class ContactSearchScene {
eventBus.register(UserSearchResult.class,
response -> Platform.runLater(() -> { userList.getItems().clear(); userList.getItems().addAll(response.get()); }));
eventBus.register(ContactOperation.class, handler);
eventBus.register(GroupCreationResult.class, e -> Platform.runLater(() -> { newGroupButton.setDisable(!e.get()); }));
}
/**

View File

@ -21,6 +21,7 @@ import envoy.data.Group;
import envoy.data.User;
import envoy.event.EventBus;
import envoy.event.GroupCreation;
import envoy.event.GroupCreationResult;
import envoy.util.Bounds;
/**
@ -54,6 +55,8 @@ public final class GroupCreationScene {
private LocalDB localDB;
private String groupName;
private static final EventBus eventBus = EventBus.getInstance();
@FXML
@ -61,6 +64,18 @@ public final class GroupCreationScene {
userList.setCellFactory(new ListCellFactory<>(ContactControl::new));
userList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
groupNameField.setClearButtonListener(e -> { groupNameField.getTextField().clear(); createButton.setDisable(true); });
eventBus.register(GroupCreationResult.class, e -> Platform.runLater(() -> {
if (e.get()) new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", groupName)).showAndWait();
else {
createButton.setDisable(true);
final var alert = new Alert(AlertType.ERROR);
alert.setTitle("Groups are not allowed");
alert.setHeaderText("Cannot create group as your current server disabled this feature");
alert.setContentText("If this is unplanned, please contact your server administrator.");
alert.showAndWait();
sceneContext.pop();
}
}));
}
/**
@ -119,10 +134,7 @@ public final class GroupCreationScene {
alert.setTitle("Create Group?");
alert.setHeaderText("Proceed?");
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> createGroup(name));
} else {
new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait();
createGroup(name);
}
} else createGroup(name);
}
/**
@ -133,9 +145,9 @@ public final class GroupCreationScene {
* @since Envoy Client v0.1-beta
*/
private void createGroup(String name) {
groupName = name;
eventBus.dispatch(new SendEvent(
new GroupCreation(name, userList.getSelectionModel().getSelectedItems().stream().map(User::getID).collect(Collectors.toSet()))));
sceneContext.pop();
}
/**

View File

@ -62,7 +62,7 @@
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing="false" prefWidth="100.0" text="New Group">
<Button fx:id="newGroupButton" mnemonicParsing="false" prefWidth="100.0" text="New Group">
<HBox.margin>
<Insets />
</HBox.margin>

View File

@ -34,7 +34,7 @@
wrapText="true" />
</tooltip>
</ClearableTextField>
<Button mnemonicParsing="false"
<Button fx:id="newGroupButton" mnemonicParsing="false"
onAction="#newGroupButtonClicked" prefHeight="26.0"
prefWidth="139.0" text="New Group">
<HBox.margin>

View File

@ -0,0 +1,25 @@
package envoy.event;
/**
* Used to communicate with a client that his request to create a group might
* have been rejected as it might be disabled on his current server.
* <p>
* Project: <strong>common</strong><br>
* File: <strong>GroupCreationResult.java</strong><br>
* Created: <strong>22.08.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.2-beta
*/
public class GroupCreationResult extends Event<Boolean> {
private static final long serialVersionUID = 1L;
/**
* Creates a new {@code GroupCreationResult}.
*
* @param success whether the GroupCreation sent before was successful
* @since Envoy Common v0.2-beta
*/
public GroupCreationResult(boolean success) { super(success); }
}

View File

@ -0,0 +1,25 @@
package envoy.event;
/**
* This event is used so that the server can tell the client that attachments
* will be filtered out.
* <p>
* Project: <strong>common</strong><br>
* File: <strong>NoAttachments.java</strong><br>
* Created: <strong>22.08.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.2-beta
*/
public class NoAttachments extends Event<Void> {
private static final long serialVersionUID = 1L;
/**
* Creates a new {@code NoAttachments}.
*
* @since Envoy Common v0.2-beta
*/
public NoAttachments() { super(null); }
}

View File

@ -1,6 +1,7 @@
package envoy.server.data;
import static envoy.data.Message.MessageStatus.*;
import static envoy.server.Startup.config;
import java.time.Instant;
@ -104,7 +105,7 @@ public class Message {
sender = persistenceManager.getUserByID(message.getSenderID());
recipient = persistenceManager.getContactByID(message.getRecipientID());
forwarded = message.isForwarded();
if (message.hasAttachment()) {
if (config.isAttachmentSupportEnabled() && message.hasAttachment()) {
final var messageAttachment = message.getAttachment();
attachment = messageAttachment.getData();
attachmentName = messageAttachment.getName();

View File

@ -33,6 +33,8 @@ public final class ServerConfig extends Config {
put("featureLabel", "l-f", identity(), true);
// enabling/ disabling several processors
put("enableIssueReporting", "e-ir", Boolean::parseBoolean, true);
put("enableGroups", "e-g", Boolean::parseBoolean, true);
put("enableAttachments", "e-a", Boolean::parseBoolean, true);
}
/**
@ -47,6 +49,18 @@ public final class ServerConfig extends Config {
*/
public Boolean isIssueReportingEnabled() { return (Boolean) items.get("enableIssueReporting").get(); }
/**
* @return {@code true} if attachment support has been enabled
* @since Envoy Client v0.3-alpha
*/
public Boolean isAttachmentSupportEnabled() { return (Boolean) items.get("enableAttachments").get(); }
/**
* @return {@code true} if group support has been enabled
* @since Envoy Client v0.3-alpha
*/
public Boolean isGroupSupportEnabled() { return (Boolean) items.get("enableGroups").get(); }
/**
* @return the URL where issues should be uploaded to
* @since Envoy Client v0.1-alpha

View File

@ -1,9 +1,12 @@
package envoy.server.processors;
import static envoy.server.Startup.config;
import java.util.HashSet;
import envoy.event.ElementOperation;
import envoy.event.GroupCreation;
import envoy.event.GroupCreationResult;
import envoy.event.contact.ContactOperation;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
@ -14,7 +17,7 @@ import envoy.server.net.ObjectWriteProxy;
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupCreationProcessor.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
@ -25,7 +28,10 @@ public final class GroupCreationProcessor implements ObjectProcessor<GroupCreati
@Override
public void process(GroupCreation groupCreation, long socketID, ObjectWriteProxy writeProxy) {
envoy.server.data.Group group = new envoy.server.data.Group();
// Don't allow the creation of groups if manually disabled
writeProxy.write(socketID, new GroupCreationResult(config.isGroupSupportEnabled()));
if (!config.isGroupSupportEnabled()) return;
final envoy.server.data.Group group = new envoy.server.data.Group();
group.setName(groupCreation.get());
group.setContacts(new HashSet<>());
groupCreation.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add);

View File

@ -1,64 +1,78 @@
package envoy.server.processors;
import static envoy.data.Message.MessageStatus.*;
import java.time.Instant;
import java.util.Collections;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import envoy.data.GroupMessage;
import envoy.event.MessageStatusChange;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupMessageProcessor.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public final class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(GroupCreationProcessor.class);
@Override
public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) {
groupMessage.nextStatus();
// Update statuses to SENT / RECEIVED depending on online status
groupMessage.getMemberStatuses().replaceAll((memberID, status) -> connectionManager.isOnline(memberID) ? RECEIVED : SENT);
// Set status for sender to READ
groupMessage.getMemberStatuses().replace(groupMessage.getSenderID(), READ);
// Increment the overall status to RECEIVED if necessary
if (Collections.min(groupMessage.getMemberStatuses().values()) == RECEIVED) {
groupMessage.nextStatus();
// Notify the sender of the status change
writeProxy.write(socketID, new MessageStatusChange(groupMessage));
}
// Deliver the message to the recipients that are online
writeProxy.writeToOnlineContacts(
persistenceManager.getGroupByID(groupMessage.getRecipientID())
.getContacts()
.stream()
.filter(c -> c.getID() != groupMessage.getSenderID()),
groupMessage);
try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, Instant.now()));
} catch (EntityExistsException e) {
logger.warning("Received a groupMessage with an ID that already exists");
}
}
}
package envoy.server.processors;
import static envoy.data.Message.MessageStatus.*;
import static envoy.server.Startup.config;
import java.time.Instant;
import java.util.Collections;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import envoy.data.GroupMessage;
import envoy.event.MessageStatusChange;
import envoy.event.NoAttachments;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupMessageProcessor.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public final class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(GroupCreationProcessor.class);
@Override
public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) {
groupMessage.nextStatus();
// Update statuses to SENT / RECEIVED depending on online status
groupMessage.getMemberStatuses().replaceAll((memberID, status) -> connectionManager.isOnline(memberID) ? RECEIVED : SENT);
// Set status for sender to READ
groupMessage.getMemberStatuses().replace(groupMessage.getSenderID(), READ);
// Increment the overall status to RECEIVED if necessary
if (Collections.min(groupMessage.getMemberStatuses().values()) == RECEIVED) {
groupMessage.nextStatus();
// Notify the sender of the status change
writeProxy.write(socketID, new MessageStatusChange(groupMessage));
}
// message attachment will be automatically removed if disabled in config
final var groupMessageServer = new envoy.server.data.GroupMessage(groupMessage, Instant.now());
// Telling the server to reload the message without the attachment and telling
// the client not to send anymore attachments
if (!config.isAttachmentSupportEnabled() && groupMessage.hasAttachment()) {
groupMessage = groupMessageServer.toCommon();
writeProxy.write(socketID, new NoAttachments());
}
// This is needed unfortunately because of f***ing lambda restrictions ("must be
// fINaL oR EFfEcTivELy FiNAl")
final var groupMessageCopy = groupMessage;
// Deliver the message to the recipients that are online
writeProxy.writeToOnlineContacts(
persistenceManager.getGroupByID(groupMessageCopy.getRecipientID())
.getContacts()
.stream()
.filter(c -> c.getID() != groupMessageCopy.getSenderID()),
groupMessageCopy);
try {
PersistenceManager.getInstance().addMessage(groupMessageServer);
} catch (final EntityExistsException e) {
logger.warning("Received a groupMessage with an ID that already exists");
}
}
}

View File

@ -1,5 +1,7 @@
package envoy.server.processors;
import static envoy.server.Startup.config;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -7,6 +9,7 @@ import javax.persistence.EntityExistsException;
import envoy.data.Message;
import envoy.event.MessageStatusChange;
import envoy.event.NoAttachments;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
@ -35,6 +38,11 @@ public final class MessageProcessor implements ObjectProcessor<Message> {
// Convert to server message
final var serverMessage = new envoy.server.data.Message(message);
// Telling the server to reload the message without the attachment
if (!config.isAttachmentSupportEnabled() && message.hasAttachment()) {
message = serverMessage.toCommon();
writeProxy.write(socketID, new NoAttachments());
}
try {
@ -54,7 +62,7 @@ public final class MessageProcessor implements ObjectProcessor<Message> {
// Note that the exact time stamp might differ slightly
writeProxy.write(socketID, new MessageStatusChange(message));
}
} catch (EntityExistsException e) {
} catch (final EntityExistsException e) {
logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
}
}

View File

@ -1,4 +1,6 @@
enter-to-stop=true
enableAttachments=true
enableGroups=true
enableIssueReporting=true
# git.kske.dev config
issueCreationURL=https://git.kske.dev/api/v1/repos/zdm/envoy/issues?access_token=6d8ec2a72d64cbaf6319434aa2e7caf0130701b3