diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java index 8aeaa2e..65df424 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/GrantConverter.java @@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableSet; import com.mongodb.Block; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.model.Grant; -import net.frozenorb.apiv3.unsorted.BlockingCallback; +import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -59,9 +59,7 @@ public final class GrantConverter implements Block { -1 ); - BlockingCallback callback = new BlockingCallback<>(); - created.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> created.insert(v)); log.info("Created grant " + created.getId() + " (" + created.getRank() + ")"); } diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java index 9a07466..e2c97b3 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/IpLogConverter.java @@ -3,8 +3,8 @@ package net.frozenorb.apiv3.dataImport.converters; import com.mongodb.Block; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.model.IpLogEntry; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.IpUtils; +import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -51,9 +51,7 @@ public final class IpLogConverter implements Block { ((Number) ipLogEntry.get("uses")).intValue() ); - BlockingCallback callback = new BlockingCallback<>(); - created.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> created.insert(v)); log.info("Created ip log entry " + created.getId() + " (" + created.getUser() + " - " + created.getUserIp() + ")"); } 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 15acbbf..523a433 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/PunishmentConverter.java @@ -5,7 +5,7 @@ import com.mongodb.Block; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.model.Punishment; -import net.frozenorb.apiv3.unsorted.BlockingCallback; +import net.frozenorb.apiv3.util.SyncUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -54,9 +54,7 @@ public final class PunishmentConverter implements Block { punishment.containsKey("removedBy") ? punishment.getString("removalReason").toString() : null ); - BlockingCallback callback = new BlockingCallback<>(); - created.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> created.insert(v)); log.info("Created punishment " + created.getId() + " (" + created.getType() + ")"); } diff --git a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java index 49fe2b9..0e8bfc6 100644 --- a/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java +++ b/src/main/java/net/frozenorb/apiv3/dataImport/converters/UserConverter.java @@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableMap; import com.mongodb.Block; import lombok.extern.slf4j.Slf4j; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; import org.bson.Document; import org.bson.types.ObjectId; @@ -56,15 +56,15 @@ public final class UserConverter implements Block { null, null, null, + null, + null, "INVALID", user.getDate("joined").toInstant(), user.getDate("joined").toInstant(), false ); - BlockingCallback callback = new BlockingCallback<>(); - created.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> created.insert(v)); log.info("Created user " + created.getLastUsername() + " (" + created.getId() + ")"); } diff --git a/src/main/java/net/frozenorb/apiv3/route/accessTokens/DELETEAccessTokensId.java b/src/main/java/net/frozenorb/apiv3/route/accessTokens/DELETEAccessTokensId.java index 94bf671..7ea7edf 100644 --- a/src/main/java/net/frozenorb/apiv3/route/accessTokens/DELETEAccessTokensId.java +++ b/src/main/java/net/frozenorb/apiv3/route/accessTokens/DELETEAccessTokensId.java @@ -9,26 +9,21 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.AccessToken; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEAccessTokensId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback accessTokenCallback = new BlockingCallback<>(); - AccessToken.findById(ctx.request().getParam("accessToken"), accessTokenCallback); - AccessToken accessToken = accessTokenCallback.get(); + AccessToken accessToken = SyncUtils.runBlocking(v -> AccessToken.findById(ctx.request().getParam("accessToken"), v)); if (accessToken == null) { ErrorUtils.respondNotFound(ctx, "Access token", ctx.request().getParam("accessToken")); return; } - BlockingCallback callback = new BlockingCallback<>(); - accessToken.delete(callback); - callback.get(); - + SyncUtils.runBlocking(v -> accessToken.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); if (requestBody.containsKey("removedBy")) { diff --git a/src/main/java/net/frozenorb/apiv3/route/accessTokens/GETAccessTokens.java b/src/main/java/net/frozenorb/apiv3/route/accessTokens/GETAccessTokens.java index 5575274..ada68bd 100644 --- a/src/main/java/net/frozenorb/apiv3/route/accessTokens/GETAccessTokens.java +++ b/src/main/java/net/frozenorb/apiv3/route/accessTokens/GETAccessTokens.java @@ -5,16 +5,14 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; public final class GETAccessTokens implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("user"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("user"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("user")); @@ -22,9 +20,7 @@ public final class GETAccessTokens implements Handler { } int code = Integer.parseInt(ctx.request().getParam("totpCode")); - BlockingCallback totpAuthorizationCallback = new BlockingCallback<>(); - user.checkTotpAuthorization(code, null, totpAuthorizationCallback); - TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get(); + TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> user.checkTotpAuthorization(code, null, v)); if (!totpAuthorizationResult.isAuthorized()) { ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); diff --git a/src/main/java/net/frozenorb/apiv3/route/accessTokens/POSTAccessTokens.java b/src/main/java/net/frozenorb/apiv3/route/accessTokens/POSTAccessTokens.java index 8f327d2..f7457c0 100644 --- a/src/main/java/net/frozenorb/apiv3/route/accessTokens/POSTAccessTokens.java +++ b/src/main/java/net/frozenorb/apiv3/route/accessTokens/POSTAccessTokens.java @@ -10,9 +10,9 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.List; @@ -23,10 +23,7 @@ public final class POSTAccessTokens implements Handler { String actorName = requestBody.getString("actorName"); ActorType actorType = ActorType.valueOf(requestBody.getString("actorType").toUpperCase()); List lockedIps = (List) requestBody.getJsonArray("lockedIps").getList(); - - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("addedBy"), userCallback); - User addedBy = userCallback.get(); + User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v)); if (addedBy == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("addedBy")); @@ -34,9 +31,7 @@ public final class POSTAccessTokens implements Handler { } int code = requestBody.getInteger("totpCode"); - BlockingCallback totpAuthorizationCallback = new BlockingCallback<>(); - addedBy.checkTotpAuthorization(code, null, totpAuthorizationCallback); - TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get(); + TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> addedBy.checkTotpAuthorization(code, null, v)); if (!totpAuthorizationResult.isAuthorized()) { ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); @@ -44,9 +39,7 @@ public final class POSTAccessTokens implements Handler { } AccessToken accessToken = new AccessToken(actorName, actorType, lockedIps); - BlockingCallback callback = new BlockingCallback<>(); - accessToken.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> accessToken.insert(v)); AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.ACCESS_TOKEN_CREATE, ImmutableMap.of("accessTokenActorName", actorName), (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/auditLog/DELETEAuditLogId.java b/src/main/java/net/frozenorb/apiv3/route/auditLog/DELETEAuditLogId.java index 9eb5ea3..099d18f 100644 --- a/src/main/java/net/frozenorb/apiv3/route/auditLog/DELETEAuditLogId.java +++ b/src/main/java/net/frozenorb/apiv3/route/auditLog/DELETEAuditLogId.java @@ -8,16 +8,14 @@ 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.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEAuditLogId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback auditLogEntryCallback = new BlockingCallback<>(); - AuditLogEntry.findById(ctx.request().getParam("auditLogEntryId"), auditLogEntryCallback); - AuditLogEntry auditLogEntry = auditLogEntryCallback.get(); + AuditLogEntry auditLogEntry = SyncUtils.runBlocking(v -> AuditLogEntry.findById(ctx.request().getParam("auditLogEntryId"), v)); if (auditLogEntry == null) { ErrorUtils.respondNotFound(ctx, "Audit log entry", ctx.request().getParam("auditLogEntryId")); @@ -29,9 +27,7 @@ public final class DELETEAuditLogId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - auditLogEntry.getType().reverse(auditLogEntry, callback); - callback.get(); + SyncUtils.runBlocking(v -> auditLogEntry.getType().reverse(auditLogEntry, v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedAsns/DELETEBannedAsnsId.java b/src/main/java/net/frozenorb/apiv3/route/bannedAsns/DELETEBannedAsnsId.java index 6231e48..ac88894 100644 --- a/src/main/java/net/frozenorb/apiv3/route/bannedAsns/DELETEBannedAsnsId.java +++ b/src/main/java/net/frozenorb/apiv3/route/bannedAsns/DELETEBannedAsnsId.java @@ -9,8 +9,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.BannedAsn; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEBannedAsnsId implements Handler { @@ -23,9 +23,7 @@ public final class DELETEBannedAsnsId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - bannedAsn.delete(callback); - callback.get(); + SyncUtils.runBlocking(v -> bannedAsn.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedAsns/POSTBannedAsns.java b/src/main/java/net/frozenorb/apiv3/route/bannedAsns/POSTBannedAsns.java index 1e56dbe..79900d5 100644 --- a/src/main/java/net/frozenorb/apiv3/route/bannedAsns/POSTBannedAsns.java +++ b/src/main/java/net/frozenorb/apiv3/route/bannedAsns/POSTBannedAsns.java @@ -8,8 +8,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.BannedAsn; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTBannedAsns implements Handler { @@ -20,9 +20,7 @@ public final class POSTBannedAsns implements Handler { String note = requestBody.getString("note"); BannedAsn bannedAsn = new BannedAsn(id, note); - BlockingCallback callback = new BlockingCallback<>(); - bannedAsn.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> bannedAsn.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.BANNED_ASN_CREATE, ImmutableMap.of("bannedAsnId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java index f0c56d5..5d8d92b 100644 --- a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/DELETEBannedCellCarriersId.java @@ -9,8 +9,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.BannedCellCarrier; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEBannedCellCarriersId implements Handler { @@ -23,9 +23,7 @@ public final class DELETEBannedCellCarriersId implements Handler return; } - BlockingCallback callback = new BlockingCallback<>(); - bannedCellCarrier.delete(callback); - callback.get(); + SyncUtils.runBlocking(v -> bannedCellCarrier.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java index fe7ece0..bbf0023 100644 --- a/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java +++ b/src/main/java/net/frozenorb/apiv3/route/bannedCellCarriers/POSTBannedCellCarriers.java @@ -8,8 +8,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.BannedCellCarrier; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTBannedCellCarriers implements Handler { @@ -20,9 +20,7 @@ public final class POSTBannedCellCarriers implements Handler { String note = requestBody.getString("note"); BannedCellCarrier bannedCellCarrier = new BannedCellCarrier(id, note); - BlockingCallback callback = new BlockingCallback<>(); - bannedCellCarrier.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> bannedCellCarrier.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.BANNED_CALL_CARRIER_CREATE, ImmutableMap.of("bannedCellCarrierId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/emailTokens/POSTEmailTokensIdConfirm.java b/src/main/java/net/frozenorb/apiv3/route/emailTokens/POSTEmailTokensIdConfirm.java index b88c73e..3d9c438 100644 --- a/src/main/java/net/frozenorb/apiv3/route/emailTokens/POSTEmailTokensIdConfirm.java +++ b/src/main/java/net/frozenorb/apiv3/route/emailTokens/POSTEmailTokensIdConfirm.java @@ -9,17 +9,15 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.concurrent.TimeUnit; public final class POSTEmailTokensIdConfirm implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findByEmailToken(ctx.request().getParam("emailToken"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findByEmailToken(ctx.request().getParam("emailToken"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "Email token", ctx.request().getParam("emailToken")); @@ -46,9 +44,7 @@ public final class POSTEmailTokensIdConfirm implements Handler { user.completeEmailRegistration(user.getPendingEmail()); user.updatePassword(password); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_EMAIL, (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/DELETEGrantsId.java b/src/main/java/net/frozenorb/apiv3/route/grants/DELETEGrantsId.java index 9bdd38f..18777bf 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/DELETEGrantsId.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/DELETEGrantsId.java @@ -10,15 +10,13 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; public final class DELETEGrantsId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback grantCallback = new BlockingCallback<>(); - Grant.findById(ctx.request().getParam("grantId"), grantCallback); - Grant grant = grantCallback.get(); + Grant grant = SyncUtils.runBlocking(v -> Grant.findById(ctx.request().getParam("grantId"), v)); if (grant == null) { ErrorUtils.respondNotFound(ctx, "Grant", ctx.request().getParam("grantId")); @@ -29,9 +27,7 @@ public final class DELETEGrantsId implements Handler { } JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("removedBy"), userCallback); - User removedBy = userCallback.get(); + User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v)); if (removedBy == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); @@ -45,9 +41,7 @@ public final class DELETEGrantsId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - grant.delete(removedBy, reason, callback); - callback.get(); + SyncUtils.runBlocking(v -> grant.delete(removedBy, reason, v)); AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.GRANT_DELETE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java b/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java index f09ea0e..6e12494 100644 --- a/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java +++ b/src/main/java/net/frozenorb/apiv3/route/grants/POSTGrants.java @@ -11,9 +11,9 @@ import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.time.Instant; import java.util.HashSet; @@ -24,9 +24,7 @@ public final class POSTGrants implements Handler { public void handle(RoutingContext ctx) { JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("user"), userCallback); - User target = userCallback.get(); + User target = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("user"), v)); if (target == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("user")); @@ -76,15 +74,11 @@ public final class POSTGrants implements Handler { } // We purposely don't fail on a null check, grants don't have to have a source. - BlockingCallback addedByCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("addedBy"), addedByCallback); - User addedBy = addedByCallback.get(); + User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v)); if (addedBy != null && rank.isHigherStaffRank()) { int code = requestBody.getInteger("totpCode"); - BlockingCallback totpAuthorizationCallback = new BlockingCallback<>(); - addedBy.checkTotpAuthorization(code, null, totpAuthorizationCallback); - TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get(); + TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> addedBy.checkTotpAuthorization(code, null, v)); if (!totpAuthorizationResult.isAuthorized()) { ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); @@ -96,9 +90,7 @@ public final class POSTGrants implements Handler { int storeOrderId = requestBody.getInteger("storeOrderId", -1); Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy, storeItemId, storeOrderId); - BlockingCallback callback = new BlockingCallback<>(); - grant.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> grant.insert(v)); if (addedBy != null) { AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.GRANT_CREATE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/DELETEIpBansId.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/DELETEIpBansId.java index 9106dff..7d248fb 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/DELETEIpBansId.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/DELETEIpBansId.java @@ -8,18 +8,15 @@ 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.IpBan; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; public final class DELETEIpBansId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback ipBanCallback = new BlockingCallback<>(); - IpBan.findById(ctx.request().getParam("ipBanId"), ipBanCallback); - IpBan ipBan = ipBanCallback.get(); + IpBan ipBan = SyncUtils.runBlocking(v -> IpBan.findById(ctx.request().getParam("ipBanId"), v)); if (ipBan == null) { ErrorUtils.respondNotFound(ctx, "IpBan", ctx.request().getParam("id")); @@ -30,9 +27,7 @@ public final class DELETEIpBansId implements Handler { } JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("removedBy"), userCallback); - User removedBy = userCallback.get(); + User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v)); if (removedBy == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); @@ -46,13 +41,15 @@ public final class DELETEIpBansId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - ipBan.delete(removedBy, reason, callback); - callback.get(); - BlockingCallback blockingCallback = new BlockingCallback<>(); - AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), blockingCallback); - blockingCallback.get(); - APIv3.respondJson(ctx, 200, ipBan); + SyncUtils.runBlocking(v -> ipBan.delete(removedBy, reason, v)); + + AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), (ignored, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, ipBan); + } + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpBans.java b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpBans.java index 364a65c..15dc7b8 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpBans.java +++ b/src/main/java/net/frozenorb/apiv3/route/ipBans/POSTIpBans.java @@ -9,9 +9,9 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.time.Instant; @@ -46,14 +46,10 @@ public final class POSTIpBans implements Handler { } // We purposely don't do a null check, ip bans don't have to have a source. - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("addedBy"), userCallback); - User addedBy = userCallback.get(); + User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v)); IpBan ipBan = new IpBan(userIp, reason, expiresAt, addedBy, ctx.get("actor")); - BlockingCallback callback = new BlockingCallback<>(); - ipBan.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> ipBan.insert(v)); if (addedBy != null) { AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.IP_BAN_CREATE, ImmutableMap.of("ipBanId", ipBan.getId()), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/DELETENotificationTemplatesId.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/DELETENotificationTemplatesId.java index dc5cc67..ebea234 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/DELETENotificationTemplatesId.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/DELETENotificationTemplatesId.java @@ -9,25 +9,21 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.NotificationTemplate; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETENotificationTemplatesId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback notificationTemplateCallback = new BlockingCallback<>(); - NotificationTemplate.findById(ctx.request().getParam("notificationTemplateId"), notificationTemplateCallback); - NotificationTemplate notificationTemplate = notificationTemplateCallback.get(); + NotificationTemplate notificationTemplate = SyncUtils.runBlocking(v -> NotificationTemplate.findById(ctx.request().getParam("notificationTemplateId"), v)); if (notificationTemplate == null) { ErrorUtils.respondNotFound(ctx, "Notification template", ctx.request().getParam("notificationTemplateId")); return; } - BlockingCallback callback = new BlockingCallback<>(); - notificationTemplate.delete(callback); - callback.get(); + SyncUtils.runBlocking(v -> notificationTemplate.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/POSTNotificationTemplates.java b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/POSTNotificationTemplates.java index 0a3db2d..1040446 100644 --- a/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/POSTNotificationTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/route/notificationTemplates/POSTNotificationTemplates.java @@ -8,8 +8,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.NotificationTemplate; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTNotificationTemplates implements Handler { @@ -21,9 +21,7 @@ public final class POSTNotificationTemplates implements Handler String body = requestBody.getString("body"); NotificationTemplate notificationTemplate = new NotificationTemplate(id, subject, body); - BlockingCallback callback = new BlockingCallback<>(); - notificationTemplate.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> notificationTemplate.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.NOTIFICATION_TEMPLATE_CREATE, ImmutableMap.of("notificationTemplateId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEPunishmentsId.java b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEPunishmentsId.java index ad813ae..b67f627 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEPunishmentsId.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEPunishmentsId.java @@ -8,18 +8,15 @@ 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.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; public final class DELETEPunishmentsId implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback punishmentCallback = new BlockingCallback<>(); - Punishment.findById(ctx.request().getParam("punishmentId"), punishmentCallback); - Punishment punishment = punishmentCallback.get(); + Punishment punishment = SyncUtils.runBlocking(v -> Punishment.findById(ctx.request().getParam("punishmentId"), v)); if (punishment == null) { ErrorUtils.respondNotFound(ctx, "Punishment", ctx.request().getParam("punishmentId")); @@ -30,9 +27,7 @@ public final class DELETEPunishmentsId implements Handler { } JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback removedByCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("removedBy"), removedByCallback); - User removedBy = removedByCallback.get(); + User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v)); if (removedBy == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); @@ -46,13 +41,15 @@ public final class DELETEPunishmentsId implements Handler { return; } - BlockingCallback removeCallback = new BlockingCallback<>(); - punishment.delete(removedBy, reason, removeCallback); - removeCallback.get(); - BlockingCallback blockingCallback = new BlockingCallback<>(); - AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), blockingCallback); - blockingCallback.get(); - APIv3.respondJson(ctx, 200, punishment); + SyncUtils.runBlocking(v -> punishment.delete(removedBy, reason, v)); + + AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), (ignored, error) -> { + if (error != null) { + ErrorUtils.respondInternalError(ctx, error); + } else { + APIv3.respondJson(ctx, 200, punishment); + } + }); } } \ No newline at end of file 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 e1bba1a..387c87d 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java @@ -9,20 +9,17 @@ 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.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.List; public final class DELETEUsersIdActivePunishment implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("id"), userCallback); - User target = userCallback.get(); + User target = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("id"), v)); if (target == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); @@ -31,10 +28,7 @@ public final class DELETEUsersIdActivePunishment implements Handler removedByCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("removedBy"), removedByCallback); - User removedBy = removedByCallback.get(); + User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v)); if (removedBy == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); @@ -48,11 +42,10 @@ public final class DELETEUsersIdActivePunishment implements Handler> callback = new BlockingCallback<>(); - Punishment.findByUserAndType(target, ImmutableSet.of(type), callback); + List punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v)); Punishment activePunishment = null; - for (Punishment punishment : callback.get()) { + for (Punishment punishment : punishments) { if (punishment.isActive()) { activePunishment = punishment; break; @@ -64,15 +57,16 @@ public final class DELETEUsersIdActivePunishment implements Handler punishmentCallback = new BlockingCallback<>(); - activePunishment.delete(removedBy, reason, punishmentCallback); - punishmentCallback.get(); + Punishment finalActivePunishment = activePunishment; + SyncUtils.runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v)); - BlockingCallback blockingCallback = new BlockingCallback<>(); - AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), blockingCallback); - blockingCallback.get(); - - APIv3.respondJson(ctx, 200, activePunishment); + 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); + } + }); } } \ No newline at end of file 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 2840158..616f820 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java @@ -11,9 +11,9 @@ import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.Permissions; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.time.Instant; import java.util.List; @@ -23,9 +23,7 @@ public final class POSTPunishments implements Handler { public void handle(RoutingContext ctx) { JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("user"), userCallback); - User target = userCallback.get(); + User target = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("user"), v)); if (target == null) { ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("user")); @@ -48,14 +46,12 @@ public final class POSTPunishments implements Handler { Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type")); if (type != Punishment.PunishmentType.WARN) { - BlockingCallback> punishmentsCallback = new BlockingCallback<>(); - Punishment.findByUserAndType(target, ImmutableSet.of(type), punishmentsCallback); + List punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v)); - for (Punishment alternatePunishment : punishmentsCallback.get()) { + for (Punishment alternatePunishment : punishments) { if (alternatePunishment.isActive()) { - BlockingCallback addedByCallback = new BlockingCallback<>(); - User.findById(alternatePunishment.getAddedBy(), addedByCallback); - ErrorUtils.respondOther(ctx, 409, "User already covered by alternate punishment.", "alreadyCoveredByAlternatePunishment", ImmutableMap.of("alternatePunishmentBy", addedByCallback.get().getLastUsername())); + User user = SyncUtils.runBlocking(v -> User.findById(alternatePunishment.getAddedBy(), v)); + ErrorUtils.respondOther(ctx, 409, "User already covered by alternate punishment.", "alreadyCoveredByAlternatePunishment", ImmutableMap.of("alternatePunishmentBy", user.getLastUsername())); return; } } @@ -81,14 +77,10 @@ public final class POSTPunishments implements Handler { } // We purposely don't do a null check, punishments don't have to have a source. - BlockingCallback addedByCallback = new BlockingCallback<>(); - User.findById(requestBody.getString("addedBy"), addedByCallback); - User addedBy = addedByCallback.get(); + User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v)); + boolean isProtected = SyncUtils.runBlocking(v -> target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT, v)); - BlockingCallback permissionsCallback = new BlockingCallback<>(); - target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT, permissionsCallback); - - if (permissionsCallback.get()) { + if (isProtected) { ErrorUtils.respondOther(ctx, 409, "User is protected from punishments.", "protectedFromPunishments", ImmutableMap.of()); return; } @@ -99,16 +91,12 @@ public final class POSTPunishments implements Handler { if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) { IpBan ipBan = new IpBan(userIp, punishment); - BlockingCallback ipBanCallback = new BlockingCallback<>(); - ipBan.insert(ipBanCallback); - ipBanCallback.get(); + SyncUtils.runBlocking(v -> ipBan.insert(v)); punishment.linkIpBan(ipBan); } - BlockingCallback callback = new BlockingCallback<>(); - punishment.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> punishment.insert(v)); if (addedBy != null) { AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.PUNISHMENT_CREATE, ImmutableMap.of("punishmentId", punishment.getId()), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/ranks/DELETERanksId.java b/src/main/java/net/frozenorb/apiv3/route/ranks/DELETERanksId.java index a126901..38df4f3 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ranks/DELETERanksId.java +++ b/src/main/java/net/frozenorb/apiv3/route/ranks/DELETERanksId.java @@ -9,8 +9,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.Rank; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETERanksId implements Handler { @@ -23,9 +23,7 @@ public final class DELETERanksId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - rank.delete(callback); - callback.get(); + SyncUtils.runBlocking(v -> rank.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java b/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java index 8fc808f..32364c2 100644 --- a/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java +++ b/src/main/java/net/frozenorb/apiv3/route/ranks/POSTRanks.java @@ -8,8 +8,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.Rank; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTRanks implements Handler { @@ -26,9 +26,7 @@ public final class POSTRanks implements Handler { boolean higherStaffRank = requestBody.getBoolean("higherStaffRank"); Rank rank = new Rank(id, inheritsFromId, weight, displayName, gameColor, websiteColor, staffRank, higherStaffRank); - BlockingCallback callback = new BlockingCallback<>(); - rank.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> rank.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.RANK_CREATE, ImmutableMap.of("rankId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/serverGroups/DELETEServerGroupsId.java b/src/main/java/net/frozenorb/apiv3/route/serverGroups/DELETEServerGroupsId.java index 66cc8c7..19375df 100644 --- a/src/main/java/net/frozenorb/apiv3/route/serverGroups/DELETEServerGroupsId.java +++ b/src/main/java/net/frozenorb/apiv3/route/serverGroups/DELETEServerGroupsId.java @@ -9,8 +9,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.ServerGroup; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEServerGroupsId implements Handler { @@ -23,9 +23,7 @@ public final class DELETEServerGroupsId implements Handler { return; } - BlockingCallback callback = new BlockingCallback<>(); - serverGroup.delete(callback); - callback.get(); + SyncUtils.runBlocking(v -> serverGroup.delete(v)); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/serverGroups/POSTServerGroups.java b/src/main/java/net/frozenorb/apiv3/route/serverGroups/POSTServerGroups.java index 1c897f6..8895668 100644 --- a/src/main/java/net/frozenorb/apiv3/route/serverGroups/POSTServerGroups.java +++ b/src/main/java/net/frozenorb/apiv3/route/serverGroups/POSTServerGroups.java @@ -8,8 +8,8 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.ServerGroup; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTServerGroups implements Handler { @@ -20,9 +20,7 @@ public final class POSTServerGroups implements Handler { String image = requestBody.getString("image"); ServerGroup serverGroup = new ServerGroup(id, image); - BlockingCallback callback = new BlockingCallback<>(); - serverGroup.insert(callback); - callback.get(); + SyncUtils.runBlocking(v -> serverGroup.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.SERVER_GROUP_CREATE, ImmutableMap.of("serverGroupId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/DELETEServersId.java b/src/main/java/net/frozenorb/apiv3/route/servers/DELETEServersId.java index 18c1833..6b49a77 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/DELETEServersId.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/DELETEServersId.java @@ -1,7 +1,6 @@ package net.frozenorb.apiv3.route.servers; import com.google.common.collect.ImmutableMap; -import com.mongodb.client.result.DeleteResult; import io.vertx.core.Handler; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.RoutingContext; @@ -11,8 +10,8 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.Server; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class DELETEServersId implements Handler { @@ -25,21 +24,25 @@ public final class DELETEServersId implements Handler { return; } - BlockingCallback deleteServerCallback = new BlockingCallback<>(); - server.delete(deleteServerCallback); - deleteServerCallback.get(); + SyncUtils.runBlocking(v -> server.insert(v)); - BlockingCallback deleteAccessTokenCallback = new BlockingCallback<>(); - AccessToken.findByNameAndType(server.getId(), ActorType.SERVER, (accessToken, error) -> { - if (error != null) { - deleteAccessTokenCallback.onResult(null, error); - } else if (accessToken != null) { - accessToken.delete(deleteServerCallback); - } else { - deleteAccessTokenCallback.onResult(null, new NullPointerException("Access token not found.")); - } + SyncUtils.runBlocking(v -> { + AccessToken.findByNameAndType(server.getId(), ActorType.SERVER, (accessToken, error) -> { + if (error != null) { + v.onResult(null, error); + } else if (accessToken != null) { + accessToken.delete((ignored, error2) -> { + if (error2 != null) { + v.onResult(null, error2); + } else { + v.onResult(null, null); + } + }); + } else { + v.onResult(null, new NullPointerException("Access token not found.")); + } + }); }); - deleteAccessTokenCallback.get(); JsonObject requestBody = ctx.getBodyAsJson(); diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServers.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServers.java index 0ce0de9..6e16af8 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServers.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServers.java @@ -10,9 +10,9 @@ import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.ServerGroup; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; public final class POSTServers implements Handler { @@ -35,14 +35,10 @@ public final class POSTServers implements Handler { } Server server = new Server(id, displayName, group, ip); - BlockingCallback insertServerCallback = new BlockingCallback<>(); - server.insert(insertServerCallback); - insertServerCallback.get(); + SyncUtils.runBlocking(v -> server.insert(v)); AccessToken accessToken = new AccessToken(server); - BlockingCallback insertAccessTokenCallback = new BlockingCallback<>(); - accessToken.insert(insertAccessTokenCallback); - insertAccessTokenCallback.get(); + SyncUtils.runBlocking(v -> accessToken.insert(v)); if (requestBody.containsKey("addedBy")) { AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.SERVER_CREATE, ImmutableMap.of("serverId", id), (ignored, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETStaff.java b/src/main/java/net/frozenorb/apiv3/route/users/GETStaff.java index 43c564a..429d1e5 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETStaff.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETStaff.java @@ -6,7 +6,7 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.*; @@ -28,15 +28,12 @@ public final class GETStaff implements Handler { return Integer.compare(firstRank.getWeight(), secondRank.getWeight()); }); - BlockingCallback> grantsCallback = new BlockingCallback<>(); - Grant.findByRank(staffRanks.values(), grantsCallback); + List staffGrants = SyncUtils.runBlocking(v -> Grant.findByRank(staffRanks.values(), v)); - grantsCallback.get().forEach(grant -> { - if (grant.isActive()) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(grant.getUser(), userCallback); - User user = userCallback.get(); - Rank rank = staffRanks.get(grant.getRank()); + for (Grant staffGrant : staffGrants) { + if (staffGrant.isActive()) { + User user = SyncUtils.runBlocking(v -> User.findById(staffGrant.getId(), v)); + Rank rank = staffRanks.get(staffGrant.getRank()); if (!result.containsKey(rank.getId())) { result.put(rank.getId(), new HashSet<>()); @@ -44,7 +41,7 @@ public final class GETStaff implements Handler { result.get(rank.getId()).add(user); } - }); + } APIv3.respondJson(ctx, 200, result); } diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdDetails.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdDetails.java index fff455f..f58c8e4 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdDetails.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdDetails.java @@ -7,8 +7,8 @@ import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.IpLogEntry; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.HashMap; import java.util.List; @@ -17,29 +17,22 @@ import java.util.Map; public final class GETUsersIdDetails implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); return; } - BlockingCallback> grantsCallback = new BlockingCallback<>(); - BlockingCallback> ipLogCallback = new BlockingCallback<>(); - BlockingCallback> punishmentsCallback = new BlockingCallback<>(); - - Grant.findByUser(user, grantsCallback); - IpLogEntry.findByUser(user, ipLogCallback); - Punishment.findByUser(user, punishmentsCallback); - + List grants = SyncUtils.runBlocking(v -> Grant.findByUser(user, v)); + List ipLog = SyncUtils.runBlocking(v -> IpLogEntry.findByUser(user, v)); + List punishments = SyncUtils.runBlocking(v -> Punishment.findByUser(user, v)); Map result = new HashMap<>(); result.put("user", user); - result.put("grants", grantsCallback.get()); - result.put("ipLog", ipLogCallback.get()); - result.put("punishments", punishmentsCallback.get()); + result.put("grants", grants); + result.put("ipLog", ipLog); + result.put("punishments", punishments); result.put("aliases", user.getAliases()); result.put("totpSetup", user.getTotpSecret() != null); diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java index 94ee91b..d2e525f 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java @@ -7,28 +7,22 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.UUID; public final class GETUsersIdVerifyPassword implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback callback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), callback); - User user = callback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { - callback = new BlockingCallback<>(); - User.findByLastUsername(ctx.request().getParam("userId"), callback); - user = callback.get(); + user = SyncUtils.runBlocking(v -> User.findByLastUsername(ctx.request().getParam("userId"), v)); } if (user == null) { - callback = new BlockingCallback<>(); - User.findByEmail(ctx.request().getParam("userId"), callback); - user = callback.get(); + user = SyncUtils.runBlocking(v -> User.findByEmail(ctx.request().getParam("userId"), v)); } if (user == null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdChangePassword.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdChangePassword.java index 1a31f7e..3394f22 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdChangePassword.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdChangePassword.java @@ -9,19 +9,17 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.RequiresTotpResult; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.concurrent.TimeUnit; public final class POSTUsersIdChangePassword implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -38,15 +36,11 @@ public final class POSTUsersIdChangePassword implements Handler boolean authorized = false; if (user.checkPassword(requestBody.getString("currentPassword"))) { - BlockingCallback totpRequiredCallback = new BlockingCallback<>(); - user.requiresTotpAuthorization(null, totpRequiredCallback); - RequiresTotpResult requiresTotp = totpRequiredCallback.get(); + RequiresTotpResult requiresTotp = SyncUtils.runBlocking(v -> user.requiresTotpAuthorization(null, v)); if (requiresTotp == RequiresTotpResult.REQUIRED_NO_EXEMPTIONS) { int code = requestBody.getInteger("totpCode"); - BlockingCallback totpAuthorizationCallback = new BlockingCallback<>(); - user.checkTotpAuthorization(code, null, totpAuthorizationCallback); - TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get(); + TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> user.checkTotpAuthorization(code, null, v)); if (!totpAuthorizationResult.isAuthorized()) { ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); @@ -77,9 +71,7 @@ public final class POSTUsersIdChangePassword implements Handler } user.updatePassword(newPassword); - BlockingCallback saveCallback = new BlockingCallback<>(); - user.save(saveCallback); - saveCallback.get(); + SyncUtils.runBlocking(v -> user.save(v)); AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CHANGE_PASSWORD, (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdConfirmPhone.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdConfirmPhone.java index 40c5b2e..fc54fe3 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdConfirmPhone.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdConfirmPhone.java @@ -9,17 +9,15 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.concurrent.TimeUnit; public final class POSTUsersIdConfirmPhone implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("id"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); @@ -56,18 +54,14 @@ public final class POSTUsersIdConfirmPhone implements Handler { if (!String.valueOf(phoneCode).equals(user.getPendingPhoneToken())) { user.failedPhoneRegistration(); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); ErrorUtils.respondOther(ctx, 409, "Phone token doesn't match", "phoneTokenNoMatch", ImmutableMap.of()); return; } user.completePhoneRegistration(user.getPendingPhone()); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_PHONE, (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLogin.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLogin.java index 30a0db0..bcd6d46 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLogin.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdLogin.java @@ -11,9 +11,9 @@ import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.model.IpLogEntry; import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.IpUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.UuidUtils; import java.util.UUID; @@ -29,9 +29,7 @@ public final class POSTUsersIdLogin implements Handler { } JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(uuid, userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(uuid, v)); String currentUsername = requestBody.getString("username"); String userIp = requestBody.getString("userIp"); Actor actor = ctx.get("actor"); @@ -50,40 +48,35 @@ public final class POSTUsersIdLogin implements Handler { if (user == null) { user = new User(uuid, currentUsername); - BlockingCallback nameCollisionCallback = new BlockingCallback<>(); - user.checkNameCollisions(nameCollisionCallback); - nameCollisionCallback.get(); - BlockingCallback insertCallback = new BlockingCallback<>(); - user.insert(insertCallback); - insertCallback.get(); + User finalUser = user; + + SyncUtils.runBlocking(v -> finalUser.checkNameCollisions(v)); + SyncUtils.runBlocking(v -> finalUser.insert(v)); } - BlockingCallback ipLogEntryCallback = new BlockingCallback<>(); - IpLogEntry.findByUserAndIp(user, userIp, ipLogEntryCallback); - IpLogEntry ipLogEntry = ipLogEntryCallback.get(); + User finalUser = user; + IpLogEntry ipLogEntry = SyncUtils.runBlocking(v -> IpLogEntry.findByUserAndIp(finalUser, userIp, v)); // We use a little bit more verbose code here to save on the // overhead of a .insert() immediately followed by a .save() if (ipLogEntry == null) { ipLogEntry = new IpLogEntry(user, userIp); ipLogEntry.used(); - BlockingCallback callback = new BlockingCallback<>(); - ipLogEntry.insert(callback); - callback.get(); + + IpLogEntry finalIpLogEntry = ipLogEntry; + SyncUtils.runBlocking(v -> finalIpLogEntry.insert(v)); } else { ipLogEntry.used(); - BlockingCallback callback = new BlockingCallback<>(); - ipLogEntry.save(callback); - callback.get(); + + IpLogEntry finalIpLogEntry = ipLogEntry; + SyncUtils.runBlocking(v -> finalIpLogEntry.save(v)); } String lastUsername = user.getLastUsername(); user.updateUsername(currentUsername); if (!currentUsername.equals(lastUsername)) { - BlockingCallback callback = new BlockingCallback<>(); - user.checkNameCollisions(callback); - callback.get(); + SyncUtils.runBlocking(v -> finalUser.checkNameCollisions(v)); } user.getLoginInfo(actorServer, userIp, (loginInfo, error) -> { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdNotify.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdNotify.java index 16fb365..01b927f 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdNotify.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdNotify.java @@ -7,18 +7,16 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.Map; public final class POSTUsersIdNotify implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -31,9 +29,7 @@ public final class POSTUsersIdNotify implements Handler { } JsonObject requestBody = ctx.getBodyAsJson(); - BlockingCallback notificationTemplateCallback = new BlockingCallback<>(); - NotificationTemplate.findById(requestBody.getString("template"), notificationTemplateCallback); - NotificationTemplate template = notificationTemplateCallback.get(); + NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById(requestBody.getString("template"), v)); if (template == null) { ErrorUtils.respondNotFound(ctx, "Notification template", requestBody.getString("template")); diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdPasswordReset.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdPasswordReset.java index 405a0ab..68f0057 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdPasswordReset.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdPasswordReset.java @@ -10,9 +10,9 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -20,9 +20,7 @@ import java.util.concurrent.TimeUnit; public final class POSTUsersIdPasswordReset implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -35,18 +33,15 @@ public final class POSTUsersIdPasswordReset implements Handler { } user.startPasswordReset(); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); Map replacements = ImmutableMap.of( "username", user.getLastUsername(), "passwordResetToken", user.getPasswordResetToken() ); - BlockingCallback notificationTemplateCallback = new BlockingCallback<>(); - NotificationTemplate.findById("password-reset", notificationTemplateCallback); - Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements); + NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("password-reset", v)); + Notification notification = new Notification(template, replacements, replacements); notification.sendAsEmail(user.getEmail(), (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterEmail.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterEmail.java index 2b72bde..1e4ccfd 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterEmail.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterEmail.java @@ -10,10 +10,10 @@ import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.util.EmailUtils; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -21,9 +21,7 @@ import java.util.concurrent.TimeUnit; public final class POSTUsersIdRegisterEmail implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -53,18 +51,15 @@ public final class POSTUsersIdRegisterEmail implements Handler { return; } - BlockingCallback sameEmailCallback = new BlockingCallback<>(); - User.findByEmail(email, sameEmailCallback); + User sameEmail = SyncUtils.runBlocking(v -> User.findByEmail(email, v)); - if (sameEmailCallback.get() != null) { + if (sameEmail != null) { ErrorUtils.respondInvalidInput(ctx, email + " is already in use."); return; } user.startEmailRegistration(email); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); Map replacements = ImmutableMap.of( "username", user.getLastUsername(), @@ -72,9 +67,8 @@ public final class POSTUsersIdRegisterEmail implements Handler { "emailToken", user.getPendingEmailToken() ); - BlockingCallback notificationTemplateCallback = new BlockingCallback<>(); - NotificationTemplate.findById("email-confirmation", notificationTemplateCallback); - Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements); + NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("email-confirmation", v)); + Notification notification = new Notification(template, replacements, replacements); notification.sendAsEmail(user.getEmail(), (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterPhone.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterPhone.java index 3abc236..d45c078 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterPhone.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdRegisterPhone.java @@ -12,10 +12,10 @@ import net.frozenorb.apiv3.model.BannedCellCarrier; import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.PhoneIntel; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.PhoneUtils; +import net.frozenorb.apiv3.util.SyncUtils; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -23,9 +23,7 @@ import java.util.concurrent.TimeUnit; public final class POSTUsersIdRegisterPhone implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -50,17 +48,14 @@ public final class POSTUsersIdRegisterPhone implements Handler { return; } - BlockingCallback samePhoneCallback = new BlockingCallback<>(); - User.findByPhone(phone, samePhoneCallback); + User samePhone = SyncUtils.runBlocking(v -> User.findByPhone(phone, v)); - if (samePhoneCallback.get() != null) { + if (samePhone != null) { ErrorUtils.respondInvalidInput(ctx, phone + " is already in use."); return; } - BlockingCallback phoneIntelCallback = new BlockingCallback<>(); - PhoneIntel.findOrCreateById(phone, phoneIntelCallback); - PhoneIntel phoneIntel = phoneIntelCallback.get(); + PhoneIntel phoneIntel = SyncUtils.runBlocking(v -> PhoneIntel.findOrCreateById(phone, v)); if (BannedCellCarrier.findById(phoneIntel.getResult().getCarrierId()) != null) { ErrorUtils.respondInvalidInput(ctx, phone + " is from a banned cell provider."); @@ -68,9 +63,7 @@ public final class POSTUsersIdRegisterPhone implements Handler { } user.startPhoneRegistration(phone); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); Map replacements = ImmutableMap.of( "username", user.getLastUsername(), @@ -78,9 +71,8 @@ public final class POSTUsersIdRegisterPhone implements Handler { "phoneToken", user.getPendingPhoneToken() ); - BlockingCallback notificationTemplateCallback = new BlockingCallback<>(); - NotificationTemplate.findById("phone-confirmation", notificationTemplateCallback); - Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements); + NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("phone-confirmation", v)); + Notification notification = new Notification(template, replacements, replacements); notification.sendAsText(user.getPendingPhone(), (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdSetupTotp.java b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdSetupTotp.java index 6e8eeef..1a48150 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdSetupTotp.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/POSTUsersIdSetupTotp.java @@ -9,16 +9,14 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SyncUtils; import net.frozenorb.apiv3.util.TotpUtils; public final class POSTUsersIdSetupTotp implements Handler { public void handle(RoutingContext ctx) { - BlockingCallback userCallback = new BlockingCallback<>(); - User.findById(ctx.request().getParam("userId"), userCallback); - User user = userCallback.get(); + User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); @@ -36,9 +34,7 @@ public final class POSTUsersIdSetupTotp implements Handler { if (TotpUtils.authorizeUser(secret, totpCode)) { user.setTotpSecret(secret); - BlockingCallback callback = new BlockingCallback<>(); - user.save(callback); - callback.get(); + SyncUtils.runBlocking(v -> user.save(v)); AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_SETUP_TOTP, (ignored, error) -> { if (error != null) { diff --git a/src/main/java/net/frozenorb/apiv3/unsorted/BlockingCallback.java b/src/main/java/net/frozenorb/apiv3/unsorted/BlockingCallback.java deleted file mode 100644 index 5db6d1b..0000000 --- a/src/main/java/net/frozenorb/apiv3/unsorted/BlockingCallback.java +++ /dev/null @@ -1,30 +0,0 @@ -package net.frozenorb.apiv3.unsorted; - -import com.google.common.util.concurrent.SettableFuture; -import com.mongodb.async.SingleResultCallback; - -import java.util.concurrent.ExecutionException; - -public final class BlockingCallback implements SingleResultCallback { - - private final SettableFuture future = SettableFuture.create(); - - @Override - public void onResult(T val, Throwable error) { - if (error != null) { - future.setException(error); - } else { - future.set(val); - } - } - - public T get() { - try { - return future.get(); - } catch (InterruptedException | ExecutionException ex) { - // No matter what we get we'll just rethrow. - throw new RuntimeException(ex); - } - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/SyncUtils.java b/src/main/java/net/frozenorb/apiv3/util/SyncUtils.java index daeb2b0..5fdcd97 100644 --- a/src/main/java/net/frozenorb/apiv3/util/SyncUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/SyncUtils.java @@ -1,10 +1,13 @@ package net.frozenorb.apiv3.util; +import com.google.common.util.concurrent.SettableFuture; import com.mongodb.async.SingleResultCallback; import io.vertx.core.Context; import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.APIv3; +import java.util.concurrent.ExecutionException; + @UtilityClass public class SyncUtils { @@ -18,4 +21,29 @@ public class SyncUtils { }; } + public static T runBlocking(BlockingWrapper wrapper) { + SettableFuture future = SettableFuture.create(); + + wrapper.run((result, error) -> { + if (error != null) { + future.setException(error); + } else { + future.set(result); + } + }); + + try { + return future.get(); + } catch (InterruptedException | ExecutionException ex) { + // No matter what we get we'll just rethrow. + throw new RuntimeException(ex); + } + } + + public interface BlockingWrapper { + + void run(SingleResultCallback v); + + } + } \ No newline at end of file