From d3bdeb6d501f7a66974ad913845bcef646e6b153 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sun, 3 Jul 2016 16:31:43 -0400 Subject: [PATCH] Public/private reasons on punishments --- .../converters/PunishmentConverter.java | 1 + .../java/net/frozenorb/apiv3/model/IpBan.java | 2 +- .../net/frozenorb/apiv3/model/Punishment.java | 8 ++++--- .../route/punishments/POSTPunishments.java | 24 ++++++++++++------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java index aa41514..15acbbf 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java @@ -39,6 +39,7 @@ public final class PunishmentConverter implements Block { Punishment created = new Punishment( new ObjectId().toString(), target, + "Imported Ban; reason hidden", punishment.getString("reason").toString(), Punishment.PunishmentType.valueOf(punishment.getString("type").toUpperCase()), punishment.containsKey("expires") ? punishment.getDate("expires").toInstant() : null, diff --git a/src/main/java/net/frozenorb/apiv3/model/IpBan.java b/src/main/java/net/frozenorb/apiv3/model/IpBan.java index 6cda46d..50c7c73 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpBan.java @@ -74,7 +74,7 @@ public final class IpBan { public IpBan(String userIp, Punishment linked) { this.id = new ObjectId().toString(); this.userIp = userIp; - this.reason = linked.getReason(); + this.reason = linked.getPublicReason(); this.expiresAt = linked.getExpiresAt(); this.addedBy = linked.getAddedBy(); this.addedAt = Instant.now(); diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index 1f1f055..41d428c 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -26,7 +26,8 @@ public final class Punishment { @Getter @Id private String id; @Getter private UUID user; - @Getter private String reason; + @Getter private String publicReason; + @Getter private String privateReason; @Getter private PunishmentType type; @Getter private Instant expiresAt; @Getter private Map metadata; @@ -97,10 +98,11 @@ public final class Punishment { private Punishment() {} // For Jackson - public Punishment(User user, String reason, PunishmentType type, Instant expiresAt, User addedBy, Actor actor, Map metadata) { + public Punishment(User user, String publicReason, String privateReason, PunishmentType type, Instant expiresAt, User addedBy, Actor actor, Map metadata) { this.id = new ObjectId().toString(); this.user = user.getId(); - this.reason = reason; + this.publicReason = publicReason; + this.privateReason = privateReason; this.type = type; this.expiresAt = expiresAt; this.addedBy = addedBy == null ? null : addedBy.getId(); diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java index 6b6eb5e..8e91985 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java @@ -32,10 +32,16 @@ public final class POSTPunishments implements Handler { return; } - String reason = requestBody.getString("reason"); + String publicReason = requestBody.getString("publicReason"); + String privateReason = requestBody.getString("privateReason"); - if (reason == null || reason.trim().isEmpty()) { - ErrorUtils.respondRequiredInput(ctx, "reason"); + if (publicReason == null || publicReason.trim().isEmpty()) { + ErrorUtils.respondRequiredInput(ctx, "publicReason"); + return; + } + + if (privateReason == null || privateReason.trim().isEmpty()) { + ErrorUtils.respondRequiredInput(ctx, "privateReason"); return; } @@ -45,11 +51,11 @@ public final class POSTPunishments implements Handler { BlockingCallback> punishmentsCallback = new BlockingCallback<>(); Punishment.findByUserAndType(target, ImmutableSet.of(type), punishmentsCallback); - for (Punishment punishment : punishmentsCallback.get()) { - if (punishment.isActive()) { + for (Punishment alternatePunishment : punishmentsCallback.get()) { + if (alternatePunishment.isActive()) { BlockingCallback addedByCallback = new BlockingCallback<>(); - User.findById(punishment.getAddedBy(), addedByCallback); - ErrorUtils.respondGeneric(ctx, 409, "A punishment by " + addedByCallback.get().getLastUsername() + " already covers this user."); + User.findById(alternatePunishment.getAddedBy(), addedByCallback); + ErrorUtils.respondOther(ctx, 409, "User already covered by alternate punishment.", "alreadyCoveredByAlternatePunishment", ImmutableMap.of("alternatePunishmentBy", addedByCallback.get().getLastUsername())); return; } } @@ -82,11 +88,11 @@ public final class POSTPunishments implements Handler { target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT, permissionsCallback); if (permissionsCallback.get()) { - ErrorUtils.respondGeneric(ctx, 409, target.getLastSeenOn() + " is protected from punishments."); + ErrorUtils.respondOther(ctx, 409, "User is protected from punishments.", "protectedFromPunishments", ImmutableMap.of()); return; } - Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, ctx.get("actor"), meta); + Punishment punishment = new Punishment(target, publicReason, privateReason, type, expiresAt, addedBy, ctx.get("actor"), meta); String accessDenialReason = punishment.getAccessDenialReason(); String userIp = requestBody.getString("userIp");