Skip to content

Commit 81e20e9

Browse files
committed
Add messaging service API methods
1 parent 36b38af commit 81e20e9

File tree

9 files changed

+313
-8
lines changed

9 files changed

+313
-8
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-20240218.224354-2'
29+
compileOnly 'net.luckperms:api:5.5-20240307.193022-3'
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import me.lucko.luckperms.extension.rest.controller.ActionController;
3939
import me.lucko.luckperms.extension.rest.controller.EventController;
4040
import me.lucko.luckperms.extension.rest.controller.GroupController;
41+
import me.lucko.luckperms.extension.rest.controller.MessagingController;
4142
import me.lucko.luckperms.extension.rest.controller.PermissionHolderController;
4243
import me.lucko.luckperms.extension.rest.controller.TrackController;
4344
import me.lucko.luckperms.extension.rest.controller.UserController;
@@ -136,6 +137,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
136137
GroupController groupController = new GroupController(luckPerms.getGroupManager(), messagingService, this.objectMapper);
137138
TrackController trackController = new TrackController(luckPerms.getTrackManager(), luckPerms.getGroupManager(), messagingService, this.objectMapper);
138139
ActionController actionController = new ActionController(luckPerms.getActionLogger());
140+
MessagingController messagingController = new MessagingController(luckPerms.getMessagingService().orElse(null), luckPerms.getUserManager(), this.objectMapper);
139141
EventController eventController = new EventController(luckPerms.getEventBus());
140142

141143
app.routes(() -> {
@@ -146,6 +148,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
146148
path("group", () -> setupControllerRoutes(groupController));
147149
path("track", () -> setupControllerRoutes(trackController));
148150
path("action", () -> setupControllerRoutes(actionController));
151+
path("messaging", () -> setupControllerRoutes(messagingController));
149152
path("event", () -> setupControllerRoutes(eventController));
150153
});
151154

