From f26fe213a249beabe5d0ba5f865b855d7d510c4b Mon Sep 17 00:00:00 2001 From: Bi Jiakai Date: Tue, 10 May 2022 17:17:54 +0800 Subject: [PATCH] Fixed account delete can not delete all related data (#767) --- .../grasscutter/database/DatabaseHelper.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index bfcd006df..8f1de0bb9 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -16,6 +16,8 @@ import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.player.Player; +import static com.mongodb.client.model.Filters.eq; + public final class DatabaseHelper { public static Account createAccount(String username) { return createAccountWithId(username, 0); @@ -101,17 +103,20 @@ public final class DatabaseHelper { // This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the // database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ... - // Delete mails, gacha records, items and avatars. - DatabaseManager.getDatastore().find(Mail.class).filter(Filters.eq("ownerUid", target.getPlayerUid())).delete(); - DatabaseManager.getDatastore().find(GachaRecord.class).filter(Filters.eq("ownerId", target.getPlayerUid())).delete(); - DatabaseManager.getDatastore().find(GameItem.class).filter(Filters.eq("ownerId", target.getPlayerUid())).delete(); - DatabaseManager.getDatastore().find(Avatar.class).filter(Filters.eq("ownerId", target.getPlayerUid())).delete(); + // Delete Mail.class data + DatabaseManager.getDatabase().getCollection("mail").deleteMany(eq("ownerUid", target.getPlayerUid())); + // Delete Avatar.class data + DatabaseManager.getDatabase().getCollection("avatars").deleteMany(eq("ownerId", target.getPlayerUid())); + // Delete GachaRecord.class data + DatabaseManager.getDatabase().getCollection("gachas").deleteMany(eq("ownerId", target.getPlayerUid())); + // Delete GameItem.class data + DatabaseManager.getDatabase().getCollection("items").deleteMany(eq("ownerId", target.getPlayerUid())); // Delete friendships. // Here, we need to make sure to not only delete the deleted account's friendships, // but also all friendship entries for that account's friends. - DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", target.getPlayerUid())).delete(); - DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", target.getPlayerUid())).delete(); + DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("ownerId", target.getPlayerUid())); + DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("friendId", target.getPlayerUid())); // Delete the player. DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete();