use of org.javacord.core.entity.auditlog.AuditLogImpl in project Javacord by BtoBastian.
the class ServerImpl method getAuditLogBefore.
@Override
public CompletableFuture<AuditLog> getAuditLogBefore(int limit, AuditLogEntry before, AuditLogActionType type) {
CompletableFuture<AuditLog> future = new CompletableFuture<>();
api.getThreadPool().getExecutorService().submit(() -> {
try {
AuditLogImpl auditLog = new AuditLogImpl(this);
boolean requestMore = true;
while (requestMore) {
int requestAmount = limit - auditLog.getEntries().size();
requestAmount = Math.min(requestAmount, 100);
RestRequest<JsonNode> request = new RestRequest<JsonNode>(getApi(), RestMethod.GET, RestEndpoint.AUDIT_LOG).setUrlParameters(getIdAsString()).addQueryParameter("limit", String.valueOf(requestAmount));
List<AuditLogEntry> lastAuditLogEntries = auditLog.getEntries();
if (!lastAuditLogEntries.isEmpty()) {
// It's not the first request, so append a "before"
request.addQueryParameter("before", lastAuditLogEntries.get(lastAuditLogEntries.size() - 1).getIdAsString());
} else if (before != null) {
// It's the first request, and we have a non-null "before" parameter
request.addQueryParameter("before", before.getIdAsString());
}
if (type != null) {
request.addQueryParameter("action_type", String.valueOf(type.getValue()));
}
JsonNode data = request.execute(RestRequestResult::getJsonBody).join();
// Add the new entries
auditLog.addEntries(data);
// Check if we have to make another request
requestMore = auditLog.getEntries().size() < limit && data.get("audit_log_entries").size() >= requestAmount;
}
future.complete(auditLog);
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}
Aggregations