@@ -173,7 +176,11 @@ private void setupControllerRoutes(PermissionHolderController controller) {
173176

174177
get("meta", controller::metaGet);
175178

176-
path("permissionCheck", () -> {
179+
path("permission-check", () -> {
180+
get(controller::permissionCheck);
181+
post(controller::permissionCheckCustom);
182+
});
183+
path("permissioncheck", () -> {
177184
get(controller::permissionCheck);
178185
post(controller::permissionCheckCustom);
179186
});
@@ -198,12 +205,21 @@ private void setupControllerRoutes(ActionController controller) {
198205
post(controller::submit);
199206
}
200207

208+
private void setupControllerRoutes(MessagingController controller) {
209+
path("update", () -> {
210+
post(controller::update);
211+
post("{id}", controller::updateUser);
212+
});
213+
post("custom", controller::custom);
214+
}
215+
201216
private void setupControllerRoutes(EventController controller) {
202217
sse("log-broadcast", controller::logBroadcast);
203218
sse("post-network-sync", controller::postNetworkSync);
204219
sse("post-sync", controller::postSync);
205220
sse("pre-network-sync", controller::preNetworkSync);
206221
sse("pre-sync", controller::preSync);
222+
sse("custom-message-receive", controller::customMessageReceive);
207223
}
208224

209225
private void setupAuth(JavalinConfig config) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.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.event.messaging.CustomMessageReceiveEvent;
32+
33+
import java.io.IOException;
34+
35+
public class CustomMessageReceiveEventSerializer extends JsonSerializer<CustomMessageReceiveEvent> {
36+
37+
@Override
38+
public void serialize(CustomMessageReceiveEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
39+
gen.writePOJO(Model.from(value));
40+
}
41+
42+
record Model(String channelId, String payload) {
43+
static Model from(CustomMessageReceiveEvent event) {
44+
return new Model(
45+
event.getChannelId(),
46+
event.getPayload()
47+
);
48+
}
49+
}
50+
51+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import net.luckperms.api.event.EventSubscription;
3232
import net.luckperms.api.event.LuckPermsEvent;
3333
import net.luckperms.api.event.log.LogBroadcastEvent;
34+
import net.luckperms.api.event.messaging.CustomMessageReceiveEvent;
3435
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
3536
import net.luckperms.api.event.sync.PostSyncEvent;
3637
import net.luckperms.api.event.sync.PreNetworkSyncEvent;
@@ -116,5 +117,9 @@ public void preSync(SseClient client) {
116117
handle(client, PreSyncEvent.class);
117118
}
118119

120+
// GET /custom-message-receive
121+
public void customMessageReceive(SseClient client) {
122+
handle(client, CustomMessageReceiveEvent.class);
123+
}
119124

120125
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.controller;
27+
28+
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.databind.ObjectMapper;
31+
import io.javalin.http.Context;
32+
import net.luckperms.api.messaging.MessagingService;
33+
import net.luckperms.api.model.user.User;
34+
import net.luckperms.api.model.user.UserManager;
35+
36+
import java.util.UUID;
37+
import java.util.concurrent.CompletableFuture;
38+
39+
public class MessagingController {
40+
41+
private final MessagingService messagingService;
42+
private final UserManager userManager;
43+
private final ObjectMapper objectMapper;
44+
45+
public MessagingController(MessagingService messagingService, UserManager userManager, ObjectMapper objectMapper) {
46+
this.messagingService = messagingService;
47+
this.userManager = userManager;
48+
this.objectMapper = objectMapper;
49+
}
50+
51+
private UUID parseUuid(String s) throws JsonProcessingException {
52+
String uuidString = "\"" + s + "\"";
53+
return this.objectMapper.readValue(uuidString, UUID.class);
54+
}
55+
56+
private UUID pathParamAsUuid(Context ctx) throws JsonProcessingException {
57+
return parseUuid(ctx.pathParam("id"));
58+
}
59+
60+
// POST /update
61+
public void update(Context ctx) {
62+
if (this.messagingService == null) {
63+
ctx.status(501).result("messaging service not available");
64+
return;
65+
}
66+
67+
this.messagingService.pushUpdate();
68+
ctx.status(202).result("ok");
69+
}
70+
71+
// POST /update/{id}
72+
public void updateUser(Context ctx) throws JsonProcessingException {
73+
if (this.messagingService == null) {
74+
ctx.status(501).result("messaging service not available");
75+
return;
76+
}
77+
78+
UUID uniqueId = pathParamAsUuid(ctx);
79+
80+
User u = this.userManager.getUser(uniqueId);
81+
CompletableFuture<User> userFuture = u != null
82+
? CompletableFuture.completedFuture(u)
83+
: this.userManager.loadUser(uniqueId);
84+
85+
ctx.future(userFuture.thenAccept(user -> {
86+
if (user != null) {
87+
this.messagingService.pushUserUpdate(user);
88+
}
89+
}), result -> ctx.status(202).result("ok"));
90+
}
91+
92+
// POST /custom
93+
public void custom(Context ctx) {
94+
if (this.messagingService == null) {
95+
ctx.status(501).result("messaging service not available");
96+
return;
97+
}
98+
99+
CustomMessageReq body = ctx.bodyAsClass(CustomMessageReq.class);
100+
this.messagingService.sendCustomMessage(body.channelId(), body.payload());
101+
ctx.status(202).result("ok");
102+
}
103+
104+
record CustomMessageReq(
105+
@JsonProperty(required = true) String channelId,
106+
@JsonProperty(required = true) String payload
107+
) { }
108+
109+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public interface PermissionHolderController {
6565
// GET /<type>/{id}/meta
6666
void metaGet(Context ctx) throws Exception;
6767

68-
// GET /<type>/{id}/permissionCheck
68+
// GET /<type>/{id}/permission-check
6969
void permissionCheck(Context ctx) throws Exception;
7070

71-
// POST /<type>/{id}/permissionCheck
71+
// POST /<type>/{id}/permission-check
7272
void permissionCheckCustom(Context ctx) throws Exception;
7373

7474
// POST /<type>/{id}/promote

src/main/java/me/lucko/luckperms/extension/rest/util/CustomObjectMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import me.lucko.luckperms.extension.rest.bind.QueryOptionsDeserializer;
4545
import me.lucko.luckperms.extension.rest.bind.TrackSerializer;
4646
import me.lucko.luckperms.extension.rest.bind.UserSerializer;
47+
import me.lucko.luckperms.extension.rest.bind.event.CustomMessageReceiveEventSerializer;
4748
import me.lucko.luckperms.extension.rest.bind.event.LogBroadcastEventSerializer;
4849
import me.lucko.luckperms.extension.rest.bind.event.PostNetworkSyncEventSerializer;
4950
import me.lucko.luckperms.extension.rest.bind.event.PostSyncEventSerializer;
@@ -53,6 +54,7 @@
5354
import net.luckperms.api.cacheddata.CachedMetaData;
5455
import net.luckperms.api.context.ContextSet;
5556
import net.luckperms.api.event.log.LogBroadcastEvent;
57+
import net.luckperms.api.event.messaging.CustomMessageReceiveEvent;
5658
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
5759
import net.luckperms.api.event.sync.PostSyncEvent;
5860
import net.luckperms.api.event.sync.PreNetworkSyncEvent;
@@ -92,6 +94,7 @@ public CustomObjectMapper() {
9294
module.addSerializer(Track.class, new TrackSerializer());
9395
module.addSerializer(User.class, new UserSerializer());
9496

97+
module.addSerializer(CustomMessageReceiveEvent.class, new CustomMessageReceiveEventSerializer());
9598
module.addSerializer(LogBroadcastEvent.class, new LogBroadcastEventSerializer());
9699
module.addSerializer(PostNetworkSyncEvent.class, new PostNetworkSyncEventSerializer());
97100
module.addSerializer(PostSyncEvent.class, new PostSyncEventSerializer());

src/main/java/me/lucko/luckperms/extension/rest/util/StubMessagingService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public void pushUpdate() {
4545
public void pushUserUpdate(User user) {
4646

4747
}
48+
49+
@Override
50+
public void sendCustomMessage(String channelId, String payload) {
51+
52+
}
4853
}

0 commit comments

Comments
 (0)