mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-18 01:46:44 +01:00
Change ccount delete to delete all referenced items in the database.
This commit is contained in:
@@ -3,6 +3,8 @@ package emu.grasscutter.database;
|
||||
import java.util.List;
|
||||
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
|
||||
import dev.morphia.experimental.MorphiaSession;
|
||||
import dev.morphia.query.FindOptions;
|
||||
import dev.morphia.query.Sort;
|
||||
import dev.morphia.query.experimental.filters.Filters;
|
||||
@@ -95,8 +97,31 @@ public final class DatabaseHelper {
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
|
||||
}
|
||||
|
||||
public static boolean deleteAccount(String username) {
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).delete().getDeletedCount() > 0;
|
||||
//public static boolean deleteAccount(String username) {
|
||||
// return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).delete().getDeletedCount() > 0;
|
||||
//}
|
||||
public static void deleteAccount(Account target) {
|
||||
// To delete an account, we need to also delete all the other documents in the database that reference the account.
|
||||
// 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 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();
|
||||
|
||||
// Delete the player.
|
||||
DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete();
|
||||
|
||||
// Finally, delete the account itself.
|
||||
DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
|
||||
}
|
||||
|
||||
public static List<Player> getAllPlayers() {
|
||||
|
||||
Reference in New Issue
Block a user