Search in sources :

Example 1 with AuditLogEntry

use of org.javacord.api.entity.auditlog.AuditLogEntry 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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(org.javacord.core.util.rest.RestRequest) AuditLogEntry(org.javacord.api.entity.auditlog.AuditLogEntry) AuditLogImpl(org.javacord.core.entity.auditlog.AuditLogImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) AuditLog(org.javacord.api.entity.auditlog.AuditLog) RestEndpoint(org.javacord.core.util.rest.RestEndpoint)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AuditLog (org.javacord.api.entity.auditlog.AuditLog)1 AuditLogEntry (org.javacord.api.entity.auditlog.AuditLogEntry)1 AuditLogImpl (org.javacord.core.entity.auditlog.AuditLogImpl)1 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)1 RestRequest (org.javacord.core.util.rest.RestRequest)1