From c7a84e6e63a782c0ac3da86bb7d6d91c2b6e832a Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Thu, 14 Jul 2016 17:54:10 -0400 Subject: [PATCH] Move POST /users/:userId/leave to an event in the server heartbeat --- src/main/java/net/frozenorb/apiv3/APIv3.java | 1 - .../route/servers/POSTServersHeartbeat.java | 50 ++++++++++++++---- .../apiv3/route/users/POSTUsersIdLeave.java | 51 ------------------- 3 files changed, 41 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLeave.java diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 2d0e5e5..8a6adfb 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -372,7 +372,6 @@ public final class APIv3 extends AbstractVerticle { http.get("/users/:userId/verifyPassword").blockingHandler(new GETUsersIdVerifyPassword(), false); http.post("/users/:userId/changePassword").blockingHandler(new POSTUsersIdChangePassword(), false); http.post("/users/:userId/confirmPhone").blockingHandler(new POSTUsersIdConfirmPhone(), false); - http.post("/users/:userId/leave").handler(new POSTUsersIdLeave()); http.post("/users/:userId/login").blockingHandler(new POSTUsersIdLogin()); http.post("/users/:userId/notify").blockingHandler(new POSTUsersIdNotify(), false); http.post("/users/:userId/passwordReset").blockingHandler(new POSTUsersIdPasswordReset(), false); diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java index 7f7b8dd..0954499 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServersHeartbeat.java @@ -18,10 +18,7 @@ import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.PermissionUtils; import net.frozenorb.apiv3.util.UuidUtils; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; @Slf4j public final class POSTServersHeartbeat implements Handler { @@ -35,7 +32,6 @@ public final class POSTServersHeartbeat implements Handler { } Server actorServer = Server.findById(actor.getName()); - ServerGroup actorServerGroup = ServerGroup.findById(actorServer.getServerGroup()); JsonObject requestBody = ctx.getBodyAsJson(); JsonObject players = requestBody.getJsonObject("players"); Map playerNames = extractPlayerNames(players); @@ -44,8 +40,8 @@ public final class POSTServersHeartbeat implements Handler { CompositeFuture.all( createInfoResponse(actorServer, requestBody.getDouble("lastTps"), playerNames), createPlayerResponse(actorServer, playerNames, playerIps), - createPermissionsResponse(actorServerGroup), - createEventsResponse(requestBody.getJsonArray("events")) + createPermissionsResponse(ServerGroup.findById(actorServer.getServerGroup())), + createEventsResponse(actorServer, requestBody.getJsonArray("events")) ).setHandler((result) -> { if (result.succeeded()) { // We don't do anything with the info callback, as @@ -144,20 +140,56 @@ public final class POSTServersHeartbeat implements Handler { return callback; } - private Future> createEventsResponse(JsonArray events) { + private Future> createEventsResponse(Server server, JsonArray events) { Future> callback = Future.future(); + List eventFutures = new ArrayList<>(); for (Object event : events) { JsonObject eventJson = (JsonObject) event; String type = eventJson.getString("type"); switch (type) { + case "leave": + Future eventFuture = Future.future(); + eventFutures.add(eventFuture); + + User.findById(eventJson.getString("user"), ((user, error) -> { + if (error != null) { + eventFuture.fail(error); + return; + } + + if (user == null) { + eventFuture.complete(); + return; + } + + if (!user.leftServer(server)) { + eventFuture.complete(); + return; + } + + user.save((ignored, saveError) -> { + if (saveError != null) { + eventFuture.fail(saveError); + } else { + eventFuture.complete(); + } + }); + })); default: log.warn("Recieved event with unknown type " + type + "."); } } - callback.complete(ImmutableMap.of()); + CompositeFuture.all(eventFutures).setHandler((allEvents) -> { + if (allEvents.failed()) { + callback.fail(allEvents.cause()); + } else { + callback.complete(ImmutableMap.of()); + } + }); + return callback; } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLeave.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLeave.java deleted file mode 100644 index bdb7e3f..0000000 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLeave.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.frozenorb.apiv3.route.users; - -import com.google.common.collect.ImmutableMap; -import io.vertx.core.Handler; -import io.vertx.ext.web.RoutingContext; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.actor.Actor; -import net.frozenorb.apiv3.actor.ActorType; -import net.frozenorb.apiv3.model.Server; -import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.util.ErrorUtils; - -public class POSTUsersIdLeave implements Handler { - - @Override - public void handle(RoutingContext ctx) { - Actor actor = ctx.get("actor"); - - if (actor.getType() != ActorType.SERVER) { - ErrorUtils.respondOther(ctx, 403, "This action can only be performed when requested by a server.", "serverOnly", ImmutableMap.of()); - return; - } - - Server actorServer = Server.findById(actor.getName()); - - User.findById(ctx.request().getParam("userId"), ((user, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - return; - } - - if (user == null) { - ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); - return; - } - - if (user.leftServer(actorServer)) { - user.save((ignored, error2) -> { - if (error2 != null) { - ErrorUtils.respondInternalError(ctx, error2); - } else { - APIv3.respondJson(ctx, 200, user); - } - }); - } else { - APIv3.respondJson(ctx, 200, user); - } - })); - } - -} \ No newline at end of file