Move POST /users/:userId/leave to an event in the server heartbeat

This commit is contained in:
Colin McDonald 2016-07-14 17:54:10 -04:00
parent 91432b3d71
commit c7a84e6e63
3 changed files with 41 additions and 61 deletions

View File

@ -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);

View File

@ -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<RoutingContext> {
@ -35,7 +32,6 @@ public final class POSTServersHeartbeat implements Handler<RoutingContext> {
}
Server actorServer = Server.findById(actor.getName());
ServerGroup actorServerGroup = ServerGroup.findById(actorServer.getServerGroup());
JsonObject requestBody = ctx.getBodyAsJson();
JsonObject players = requestBody.getJsonObject("players");
Map<UUID, String> playerNames = extractPlayerNames(players);
@ -44,8 +40,8 @@ public final class POSTServersHeartbeat implements Handler<RoutingContext> {
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<RoutingContext> {
return callback;
}
private Future<Map<String, Object>> createEventsResponse(JsonArray events) {
private Future<Map<String, Object>> createEventsResponse(Server server, JsonArray events) {
Future<Map<String, Object>> callback = Future.future();
List<Future> 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 + ".");
}
}
CompositeFuture.all(eventFutures).setHandler((allEvents) -> {
if (allEvents.failed()) {
callback.fail(allEvents.cause());
} else {
callback.complete(ImmutableMap.of());
}
});
return callback;
}

View File

@ -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<RoutingContext> {
@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);
}
}));
}
}