Replace 3 line BlockingCallback pattern with 1 line SyncUtils#runBlocking lambda pattern

This commit is contained in:
Colin McDonald 2016-07-10 15:42:42 -04:00
parent 6319f944bb
commit 529bf25daf
41 changed files with 226 additions and 387 deletions

View File

@ -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<Document> {
-1
);
BlockingCallback<Void> callback = new BlockingCallback<>();
created.insert(callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> created.insert(v));
log.info("Created grant " + created.getId() + " (" + created.getRank() + ")");
}

View File

@ -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<Document> {
((Number) ipLogEntry.get("uses")).intValue()
);
BlockingCallback<Void> callback = new BlockingCallback<>();
created.insert(callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> created.insert(v));
log.info("Created ip log entry " + created.getId() + " (" + created.getUser() + " - " + created.getUserIp() + ")");
}

View File

@ -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<Document> {
punishment.containsKey("removedBy") ? punishment.getString("removalReason").toString() : null
);
BlockingCallback<Void> callback = new BlockingCallback<>();
created.insert(callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> created.insert(v));
log.info("Created punishment " + created.getId() + " (" + created.getType() + ")");
}

View File

@ -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<Document> {
null,
null,
null,
null,
null,
"INVALID",
user.getDate("joined").toInstant(),
user.getDate("joined").toInstant(),
false
);
BlockingCallback<Void> callback = new BlockingCallback<>();
created.insert(callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> created.insert(v));
log.info("Created user " + created.getLastUsername() + " (" + created.getId() + ")");
}

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<AccessToken> 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<DeleteResult> callback = new BlockingCallback<>();
accessToken.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> accessToken.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();
if (requestBody.containsKey("removedBy")) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
}
int code = Integer.parseInt(ctx.request().getParam("totpCode"));
BlockingCallback<TotpAuthorizationResult> 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());

View File

@ -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<RoutingContext> {
String actorName = requestBody.getString("actorName");
ActorType actorType = ActorType.valueOf(requestBody.getString("actorType").toUpperCase());
List<String> lockedIps = (List<String>) requestBody.getJsonArray("lockedIps").getList();
BlockingCallback<User> 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<RoutingContext> {
}
int code = requestBody.getInteger("totpCode");
BlockingCallback<TotpAuthorizationResult> 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<RoutingContext> {
}
AccessToken accessToken = new AccessToken(actorName, actorType, lockedIps);
BlockingCallback<Void> callback = new BlockingCallback<>();
accessToken.insert(callback);
callback.get();
SyncUtils.<Void>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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<AuditLogEntry> 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<RoutingContext> {
return;
}
BlockingCallback<Void> callback = new BlockingCallback<>();
auditLogEntry.getType().reverse(auditLogEntry, callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> auditLogEntry.getType().reverse(auditLogEntry, v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEBannedAsnsId implements Handler<RoutingContext> {
return;
}
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
bannedAsn.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> bannedAsn.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTBannedAsns implements Handler<RoutingContext> {
String note = requestBody.getString("note");
BannedAsn bannedAsn = new BannedAsn(id, note);
BlockingCallback<Void> callback = new BlockingCallback<>();
bannedAsn.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEBannedCellCarriersId implements Handler<RoutingContext>
return;
}
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
bannedCellCarrier.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> bannedCellCarrier.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTBannedCellCarriers implements Handler<RoutingContext> {
String note = requestBody.getString("note");
BannedCellCarrier bannedCellCarrier = new BannedCellCarrier(id, note);
BlockingCallback<Void> callback = new BlockingCallback<>();
bannedCellCarrier.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
user.completeEmailRegistration(user.getPendingEmail());
user.updatePassword(password);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_EMAIL, (ignored, error) -> {
if (error != null) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<Grant> 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<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
return;
}
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
grant.delete(removedBy, reason, callback);
callback.get();
SyncUtils.<UpdateResult>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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
}
// We purposely don't fail on a null check, grants don't have to have a source.
BlockingCallback<User> 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<TotpAuthorizationResult> 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<RoutingContext> {
int storeOrderId = requestBody.getInteger("storeOrderId", -1);
Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy, storeItemId, storeOrderId);
BlockingCallback<Void> callback = new BlockingCallback<>();
grant.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<IpBan> 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<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
return;
}
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
ipBan.delete(removedBy, reason, callback);
callback.get();
BlockingCallback<AuditLogEntry> blockingCallback = new BlockingCallback<>();
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), blockingCallback);
blockingCallback.get();
SyncUtils.<UpdateResult>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);
}
});
}
}

