From 26e84251b620b7b01632e8eca28d228862e70aed Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Fri, 17 Jun 2016 01:12:32 -0400 Subject: [PATCH] Add some more async functionality --- .../java/net/frozenorb/apiv3/model/IpBan.java | 49 ++++++++++--- .../java/net/frozenorb/apiv3/model/User.java | 70 +++++++++---------- 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/model/IpBan.java b/src/main/java/net/frozenorb/apiv3/model/IpBan.java index dbff14a..cb23fea 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpBan.java @@ -131,16 +131,49 @@ public final class IpBan { return removedBy != null; } - public String getAccessDenialReason() { - String accessDenialReason = "Your ip address has been suspended from the MineHQ Network. \n\n"; + // TODO: CLEANUP + public void getAccessDenialReason(SingleResultCallback callback) { + Punishment.findByLinkedIpBanId(id, (punishment, error) -> { + if (error != null) { + callback.onResult(null, error); + return; + }; - if (getExpiresAt() != null) { - accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); - } else { - accessDenialReason += "Appeal at MineHQ.com/appeal"; - } + if (punishment != null) { + User.findById(punishment.getUser(), (user, error2) -> { + if (error2 != null) { + callback.onResult(null, error2); + return; + } - return accessDenialReason; + String accessDenialReason; + + if (user != null) { + accessDenialReason = "Your IP address has been suspended from the MineHQ Network for a punishment related to " + user.getLastUsername() + ". \n\n"; + } else { + accessDenialReason = "Your IP address has been suspended from the MineHQ Network. \n\n"; + } + + if (getExpiresAt() != null) { + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); + } else { + accessDenialReason += "Appeal at MineHQ.com/appeal"; + } + + callback.onResult(accessDenialReason, null); + }); + } else { + String accessDenialReason = "Your IP address has been suspended from the MineHQ Network. \n\n"; + + if (getExpiresAt() != null) { + accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), Instant.now())); + } else { + accessDenialReason += "Appeal at MineHQ.com/appeal"; + } + + callback.onResult(accessDenialReason, null); + } + }); } public void insert() { diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 7a47dc1..22e878d 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -13,18 +13,19 @@ import fr.javatic.mongo.jacksonCodec.Entity; import fr.javatic.mongo.jacksonCodec.objectId.Id; import io.vertx.core.CompositeFuture; import io.vertx.core.Future; +import io.vertx.core.cli.converters.BooleanConverter; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonDeserializer; -import net.frozenorb.apiv3.serialization.jackson.UUIDJsonSerializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer; +import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer; import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.MojangUtils; import net.frozenorb.apiv3.util.PermissionUtils; import net.frozenorb.apiv3.util.SyncUtils; -import net.frozenorb.apiv3.util.UUIDUtils; +import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; import java.time.Instant; @@ -36,7 +37,7 @@ public final class User { private static final MongoCollection usersCollection = APIv3.getDatabase().getCollection("users", User.class); - @Getter @Id @JsonSerialize(using=UUIDJsonSerializer.class) @JsonDeserialize(using=UUIDJsonDeserializer.class) private UUID id; + @Getter @Id @JsonSerialize(using=UuidJsonSerializer.class) @JsonDeserialize(using=UuidJsonDeserializer.class) private UUID id; @Getter private String lastUsername; @Getter @ExcludeFromReplies private Map aliases = new HashMap<>(); @Getter @ExcludeFromReplies @Setter private String totpSecret; @@ -59,7 +60,7 @@ public final class User { try { uuid = UUID.fromString(id); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException | IllegalArgumentException ex) { return null; } @@ -67,7 +68,7 @@ public final class User { } public static User findByIdSync(UUID id) { - if (UUIDUtils.isAcceptableUUID(id)) { + if (UuidUtils.isAcceptableUuid(id)) { return SyncUtils.blockOne(usersCollection.find(new Document("_id", id))); } else { return null; @@ -96,7 +97,7 @@ public final class User { } public static void findById(UUID id, SingleResultCallback callback) { - if (UUIDUtils.isAcceptableUUID(id)) { + if (UuidUtils.isAcceptableUuid(id)) { usersCollection.find(new Document("_id", id)).first(callback); } else { callback.onResult(null, null); @@ -133,6 +134,8 @@ public final class User { public User() {} // For Morphia + // TODO: THIS IS CURRENTLY BLOCKING. MAYBE FOR THE HEARTBEAT WE CAN DO SOMETHING + // TO MAKE IT NOT SO BLOCKING public User(UUID id, String lastUsername) { this.id = id; this.lastUsername = ""; // Intentional, so updateUsername actually does something. @@ -145,13 +148,16 @@ public final class User { this.lastSeenAt = Instant.now(); this.firstSeenAt = Instant.now(); - // TODO: MAKE THIS ASYNC? SOMEHOW? - BlockingCallback blockingCallback = new BlockingCallback<>(); - updateUsername(lastUsername, blockingCallback); - blockingCallback.get(); + updateUsername(lastUsername); } public boolean hasPermissionAnywhere(String permission) { + Map globalPermissions = getGlobalPermissions(); + return globalPermissions.containsKey(permission) && globalPermissions.get(permission); + } + + // TODO: ASYNC + public Map getGlobalPermissions() { Map globalPermissions = PermissionUtils.getDefaultPermissions(getHighestRankAnywhere()); for (Map.Entry serverGroupEntry : getHighestRanks().entrySet()) { @@ -164,7 +170,7 @@ public final class User { ); } - return globalPermissions.containsKey(permission) && globalPermissions.get(permission); + return ImmutableMap.copyOf(globalPermissions); } // TODO: Clean @@ -188,31 +194,20 @@ public final class User { this.online = false; } - public void updateUsername(String newUsername, SingleResultCallback callback) { - this.aliases.put(newUsername, Instant.now()); + public void updateUsername(String newUsername) { + if (!newUsername.equals(lastUsername)) { + this.lastUsername = newUsername; - if (newUsername.equalsIgnoreCase(lastUsername)) { - callback.onResult(null, null); - return; + User withNewUsername; + + while ((withNewUsername = User.findByLastUsernameSync(newUsername)) != null) { + BlockingCallback callback = new BlockingCallback<>(); + MojangUtils.getName(withNewUsername.getId(), callback); + withNewUsername.updateUsername(callback.get()); + } } - this.lastUsername = newUsername; - - User.findByLastUsername(newUsername, (otherUser, error) -> { - if (error != null) { - callback.onResult(null, error); - } else if (otherUser != null) { - MojangUtils.getName(otherUser.getId(), (newName, error2) -> { - if (error2 != null) { - callback.onResult(null, error2); - } else { - otherUser.updateUsername(newName, callback); - } - }); - } else { - callback.onResult(null, null); - } - }); + this.aliases.put(newUsername, Instant.now()); } public void setPassword(String input) { @@ -378,9 +373,14 @@ public final class User { "activeBanId", activeBan.getId() ); } else if (activeIpBan != null) { + // TODO: ASYNC + BlockingCallback callback = new BlockingCallback<>(); + activeIpBan.getAccessDenialReason(callback); + String reason = callback.get(); + access = ImmutableMap.of( "allowed", false, - "message", activeIpBan.getAccessDenialReason(), + "message", reason, "activeIpBanId", activeIpBan.getId() ); }