From 34415929c4b27b0c4d6ce7c535595d256ac0a683 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 6 May 2016 21:57:32 -0400 Subject: [PATCH] More stuff --- src/main/java/net/frozenorb/apiv3/APIv3.java | 3 +++ .../frozenorb/apiv3/models/Punishment.java | 20 ++++++++++++++++ .../java/net/frozenorb/apiv3/models/User.java | 13 +---------- .../routes/punishments/POSTUserPunish.java | 7 +++++- .../apiv3/serialization/DateTypeAdapter.java | 23 +++++++++++++++++++ 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 src/main/java/net/frozenorb/apiv3/serialization/DateTypeAdapter.java diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 1e1fddb..7855e30 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -34,6 +34,7 @@ import net.frozenorb.apiv3.routes.serverGroups.GETServerGroups; import net.frozenorb.apiv3.routes.serverGroups.POSTServerGroup; import net.frozenorb.apiv3.routes.servers.*; import net.frozenorb.apiv3.routes.users.*; +import net.frozenorb.apiv3.serialization.DateTypeAdapter; import net.frozenorb.apiv3.serialization.FollowAnnotationExclusionStrategy; import net.frozenorb.apiv3.serialization.ObjectIdTypeAdapter; import net.frozenorb.apiv3.unsorted.LoggingExceptionHandler; @@ -46,6 +47,7 @@ import redis.clients.jedis.JedisPool; import java.io.FileInputStream; import java.io.InputStream; +import java.util.Date; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; @@ -62,6 +64,7 @@ public final class APIv3 { @Getter private static StatsDClient statsD; @Getter private static final Gson gson = new GsonBuilder() .registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()) + .registerTypeAdapter(Date.class, new DateTypeAdapter()) .setExclusionStrategies(new FollowAnnotationExclusionStrategy()) .create(); diff --git a/src/main/java/net/frozenorb/apiv3/models/Punishment.java b/src/main/java/net/frozenorb/apiv3/models/Punishment.java index 9627ce1..8c4b778 100644 --- a/src/main/java/net/frozenorb/apiv3/models/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/models/Punishment.java @@ -4,6 +4,7 @@ import lombok.Getter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.ActorType; +import net.frozenorb.apiv3.utils.TimeUtils; import org.bson.types.ObjectId; import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Id; @@ -72,6 +73,25 @@ public final class Punishment { return removedBy != null; } + public String getAccessDenialReason() { + switch (type) { + case BLACKLIST: + return "Your account has been blacklisted from the MineHQ Network. \n\nThis type of punishment cannot be appealed."; + case BAN: + String accessDenialReason = "Your account has been suspended from the MineHQ Network. \n\n"; + + if (getExpiresAt() != null) { + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), new Date())); + } else { + accessDenialReason += "Appeal at MineHQ.com/appeal"; + } + + return accessDenialReason; + default: + return null; + } + } + public enum PunishmentType { BLACKLIST, BAN, MUTE, WARN diff --git a/src/main/java/net/frozenorb/apiv3/models/User.java b/src/main/java/net/frozenorb/apiv3/models/User.java index 561a0bb..62374e5 100644 --- a/src/main/java/net/frozenorb/apiv3/models/User.java +++ b/src/main/java/net/frozenorb/apiv3/models/User.java @@ -7,7 +7,6 @@ import lombok.Setter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.serialization.ExcludeFromReplies; import net.frozenorb.apiv3.utils.PermissionUtils; -import net.frozenorb.apiv3.utils.TimeUtils; import org.bson.Document; import org.mindrot.jbcrypt.BCrypt; import org.mongodb.morphia.annotations.Entity; @@ -215,17 +214,7 @@ public final class User { continue; } - if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) { - accessDenialReason = "Your account has been blacklisted from the MineHQ Network. \n\nThis type of punishment cannot be appealed."; - } else { - accessDenialReason = "Your account has been suspended from the MineHQ Network. \n\n"; - - if (punishment.getExpiresAt() != null) { - accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(punishment.getExpiresAt(), new Date())); - } else { - accessDenialReason += "Appeal at MineHQ.com/appeal"; - } - } + accessDenialReason = punishment.getAccessDenialReason(); } diff --git a/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java b/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java index d732d93..de3b53e 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java +++ b/src/main/java/net/frozenorb/apiv3/routes/punishments/POSTUserPunish.java @@ -1,5 +1,6 @@ package net.frozenorb.apiv3.routes.punishments; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Punishment; @@ -58,7 +59,11 @@ public final class POSTUserPunish implements Route { Punishment punishment = new Punishment(target, reason, type, expiresAt, addedBy, req.attribute("actor")); APIv3.getDatastore().save(punishment); - return punishment; + + return ImmutableMap.of( + "punishment", punishment, + "accessDenialReason", punishment.getAccessDenialReason() + ); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/serialization/DateTypeAdapter.java b/src/main/java/net/frozenorb/apiv3/serialization/DateTypeAdapter.java new file mode 100644 index 0000000..a1f3ba7 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/serialization/DateTypeAdapter.java @@ -0,0 +1,23 @@ +package net.frozenorb.apiv3.serialization; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.util.Date; + +public final class DateTypeAdapter extends TypeAdapter { + + public void write(JsonWriter writer, Date write) throws IOException { + writer.value(write.getTime()); + } + + // This is used with Gson, which is only used + // to serialize outgoing responses, thus we + // don't need to have a read method. + public Date read(JsonReader reader) { + throw new IllegalArgumentException(); + } + +} \ No newline at end of file