Skip to content

Commit

Permalink
Add action query API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 11, 2024
1 parent 81e20e9 commit 061fc88
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.36'
compileOnly 'net.luckperms:api:5.5-20240307.193022-3'
compileOnly 'net.luckperms:api:5.5-20240616.203859-4'
implementation 'io.javalin:javalin:4.6.4'
implementation 'io.javalin:javalin-openapi:4.6.4'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
UserController userController = new UserController(luckPerms.getUserManager(), luckPerms.getTrackManager(), messagingService, this.objectMapper);
GroupController groupController = new GroupController(luckPerms.getGroupManager(), messagingService, this.objectMapper);
TrackController trackController = new TrackController(luckPerms.getTrackManager(), luckPerms.getGroupManager(), messagingService, this.objectMapper);
ActionController actionController = new ActionController(luckPerms.getActionLogger());
ActionController actionController = new ActionController(luckPerms.getActionLogger(), this.objectMapper);
MessagingController messagingController = new MessagingController(luckPerms.getMessagingService().orElse(null), luckPerms.getUserManager(), this.objectMapper);
EventController eventController = new EventController(luckPerms.getEventBus());

Expand Down Expand Up @@ -202,6 +202,7 @@ private void setupControllerRoutes(TrackController controller) {
}

private void setupControllerRoutes(ActionController controller) {
get(controller::get);
post(controller::submit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,51 @@

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import me.lucko.luckperms.extension.rest.model.ActionPage;
import me.lucko.luckperms.extension.rest.model.ActionRequest;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.actionlog.ActionLogger;
import net.luckperms.api.actionlog.filter.ActionFilter;

import java.util.concurrent.CompletableFuture;

public class ActionController {

private final ActionLogger actionLogger;
private final ObjectMapper objectMapper;

public ActionController(ActionLogger actionLogger) {
public ActionController(ActionLogger actionLogger, ObjectMapper objectMapper) {
this.actionLogger = actionLogger;
this.objectMapper = objectMapper;
}

// GET /action
public void get(Context ctx) throws JsonProcessingException {
ActionFilter filter = ActionRequest.parseFilter(this.objectMapper, ctx);

Integer pageSize = ctx.queryParamAsClass("pageSize", Integer.class).getOrDefault(null);
Integer pageNumber = ctx.queryParamAsClass("pageNumber", Integer.class).getOrDefault(null);

if (pageSize == null && pageNumber == null) {
CompletableFuture<ActionPage> future = this.actionLogger.queryActions(filter)
.thenApply(list -> new ActionPage(list, list.size()));
ctx.future(future);
} else {
if (pageSize == null) {
ctx.status(400).result("pageSize query parameter is required when pageNumber is provided");
return;
} else if (pageNumber == null) {
ctx.status(400).result("pageNumber query parameter is required when pageSize is provided");
return;
}

CompletableFuture<ActionPage> future = this.actionLogger.queryActions(filter, pageSize, pageNumber)
.thenApply(ActionPage::from);
ctx.future(future);
}
}

// POST /action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public void metaGet(Context ctx) {
});
}

// GET /group/{id}/permissionCheck
// GET /group/{id}/permission-check
@Override
public void permissionCheck(Context ctx) {
String name = ctx.pathParam("id");
Expand All @@ -340,7 +340,7 @@ public void permissionCheck(Context ctx) {
});
}

// POST /group/{id}/permissionCheck
// POST /group/{id}/permission-check
@Override
public void permissionCheckCustom(Context ctx) {
String name = ctx.pathParam("id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void metaGet(Context ctx) throws JsonProcessingException {
ctx.future(future);
}

// GET /user/{id}/permissionCheck
// GET /user/{id}/permission-check
@Override
public void permissionCheck(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
Expand All @@ -313,7 +313,7 @@ public void permissionCheck(Context ctx) throws JsonProcessingException {
ctx.future(future);
}

// POST /user/{id}/permissionCheck
// POST /user/{id}/permission-check
@Override
public void permissionCheckCustom(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* 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.model;

import net.luckperms.api.actionlog.Action;
import net.luckperms.api.util.Page;

import java.util.List;

public record ActionPage(List<Action> entries, int overallSize) {
public static ActionPage from(Page<Action> page) {
return new ActionPage(
page.entries(),
page.overallSize()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* 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.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import net.luckperms.api.actionlog.filter.ActionFilter;

import java.util.UUID;

public class ActionRequest {

private static UUID parseUuid(ObjectMapper objectMapper, String s) throws JsonProcessingException {
String uuidString = "\"" + s + "\"";
return objectMapper.readValue(uuidString, UUID.class);
}

public static ActionFilter parseFilter(ObjectMapper objectMapper, Context ctx) throws JsonProcessingException {
String source = ctx.queryParam("source");
if (source != null && !source.isEmpty()) {
return ActionFilter.source(parseUuid(objectMapper, source));
}

String user = ctx.queryParam("user");
if (user != null && !user.isEmpty()) {
return ActionFilter.user(parseUuid(objectMapper, user));
}

String group = ctx.queryParam("group");
if (group != null && !group.isEmpty()) {
return ActionFilter.group(group);
}

String track = ctx.queryParam("track");
if (track != null && !track.isEmpty()) {
return ActionFilter.track(track);
}

String search = ctx.queryParam("search");
if (search != null && !search.isEmpty()) {
return ActionFilter.search(search);
}

return ActionFilter.any();
}

}
83 changes: 83 additions & 0 deletions src/main/resources/luckperms-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,89 @@ paths:
tags:
- Tracks
/action:
get:
summary: Query actions
operationId: get-actions
responses:
'200':
description: Ok
content:
application/json:
schema:
type: object
properties:
entries:
type: array
items:
$ref: '#/components/schemas/Action'
overallSize:
type: integer
examples:
example-1:
value:
entries:
- timestamp: 1658428395
source:
uniqueId: c1d60c50-70b5-4722-8057-87767557e50d
name: Luck
target:
uniqueId: 069a79f4-44e9-4726-a5be-fca90e38aaf5
name: Notch
type: user
description: permission set minecraft.command.ban true
overallSize: 1
'400':
description: Missing required information
description: |
Query actions from the action logger.
If pageSize or pageNumber are specified, both must be specified.
If neither are specified, no pagination will be used and all results will be returned.
parameters:
- schema:
type: integer
minimum: 1
in: query
name: pageSize
description: The number of actions to return on each page
- schema:
type: integer
minimum: 1
in: query
name: pageNumber
description: The page to return
- schema:
type: string
minLength: 1
in: query
name: source
description: Filter by source user unique id
- schema:
type: string
minLength: 1
in: query
name: user
description: Filter by target user unique id
- schema:
type: string
minLength: 1
in: query
name: group
description: Filter by target group name
- schema:
type: string
minLength: 1
in: query
name: track
description: Filter by target track name
- schema:
type: string
minLength: 1
in: query
name: search
description: Filter by search value in source name, target name or description.
tags:
- Actions
post:
summary: Submit a new action
operationId: submit-action
Expand Down

0 comments on commit 061fc88

Please sign in to comment.