diff --git a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java index a746fe8..a9d60d5 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IpLogEntry.java @@ -46,6 +46,10 @@ public final class IpLogEntry { ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); } + public static void findLatestByUser(UUID user, SingleResultCallback callback) { + ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).limit(1).first(SyncUtils.vertxWrap(callback)); + } + public static void findByIp(String userIp, SingleResultCallback> callback) { ipLogCollection.find(new Document("userIp", userIp)).into(new LinkedList<>(), SyncUtils.vertxWrap(callback)); } diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java index a024cc4..9de2081 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/POSTPunishments.java @@ -9,6 +9,7 @@ import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; import net.frozenorb.apiv3.model.IpBan; +import net.frozenorb.apiv3.model.IpLogEntry; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.unsorted.Permissions; @@ -89,6 +90,14 @@ public final class POSTPunishments implements Handler { String accessDenialReason = punishment.getAccessDenialReason(); String userIp = requestBody.getString("userIp"); + if (userIp == null) { + IpLogEntry latestIpLogEntry = SyncUtils.runBlocking(v -> IpLogEntry.findLatestByUser(target.getId(), v)); + + if (latestIpLogEntry != null) { + userIp = latestIpLogEntry.getUserIp(); + } + } + if ((type == Punishment.PunishmentType.BAN || type == Punishment.PunishmentType.BLACKLIST) && userIp != null) { IpBan ipBan = new IpBan(userIp, punishment); SyncUtils.runBlocking(v -> ipBan.insert(v));