From 81a7806629f0f72608de236e2909d3e4f233d570 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sun, 17 Jul 2016 16:20:42 -0400 Subject: [PATCH] Modify DELETE /users/:id/activePunishment to remove all active punishments (instead of just the first one found) - this removes any possible confusion due to having to unban someone multiple times (due to the data importer creating duplicate punishments) --- .../DELETEUsersIdActivePunishment.java | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java index 53a642e..274d461 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java @@ -8,11 +8,13 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; +import net.frozenorb.apiv3.model.AuditLogEntry; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.SyncUtils; +import java.util.LinkedList; import java.util.List; public final class DELETEUsersIdActivePunishment implements Handler { @@ -37,33 +39,20 @@ public final class DELETEUsersIdActivePunishment implements Handler punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v)); - Punishment activePunishment = null; + List removedPunishments = new LinkedList<>(); for (Punishment punishment : punishments) { - if (punishment.isActive()) { - activePunishment = punishment; - break; - } + if (!punishment.isActive()) continue; + + SyncUtils.runBlocking(v -> punishment.delete(removedBy, reason, v)); + SyncUtils.runBlocking(v -> AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v)); + removedPunishments.add(punishment); } - if (activePunishment == null) { - ErrorUtils.respondOther(ctx, 409, "User provided has no active punishments.", "noActivePunishments", ImmutableMap.of()); - return; - } - - Punishment finalActivePunishment = activePunishment; - SyncUtils.runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v)); - - if (removedBy != null) { - AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), (ignored, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - } else { - APIv3.respondJson(ctx, 200, finalActivePunishment); - } - }); + if (!removedPunishments.isEmpty()) { + APIv3.respondJson(ctx, 200, removedPunishments); } else { - APIv3.respondJson(ctx, 200, finalActivePunishment); + ErrorUtils.respondOther(ctx, 409, "User provided has no active punishments.", "noActivePunishments", ImmutableMap.of()); } }