Skip to content

Commit

Permalink
Add messaging service API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jun 16, 2024
1 parent 36b38af commit 81e20e9
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 8 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-20240218.224354-2'
compileOnly 'net.luckperms:api:5.5-20240307.193022-3'
implementation 'io.javalin:javalin:4.6.4'
implementation 'io.javalin:javalin-openapi:4.6.4'
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/me/lucko/luckperms/extension/rest/RestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import me.lucko.luckperms.extension.rest.controller.ActionController;
import me.lucko.luckperms.extension.rest.controller.EventController;
import me.lucko.luckperms.extension.rest.controller.GroupController;
import me.lucko.luckperms.extension.rest.controller.MessagingController;
import me.lucko.luckperms.extension.rest.controller.PermissionHolderController;
import me.lucko.luckperms.extension.rest.controller.TrackController;
import me.lucko.luckperms.extension.rest.controller.UserController;
Expand Down Expand Up @@ -136,6 +137,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
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());
MessagingController messagingController = new MessagingController(luckPerms.getMessagingService().orElse(null), luckPerms.getUserManager(), this.objectMapper);
EventController eventController = new EventController(luckPerms.getEventBus());

app.routes(() -> {
Expand All @@ -146,6 +148,7 @@ private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
path("group", () -> setupControllerRoutes(groupController));
path("track", () -> setupControllerRoutes(trackController));
path("action", () -> setupControllerRoutes(actionController));
path("messaging", () -> setupControllerRoutes(messagingController));
path("event", () -> setupControllerRoutes(eventController));
});

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

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

path("permissionCheck", () -> {
path("permission-check", () -> {
get(controller::permissionCheck);
post(controller::permissionCheckCustom);
});
path("permissioncheck", () -> {
get(controller::permissionCheck);
post(controller::permissionCheckCustom);
});
Expand All @@ -198,12 +205,21 @@ private void setupControllerRoutes(ActionController controller) {
post(controller::submit);
}

private void setupControllerRoutes(MessagingController controller) {
path("update", () -> {
post(controller::update);
post("{id}", controller::updateUser);
});
post("custom", controller::custom);
}

private void setupControllerRoutes(EventController controller) {
sse("log-broadcast", controller::logBroadcast);
sse("post-network-sync", controller::postNetworkSync);
sse("post-sync", controller::postSync);
sse("pre-network-sync", controller::preNetworkSync);
sse("pre-sync", controller::preSync);
sse("custom-message-receive", controller::customMessageReceive);
}

private void setupAuth(JavalinConfig config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.bind.event;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.event.messaging.CustomMessageReceiveEvent;

import java.io.IOException;

public class CustomMessageReceiveEventSerializer extends JsonSerializer<CustomMessageReceiveEvent> {

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

record Model(String channelId, String payload) {
static Model from(CustomMessageReceiveEvent event) {
return new Model(
event.getChannelId(),
event.getPayload()
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.luckperms.api.event.EventSubscription;
import net.luckperms.api.event.LuckPermsEvent;
import net.luckperms.api.event.log.LogBroadcastEvent;
import net.luckperms.api.event.messaging.CustomMessageReceiveEvent;
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
import net.luckperms.api.event.sync.PostSyncEvent;
import net.luckperms.api.event.sync.PreNetworkSyncEvent;
Expand Down Expand Up @@ -116,5 +117,9 @@ public void preSync(SseClient client) {
handle(client, PreSyncEvent.class);
}

// GET /custom-message-receive
public void customMessageReceive(SseClient client) {
handle(client, CustomMessageReceiveEvent.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import net.luckperms.api.messaging.MessagingService;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class MessagingController {

private final MessagingService messagingService;
private final UserManager userManager;
private final ObjectMapper objectMapper;

public MessagingController(MessagingService messagingService, UserManager userManager, ObjectMapper objectMapper) {
this.messagingService = messagingService;
this.userManager = userManager;
this.objectMapper = objectMapper;
}

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

private UUID pathParamAsUuid(Context ctx) throws JsonProcessingException {
return parseUuid(ctx.pathParam("id"));
}

// POST /update
public void update(Context ctx) {
if (this.messagingService == null) {
ctx.status(501).result("messaging service not available");
return;
}

this.messagingService.pushUpdate();
ctx.status(202).result("ok");
}

// POST /update/{id}
public void updateUser(Context ctx) throws JsonProcessingException {
if (this.messagingService == null) {
ctx.status(501).result("messaging service not available");
return;
}

UUID uniqueId = pathParamAsUuid(ctx);

User u = this.userManager.getUser(uniqueId);
CompletableFuture<User> userFuture = u != null
? CompletableFuture.completedFuture(u)
: this.userManager.loadUser(uniqueId);

ctx.future(userFuture.thenAccept(user -> {
if (user != null) {
this.messagingService.pushUserUpdate(user);
}
}), result -> ctx.status(202).result("ok"));
}

// POST /custom
public void custom(Context ctx) {
if (this.messagingService == null) {
ctx.status(501).result("messaging service not available");
return;
}

CustomMessageReq body = ctx.bodyAsClass(CustomMessageReq.class);
this.messagingService.sendCustomMessage(body.channelId(), body.payload());
ctx.status(202).result("ok");
}

record CustomMessageReq(
@JsonProperty(required = true) String channelId,
@JsonProperty(required = true) String payload
) { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public interface PermissionHolderController {
// GET /<type>/{id}/meta
void metaGet(Context ctx) throws Exception;

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

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

// POST /<type>/{id}/promote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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 me.lucko.luckperms.extension.rest.bind.event.CustomMessageReceiveEventSerializer;
import me.lucko.luckperms.extension.rest.bind.event.LogBroadcastEventSerializer;
import me.lucko.luckperms.extension.rest.bind.event.PostNetworkSyncEventSerializer;
import me.lucko.luckperms.extension.rest.bind.event.PostSyncEventSerializer;
Expand All @@ -53,6 +54,7 @@
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.event.log.LogBroadcastEvent;
import net.luckperms.api.event.messaging.CustomMessageReceiveEvent;
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
import net.luckperms.api.event.sync.PostSyncEvent;
import net.luckperms.api.event.sync.PreNetworkSyncEvent;
Expand Down Expand Up @@ -92,6 +94,7 @@ public CustomObjectMapper() {
module.addSerializer(Track.class, new TrackSerializer());
module.addSerializer(User.class, new UserSerializer());

module.addSerializer(CustomMessageReceiveEvent.class, new CustomMessageReceiveEventSerializer());
module.addSerializer(LogBroadcastEvent.class, new LogBroadcastEventSerializer());
module.addSerializer(PostNetworkSyncEvent.class, new PostNetworkSyncEventSerializer());
module.addSerializer(PostSyncEvent.class, new PostSyncEventSerializer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public void pushUpdate() {
public void pushUserUpdate(User user) {

}

@Override
public void sendCustomMessage(String channelId, String payload) {

}
}
Loading

0 comments on commit 81e20e9

Please sign in to comment.