Skip to content

Commit

Permalink
Return PlayerSaveResult data when creating a user
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Oct 23, 2023
1 parent d415fba commit 0c4939b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.model.PlayerSaveResult;

import java.io.IOException;
import java.util.Set;
import java.util.UUID;

public class PlayerSaveResultSerializer extends JsonSerializer<PlayerSaveResult> {

@Override
public void serialize(PlayerSaveResult value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}

record Model(Set<PlayerSaveResult.Outcome> outcomes, String previousUsername, Set<UUID> otherUniqueIds) {
static Model from(PlayerSaveResult result) {
return new Model(
result.getOutcomes(),
result.getPreviousUsername(),
result.getOtherUniqueIds()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,12 @@ private CompletableFuture<User> loadUserCached(UUID uniqueId) {
public void create(Context ctx) {
CreateReq body = ctx.bodyAsClass(CreateReq.class);

CompletableFuture<User> future = this.userManager.savePlayerData(body.uniqueId, body.username)
.thenCompose(result -> {
if (result.includes(PlayerSaveResult.Outcome.CLEAN_INSERT)) {
return this.userManager.loadUser(body.uniqueId, body.username);
} else {
return CompletableFuture.completedFuture(null);
}
});

CompletableFuture<PlayerSaveResult> future = this.userManager.savePlayerData(body.uniqueId, body.username);
ctx.future(future, result -> {
if (result == null) {
ctx.status(409).result("User already exists!");
} else {
ctx.status(201).json(result);
if (((PlayerSaveResult) result).includes(PlayerSaveResult.Outcome.CLEAN_INSERT)) {
ctx.status(201);
}
ctx.json(result);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import me.lucko.luckperms.extension.rest.bind.MetadataSerializer;
import me.lucko.luckperms.extension.rest.bind.NodeDeserializer;
import me.lucko.luckperms.extension.rest.bind.NodeSerializer;
import me.lucko.luckperms.extension.rest.bind.PlayerSaveResultSerializer;
import me.lucko.luckperms.extension.rest.bind.PromotionResultSerializer;
import me.lucko.luckperms.extension.rest.bind.QueryOptionsDeserializer;
import me.lucko.luckperms.extension.rest.bind.TrackSerializer;
import me.lucko.luckperms.extension.rest.bind.UserSerializer;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.model.PlayerSaveResult;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node;
Expand Down Expand Up @@ -72,6 +74,7 @@ public CustomObjectMapper() {
module.addSerializer(CachedMetaData.class, new MetadataSerializer());
module.addDeserializer(Node.class, new NodeDeserializer());
module.addSerializer(Node.class, new NodeSerializer());
module.addSerializer(PlayerSaveResult.class, new PlayerSaveResultSerializer());
module.addSerializer(PromotionResult.class, new PromotionResultSerializer());
module.addDeserializer(QueryOptions.class, new QueryOptionsDeserializer());
module.addSerializer(Track.class, new TrackSerializer());
Expand Down
63 changes: 50 additions & 13 deletions src/main/resources/luckperms-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,34 @@ paths:
summary: Create a new user
operationId: create-user
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/PlayerSaveResult'
examples:
example-1:
value:
outcomes:
- username_updated
- other_unique_ids_present_for_username
previousUsername: Luck
otherUniqueIds:
- c1d60c50-70b5-4722-8057-87767557e50d
'201':
description: User created
content:
application/json:
schema:
$ref: '#/components/schemas/User'
$ref: '#/components/schemas/PlayerSaveResult'
examples:
example-1:
value:
uniqueId: c1d60c50-70b5-4722-8057-87767557e50d
username: Luck
nodes:
- key: group.default
type: inheritance
value: true
context: []
metadata:
meta: {}
primaryGroup: default
outcomes:
- clean_insert
'400':
description: Missing required information
'409':
description: User already exists
requestBody:
content:
application/json:
Expand Down Expand Up @@ -1776,6 +1781,38 @@ components:
required:
- health
- details
PlayerSaveResult:
title: PlayerSaveResult
type: object
description: The result of an operation to save data about a player
properties:
outcomes:
type: array
description: if the app is healthy
uniqueItems: true
items:
$ref: '#/components/schemas/PlayerSaveResultOutcome'
previousUsername:
type: string
description: the previous username involved in the result (only applies for the username_updated outcome)
minLength: 1
example: Luck
otherUniqueIds:
type: array
description: the other unique ids involved in the result (only applies for the other_unique_ids_present_for_username outcome)
items:
$ref: '#/components/schemas/UniqueId'
required:
- outcomes
PlayerSaveResultOutcome:
title: PlayerSaveResultOutcome
type: string
enum:
- clean_insert
- no_change
- username_updated
- other_unique_ids_present_for_username
description: The statuses returned in a PlayerSaveResult
securitySchemes:
apikey:
type: http
Expand Down

0 comments on commit 0c4939b

Please sign in to comment.