Save renamed group before syncing with storage (#3129)

This commit is contained in:
Emily 2021-08-06 05:59:38 -03:00 committed by GitHub
parent 433272518f
commit 0fa3095241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 36 deletions

View File

@ -37,6 +37,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
* Utility methods for saving users, groups and tracks.
@ -88,40 +89,28 @@ public final class StorageAssistant {
}
}
public static void save(Group group, Sender sender, LuckPermsPlugin plugin) {
public static CompletableFuture<Void> save(Group group, Sender sender, LuckPermsPlugin plugin) {
try {
plugin.getStorage().saveGroup(group).get();
} catch (Exception e) {
plugin.getLogger().warn("Error whilst saving group", e);
Message.GROUP_SAVE_ERROR.send(sender, group);
return;
return failedFuture(e);
}
plugin.getGroupManager().invalidateAllGroupCaches();
plugin.getUserManager().invalidateAllUserCaches();
Optional<InternalMessagingService> messagingService = plugin.getMessagingService();
if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
messagingService.get().getUpdateBuffer().request();
}
return invalidateCachesAndPushUpdates(plugin);
}
public static void save(Track track, Sender sender, LuckPermsPlugin plugin) {
public static CompletableFuture<Void> save(Track track, Sender sender, LuckPermsPlugin plugin) {
try {
plugin.getStorage().saveTrack(track).get();
} catch (Exception e) {
plugin.getLogger().warn("Error whilst saving track", e);
Message.TRACK_SAVE_ERROR.send(sender, track.getName());
return;
return failedFuture(e);
}
plugin.getGroupManager().invalidateAllGroupCaches();
plugin.getUserManager().invalidateAllUserCaches();
Optional<InternalMessagingService> messagingService = plugin.getMessagingService();
if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
messagingService.get().getUpdateBuffer().request();
}
return invalidateCachesAndPushUpdates(plugin);
}
public static void save(PermissionHolder holder, Sender sender, LuckPermsPlugin plugin) {
@ -136,4 +125,21 @@ public final class StorageAssistant {
}
}
public static CompletableFuture<Void> invalidateCachesAndPushUpdates(LuckPermsPlugin plugin) {
plugin.getGroupManager().invalidateAllGroupCaches();
plugin.getUserManager().invalidateAllUserCaches();
Optional<InternalMessagingService> messagingService = plugin.getMessagingService();
if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
return messagingService.get().getUpdateBuffer().request();
} else {
return CompletableFuture.completedFuture(null);
}
}
private static <T> CompletableFuture<T> failedFuture(Throwable ex) {
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(ex);
return future;
}
}

View File

@ -55,6 +55,7 @@ import net.luckperms.api.event.cause.DeletionCause;
import net.luckperms.api.model.data.DataType;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class GroupRename extends ChildCommand<Group> {
public GroupRename() {
@ -104,24 +105,27 @@ public class GroupRename extends ChildCommand<Group> {
.description("rename", newGroup.getName())
.build().submit(plugin, sender);
if (!args.remove("--update-parent-lists")) {
StorageAssistant.save(newGroup, sender, plugin);
} else {
// the group is now renamed, proceed to update its representing inheritance nodes
BulkUpdate operation = BulkUpdateBuilder.create()
.trackStatistics(false)
.dataType(me.lucko.luckperms.common.bulkupdate.DataType.ALL)
.action(UpdateAction.of(QueryField.PERMISSION, Inheritance.key(newGroupName)))
.query(Query.of(QueryField.PERMISSION, Constraint.of(StandardComparison.EQUAL, Inheritance.key(target.getName()))))
.build();
plugin.getStorage().applyBulkUpdate(operation).whenCompleteAsync((v, ex) -> {
plugin.getSyncTaskBuffer().requestDirectly();
if (ex != null) {
ex.printStackTrace();
}
}, plugin.getBootstrap().getScheduler().async())
.thenRunAsync(() -> StorageAssistant.save(newGroup, sender, plugin));
}
StorageAssistant.save(newGroup, sender, plugin)
.thenCompose((v) -> {
if (args.remove("--update-parent-lists")) {
// the group is now renamed, proceed to update its representing inheritance nodes
BulkUpdate operation = BulkUpdateBuilder.create()
.trackStatistics(false)
.dataType(me.lucko.luckperms.common.bulkupdate.DataType.ALL)
.action(UpdateAction.of(QueryField.PERMISSION, Inheritance.key(newGroupName)))
.query(Query.of(QueryField.PERMISSION, Constraint.of(StandardComparison.EQUAL, Inheritance.key(target.getName()))))
.build();
return plugin.getStorage().applyBulkUpdate(operation);
} else {
return CompletableFuture.completedFuture(v);
}
}).whenCompleteAsync((v, ex) -> {
if (ex != null) {
ex.printStackTrace();
}
plugin.getSyncTaskBuffer().requestDirectly();
}, plugin.getBootstrap().getScheduler().async());
}
@Override