Move context menu from ChatScene globally to ChatControl specific

Additionally fixed a small bug in UserCreationProcessor and when
deleting a contact offline
This commit is contained in:
2020-10-14 23:41:24 +02:00
committed by KSKE Git
parent dd477b6cbc
commit ebe19c00c9
8 changed files with 76 additions and 89 deletions

View File

@ -51,7 +51,7 @@ public final class Startup {
new UserStatusChangeProcessor(),
new IDGeneratorRequestProcessor(),
new UserSearchProcessor(),
new ContactOperationProcessor(),
new UserOperationProcessor(),
new IsTypingProcessor(),
new NameChangeProcessor(),
new ProfilePicChangeProcessor(),

View File

@ -2,11 +2,13 @@ package envoy.server.data;
import java.time.Instant;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.*;
import envoy.data.User.UserStatus;
import envoy.server.net.ConnectionManager;
import envoy.util.EnvoyLog;
/**
* Contains operations used for persistence.
@ -240,17 +242,24 @@ public final class PersistenceManager {
* @since Envoy Server Standalone v0.1-alpha
*/
public void addContactBidirectional(long contactID1, long contactID2) {
addContactBidirectional(getContactByID(contactID1), getContactByID(contactID2));
}
// Get users by ID
final var c1 = getContactByID(contactID1);
final var c2 = getContactByID(contactID2);
/**
* Adds a contact to the contact list of another contact and vice versa.
*
* @param contact1 the first contact
* @param contact2 the second contact
* @since Envoy Server v0.3-beta
*/
public void addContactBidirectional(Contact contact1, Contact contact2) {
// Add users to each others contact list
c1.getContacts().add(c2);
c2.getContacts().add(c1);
contact1.getContacts().add(contact2);
contact2.getContacts().add(contact1);
// Synchronize changes with the database
transaction(() -> { entityManager.merge(c1); entityManager.merge(c2); });
transaction(() -> { entityManager.merge(contact1); entityManager.merge(contact2); });
}
/**
@ -261,17 +270,24 @@ public final class PersistenceManager {
* @since Envoy Server v0.3-beta
*/
public void removeContactBidirectional(long contactID1, long contactID2) {
removeContactBidirectional(getContactByID(contactID1), getContactByID(contactID2));
}
// Get users by ID
final var c1 = getContactByID(contactID1);
final var c2 = getContactByID(contactID2);
/**
* Removes a contact from the contact list of another contact and vice versa.
*
* @param contact1 the first contact
* @param contact2 the second contact
* @since Envoy Server v0.3-beta
*/
public void removeContactBidirectional(Contact contact1, Contact contact2) {
// Remove users from each others contact list
c1.getContacts().remove(c2);
c2.getContacts().remove(c1);
contact1.getContacts().remove(contact2);
contact2.getContacts().remove(contact1);
// Synchronize changes with the database
transaction(() -> { entityManager.merge(c1); entityManager.merge(c2); });
transaction(() -> { entityManager.merge(contact1); entityManager.merge(contact2); });
}
/**
@ -311,6 +327,14 @@ public final class PersistenceManager {
entityManagerRelatedAction.run();
transaction.commit();
}
} catch (final RollbackException e2) {
// Apparently a major problem exists. Discard faulty transaction and then go on.
if (transaction.isActive()) {
transaction.rollback();
EnvoyLog.getLogger(PersistenceManager.class)
.log(Level.SEVERE, "Could not perform transaction, hence discarding it. It's likely that a serious issue exists.");
} else throw e2;
}
}
}

View File

@ -5,7 +5,7 @@ import static envoy.server.Startup.config;
import java.util.HashSet;
import envoy.event.*;
import envoy.server.data.*;
import envoy.server.data.PersistenceManager;
import envoy.server.net.*;
/**
@ -27,18 +27,12 @@ public final class GroupCreationProcessor implements ObjectProcessor<GroupCreati
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);
group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIDBySocketID(socketID)));
group.getContacts().forEach(c -> c.getContacts().add(group));
group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIDBySocketID(socketID)));
persistenceManager.addContact(group);
final var resultGroup = group.toCommon();
group.getContacts()
groupCreation.getInitialMemberIDs()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketID)
.forEach(memberSocketID -> writeProxy.write(memberSocketID, new GroupCreationResult(resultGroup)));
writeProxy.write(socketID, new GroupCreationResult(resultGroup));
.map(persistenceManager::getUserByID)
.forEach(member -> persistenceManager.addContactBidirectional(member, group));
persistenceManager.addContactBidirectional(persistenceManager.getUserByID(connectionManager.getUserIDBySocketID(socketID)), group);
writeProxy.writeToOnlineContacts(group.getContacts(), new GroupCreationResult(group.toCommon()));
}
}

View File

@ -5,7 +5,7 @@ import java.util.logging.*;
import envoy.event.ElementOperation;
import envoy.event.contact.UserOperation;
import envoy.server.data.*;
import envoy.server.data.PersistenceManager;
import envoy.server.net.*;
import envoy.util.EnvoyLog;
@ -13,10 +13,10 @@ import envoy.util.EnvoyLog;
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public final class ContactOperationProcessor implements ObjectProcessor<UserOperation> {
public final class UserOperationProcessor implements ObjectProcessor<UserOperation> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class);
private static final Logger logger = EnvoyLog.getLogger(UserOperationProcessor.class);
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
@ -38,35 +38,15 @@ public final class ContactOperationProcessor implements ObjectProcessor<UserOper
// Remove the relationships and notify sender if he logs in using another
// LocalDB
persistenceManager.removeContactBidirectional(userID, contactID);
final var contact = persistenceManager.getContactByID(contactID);
sender.setLatestContactDeletion(Instant.now());
// The sender wants to remove a normal contact
if (contact instanceof User) {
// Notify the removed contact on next startup(s) of this deletion
persistenceManager.getUserByID(contactID).setLatestContactDeletion(Instant.now());
// Notify the removed contact on next startup(s) of this deletion
persistenceManager.getUserByID(contactID).setLatestContactDeletion(Instant.now());
// Notify the removed contact if online
if (connectionManager.isOnline(contactID))
writeProxy.write(connectionManager.getSocketID(contactID), new UserOperation(sender.toCommon(), ElementOperation.REMOVE));
} else {
// The sender wants to be removed from a Group
final var group = persistenceManager.getGroupByID(contactID);
// The group has no more members and hence will be deleted
if (group.getContacts().isEmpty()) persistenceManager.deleteContact(group);
else {
// Informing the other members
writeProxy.writeToOnlineContacts(group.getContacts(), new UserOperation(sender.toCommon(), ElementOperation.REMOVE));
group.getContacts().forEach(c -> ((User) c).setLatestContactDeletion(Instant.now()));
}
}
// Notify the removed contact if online
if (connectionManager.isOnline(contactID))
writeProxy.write(connectionManager.getSocketID(contactID), new UserOperation(sender.toCommon(), ElementOperation.REMOVE));
break;
default:
logger.warning(String.format("Received %s with an unsupported operation.", evt));
}
}
}