Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add a replace parameter for single node additions #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feature: Add a replace parameter for single node additions
This provides an "atomic" way of performing a replacement on a node without having to make two separate rest calls.
kezz committed Nov 8, 2023
commit cf6074b371848f3f015931206ce2b6e81ff25922
Original file line number Diff line number Diff line change
@@ -251,11 +251,19 @@ public void nodesAddSingle(Context ctx) throws JsonProcessingException {
String name = ctx.pathParam("id");
Node node = ctx.bodyAsClass(Node.class);
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);
boolean replace = ParamUtils.readBoolean(ctx, "replace", false);

CompletableFuture<Collection<Node>> future = this.groupManager.loadGroup(name).thenCompose(opt -> {
if (opt.isPresent()) {
Group group = opt.get();
group.data().add(node, mergeStrategy);

if (replace) {
group.data().remove(node);
group.data().add(node);
} else {
group.data().add(node, mergeStrategy);
}

return this.groupManager.saveGroup(group).thenApply(v -> {
this.messagingService.pushUpdate();
return group.getNodes();
Original file line number Diff line number Diff line change
@@ -258,9 +258,16 @@ public void nodesAddSingle(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
Node node = ctx.bodyAsClass(Node.class);
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);
boolean replace = ParamUtils.readBoolean(ctx, "replace", false);

CompletableFuture<Collection<Node>> future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
user.data().add(node, mergeStrategy);
if (replace) {
user.data().remove(node);
user.data().add(node);
} else {
user.data().add(node, mergeStrategy);
}

return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
Original file line number Diff line number Diff line change
@@ -47,4 +47,18 @@ public static TemporaryNodeMergeStrategy queryParamAsTemporaryNodeMergeStrategy(
return objectMapper.readValue("\"" + string + "\"", TemporaryNodeMergeStrategy.class);
}
}

public static boolean readBoolean(Context ctx, String name, boolean defaultIfMissing) {
final String string = ctx.queryParam(name);

if (string == null) {
return defaultIfMissing;
} else if (string.equals("true")) {
return true;
} else if (string.equals("false")) {
return false;
} else {
throw new IllegalArgumentException("invalid boolean '" + string + "' for query param '" + name + "'");
}
}
}
9 changes: 9 additions & 0 deletions src/main/resources/luckperms-openapi.yml
Original file line number Diff line number Diff line change
@@ -323,6 +323,7 @@ paths:
operationId: add-user-node
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
- $ref: '#/components/parameters/replace'
responses:
'200':
description: Ok - returns the updated nodes
@@ -862,6 +863,7 @@ paths:
operationId: add-group-node
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
- $ref: '#/components/parameters/replace'
responses:
'200':
description: Ok - returns the updated nodes
@@ -1848,5 +1850,12 @@ components:
$ref: '#/components/schemas/TemporaryNodeMergeStrategy'
required: false
description: The node merge strategy
replace:
name: replace
in: query
schema:
type: boolean
required: false
description: If the node should replace an existing node with the same key (delete and add)
security:
- apikey: []