Add option to delete your account

This commit is contained in:
2020-10-19 22:16:18 +02:00
committed by kske
parent 8bdd201b28
commit f67ca1d61d
14 changed files with 221 additions and 55 deletions

View File

@ -59,7 +59,8 @@ public final class Startup {
new NameChangeProcessor(),
new ProfilePicChangeProcessor(),
new PasswordChangeRequestProcessor(),
new IssueProposalProcessor())));
new IssueProposalProcessor(),
new AccountDeletionProcessor())));
// Initialize the current message ID
final var persistenceManager = PersistenceManager.getInstance();

View File

@ -27,14 +27,18 @@ import envoy.data.Message.MessageStatus;
@Entity
@Table(name = "messages")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQuery(name = Message.getPending, query = "SELECT m FROM Message m WHERE "
// Send to or by the user before last seen
+ "(m.sender = :user OR m.recipient = :user) AND m.creationDate > :lastSeen "
// SENT to the user
+ "OR m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT "
// Sent by the user and RECEIVED / READ after last seen
+ "OR m.sender = :user AND (m.status = envoy.data.Message$MessageStatus.RECEIVED AND m.receivedDate > :lastSeen "
+ "OR m.status = envoy.data.Message$MessageStatus.READ AND m.readDate > :lastSeen)")
@NamedQueries({
@NamedQuery(name = Message.getPending, query = "SELECT m FROM Message m WHERE "
// Send to or by the user before last seen
+ "(m.sender = :user OR m.recipient = :user) AND m.creationDate > :lastSeen "
// SENT to the user
+ "OR m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT "
// Sent by the user and RECEIVED / READ after last seen
+ "OR m.sender = :user AND (m.status = envoy.data.Message$MessageStatus.RECEIVED AND m.receivedDate > :lastSeen "
+ "OR m.status = envoy.data.Message$MessageStatus.READ AND m.readDate > :lastSeen)"),
@NamedQuery(name = Message.deleteByRecipient, query = "DELETE FROM Message m WHERE m.recipient = :deleted OR m.sender = :deleted")
})
public class Message {
/**
@ -45,6 +49,13 @@ public class Message {
*/
public static final String getPending = "Message.getPending";
/**
* Named query deleting all messages of a user (parameter {@code :deleted}).
*
* @since Envoy Server v0.3-beta
*/
public static final String deleteByRecipient = "Message.deleteByRecipient";
@Id
protected long id;

View File

@ -123,6 +123,10 @@ public final class PersistenceManager {
// Remove this contact from the contact list of his contacts
for (final var remainingContact : contact.getContacts())
remainingContact.getContacts().remove(contact);
entityManager
.createNamedQuery(Message.deleteByRecipient).setParameter("deleted", contact)
.executeUpdate();
});
remove(contact);
}

View File

@ -49,8 +49,10 @@ public final class ConnectionManager implements ISocketIdListener {
// Notify contacts of this users offline-going
final envoy.server.data.User user =
PersistenceManager.getInstance().getUserByID(getUserIDBySocketID(socketID));
user.setLastSeen(Instant.now());
UserStatusChangeProcessor.updateUserStatus(user, UserStatus.OFFLINE);
if (user != null) {
user.setLastSeen(Instant.now());
UserStatusChangeProcessor.updateUserStatus(user, UserStatus.OFFLINE);
}
// Remove the socket
sockets.entrySet().removeIf(e -> e.getValue() == socketID);

View File

@ -0,0 +1,33 @@
package envoy.server.processors;
import java.io.IOException;
import java.time.Instant;
import envoy.event.contact.AccountDeletion;
import envoy.server.data.*;
import envoy.server.net.ObjectWriteProxy;
/**
* @author Leon Hofmeister
* @since Envoy Server v0.3-beta
*/
public class AccountDeletionProcessor implements ObjectProcessor<AccountDeletion> {
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public void process(AccountDeletion input, long socketID, ObjectWriteProxy writeProxy)
throws IOException {
final var contact = persistenceManager.getContactByID(input.get());
contact.getContacts().forEach(c -> {
persistenceManager.removeContactBidirectional(contact, c);
if (c instanceof User)
((User) c).setLatestContactDeletion(Instant.now());
});
writeProxy.writeToOnlineContacts(contact.getContacts(), input);
persistenceManager.deleteContact(contact);
}
}

View File

@ -4,6 +4,7 @@ import java.time.Instant;
import java.util.logging.Level;
import envoy.event.GroupResize;
import envoy.event.contact.AccountDeletion;
import envoy.util.EnvoyLog;
import envoy.server.data.*;
@ -24,6 +25,12 @@ public final class GroupResizeProcessor implements ObjectProcessor<GroupResize>
final var group = persistenceManager.getGroupByID(groupResize.getGroupID());
final var sender = persistenceManager.getUserByID(groupResize.get().getID());
// Inform the sender that this group has already been deleted
if (group == null) {
writeProxy.write(socketID, new AccountDeletion(groupResize.getGroupID()));
return;
}
// Perform the desired operation
switch (groupResize.getOperation()) {
case ADD:

View File

@ -4,7 +4,7 @@ import java.time.Instant;
import java.util.logging.*;
import envoy.event.ElementOperation;
import envoy.event.contact.UserOperation;
import envoy.event.contact.*;
import envoy.util.EnvoyLog;
import envoy.server.data.PersistenceManager;
@ -22,10 +22,18 @@ public final class UserOperationProcessor implements ObjectProcessor<UserOperati
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public void process(UserOperation evt, long socketId, ObjectWriteProxy writeProxy) {
final long userID = ConnectionManager.getInstance().getUserIDBySocketID(socketId);
public void process(UserOperation evt, long socketID, ObjectWriteProxy writeProxy) {
final long userID = ConnectionManager.getInstance().getUserIDBySocketID(socketID);
final long contactID = evt.get().getID();
final var sender = persistenceManager.getUserByID(userID);
final var recipient = persistenceManager.getUserByID(contactID);
// Inform the sender if the requested contact has already been deleted
if (recipient == null) {
writeProxy.write(socketID, new AccountDeletion(contactID));
return;
}
final var sender = persistenceManager.getUserByID(userID);
switch (evt.getOperationType()) {
case ADD:
logger.log(Level.FINE,
@ -45,7 +53,7 @@ public final class UserOperationProcessor implements ObjectProcessor<UserOperati
sender.setLatestContactDeletion(Instant.now());
// Notify the removed contact on next startup(s) of this deletion
persistenceManager.getUserByID(contactID).setLatestContactDeletion(Instant.now());
recipient.setLatestContactDeletion(Instant.now());
// Notify the removed contact if online
if (connectionManager.isOnline(contactID))