From f8f23e7de65f71c9b536992b68dbc16c0d60f63c Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 12 Feb 2016 15:12:05 -0500 Subject: [PATCH] More models! --- .../frozenorb/apiv3/model/EmailTemplate.java | 12 +-- .../java/net/frozenorb/apiv3/model/Grant.java | 24 +++-- .../net/frozenorb/apiv3/model/IPLogEntry.java | 41 +++++++-- .../apiv3/model/NotificationLogEntry.java | 26 ++++-- .../net/frozenorb/apiv3/model/Punishment.java | 88 +++++++++++++++++-- .../net/frozenorb/apiv3/model/Server.java | 4 - 6 files changed, 158 insertions(+), 37 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java index 15118b5..7fb091d 100644 --- a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java @@ -48,14 +48,14 @@ public final class EmailTemplate extends BaseModel { this.lastUpdatedAt = Instant.now(); this.lastUpdatedBy = updatedBy; - Document update = new Document(); + Document set = new Document(); - update.put("title", title); - update.put("body", body); - update.put("lastUpdatedAt", lastUpdatedAt.toString()); - update.put("lastUpdatedBy", lastUpdatedBy.toString()); + set.put("title", title); + set.put("body", body); + set.put("lastUpdatedAt", lastUpdatedAt.toString()); + set.put("lastUpdatedBy", lastUpdatedBy.toString()); - super.update(new Document("$set", update), (result, error) -> {}); + super.update(new Document("$set", set), (result, error) -> {}); } public void delete() { diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index 7e695be..a060d82 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -20,6 +20,7 @@ public final class Grant extends BaseModel { @Getter private Set scopes; @Getter private String rank; @Getter private Instant expiresAt; + @Getter private UUID addedBy; @Getter private Instant addedAt; @@ -36,6 +37,7 @@ public final class Grant extends BaseModel { this.scopes = ImmutableSet.copyOf((Collection) json.get("scopes")); // This is a safe cast, the collection's type is always String this.rank = json.getString("rank"); this.expiresAt = (Instant) json.get("expiresAt"); + this.addedBy = UUID.fromString(json.getString("addedBy")); this.addedAt = (Instant) json.get("addedAt"); @@ -53,13 +55,13 @@ public final class Grant extends BaseModel { this.removedAt = Instant.now(); this.removalReason = reason; - Document update = new Document(); + Document set = new Document(); - update.put("removedBy", removedBy.toString()); - update.put("removedAt", removedAt); - update.put("removalReason", removalReason); + set.put("removedBy", removedBy.toString()); + set.put("removedAt", removedAt); + set.put("removalReason", removalReason); - super.update(new Document("$set", update), (result, error) -> {}); + super.update(new Document("$set", set), (result, error) -> {}); } public boolean isActive() { @@ -86,12 +88,18 @@ public final class Grant extends BaseModel { json.put("reason", reason); json.put("scopes", scopes); json.put("rank", rank); + json.put("expiresAt", expiresAt == null ? null : expiresAt.toString()); + json.put("addedBy", addedBy.toString()); json.put("addedAt", addedAt.toString()); - json.put("active", isActive()); - json.put("expired", isExpired()); - json.put("removed", isRemoved()); + Document statusJson = new Document(); + + statusJson.put("active", isActive()); + statusJson.put("expired", isExpired()); + statusJson.put("removed", isRemoved()); + + json.put("status", statusJson); if (removedBy != null) { json.put("removedBy", removedBy.toString()); diff --git a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java index a9aa284..834caee 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java @@ -3,33 +3,56 @@ package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.accessor.IPLog; +import net.frozenorb.apiv3.util.IPUtils; import org.bson.Document; +import java.time.Instant; +import java.util.UUID; + @ToString public final class IPLogEntry extends BaseModel { @Getter private String id; + @Getter private UUID user; + @Getter private long ip; + @Getter private Instant firstSeen; + @Getter private Instant lastSeen; + @Getter private int uses; public IPLogEntry(Document json) { super(IPLog.COLLECTION_NAME); this.id = json.getString("_id"); + this.user = UUID.fromString(json.getString("user")); + this.ip = json.getLong("ip"); + this.lastSeen = (Instant) json.get("lastSeen"); + this.firstSeen = (Instant) json.get("firstSeen"); + this.uses = json.getInteger("uses"); setId(id); } + public void used() { + this.lastSeen = Instant.now(); + this.uses++; + + Document set = new Document(); + + set.put("lastSeen", lastSeen); + set.put("uses", uses); + + update(new Document("$set", set), (result, error) -> {}); + } + public Document toLiteJson() { Document json = new Document(); - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - + json.put("id", id); + json.put("user", user.toString()); + json.put("ip", IPUtils.machineToHuman(ip)); + json.put("firstSeen", firstSeen.toString()); + json.put("lastSeen", lastSeen.toString()); + json.put("uses", uses); return json; } diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java index e9eba3c..13d696d 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java @@ -5,15 +5,28 @@ import lombok.ToString; import net.frozenorb.apiv3.accessor.NotificationLog; import org.bson.Document; +import java.time.Instant; +import java.util.UUID; + @ToString public final class NotificationLogEntry extends BaseModel { @Getter private String id; + @Getter private UUID target; + @Getter private Instant sentAt; + @Getter private NotificationType type; + @Getter private String title; + @Getter private String body; public NotificationLogEntry(Document json) { super(NotificationLog.COLLECTION_NAME); this.id = json.getString("_id"); + this.target = UUID.fromString(json.getString("target")); + this.sentAt = (Instant) json.get("sentAt"); + this.type = NotificationType.valueOf(json.getString("type")); + this.title = json.getString("title"); + this.body = json.getString("body"); setId(id); } @@ -21,17 +34,20 @@ public final class NotificationLogEntry extends BaseModel { public Document toLiteJson() { Document json = new Document(); - json.put("_id", id); + json.put("id", id); + json.put("target", target.toString()); + json.put("sentAt", sentAt.toString()); + json.put("type", type.name()); + json.put("title", title); + json.put("body", body); return json; } - public Document toFullJson() { - Document json = toLiteJson(); + public enum NotificationType { + EMAIL, TEXT - - return json; } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index eb84e96..cf589b9 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -5,33 +5,111 @@ import lombok.ToString; import net.frozenorb.apiv3.accessor.Punishments; import org.bson.Document; +import java.time.Instant; +import java.util.UUID; + @ToString public final class Punishment extends BaseModel { @Getter private String id; + @Getter private UUID target; + @Getter private String reason; + @Getter private PunishmentType type; + @Getter private Instant expiresAt; + + @Getter private UUID addedBy; + @Getter private Instant addedAt; + @Getter private String addedOn; + + @Getter private UUID removedBy; + @Getter private Instant removedAt; + @Getter private String removalReason; public Punishment(Document json) { super(Punishments.COLLECTION_NAME); this.id = json.getString("_id"); + this.target = UUID.fromString(json.getString("target")); + this.reason = json.getString("reason"); + this.type = PunishmentType.valueOf(json.getString("type")); + this.expiresAt = (Instant) json.get("expiresAt"); + + this.addedBy = UUID.fromString(json.getString("addedBy")); + this.addedAt = (Instant) json.get("addedAt"); + this.addedOn = json.getString("addedOn"); + + if (json.containsKey("removedBy")) { + this.removedBy = UUID.fromString(json.getString("removedBy")); + this.removedAt = (Instant) json.get("removedAt"); + this.removalReason = json.getString("removalReason"); + } setId(id); } + public void delete(UUID removedBy, String reason) { + this.removedBy = removedBy; + this.removedAt = Instant.now(); + this.removalReason = reason; + + Document set = new Document(); + + set.put("removedBy", removedBy.toString()); + set.put("removedAt", removedAt); + set.put("removalReason", removalReason); + + super.update(new Document("$set", set), (result, error) -> {}); + } + + public boolean isActive() { + return !(isExpired() || isRemoved()); + } + + public boolean isExpired() { + if (expiresAt == null) { + return false; // Never expires + } else { + return expiresAt.isAfter(Instant.now()); + } + } + + public boolean isRemoved() { + return removedBy != null; + } + public Document toLiteJson() { Document json = new Document(); - json.put("_id", id); + json.put("id", id); + json.put("target", target.toString()); + json.put("reason", reason); + json.put("type", type.name()); + json.put("expiresAt", expiresAt == null ? null : expiresAt.toString()); + + json.put("addedBy", addedBy.toString()); + json.put("addedAt", addedAt.toString()); + + Document statusJson = new Document(); + + statusJson.put("active", isActive()); + statusJson.put("expired", isExpired()); + statusJson.put("removed", isRemoved()); + + json.put("status", statusJson); + + if (removedBy != null) { + json.put("removedBy", removedBy.toString()); + json.put("removedAt", removedAt.toString()); + json.put("removalReason", removalReason); + } return json; } - public Document toFullJson() { - Document json = toLiteJson(); + public enum PunishmentType { + BAN, MUTE, WARN - - return json; } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 0e1d942..4857a33 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -45,10 +45,6 @@ public final class Server extends BaseModel { setId(id); } - public void update() { - super.update(new Document("$set", new Document("lastUpdate", Instant.now())), (result, error) -> {}); - } - public Document toLiteJson() { Document json = new Document();