View File

@ -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<RoutingContext> {
}
// We purposely don't do a null check, ip bans don't have to have a source.
BlockingCallback<User> 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<Void> callback = new BlockingCallback<>();
ipBan.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<NotificationTemplate> 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<DeleteResult> callback = new BlockingCallback<>();
notificationTemplate.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> notificationTemplate.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -21,9 +21,7 @@ public final class POSTNotificationTemplates implements Handler<RoutingContext>
String body = requestBody.getString("body");
NotificationTemplate notificationTemplate = new NotificationTemplate(id, subject, body);
BlockingCallback<Void> callback = new BlockingCallback<>();
notificationTemplate.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<Punishment> 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<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
return;
}
BlockingCallback<UpdateResult> removeCallback = new BlockingCallback<>();
punishment.delete(removedBy, reason, removeCallback);
removeCallback.get();
BlockingCallback<AuditLogEntry> blockingCallback = new BlockingCallback<>();
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), blockingCallback);
blockingCallback.get();
SyncUtils.<UpdateResult>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);
}
});
}
}

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingConte
JsonObject requestBody = ctx.getBodyAsJson();
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type").toUpperCase());
BlockingCallback<User> 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<RoutingConte
return;
}
BlockingCallback<List<Punishment>> callback = new BlockingCallback<>();
Punishment.findByUserAndType(target, ImmutableSet.of(type), callback);
List<Punishment> 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<RoutingConte
return;
}
BlockingCallback<UpdateResult> punishmentCallback = new BlockingCallback<>();
activePunishment.delete(removedBy, reason, punishmentCallback);
punishmentCallback.get();
Punishment finalActivePunishment = activePunishment;
SyncUtils.<UpdateResult>runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v));
BlockingCallback<AuditLogEntry> 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);
}
});
}
}

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type"));
if (type != Punishment.PunishmentType.WARN) {
BlockingCallback<List<Punishment>> punishmentsCallback = new BlockingCallback<>();
Punishment.findByUserAndType(target, ImmutableSet.of(type), punishmentsCallback);
List<Punishment> punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v));
for (Punishment alternatePunishment : punishmentsCallback.get()) {
for (Punishment alternatePunishment : punishments) {
if (alternatePunishment.isActive()) {
BlockingCallback<User> 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<RoutingContext> {
}
// We purposely don't do a null check, punishments don't have to have a source.
BlockingCallback<User> 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<Boolean> 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<RoutingContext> {
if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) {
IpBan ipBan = new IpBan(userIp, punishment);
BlockingCallback<Void> ipBanCallback = new BlockingCallback<>();
ipBan.insert(ipBanCallback);
ipBanCallback.get();
SyncUtils.<Void>runBlocking(v -> ipBan.insert(v));
punishment.linkIpBan(ipBan);
}
BlockingCallback<Void> callback = new BlockingCallback<>();
punishment.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETERanksId implements Handler<RoutingContext> {
return;
}
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
rank.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> rank.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -26,9 +26,7 @@ public final class POSTRanks implements Handler<RoutingContext> {
boolean higherStaffRank = requestBody.getBoolean("higherStaffRank");
Rank rank = new Rank(id, inheritsFromId, weight, displayName, gameColor, websiteColor, staffRank, higherStaffRank);
BlockingCallback<Void> callback = new BlockingCallback<>();
rank.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEServerGroupsId implements Handler<RoutingContext> {
return;
}
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
serverGroup.delete(callback);
callback.get();
SyncUtils.<DeleteResult>runBlocking(v -> serverGroup.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTServerGroups implements Handler<RoutingContext> {
String image = requestBody.getString("image");
ServerGroup serverGroup = new ServerGroup(id, image);
BlockingCallback<Void> callback = new BlockingCallback<>();
serverGroup.insert(callback);
callback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
@ -25,21 +24,25 @@ public final class DELETEServersId implements Handler<RoutingContext> {
return;
}
BlockingCallback<DeleteResult> deleteServerCallback = new BlockingCallback<>();
server.delete(deleteServerCallback);
deleteServerCallback.get();
SyncUtils.<Void>runBlocking(v -> server.insert(v));
BlockingCallback<DeleteResult> deleteAccessTokenCallback = new BlockingCallback<>();
SyncUtils.runBlocking(v -> {
AccessToken.findByNameAndType(server.getId(), ActorType.SERVER, (accessToken, error) -> {
if (error != null) {
deleteAccessTokenCallback.onResult(null, error);
v.onResult(null, error);
} else if (accessToken != null) {
accessToken.delete(deleteServerCallback);
accessToken.delete((ignored, error2) -> {
if (error2 != null) {
v.onResult(null, error2);
} else {
deleteAccessTokenCallback.onResult(null, new NullPointerException("Access token not found."));
v.onResult(null, null);
}
});
deleteAccessTokenCallback.get();
} else {
v.onResult(null, new NullPointerException("Access token not found."));
}
});
});
JsonObject requestBody = ctx.getBodyAsJson();

View File

@ -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<RoutingContext> {
@ -35,14 +35,10 @@ public final class POSTServers implements Handler<RoutingContext> {
}
Server server = new Server(id, displayName, group, ip);
BlockingCallback<Void> insertServerCallback = new BlockingCallback<>();
server.insert(insertServerCallback);
insertServerCallback.get();
SyncUtils.<Void>runBlocking(v -> server.insert(v));
AccessToken accessToken = new AccessToken(server);
BlockingCallback<Void> insertAccessTokenCallback = new BlockingCallback<>();
accessToken.insert(insertAccessTokenCallback);
insertAccessTokenCallback.get();
SyncUtils.<Void>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) -> {

View File

@ -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<RoutingContext> {
return Integer.compare(firstRank.getWeight(), secondRank.getWeight());
});
BlockingCallback<List<Grant>> grantsCallback = new BlockingCallback<>();
Grant.findByRank(staffRanks.values(), grantsCallback);
List<Grant> staffGrants = SyncUtils.runBlocking(v -> Grant.findByRank(staffRanks.values(), v));
grantsCallback.get().forEach(grant -> {
if (grant.isActive()) {
BlockingCallback<User> 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<RoutingContext> {
result.get(rank.getId()).add(user);
}
});
}
APIv3.respondJson(ctx, 200, result);
}

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<List<Grant>> grantsCallback = new BlockingCallback<>();
BlockingCallback<List<IpLogEntry>> ipLogCallback = new BlockingCallback<>();
BlockingCallback<List<Punishment>> punishmentsCallback = new BlockingCallback<>();
Grant.findByUser(user, grantsCallback);
IpLogEntry.findByUser(user, ipLogCallback);
Punishment.findByUser(user, punishmentsCallback);
List<Grant> grants = SyncUtils.runBlocking(v -> Grant.findByUser(user, v));
List<IpLogEntry> ipLog = SyncUtils.runBlocking(v -> IpLogEntry.findByUser(user, v));
List<Punishment> punishments = SyncUtils.runBlocking(v -> Punishment.findByUser(user, v));
Map<String, Object> 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);

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext>
boolean authorized = false;
if (user.checkPassword(requestBody.getString("currentPassword"))) {
BlockingCallback<RequiresTotpResult> 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<TotpAuthorizationResult> 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<RoutingContext>
}
user.updatePassword(newPassword);
BlockingCallback<UpdateResult> saveCallback = new BlockingCallback<>();
user.save(saveCallback);
saveCallback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CHANGE_PASSWORD, (ignored, error) -> {
if (error != null) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
if (!String.valueOf(phoneCode).equals(user.getPendingPhoneToken())) {
user.failedPhoneRegistration();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
ErrorUtils.respondOther(ctx, 409, "Phone token doesn't match", "phoneTokenNoMatch", ImmutableMap.of());
return;
}
user.completePhoneRegistration(user.getPendingPhone());
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_PHONE, (ignored, error) -> {
if (error != null) {

View File

@ -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<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> 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<RoutingContext> {
if (user == null) {
user = new User(uuid, currentUsername);
BlockingCallback<Void> nameCollisionCallback = new BlockingCallback<>();
user.checkNameCollisions(nameCollisionCallback);
nameCollisionCallback.get();
BlockingCallback<Void> insertCallback = new BlockingCallback<>();
user.insert(insertCallback);
insertCallback.get();
User finalUser = user;
SyncUtils.<Void>runBlocking(v -> finalUser.checkNameCollisions(v));
SyncUtils.<Void>runBlocking(v -> finalUser.insert(v));
}
BlockingCallback<IpLogEntry> 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<Void> callback = new BlockingCallback<>();
ipLogEntry.insert(callback);
callback.get();
IpLogEntry finalIpLogEntry = ipLogEntry;
SyncUtils.<Void>runBlocking(v -> finalIpLogEntry.insert(v));
} else {
ipLogEntry.used();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
ipLogEntry.save(callback);
callback.get();
IpLogEntry finalIpLogEntry = ipLogEntry;
SyncUtils.<UpdateResult>runBlocking(v -> finalIpLogEntry.save(v));
}
String lastUsername = user.getLastUsername();
user.updateUsername(currentUsername);
if (!currentUsername.equals(lastUsername)) {
BlockingCallback<Void> callback = new BlockingCallback<>();
user.checkNameCollisions(callback);
callback.get();
SyncUtils.<Void>runBlocking(v -> finalUser.checkNameCollisions(v));
}
user.getLoginInfo(actorServer, userIp, (loginInfo, error) -> {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
}
JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<NotificationTemplate> 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"));

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
}
user.startPasswordReset();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(),
"passwordResetToken", user.getPasswordResetToken()
);
BlockingCallback<NotificationTemplate> 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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
return;
}
BlockingCallback<User> 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<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(),
@ -72,9 +67,8 @@ public final class POSTUsersIdRegisterEmail implements Handler<RoutingContext> {
"emailToken", user.getPendingEmailToken()
);
BlockingCallback<NotificationTemplate> 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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
return;
}
BlockingCallback<User> 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<PhoneIntel> 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<RoutingContext> {
}
user.startPhoneRegistration(phone);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(),
@ -78,9 +71,8 @@ public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> {
"phoneToken", user.getPendingPhoneToken()
);
BlockingCallback<NotificationTemplate> 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) {

View File

@ -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<RoutingContext> {
public void handle(RoutingContext ctx) {
BlockingCallback<User> 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<RoutingContext> {
if (TotpUtils.authorizeUser(secret, totpCode)) {
user.setTotpSecret(secret);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
user.save(callback);
callback.get();
SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_SETUP_TOTP, (ignored, error) -> {
if (error != null) {

View File

@ -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<T> implements SingleResultCallback<T> {
private final SettableFuture<T> 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);
}
}
}

View File

@ -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> T runBlocking(BlockingWrapper<T> wrapper) {
SettableFuture<T> 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<T> {
void run(SingleResultCallback<T> v);
}
}