Add working leaving of a group

Additionally fixed a two bugs:
- one group member will no longer show "1 members"
- deletion of empty groups no longer throws an exception
This commit is contained in:
2020-10-16 20:21:22 +02:00
committed by KSKE Git
parent ebe19c00c9
commit a0812f193e
8 changed files with 66 additions and 34 deletions

View File

@ -49,6 +49,7 @@ public final class Startup {
new MessageStatusChangeProcessor(),
new GroupMessageStatusChangeProcessor(),
new UserStatusChangeProcessor(),
new GroupResizeProcessor(),
new IDGeneratorRequestProcessor(),
new UserSearchProcessor(),
new UserOperationProcessor(),

View File

@ -51,7 +51,7 @@ public class Message {
@Id
protected long id;
@ManyToOne
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn
protected User sender;

View File

@ -105,11 +105,6 @@ public final class PersistenceManager {
// Remove this contact from the contact list of his contacts
for (final var remainingContact : contact.getContacts())
remainingContact.getContacts().remove(contact);
// Delete messages sent or received by this contact
entityManager.createQuery("DELETE FROM Message m WHERE (m.sender = :contact OR m.recipient = :contact )")
.setParameter("contact", contact)
.executeUpdate();
});
remove(contact);
}

View File

@ -1,8 +1,12 @@
package envoy.server.processors;
import java.time.Instant;
import java.util.logging.Level;
import envoy.event.GroupResize;
import envoy.server.data.*;
import envoy.server.net.*;
import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/**
* @author Maximilian Käfer
@ -10,35 +14,38 @@ import envoy.server.net.*;
*/
public final class GroupResizeProcessor implements ObjectProcessor<GroupResize> {
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public void process(GroupResize groupResize, long socketID, ObjectWriteProxy writeProxy) {
// Acquire the group to resize from the database
var group = persistenceManager.getGroupByID(groupResize.getGroupID());
final var group = persistenceManager.getGroupByID(groupResize.getGroupID());
final var sender = persistenceManager.getUserByID(groupResize.get().getID());
// Perform the desired operation
switch (groupResize.getOperation()) {
case ADD:
group.getContacts().add(persistenceManager.getUserByID(groupResize.get().getID()));
break;
persistenceManager.addContactBidirectional(sender, group);
writeProxy.writeToOnlineContacts(group.getContacts(), group.toCommon());
return;
case REMOVE:
persistenceManager.removeContactBidirectional(sender, group);
sender.setLatestContactDeletion(Instant.now());
// The group has no more members and hence will be deleted
if (group.getContacts().isEmpty()) {
EnvoyLog.getLogger(GroupResizeProcessor.class).log(Level.INFO, "Deleting now empty group " + group.getName());
persistenceManager.deleteContact(group);
} else {
// Informing the other members
writeProxy.writeToOnlineContacts(group.getContacts(), groupResize);
group.getContacts().forEach(c -> ((User) c).setLatestContactDeletion(Instant.now()));
}
group.getContacts().remove(persistenceManager.getUserByID(groupResize.get().getID()));
break;
return;
}
// Update the group in the database
persistenceManager.updateContact(group);
// Send the updated group to all of its members
var commonGroup = group.toCommon();
group.getContacts()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketID)
.forEach(memberSocketID -> writeProxy.write(memberSocketID, commonGroup));
}
}