Skip to content

Commit 061fc88

Browse files
committed
Add action query API methods
1 parent 81e20e9 commit 061fc88

File tree

8 files changed

+235
-7
lines changed

8 files changed

+235
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repositories {
2626

2727
dependencies {
2828
compileOnly 'org.slf4j:slf4j-api:1.7.36'
29-
compileOnly 'net.luckperms:api:5.5-20240307.193022-3'
29+
compileOnly 'net.luckperms:api:5.5-20240616.203859-4'
3030
implementation 'io.javalin:javalin:4.6.4'
3131
implementation 'io.javalin:javalin-openapi:4.6.4'
3232
}

src/main/java/me/lucko/luckperms/extension/rest/RestServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
136136
UserController userController = new UserController(luckPerms.getUserManager(), luckPerms.getTrackManager(), messagingService, this.objectMapper);
137137
GroupController groupController = new GroupController(luckPerms.getGroupManager(), messagingService, this.objectMapper);
138138
TrackController trackController = new TrackController(luckPerms.getTrackManager(), luckPerms.getGroupManager(), messagingService, this.objectMapper);
139-
ActionController actionController = new ActionController(luckPerms.getActionLogger());
139+
ActionController actionController = new ActionController(luckPerms.getActionLogger(), this.objectMapper);
140140
MessagingController messagingController = new MessagingController(luckPerms.getMessagingService().orElse(null), luckPerms.getUserManager(), this.objectMapper);
141141
EventController eventController = new EventController(luckPerms.getEventBus());
142142

@@ -202,6 +202,7 @@ private void setupControllerRoutes(TrackController controller) {
202202
}
203203

204204
private void setupControllerRoutes(ActionController controller) {
205+
get(controller::get);
205206
post(controller::submit);
206207
}
207208

src/main/java/me/lucko/luckperms/extension/rest/controller/ActionController.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,51 @@
2525

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

28+
import com.fasterxml.jackson.core.JsonProcessingException;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
2830
import io.javalin.http.Context;
31+
import me.lucko.luckperms.extension.rest.model.ActionPage;
32+
import me.lucko.luckperms.extension.rest.model.ActionRequest;
2933
import net.luckperms.api.actionlog.Action;
3034
import net.luckperms.api.actionlog.ActionLogger;
35+
import net.luckperms.api.actionlog.filter.ActionFilter;
3136

3237
import java.util.concurrent.CompletableFuture;
3338

3439
public class ActionController {
3540

3641
private final ActionLogger actionLogger;
42+
private final ObjectMapper objectMapper;
3743

38-
public ActionController(ActionLogger actionLogger) {
44+
public ActionController(ActionLogger actionLogger, ObjectMapper objectMapper) {
3945
this.actionLogger = actionLogger;
46+
this.objectMapper = objectMapper;
47+
}
48+
49+
// GET /action
50+
public void get(Context ctx) throws JsonProcessingException {
51+
ActionFilter filter = ActionRequest.parseFilter(this.objectMapper, ctx);
52+
53+
Integer pageSize = ctx.queryParamAsClass("pageSize", Integer.class).getOrDefault(null);
54+
Integer pageNumber = ctx.queryParamAsClass("pageNumber", Integer.class).getOrDefault(null);
55+
56+
if (pageSize == null && pageNumber == null) {
57+
CompletableFuture<ActionPage> future = this.actionLogger.queryActions(filter)
58+
.thenApply(list -> new ActionPage(list, list.size()));
59+
ctx.future(future);
60+
} else {
61+
if (pageSize == null) {
62+
ctx.status(400).result("pageSize query parameter is required when pageNumber is provided");
63+
return;
64+
} else if (pageNumber == null) {
65+
ctx.status(400).result("pageNumber query parameter is required when pageSize is provided");
66+
return;
67+
}
68+
69+
CompletableFuture<ActionPage> future = this.actionLogger.queryActions(filter, pageSize, pageNumber)
70+
.thenApply(ActionPage::from);
71+
ctx.future(future);
72+
}
4073
}
4174

4275
// POST /action

src/main/java/me/lucko/luckperms/extension/rest/controller/GroupController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public void metaGet(Context ctx) {
320320
});
321321
}
322322

323-
// GET /group/{id}/permissionCheck
323+
// GET /group/{id}/permission-check
324324
@Override
325325
public void permissionCheck(Context ctx) {
326326
String name = ctx.pathParam("id");
@@ -340,7 +340,7 @@ public void permissionCheck(Context ctx) {
340340
});
341341
}
342342

