Skip to content

Commit 13918e9

Browse files
committed
Add events controller
1 parent 0c4939b commit 13918e9

10 files changed

+501
-14
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ license {
2121

2222
repositories {
2323
mavenCentral()
24-
maven { url 'https://nexus.lucko.me/repository/maven-snapshots/' }
24+
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
2525
}
2626

2727
dependencies {
2828
compileOnly 'org.slf4j:slf4j-api:1.7.36'
29-
compileOnly 'net.luckperms:api:5.5-20231022.200451-2'
29+
compileOnly 'net.luckperms:api:5.5-20240218.224354-2'
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: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.javalin.plugin.json.JavalinJackson;
3737
import io.javalin.plugin.openapi.utils.OpenApiVersionUtil;
3838
import me.lucko.luckperms.extension.rest.controller.ActionController;
39+
import me.lucko.luckperms.extension.rest.controller.EventController;
3940
import me.lucko.luckperms.extension.rest.controller.GroupController;
4041
import me.lucko.luckperms.extension.rest.controller.PermissionHolderController;
4142
import me.lucko.luckperms.extension.rest.controller.TrackController;
@@ -58,6 +59,7 @@
5859
import static io.javalin.apibuilder.ApiBuilder.path;
5960
import static io.javalin.apibuilder.ApiBuilder.post;
6061
import static io.javalin.apibuilder.ApiBuilder.put;
62+
import static io.javalin.apibuilder.ApiBuilder.sse;
6163

6264
/**
6365
* An HTTP server that implements a REST API for LuckPerms.
@@ -67,6 +69,7 @@ public class RestServer implements AutoCloseable {
6769

6870
private final ObjectMapper objectMapper;
6971
private final Javalin app;
72+
private final AutoCloseable routesClosable;
7073

7174
public RestServer(LuckPerms luckPerms, int port) {
7275
LOGGER.info("[REST] Starting server...");
@@ -78,13 +81,18 @@ public RestServer(LuckPerms luckPerms, int port) {
7881

7982
this.setupLogging(this.app);
8083
this.setupErrorHandlers(this.app);
81-
this.setupRoutes(this.app, luckPerms);
84+
this.routesClosable = this.setupRoutes(this.app, luckPerms);
8285

8386
LOGGER.info("[REST] Startup complete! Listening on http://localhost:" + port);
8487
}
8588

8689
@Override
8790
public void close() {
91+
try {
92+
this.routesClosable.close();
93+
} catch (Exception e) {
94+
throw new RuntimeException(e);
95+
}
8896
this.app.close();
8997
}
9098

@@ -114,7 +122,7 @@ private void setupErrorHandlers(Javalin app) {
114122
});
115123
}
116124

117-
private void setupRoutes(Javalin app, LuckPerms luckPerms) {
125+
private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
118126
app.get("/", ctx -> ctx.redirect("/docs/swagger-ui"));
119127

120128
app.get("health", ctx -> {
@@ -128,22 +136,20 @@ private void setupRoutes(Javalin app, LuckPerms luckPerms) {
128136
GroupController groupController = new GroupController(luckPerms.getGroupManager(), messagingService, this.objectMapper);
129137
TrackController trackController = new TrackController(luckPerms.getTrackManager(), luckPerms.getGroupManager(), messagingService, this.objectMapper);
130138
ActionController actionController = new ActionController(luckPerms.getActionLogger());
139+
EventController eventController = new EventController(luckPerms.getEventBus());
131140

132141
app.routes(() -> {
133142
path("user", () -> {
134143
get("lookup", userController::lookup);
135144
setupControllerRoutes(userController);
136145
});
137-
path("group", () -> {
138-
setupControllerRoutes(groupController);
139-
});
140-
path("track", () -> {
141-
setupControllerRoutes(trackController);
142-
});
143-
path("action", () -> {
144-
setupControllerRoutes(actionController);
145-
});
146+
path("group", () -> setupControllerRoutes(groupController));
147+
path("track", () -> setupControllerRoutes(trackController));
148+
path("action", () -> setupControllerRoutes(actionController));
149+
path("event", () -> setupControllerRoutes(eventController));
146150
});
151+
152+
return eventController;
147153
}
148154

149155
private void setupControllerRoutes(PermissionHolderController controller) {
@@ -192,6 +198,14 @@ private void setupControllerRoutes(ActionController controller) {
192198
post(controller::submit);
193199
}
194200

201+
private void setupControllerRoutes(EventController controller) {
202+
sse("log-broadcast", controller::logBroadcast);
203+
sse("post-network-sync", controller::postNetworkSync);
204+
sse("post-sync", controller::postSync);
205+
sse("pre-network-sync", controller::preNetworkSync);
206+
sse("pre-sync", controller::preSync);
207+
}
208+
195209
private void setupAuth(JavalinConfig config) {
196210
if (RestConfig.getBoolean("auth", false)) {
197211
Set<String> keys = ImmutableSet.copyOf(
@@ -237,7 +251,12 @@ private void setupAuth(JavalinConfig config) {
237251
}
238252

239253
private void setupLogging(Javalin app) {
240-
app.before(ctx -> ctx.attribute("startTime", System.currentTimeMillis()));
254+
app.before(ctx -> {
255+
ctx.attribute("startTime", System.currentTimeMillis());
256+
if (ctx.path().startsWith("/event/")) {
257+
LOGGER.info("[REST] %s %s - %d".formatted(ctx.method(), ctx.path(), ctx.status()));
258+
}
259+
});
241260
app.after(ctx -> {
242261
//noinspection ConstantConditions
243262
long startTime = ctx.attribute("startTime");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
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.bind;
27+
28+
import com.fasterxml.jackson.core.JsonGenerator;
29+
import com.fasterxml.jackson.databind.JsonSerializer;
30+
import com.fasterxml.jackson.databind.SerializerProvider;
31+
import net.luckperms.api.actionlog.Action;
32+
33+
import java.io.IOException;
34+
import java.util.UUID;
35+
36+
public class ActionSerializer extends JsonSerializer<Action> {
37+
38+
@Override
39+
public void serialize(Action value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
40+
gen.writePOJO(Model.from(value));
41+
}
42+
43+
44+
record Model(long timestamp, SourceModel source, TargetModel target, String description) {
45+
static Model from(Action action) {
46+
return new Model(
47+
action.getTimestamp().getEpochSecond(),
48+
SourceModel.from(action.getSource()),
49+
TargetModel.from(action.getTarget()),
50+
action.getDescription()
51+
);
52+
}
53+
}
54+
55+
record SourceModel(UUID uniqueId, String name) {
56+
static SourceModel from(Action.Source source) {
57+
return new SourceModel(source.getUniqueId(), source.getName());
58+
}
59+
}
60+
61+
record TargetModel(UUID uniqueId, String name, Action.Target.Type type) {
62+
static TargetModel from(Action.Target action) {
63+
return new TargetModel(action.getUniqueId().orElse(null), action.getName(), action.getType());
64+
}
65+
}
66+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
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.bind.event;
27+
28+
import com.fasterxml.jackson.core.JsonGenerator;
29+
import com.fasterxml.jackson.databind.JsonSerializer;
30+
import com.fasterxml.jackson.databind.SerializerProvider;
31+
import net.luckperms.api.actionlog.Action;
32+
import net.luckperms.api.event.log.LogBroadcastEvent;
33+
34+
import java.io.IOException;
35+
36+
public class LogBroadcastEventSerializer extends JsonSerializer<LogBroadcastEvent> {
37+
38+
@Override
39+
public void serialize(LogBroadcastEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
40+
gen.writePOJO(Model.from(value));
41+
}
42+
43+
record Model(Action entry, LogBroadcastEvent.Origin origin) {
44+
static Model from(LogBroadcastEvent event) {
45+
return new Model(
46+
event.getEntry(),
47+
event.getOrigin()
48+
);
49+
}
50+
}
51+
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
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.bind.event;
27+
28+
import com.fasterxml.jackson.core.JsonGenerator;
29+
import com.fasterxml.jackson.databind.JsonSerializer;
30+
import com.fasterxml.jackson.databind.SerializerProvider;
31+
import net.luckperms.api.actionlog.Action;
32+
import net.luckperms.api.event.log.LogBroadcastEvent;
33+
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
34+
import net.luckperms.api.event.sync.SyncType;
35+
36+
import java.io.IOException;
37+
import java.util.UUID;
38+
39+
public class PostNetworkSyncEventSerializer extends JsonSerializer<PostNetworkSyncEvent> {
40+
41+
@Override
42+
public void serialize(PostNetworkSyncEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
43+
gen.writePOJO(Model.from(value));
44+
}
45+
46+
record Model(UUID syncId, SyncType type, boolean didSyncOccur, UUID specificUserUniqueId) {
47+
static Model from(PostNetworkSyncEvent event) {
48+
return new Model(
49+
event.getSyncId(),
50+
event.getType(),
51+
event.didSyncOccur(),
52+
event.getSpecificUserUniqueId()
53+
);
54+
}
55+
}
56+
57+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
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.bind.event;
27+
28+
import com.fasterxml.jackson.core.JsonGenerator;
29+
import com.fasterxml.jackson.databind.JsonSerializer;
30+
import com.fasterxml.jackson.databind.SerializerProvider;
31+
import net.luckperms.api.actionlog.Action;
32+
import net.luckperms.api.event.log.LogBroadcastEvent;
33+
import net.luckperms.api.event.sync.PostSyncEvent;
34+
35+
import java.io.IOException;
36+
37+
public class PostSyncEventSerializer extends JsonSerializer<PostSyncEvent> {
38+
39+
@Override
40+
public void serialize(PostSyncEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
41+
gen.writePOJO(Model.from(value));
42+
}
43+
44+
record Model() {
45+
static Model from(PostSyncEvent event) {
46+
return new Model();
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)