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 com.mongodb.Block;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.model.Grant; 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.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -59,9 +59,7 @@ public final class GrantConverter implements Block<Document> {
-1 -1
); );
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> created.insert(v));
created.insert(callback);
callback.get();
log.info("Created grant " + created.getId() + " (" + created.getRank() + ")"); 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 com.mongodb.Block;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.model.IpLogEntry; import net.frozenorb.apiv3.model.IpLogEntry;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.IpUtils; import net.frozenorb.apiv3.util.IpUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -51,9 +51,7 @@ public final class IpLogConverter implements Block<Document> {
((Number) ipLogEntry.get("uses")).intValue() ((Number) ipLogEntry.get("uses")).intValue()
); );
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> created.insert(v));
created.insert(callback);
callback.get();
log.info("Created ip log entry " + created.getId() + " (" + created.getUser() + " - " + created.getUserIp() + ")"); 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 lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.actor.ActorType;
import net.frozenorb.apiv3.model.Punishment; 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.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -54,9 +54,7 @@ public final class PunishmentConverter implements Block<Document> {
punishment.containsKey("removedBy") ? punishment.getString("removalReason").toString() : null punishment.containsKey("removedBy") ? punishment.getString("removalReason").toString() : null
); );
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> created.insert(v));
created.insert(callback);
callback.get();
log.info("Created punishment " + created.getId() + " (" + created.getType() + ")"); 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 com.mongodb.Block;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.model.User; 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 net.frozenorb.apiv3.util.UuidUtils;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -56,15 +56,15 @@ public final class UserConverter implements Block<Document> {
null, null,
null, null,
null, null,
null,
null,
"INVALID", "INVALID",
user.getDate("joined").toInstant(), user.getDate("joined").toInstant(),
user.getDate("joined").toInstant(), user.getDate("joined").toInstant(),
false false
); );
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> created.insert(v));
created.insert(callback);
callback.get();
log.info("Created user " + created.getLastUsername() + " (" + created.getId() + ")"); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.AccessToken;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEAccessTokensId implements Handler<RoutingContext> { public final class DELETEAccessTokensId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<AccessToken> accessTokenCallback = new BlockingCallback<>(); AccessToken accessToken = SyncUtils.runBlocking(v -> AccessToken.findById(ctx.request().getParam("accessToken"), v));
AccessToken.findById(ctx.request().getParam("accessToken"), accessTokenCallback);
AccessToken accessToken = accessTokenCallback.get();
if (accessToken == null) { if (accessToken == null) {
ErrorUtils.respondNotFound(ctx, "Access token", ctx.request().getParam("accessToken")); ErrorUtils.respondNotFound(ctx, "Access token", ctx.request().getParam("accessToken"));
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> accessToken.delete(v));
accessToken.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
if (requestBody.containsKey("removedBy")) { 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.APIv3;
import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.AccessToken;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
public final class GETAccessTokens implements Handler<RoutingContext> { public final class GETAccessTokens implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("user"), v));
User.findById(ctx.request().getParam("user"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("user")); 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")); int code = Integer.parseInt(ctx.request().getParam("totpCode"));
BlockingCallback<TotpAuthorizationResult> totpAuthorizationCallback = new BlockingCallback<>(); TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> user.checkTotpAuthorization(code, null, v));
user.checkTotpAuthorization(code, null, totpAuthorizationCallback);
TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get();
if (!totpAuthorizationResult.isAuthorized()) { if (!totpAuthorizationResult.isAuthorized()) {
ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.AccessToken;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.List; import java.util.List;
@ -23,10 +23,7 @@ public final class POSTAccessTokens implements Handler<RoutingContext> {
String actorName = requestBody.getString("actorName"); String actorName = requestBody.getString("actorName");
ActorType actorType = ActorType.valueOf(requestBody.getString("actorType").toUpperCase()); ActorType actorType = ActorType.valueOf(requestBody.getString("actorType").toUpperCase());
List<String> lockedIps = (List<String>) requestBody.getJsonArray("lockedIps").getList(); List<String> lockedIps = (List<String>) requestBody.getJsonArray("lockedIps").getList();
User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v));
BlockingCallback<User> userCallback = new BlockingCallback<>();
User.findById(requestBody.getString("addedBy"), userCallback);
User addedBy = userCallback.get();
if (addedBy == null) { if (addedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("addedBy")); ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("addedBy"));
@ -34,9 +31,7 @@ public final class POSTAccessTokens implements Handler<RoutingContext> {
} }
int code = requestBody.getInteger("totpCode"); int code = requestBody.getInteger("totpCode");
BlockingCallback<TotpAuthorizationResult> totpAuthorizationCallback = new BlockingCallback<>(); TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> addedBy.checkTotpAuthorization(code, null, v));
addedBy.checkTotpAuthorization(code, null, totpAuthorizationCallback);
TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get();
if (!totpAuthorizationResult.isAuthorized()) { if (!totpAuthorizationResult.isAuthorized()) {
ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); 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); AccessToken accessToken = new AccessToken(actorName, actorType, lockedIps);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> accessToken.insert(v));
accessToken.insert(callback);
callback.get();
AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.ACCESS_TOKEN_CREATE, ImmutableMap.of("accessTokenActorName", actorName), (ignored, error) -> { AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.ACCESS_TOKEN_CREATE, ImmutableMap.of("accessTokenActorName", actorName), (ignored, error) -> {
if (error != null) { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AuditLogEntry; import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEAuditLogId implements Handler<RoutingContext> { public final class DELETEAuditLogId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<AuditLogEntry> auditLogEntryCallback = new BlockingCallback<>(); AuditLogEntry auditLogEntry = SyncUtils.runBlocking(v -> AuditLogEntry.findById(ctx.request().getParam("auditLogEntryId"), v));
AuditLogEntry.findById(ctx.request().getParam("auditLogEntryId"), auditLogEntryCallback);
AuditLogEntry auditLogEntry = auditLogEntryCallback.get();
if (auditLogEntry == null) { if (auditLogEntry == null) {
ErrorUtils.respondNotFound(ctx, "Audit log entry", ctx.request().getParam("auditLogEntryId")); ErrorUtils.respondNotFound(ctx, "Audit log entry", ctx.request().getParam("auditLogEntryId"));
@ -29,9 +27,7 @@ public final class DELETEAuditLogId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> auditLogEntry.getType().reverse(auditLogEntry, v));
auditLogEntry.getType().reverse(auditLogEntry, callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.BannedAsn; import net.frozenorb.apiv3.model.BannedAsn;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEBannedAsnsId implements Handler<RoutingContext> { public final class DELETEBannedAsnsId implements Handler<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEBannedAsnsId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> bannedAsn.delete(v));
bannedAsn.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.BannedAsn; import net.frozenorb.apiv3.model.BannedAsn;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTBannedAsns implements Handler<RoutingContext> { public final class POSTBannedAsns implements Handler<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTBannedAsns implements Handler<RoutingContext> {
String note = requestBody.getString("note"); String note = requestBody.getString("note");
BannedAsn bannedAsn = new BannedAsn(id, note); BannedAsn bannedAsn = new BannedAsn(id, note);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> bannedAsn.insert(v));
bannedAsn.insert(callback);
callback.get();
if (requestBody.containsKey("addedBy")) { 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) -> { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.BannedCellCarrier; import net.frozenorb.apiv3.model.BannedCellCarrier;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEBannedCellCarriersId implements Handler<RoutingContext> { public final class DELETEBannedCellCarriersId implements Handler<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEBannedCellCarriersId implements Handler<RoutingContext>
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> bannedCellCarrier.delete(v));
bannedCellCarrier.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.BannedCellCarrier; import net.frozenorb.apiv3.model.BannedCellCarrier;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTBannedCellCarriers implements Handler<RoutingContext> { public final class POSTBannedCellCarriers implements Handler<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTBannedCellCarriers implements Handler<RoutingContext> {
String note = requestBody.getString("note"); String note = requestBody.getString("note");
BannedCellCarrier bannedCellCarrier = new BannedCellCarrier(id, note); BannedCellCarrier bannedCellCarrier = new BannedCellCarrier(id, note);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> bannedCellCarrier.insert(v));
bannedCellCarrier.insert(callback);
callback.get();
if (requestBody.containsKey("addedBy")) { 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) -> { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class POSTEmailTokensIdConfirm implements Handler<RoutingContext> { public final class POSTEmailTokensIdConfirm implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findByEmailToken(ctx.request().getParam("emailToken"), v));
User.findByEmailToken(ctx.request().getParam("emailToken"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "Email token", ctx.request().getParam("emailToken")); 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.completeEmailRegistration(user.getPendingEmail());
user.updatePassword(password); user.updatePassword(password);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_EMAIL, (ignored, error) -> { AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_EMAIL, (ignored, error) -> {
if (error != null) { 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.Grant; import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
public final class DELETEGrantsId implements Handler<RoutingContext> { public final class DELETEGrantsId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<Grant> grantCallback = new BlockingCallback<>(); Grant grant = SyncUtils.runBlocking(v -> Grant.findById(ctx.request().getParam("grantId"), v));
Grant.findById(ctx.request().getParam("grantId"), grantCallback);
Grant grant = grantCallback.get();
if (grant == null) { if (grant == null) {
ErrorUtils.respondNotFound(ctx, "Grant", ctx.request().getParam("grantId")); ErrorUtils.respondNotFound(ctx, "Grant", ctx.request().getParam("grantId"));
@ -29,9 +27,7 @@ public final class DELETEGrantsId implements Handler<RoutingContext> {
} }
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> userCallback = new BlockingCallback<>(); User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
User.findById(requestBody.getString("removedBy"), userCallback);
User removedBy = userCallback.get();
if (removedBy == null) { if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
@ -45,9 +41,7 @@ public final class DELETEGrantsId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> grant.delete(removedBy, reason, v));
grant.delete(removedBy, reason, callback);
callback.get();
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.GRANT_DELETE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> { AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.GRANT_DELETE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> {
if (error != null) { 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.Rank;
import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.time.Instant; import java.time.Instant;
import java.util.HashSet; import java.util.HashSet;
@ -24,9 +24,7 @@ public final class POSTGrants implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> userCallback = new BlockingCallback<>(); User target = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("user"), v));
User.findById(requestBody.getString("user"), userCallback);
User target = userCallback.get();
if (target == null) { if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("user")); 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. // We purposely don't fail on a null check, grants don't have to have a source.
BlockingCallback<User> addedByCallback = new BlockingCallback<>(); User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v));
User.findById(requestBody.getString("addedBy"), addedByCallback);
User addedBy = addedByCallback.get();
if (addedBy != null && rank.isHigherStaffRank()) { if (addedBy != null && rank.isHigherStaffRank()) {
int code = requestBody.getInteger("totpCode"); int code = requestBody.getInteger("totpCode");
BlockingCallback<TotpAuthorizationResult> totpAuthorizationCallback = new BlockingCallback<>(); TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> addedBy.checkTotpAuthorization(code, null, v));
addedBy.checkTotpAuthorization(code, null, totpAuthorizationCallback);
TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get();
if (!totpAuthorizationResult.isAuthorized()) { if (!totpAuthorizationResult.isAuthorized()) {
ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); 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); int storeOrderId = requestBody.getInteger("storeOrderId", -1);
Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy, storeItemId, storeOrderId); Grant grant = new Grant(target, reason, scopes, rank, expiresAt, addedBy, storeItemId, storeOrderId);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> grant.insert(v));
grant.insert(callback);
callback.get();
if (addedBy != null) { if (addedBy != null) {
AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.GRANT_CREATE, ImmutableMap.of("grantId", grant.getId()), (ignored, error) -> { 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.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.IpBan;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
public final class DELETEIpBansId implements Handler<RoutingContext> { public final class DELETEIpBansId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<IpBan> ipBanCallback = new BlockingCallback<>(); IpBan ipBan = SyncUtils.runBlocking(v -> IpBan.findById(ctx.request().getParam("ipBanId"), v));
IpBan.findById(ctx.request().getParam("ipBanId"), ipBanCallback);
IpBan ipBan = ipBanCallback.get();
if (ipBan == null) { if (ipBan == null) {
ErrorUtils.respondNotFound(ctx, "IpBan", ctx.request().getParam("id")); ErrorUtils.respondNotFound(ctx, "IpBan", ctx.request().getParam("id"));
@ -30,9 +27,7 @@ public final class DELETEIpBansId implements Handler<RoutingContext> {
} }
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> userCallback = new BlockingCallback<>(); User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
User.findById(requestBody.getString("removedBy"), userCallback);
User removedBy = userCallback.get();
if (removedBy == null) { if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
@ -46,13 +41,15 @@ public final class DELETEIpBansId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> ipBan.delete(removedBy, reason, v));
ipBan.delete(removedBy, reason, callback);
callback.get(); AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), (ignored, error) -> {
BlockingCallback<AuditLogEntry> blockingCallback = new BlockingCallback<>(); if (error != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.IP_BAN_DELETE, ImmutableMap.of("punishmentId", ipBan.getId()), blockingCallback); ErrorUtils.respondInternalError(ctx, error);
blockingCallback.get(); } else {
APIv3.respondJson(ctx, 200, ipBan); 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.IpBan; import net.frozenorb.apiv3.model.IpBan;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.IpUtils; import net.frozenorb.apiv3.util.IpUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.time.Instant; 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. // We purposely don't do a null check, ip bans don't have to have a source.
BlockingCallback<User> userCallback = new BlockingCallback<>(); User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v));
User.findById(requestBody.getString("addedBy"), userCallback);
User addedBy = userCallback.get();
IpBan ipBan = new IpBan(userIp, reason, expiresAt, addedBy, ctx.get("actor")); IpBan ipBan = new IpBan(userIp, reason, expiresAt, addedBy, ctx.get("actor"));
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> ipBan.insert(v));
ipBan.insert(callback);
callback.get();
if (addedBy != null) { if (addedBy != null) {
AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.IP_BAN_CREATE, ImmutableMap.of("ipBanId", ipBan.getId()), (ignored, error) -> { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETENotificationTemplatesId implements Handler<RoutingContext> { public final class DELETENotificationTemplatesId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<NotificationTemplate> notificationTemplateCallback = new BlockingCallback<>(); NotificationTemplate notificationTemplate = SyncUtils.runBlocking(v -> NotificationTemplate.findById(ctx.request().getParam("notificationTemplateId"), v));
NotificationTemplate.findById(ctx.request().getParam("notificationTemplateId"), notificationTemplateCallback);
NotificationTemplate notificationTemplate = notificationTemplateCallback.get();
if (notificationTemplate == null) { if (notificationTemplate == null) {
ErrorUtils.respondNotFound(ctx, "Notification template", ctx.request().getParam("notificationTemplateId")); ErrorUtils.respondNotFound(ctx, "Notification template", ctx.request().getParam("notificationTemplateId"));
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> notificationTemplate.delete(v));
notificationTemplate.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTNotificationTemplates implements Handler<RoutingContext> { public final class POSTNotificationTemplates implements Handler<RoutingContext> {
@ -21,9 +21,7 @@ public final class POSTNotificationTemplates implements Handler<RoutingContext>
String body = requestBody.getString("body"); String body = requestBody.getString("body");
NotificationTemplate notificationTemplate = new NotificationTemplate(id, subject, body); NotificationTemplate notificationTemplate = new NotificationTemplate(id, subject, body);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> notificationTemplate.insert(v));
notificationTemplate.insert(callback);
callback.get();
if (requestBody.containsKey("addedBy")) { 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) -> { 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.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
public final class DELETEPunishmentsId implements Handler<RoutingContext> { public final class DELETEPunishmentsId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<Punishment> punishmentCallback = new BlockingCallback<>(); Punishment punishment = SyncUtils.runBlocking(v -> Punishment.findById(ctx.request().getParam("punishmentId"), v));
Punishment.findById(ctx.request().getParam("punishmentId"), punishmentCallback);
Punishment punishment = punishmentCallback.get();
if (punishment == null) { if (punishment == null) {
ErrorUtils.respondNotFound(ctx, "Punishment", ctx.request().getParam("punishmentId")); ErrorUtils.respondNotFound(ctx, "Punishment", ctx.request().getParam("punishmentId"));
@ -30,9 +27,7 @@ public final class DELETEPunishmentsId implements Handler<RoutingContext> {
} }
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> removedByCallback = new BlockingCallback<>(); User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
User.findById(requestBody.getString("removedBy"), removedByCallback);
User removedBy = removedByCallback.get();
if (removedBy == null) { if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
@ -46,13 +41,15 @@ public final class DELETEPunishmentsId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<UpdateResult> removeCallback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> punishment.delete(removedBy, reason, v));
punishment.delete(removedBy, reason, removeCallback);
removeCallback.get(); AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), (ignored, error) -> {
BlockingCallback<AuditLogEntry> blockingCallback = new BlockingCallback<>(); if (error != null) {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), blockingCallback); ErrorUtils.respondInternalError(ctx, error);
blockingCallback.get(); } else {
APIv3.respondJson(ctx, 200, punishment); 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.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.List; import java.util.List;
public final class DELETEUsersIdActivePunishment implements Handler<RoutingContext> { public final class DELETEUsersIdActivePunishment implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User target = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("id"), v));
User.findById(ctx.request().getParam("id"), userCallback);
User target = userCallback.get();
if (target == null) { if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id"));
@ -31,10 +28,7 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type").toUpperCase()); Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type").toUpperCase());
User removedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("removedBy"), v));
BlockingCallback<User> removedByCallback = new BlockingCallback<>();
User.findById(requestBody.getString("removedBy"), removedByCallback);
User removedBy = removedByCallback.get();
if (removedBy == null) { if (removedBy == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy")); ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("removedBy"));
@ -48,11 +42,10 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
return; return;
} }
BlockingCallback<List<Punishment>> callback = new BlockingCallback<>(); List<Punishment> punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v));
Punishment.findByUserAndType(target, ImmutableSet.of(type), callback);
Punishment activePunishment = null; Punishment activePunishment = null;
for (Punishment punishment : callback.get()) { for (Punishment punishment : punishments) {
if (punishment.isActive()) { if (punishment.isActive()) {
activePunishment = punishment; activePunishment = punishment;
break; break;
@ -64,15 +57,16 @@ public final class DELETEUsersIdActivePunishment implements Handler<RoutingConte
return; return;
} }
BlockingCallback<UpdateResult> punishmentCallback = new BlockingCallback<>(); Punishment finalActivePunishment = activePunishment;
activePunishment.delete(removedBy, reason, punishmentCallback); SyncUtils.<UpdateResult>runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v));
punishmentCallback.get();
BlockingCallback<AuditLogEntry> blockingCallback = new BlockingCallback<>(); AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), (ignored, error) -> {
AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), blockingCallback); if (error != null) {
blockingCallback.get(); ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, activePunishment); 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.IpBan;
import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.Permissions; import net.frozenorb.apiv3.unsorted.Permissions;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
@ -23,9 +23,7 @@ public final class POSTPunishments implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> userCallback = new BlockingCallback<>(); User target = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("user"), v));
User.findById(requestBody.getString("user"), userCallback);
User target = userCallback.get();
if (target == null) { if (target == null) {
ErrorUtils.respondNotFound(ctx, "User", requestBody.getString("user")); 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")); Punishment.PunishmentType type = Punishment.PunishmentType.valueOf(requestBody.getString("type"));
if (type != Punishment.PunishmentType.WARN) { if (type != Punishment.PunishmentType.WARN) {
BlockingCallback<List<Punishment>> punishmentsCallback = new BlockingCallback<>(); List<Punishment> punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v));
Punishment.findByUserAndType(target, ImmutableSet.of(type), punishmentsCallback);
for (Punishment alternatePunishment : punishmentsCallback.get()) { for (Punishment alternatePunishment : punishments) {
if (alternatePunishment.isActive()) { if (alternatePunishment.isActive()) {
BlockingCallback<User> addedByCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(alternatePunishment.getAddedBy(), v));
User.findById(alternatePunishment.getAddedBy(), addedByCallback); ErrorUtils.respondOther(ctx, 409, "User already covered by alternate punishment.", "alreadyCoveredByAlternatePunishment", ImmutableMap.of("alternatePunishmentBy", user.getLastUsername()));
ErrorUtils.respondOther(ctx, 409, "User already covered by alternate punishment.", "alreadyCoveredByAlternatePunishment", ImmutableMap.of("alternatePunishmentBy", addedByCallback.get().getLastUsername()));
return; 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. // We purposely don't do a null check, punishments don't have to have a source.
BlockingCallback<User> addedByCallback = new BlockingCallback<>(); User addedBy = SyncUtils.runBlocking(v -> User.findById(requestBody.getString("addedBy"), v));
User.findById(requestBody.getString("addedBy"), addedByCallback); boolean isProtected = SyncUtils.runBlocking(v -> target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT, v));
User addedBy = addedByCallback.get();
BlockingCallback<Boolean> permissionsCallback = new BlockingCallback<>(); if (isProtected) {
target.hasPermissionAnywhere(Permissions.PROTECTED_PUNISHMENT, permissionsCallback);
if (permissionsCallback.get()) {
ErrorUtils.respondOther(ctx, 409, "User is protected from punishments.", "protectedFromPunishments", ImmutableMap.of()); ErrorUtils.respondOther(ctx, 409, "User is protected from punishments.", "protectedFromPunishments", ImmutableMap.of());
return; return;
} }
@ -99,16 +91,12 @@ public final class POSTPunishments implements Handler<RoutingContext> {
if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) { if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) {
IpBan ipBan = new IpBan(userIp, punishment); IpBan ipBan = new IpBan(userIp, punishment);
BlockingCallback<Void> ipBanCallback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> ipBan.insert(v));
ipBan.insert(ipBanCallback);
ipBanCallback.get();
punishment.linkIpBan(ipBan); punishment.linkIpBan(ipBan);
} }
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> punishment.insert(v));
punishment.insert(callback);
callback.get();
if (addedBy != null) { if (addedBy != null) {
AuditLog.log(addedBy.getId(), requestBody.getString("addedByIp"), ctx, AuditLogActionType.PUNISHMENT_CREATE, ImmutableMap.of("punishmentId", punishment.getId()), (ignored, error) -> { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.Rank;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETERanksId implements Handler<RoutingContext> { public final class DELETERanksId implements Handler<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETERanksId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> rank.delete(v));
rank.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.Rank;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTRanks implements Handler<RoutingContext> { public final class POSTRanks implements Handler<RoutingContext> {
@ -26,9 +26,7 @@ public final class POSTRanks implements Handler<RoutingContext> {
boolean higherStaffRank = requestBody.getBoolean("higherStaffRank"); boolean higherStaffRank = requestBody.getBoolean("higherStaffRank");
Rank rank = new Rank(id, inheritsFromId, weight, displayName, gameColor, websiteColor, staffRank, higherStaffRank); Rank rank = new Rank(id, inheritsFromId, weight, displayName, gameColor, websiteColor, staffRank, higherStaffRank);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> rank.insert(v));
rank.insert(callback);
callback.get();
if (requestBody.containsKey("addedBy")) { if (requestBody.containsKey("addedBy")) {
AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.RANK_CREATE, ImmutableMap.of("rankId", id), (ignored, error) -> { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEServerGroupsId implements Handler<RoutingContext> { public final class DELETEServerGroupsId implements Handler<RoutingContext> {
@ -23,9 +23,7 @@ public final class DELETEServerGroupsId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<DeleteResult> callback = new BlockingCallback<>(); SyncUtils.<DeleteResult>runBlocking(v -> serverGroup.delete(v));
serverGroup.delete(callback);
callback.get();
JsonObject requestBody = ctx.getBodyAsJson(); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTServerGroups implements Handler<RoutingContext> { public final class POSTServerGroups implements Handler<RoutingContext> {
@ -20,9 +20,7 @@ public final class POSTServerGroups implements Handler<RoutingContext> {
String image = requestBody.getString("image"); String image = requestBody.getString("image");
ServerGroup serverGroup = new ServerGroup(id, image); ServerGroup serverGroup = new ServerGroup(id, image);
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> serverGroup.insert(v));
serverGroup.insert(callback);
callback.get();
if (requestBody.containsKey("addedBy")) { 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) -> { 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; package net.frozenorb.apiv3.route.servers;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.mongodb.client.result.DeleteResult;
import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext; 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.AccessToken;
import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEServersId implements Handler<RoutingContext> { public final class DELETEServersId implements Handler<RoutingContext> {
@ -25,21 +24,25 @@ public final class DELETEServersId implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<DeleteResult> deleteServerCallback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> server.insert(v));
server.delete(deleteServerCallback);
deleteServerCallback.get();
BlockingCallback<DeleteResult> deleteAccessTokenCallback = new BlockingCallback<>(); SyncUtils.runBlocking(v -> {
AccessToken.findByNameAndType(server.getId(), ActorType.SERVER, (accessToken, error) -> { AccessToken.findByNameAndType(server.getId(), ActorType.SERVER, (accessToken, error) -> {
if (error != null) { if (error != null) {
deleteAccessTokenCallback.onResult(null, error); v.onResult(null, error);
} else if (accessToken != null) { } else if (accessToken != null) {
accessToken.delete(deleteServerCallback); accessToken.delete((ignored, error2) -> {
} else { if (error2 != null) {
deleteAccessTokenCallback.onResult(null, new NullPointerException("Access token not found.")); 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(); 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.AccessToken;
import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.IpUtils; import net.frozenorb.apiv3.util.IpUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTServers implements Handler<RoutingContext> { 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); Server server = new Server(id, displayName, group, ip);
BlockingCallback<Void> insertServerCallback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> server.insert(v));
server.insert(insertServerCallback);
insertServerCallback.get();
AccessToken accessToken = new AccessToken(server); AccessToken accessToken = new AccessToken(server);
BlockingCallback<Void> insertAccessTokenCallback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> accessToken.insert(v));
accessToken.insert(insertAccessTokenCallback);
insertAccessTokenCallback.get();
if (requestBody.containsKey("addedBy")) { if (requestBody.containsKey("addedBy")) {
AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.SERVER_CREATE, ImmutableMap.of("serverId", id), (ignored, error) -> { 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.Grant;
import net.frozenorb.apiv3.model.Rank; import net.frozenorb.apiv3.model.Rank;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.util.SyncUtils;
import java.util.*; import java.util.*;
@ -28,15 +28,12 @@ public final class GETStaff implements Handler<RoutingContext> {
return Integer.compare(firstRank.getWeight(), secondRank.getWeight()); return Integer.compare(firstRank.getWeight(), secondRank.getWeight());
}); });
BlockingCallback<List<Grant>> grantsCallback = new BlockingCallback<>(); List<Grant> staffGrants = SyncUtils.runBlocking(v -> Grant.findByRank(staffRanks.values(), v));
Grant.findByRank(staffRanks.values(), grantsCallback);
grantsCallback.get().forEach(grant -> { for (Grant staffGrant : staffGrants) {
if (grant.isActive()) { if (staffGrant.isActive()) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(staffGrant.getId(), v));
User.findById(grant.getUser(), userCallback); Rank rank = staffRanks.get(staffGrant.getRank());
User user = userCallback.get();
Rank rank = staffRanks.get(grant.getRank());
if (!result.containsKey(rank.getId())) { if (!result.containsKey(rank.getId())) {
result.put(rank.getId(), new HashSet<>()); result.put(rank.getId(), new HashSet<>());
@ -44,7 +41,7 @@ public final class GETStaff implements Handler<RoutingContext> {
result.get(rank.getId()).add(user); result.get(rank.getId()).add(user);
} }
}); }
APIv3.respondJson(ctx, 200, result); 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.IpLogEntry;
import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -17,29 +17,22 @@ import java.util.Map;
public final class GETUsersIdDetails implements Handler<RoutingContext> { public final class GETUsersIdDetails implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
return; return;
} }
BlockingCallback<List<Grant>> grantsCallback = new BlockingCallback<>(); List<Grant> grants = SyncUtils.runBlocking(v -> Grant.findByUser(user, v));
BlockingCallback<List<IpLogEntry>> ipLogCallback = new BlockingCallback<>(); List<IpLogEntry> ipLog = SyncUtils.runBlocking(v -> IpLogEntry.findByUser(user, v));
BlockingCallback<List<Punishment>> punishmentsCallback = new BlockingCallback<>(); List<Punishment> punishments = SyncUtils.runBlocking(v -> Punishment.findByUser(user, v));
Grant.findByUser(user, grantsCallback);
IpLogEntry.findByUser(user, ipLogCallback);
Punishment.findByUser(user, punishmentsCallback);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("user", user); result.put("user", user);
result.put("grants", grantsCallback.get()); result.put("grants", grants);
result.put("ipLog", ipLogCallback.get()); result.put("ipLog", ipLog);
result.put("punishments", punishmentsCallback.get()); result.put("punishments", punishments);
result.put("aliases", user.getAliases()); result.put("aliases", user.getAliases());
result.put("totpSetup", user.getTotpSecret() != null); 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.UUID; import java.util.UUID;
public final class GETUsersIdVerifyPassword implements Handler<RoutingContext> { public final class GETUsersIdVerifyPassword implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> callback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), callback);
User user = callback.get();
if (user == null) { if (user == null) {
callback = new BlockingCallback<>(); user = SyncUtils.runBlocking(v -> User.findByLastUsername(ctx.request().getParam("userId"), v));
User.findByLastUsername(ctx.request().getParam("userId"), callback);
user = callback.get();
} }
if (user == null) { if (user == null) {
callback = new BlockingCallback<>(); user = SyncUtils.runBlocking(v -> User.findByEmail(ctx.request().getParam("userId"), v));
User.findByEmail(ctx.request().getParam("userId"), callback);
user = callback.get();
} }
if (user == null) { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.RequiresTotpResult; import net.frozenorb.apiv3.unsorted.RequiresTotpResult;
import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult; import net.frozenorb.apiv3.unsorted.TotpAuthorizationResult;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class POSTUsersIdChangePassword implements Handler<RoutingContext> { public final class POSTUsersIdChangePassword implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -38,15 +36,11 @@ public final class POSTUsersIdChangePassword implements Handler<RoutingContext>
boolean authorized = false; boolean authorized = false;
if (user.checkPassword(requestBody.getString("currentPassword"))) { if (user.checkPassword(requestBody.getString("currentPassword"))) {
BlockingCallback<RequiresTotpResult> totpRequiredCallback = new BlockingCallback<>(); RequiresTotpResult requiresTotp = SyncUtils.runBlocking(v -> user.requiresTotpAuthorization(null, v));
user.requiresTotpAuthorization(null, totpRequiredCallback);
RequiresTotpResult requiresTotp = totpRequiredCallback.get();
if (requiresTotp == RequiresTotpResult.REQUIRED_NO_EXEMPTIONS) { if (requiresTotp == RequiresTotpResult.REQUIRED_NO_EXEMPTIONS) {
int code = requestBody.getInteger("totpCode"); int code = requestBody.getInteger("totpCode");
BlockingCallback<TotpAuthorizationResult> totpAuthorizationCallback = new BlockingCallback<>(); TotpAuthorizationResult totpAuthorizationResult = SyncUtils.runBlocking(v -> user.checkTotpAuthorization(code, null, v));
user.checkTotpAuthorization(code, null, totpAuthorizationCallback);
TotpAuthorizationResult totpAuthorizationResult = totpAuthorizationCallback.get();
if (!totpAuthorizationResult.isAuthorized()) { if (!totpAuthorizationResult.isAuthorized()) {
ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name()); ErrorUtils.respondInvalidInput(ctx, "Totp authorization failed: " + totpAuthorizationResult.name());
@ -77,9 +71,7 @@ public final class POSTUsersIdChangePassword implements Handler<RoutingContext>
} }
user.updatePassword(newPassword); user.updatePassword(newPassword);
BlockingCallback<UpdateResult> saveCallback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(saveCallback);
saveCallback.get();
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CHANGE_PASSWORD, (ignored, error) -> { AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CHANGE_PASSWORD, (ignored, error) -> {
if (error != null) { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class POSTUsersIdConfirmPhone implements Handler<RoutingContext> { public final class POSTUsersIdConfirmPhone implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("id"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("id")); 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())) { if (!String.valueOf(phoneCode).equals(user.getPendingPhoneToken())) {
user.failedPhoneRegistration(); user.failedPhoneRegistration();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
ErrorUtils.respondOther(ctx, 409, "Phone token doesn't match", "phoneTokenNoMatch", ImmutableMap.of()); ErrorUtils.respondOther(ctx, 409, "Phone token doesn't match", "phoneTokenNoMatch", ImmutableMap.of());
return; return;
} }
user.completePhoneRegistration(user.getPendingPhone()); user.completePhoneRegistration(user.getPendingPhone());
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_PHONE, (ignored, error) -> { AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_CONFIRM_PHONE, (ignored, error) -> {
if (error != null) { 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.IpLogEntry;
import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.IpUtils; import net.frozenorb.apiv3.util.IpUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils; import net.frozenorb.apiv3.util.UuidUtils;
import java.util.UUID; import java.util.UUID;
@ -29,9 +29,7 @@ public final class POSTUsersIdLogin implements Handler<RoutingContext> {
} }
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(uuid, v));
User.findById(uuid, userCallback);
User user = userCallback.get();
String currentUsername = requestBody.getString("username"); String currentUsername = requestBody.getString("username");
String userIp = requestBody.getString("userIp"); String userIp = requestBody.getString("userIp");
Actor actor = ctx.get("actor"); Actor actor = ctx.get("actor");
@ -50,40 +48,35 @@ public final class POSTUsersIdLogin implements Handler<RoutingContext> {
if (user == null) { if (user == null) {
user = new User(uuid, currentUsername); user = new User(uuid, currentUsername);
BlockingCallback<Void> nameCollisionCallback = new BlockingCallback<>(); User finalUser = user;
user.checkNameCollisions(nameCollisionCallback);
nameCollisionCallback.get(); SyncUtils.<Void>runBlocking(v -> finalUser.checkNameCollisions(v));
BlockingCallback<Void> insertCallback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> finalUser.insert(v));
user.insert(insertCallback);
insertCallback.get();
} }
BlockingCallback<IpLogEntry> ipLogEntryCallback = new BlockingCallback<>(); User finalUser = user;
IpLogEntry.findByUserAndIp(user, userIp, ipLogEntryCallback); IpLogEntry ipLogEntry = SyncUtils.runBlocking(v -> IpLogEntry.findByUserAndIp(finalUser, userIp, v));
IpLogEntry ipLogEntry = ipLogEntryCallback.get();
// We use a little bit more verbose code here to save on the // We use a little bit more verbose code here to save on the
// overhead of a .insert() immediately followed by a .save() // overhead of a .insert() immediately followed by a .save()
if (ipLogEntry == null) { if (ipLogEntry == null) {
ipLogEntry = new IpLogEntry(user, userIp); ipLogEntry = new IpLogEntry(user, userIp);
ipLogEntry.used(); ipLogEntry.used();
BlockingCallback<Void> callback = new BlockingCallback<>();
ipLogEntry.insert(callback); IpLogEntry finalIpLogEntry = ipLogEntry;
callback.get(); SyncUtils.<Void>runBlocking(v -> finalIpLogEntry.insert(v));
} else { } else {
ipLogEntry.used(); ipLogEntry.used();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
ipLogEntry.save(callback); IpLogEntry finalIpLogEntry = ipLogEntry;
callback.get(); SyncUtils.<UpdateResult>runBlocking(v -> finalIpLogEntry.save(v));
} }
String lastUsername = user.getLastUsername(); String lastUsername = user.getLastUsername();
user.updateUsername(currentUsername); user.updateUsername(currentUsername);
if (!currentUsername.equals(lastUsername)) { if (!currentUsername.equals(lastUsername)) {
BlockingCallback<Void> callback = new BlockingCallback<>(); SyncUtils.<Void>runBlocking(v -> finalUser.checkNameCollisions(v));
user.checkNameCollisions(callback);
callback.get();
} }
user.getLoginInfo(actorServer, userIp, (loginInfo, error) -> { 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.APIv3;
import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.unsorted.Notification;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.Map; import java.util.Map;
public final class POSTUsersIdNotify implements Handler<RoutingContext> { public final class POSTUsersIdNotify implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -31,9 +29,7 @@ public final class POSTUsersIdNotify implements Handler<RoutingContext> {
} }
JsonObject requestBody = ctx.getBodyAsJson(); JsonObject requestBody = ctx.getBodyAsJson();
BlockingCallback<NotificationTemplate> notificationTemplateCallback = new BlockingCallback<>(); NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById(requestBody.getString("template"), v));
NotificationTemplate.findById(requestBody.getString("template"), notificationTemplateCallback);
NotificationTemplate template = notificationTemplateCallback.get();
if (template == null) { if (template == null) {
ErrorUtils.respondNotFound(ctx, "Notification template", requestBody.getString("template")); 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.unsorted.Notification;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -20,9 +20,7 @@ import java.util.concurrent.TimeUnit;
public final class POSTUsersIdPasswordReset implements Handler<RoutingContext> { public final class POSTUsersIdPasswordReset implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -35,18 +33,15 @@ public final class POSTUsersIdPasswordReset implements Handler<RoutingContext> {
} }
user.startPasswordReset(); user.startPasswordReset();
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
Map<String, Object> replacements = ImmutableMap.of( Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(), "username", user.getLastUsername(),
"passwordResetToken", user.getPasswordResetToken() "passwordResetToken", user.getPasswordResetToken()
); );
BlockingCallback<NotificationTemplate> notificationTemplateCallback = new BlockingCallback<>(); NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("password-reset", v));
NotificationTemplate.findById("password-reset", notificationTemplateCallback); Notification notification = new Notification(template, replacements, replacements);
Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements);
notification.sendAsEmail(user.getEmail(), (ignored, error) -> { notification.sendAsEmail(user.getEmail(), (ignored, error) -> {
if (error != null) { 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.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.model.NotificationTemplate;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.unsorted.Notification;
import net.frozenorb.apiv3.util.EmailUtils; import net.frozenorb.apiv3.util.EmailUtils;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -21,9 +21,7 @@ import java.util.concurrent.TimeUnit;
public final class POSTUsersIdRegisterEmail implements Handler<RoutingContext> { public final class POSTUsersIdRegisterEmail implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -53,18 +51,15 @@ public final class POSTUsersIdRegisterEmail implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<User> sameEmailCallback = new BlockingCallback<>(); User sameEmail = SyncUtils.runBlocking(v -> User.findByEmail(email, v));
User.findByEmail(email, sameEmailCallback);
if (sameEmailCallback.get() != null) { if (sameEmail != null) {
ErrorUtils.respondInvalidInput(ctx, email + " is already in use."); ErrorUtils.respondInvalidInput(ctx, email + " is already in use.");
return; return;
} }
user.startEmailRegistration(email); user.startEmailRegistration(email);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
Map<String, Object> replacements = ImmutableMap.of( Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(), "username", user.getLastUsername(),
@ -72,9 +67,8 @@ public final class POSTUsersIdRegisterEmail implements Handler<RoutingContext> {
"emailToken", user.getPendingEmailToken() "emailToken", user.getPendingEmailToken()
); );
BlockingCallback<NotificationTemplate> notificationTemplateCallback = new BlockingCallback<>(); NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("email-confirmation", v));
NotificationTemplate.findById("email-confirmation", notificationTemplateCallback); Notification notification = new Notification(template, replacements, replacements);
Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements);
notification.sendAsEmail(user.getEmail(), (ignored, error) -> { notification.sendAsEmail(user.getEmail(), (ignored, error) -> {
if (error != null) { 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.NotificationTemplate;
import net.frozenorb.apiv3.model.PhoneIntel; import net.frozenorb.apiv3.model.PhoneIntel;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.unsorted.Notification; import net.frozenorb.apiv3.unsorted.Notification;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.PhoneUtils; import net.frozenorb.apiv3.util.PhoneUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -23,9 +23,7 @@ import java.util.concurrent.TimeUnit;
public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> { public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -50,17 +48,14 @@ public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> {
return; return;
} }
BlockingCallback<User> samePhoneCallback = new BlockingCallback<>(); User samePhone = SyncUtils.runBlocking(v -> User.findByPhone(phone, v));
User.findByPhone(phone, samePhoneCallback);
if (samePhoneCallback.get() != null) { if (samePhone != null) {
ErrorUtils.respondInvalidInput(ctx, phone + " is already in use."); ErrorUtils.respondInvalidInput(ctx, phone + " is already in use.");
return; return;
} }
BlockingCallback<PhoneIntel> phoneIntelCallback = new BlockingCallback<>(); PhoneIntel phoneIntel = SyncUtils.runBlocking(v -> PhoneIntel.findOrCreateById(phone, v));
PhoneIntel.findOrCreateById(phone, phoneIntelCallback);
PhoneIntel phoneIntel = phoneIntelCallback.get();
if (BannedCellCarrier.findById(phoneIntel.getResult().getCarrierId()) != null) { if (BannedCellCarrier.findById(phoneIntel.getResult().getCarrierId()) != null) {
ErrorUtils.respondInvalidInput(ctx, phone + " is from a banned cell provider."); ErrorUtils.respondInvalidInput(ctx, phone + " is from a banned cell provider.");
@ -68,9 +63,7 @@ public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> {
} }
user.startPhoneRegistration(phone); user.startPhoneRegistration(phone);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
Map<String, Object> replacements = ImmutableMap.of( Map<String, Object> replacements = ImmutableMap.of(
"username", user.getLastUsername(), "username", user.getLastUsername(),
@ -78,9 +71,8 @@ public final class POSTUsersIdRegisterPhone implements Handler<RoutingContext> {
"phoneToken", user.getPendingPhoneToken() "phoneToken", user.getPendingPhoneToken()
); );
BlockingCallback<NotificationTemplate> notificationTemplateCallback = new BlockingCallback<>(); NotificationTemplate template = SyncUtils.runBlocking(v -> NotificationTemplate.findById("phone-confirmation", v));
NotificationTemplate.findById("phone-confirmation", notificationTemplateCallback); Notification notification = new Notification(template, replacements, replacements);
Notification notification = new Notification(notificationTemplateCallback.get(), replacements, replacements);
notification.sendAsText(user.getPendingPhone(), (ignored, error) -> { notification.sendAsText(user.getPendingPhone(), (ignored, error) -> {
if (error != null) { 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.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.TotpUtils; import net.frozenorb.apiv3.util.TotpUtils;
public final class POSTUsersIdSetupTotp implements Handler<RoutingContext> { public final class POSTUsersIdSetupTotp implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) { public void handle(RoutingContext ctx) {
BlockingCallback<User> userCallback = new BlockingCallback<>(); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
User.findById(ctx.request().getParam("userId"), userCallback);
User user = userCallback.get();
if (user == null) { if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId")); ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
@ -36,9 +34,7 @@ public final class POSTUsersIdSetupTotp implements Handler<RoutingContext> {
if (TotpUtils.authorizeUser(secret, totpCode)) { if (TotpUtils.authorizeUser(secret, totpCode)) {
user.setTotpSecret(secret); user.setTotpSecret(secret);
BlockingCallback<UpdateResult> callback = new BlockingCallback<>(); SyncUtils.<UpdateResult>runBlocking(v -> user.save(v));
user.save(callback);
callback.get();
AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_SETUP_TOTP, (ignored, error) -> { AuditLog.log(user.getId(), requestBody.getString("userIp"), ctx, AuditLogActionType.USER_SETUP_TOTP, (ignored, error) -> {
if (error != null) { 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; package net.frozenorb.apiv3.util;
import com.google.common.util.concurrent.SettableFuture;
import com.mongodb.async.SingleResultCallback; import com.mongodb.async.SingleResultCallback;
import io.vertx.core.Context; import io.vertx.core.Context;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.APIv3;
import java.util.concurrent.ExecutionException;
@UtilityClass @UtilityClass
public class SyncUtils { 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);
}
} }