343-
// POST /group/{id}/permissionCheck
343+
// POST /group/{id}/permission-check
344344
@Override
345345
public void permissionCheckCustom(Context ctx) {
346346
String name = ctx.pathParam("id");

src/main/java/me/lucko/luckperms/extension/rest/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public void metaGet(Context ctx) throws JsonProcessingException {
297297
ctx.future(future);
298298
}
299299

300-
// GET /user/{id}/permissionCheck
300+
// GET /user/{id}/permission-check
301301
@Override
302302
public void permissionCheck(Context ctx) throws JsonProcessingException {
303303
UUID uniqueId = pathParamAsUuid(ctx);
@@ -313,7 +313,7 @@ public void permissionCheck(Context ctx) throws JsonProcessingException {
313313
ctx.future(future);
314314
}
315315

316-
// POST /user/{id}/permissionCheck
316+
// POST /user/{id}/permission-check
317317
@Override
318318
public void permissionCheckCustom(Context ctx) throws JsonProcessingException {
319319
UUID uniqueId = pathParamAsUuid(ctx);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <luck@lucko.me>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package me.lucko.luckperms.extension.rest.model;
27+
28+
import net.luckperms.api.actionlog.Action;
29+
import net.luckperms.api.util.Page;
30+
31+
import java.util.List;
32+
33+
public record ActionPage(List<Action> entries, int overallSize) {
34+
public static ActionPage from(Page<Action> page) {
35+
return new ActionPage(
36+
page.entries(),
37+
page.overallSize()
38+
);
39+
}
40+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <luck@lucko.me>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package me.lucko.luckperms.extension.rest.model;
27+
28+
import com.fasterxml.jackson.core.JsonProcessingException;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
import io.javalin.http.Context;
31+
import net.luckperms.api.actionlog.filter.ActionFilter;
32+
33+
import java.util.UUID;
34+
35+
public class ActionRequest {
36+
37+
private static UUID parseUuid(ObjectMapper objectMapper, String s) throws JsonProcessingException {
38+
String uuidString = "\"" + s + "\"";
39+
return objectMapper.readValue(uuidString, UUID.class);
40+
}
41+
42+
public static ActionFilter parseFilter(ObjectMapper objectMapper, Context ctx) throws JsonProcessingException {
43+
String source = ctx.queryParam("source");
44+
if (source != null && !source.isEmpty()) {
45+
return ActionFilter.source(parseUuid(objectMapper, source));
46+
}
47+
48+
String user = ctx.queryParam("user");
49+
if (user != null && !user.isEmpty()) {
50+
return ActionFilter.user(parseUuid(objectMapper, user));
51+
}
52+
53+
String group = ctx.queryParam("group");
54+
if (group != null && !group.isEmpty()) {
55+
return ActionFilter.group(group);
56+
}
57+
58+
String track = ctx.queryParam("track");
59+
if (track != null && !track.isEmpty()) {
60+
return ActionFilter.track(track);
61+
}
62+
63+
String search = ctx.queryParam("search");
64+
if (search != null && !search.isEmpty()) {
65+
return ActionFilter.search(search);
66+
}
67+
68+
return ActionFilter.any();
69+
}
70+
71+
}

src/main/resources/luckperms-openapi.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,89 @@ paths:
12481248
tags:
12491249
- Tracks
12501250
/action:
1251+
get:
1252+
summary: Query actions
1253+
operationId: get-actions
1254+
responses:
1255+
'200':
1256+
description: Ok
1257+
content:
1258+
application/json:
1259+
schema:
1260+
type: object
1261+
properties:
1262+
entries:
1263+
type: array
1264+
items:
1265+
$ref: '#/components/schemas/Action'
1266+
overallSize:
1267+
type: integer
1268+
examples:
1269+
example-1:
1270+
value:
1271+
entries:
1272+
- timestamp: 1658428395
1273+
source:
1274+
uniqueId: c1d60c50-70b5-4722-8057-87767557e50d
1275+
name: Luck
1276+
target:
1277+
uniqueId: 069a79f4-44e9-4726-a5be-fca90e38aaf5
1278+
name: Notch
1279+
type: user
1280+
description: permission set minecraft.command.ban true
1281+
overallSize: 1
1282+
'400':
1283+
description: Missing required information
1284+
description: |
1285+
Query actions from the action logger.
1286+
1287+
If pageSize or pageNumber are specified, both must be specified.
1288+
If neither are specified, no pagination will be used and all results will be returned.
1289+
parameters:
1290+
- schema:
1291+
type: integer
1292+
minimum: 1
1293+
in: query
1294+
name: pageSize
1295+
description: The number of actions to return on each page
1296+
- schema:
1297+
type: integer
1298+
minimum: 1
1299+
in: query
1300+
name: pageNumber
1301+
description: The page to return
1302+
- schema:
1303+
type: string
1304+
minLength: 1
1305+
in: query
1306+
name: source
1307+
description: Filter by source user unique id
1308+
- schema:
1309+
type: string
1310+
minLength: 1
1311+
in: query
1312+
name: user
1313+
description: Filter by target user unique id
1314+
- schema:
1315+
type: string
1316+
minLength: 1
1317+
in: query
1318+
name: group
1319+
description: Filter by target group name
1320+
- schema:
1321+
type: string
1322+
minLength: 1
1323+
in: query
1324+
name: track
1325+
description: Filter by target track name
1326+
- schema:
1327+
type: string
1328+
minLength: 1
1329+
in: query
1330+
name: search
1331+
description: Filter by search value in source name, target name or description.
1332+
tags:
1333+
- Actions
12511334
post:
12521335
summary: Submit a new action
12531336
operationId: submit-action

0 commit comments

Comments
 (0)