Skip to content

Commit

Permalink
feature: Add a replace parameter for single node additions
Browse files Browse the repository at this point in the history
This provides an "atomic" way of performing a replacement on a node without having to make two separate rest calls.
  • Loading branch information
kezz committed Nov 8, 2023
1 parent 0c4939b commit cf6074b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: []

0 comments on commit cf6074b

Please sign in to comment.