Skip to content

Commit

Permalink
fix(binder): resolve fetching all servers from the user
Browse files Browse the repository at this point in the history
  • Loading branch information
luiz-otavio committed Dec 6, 2022
1 parent 1fbb3b6 commit d9cb029
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
38 changes: 15 additions & 23 deletions binder/src/main/java/net/luxcube/minecraft/user/PteroUserImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.luxcube.minecraft.user;

import com.mattmalec.pterodactyl4j.ClientType;
import com.mattmalec.pterodactyl4j.application.entities.ApplicationServer;
import com.mattmalec.pterodactyl4j.application.entities.ApplicationUser;
import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.exceptions.NotFoundException;
import net.luxcube.minecraft.exception.UserDoesntExistException;
import net.luxcube.minecraft.server.PteroServer;
Expand All @@ -16,6 +18,7 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* @author Luiz O. F. Corrêa
Expand Down Expand Up @@ -76,32 +79,21 @@ public PteroUserImpl(
@Override
public @NotNull CompletableFuture<List<PteroServer>> getServers() {
return CompletableFuture.supplyAsync(() -> {
Try<Optional<ApplicationUser>> catching = Try.catching(() -> {
return bridge.getApplication()
.retrieveUsersByUsername(name, true)
.timeout(5, TimeUnit.SECONDS)
.execute()
return bridge.getClient()
.retrieveServers(ClientType.OWNER)
.cache(true)
.stream()
.takeWhile(server -> server.getSubusers()
.stream()
.findFirst();
});

catching.catching(NotFoundException.class, e -> {
throw new UserDoesntExistException(name);
});

return catching.unwrap()
.orElseThrow(() -> new UserDoesntExistException(name));
}, bridge.getWorker()).thenApply(user -> {
List<ApplicationServer> servers = user.retrieveServers()
.timeout(5, TimeUnit.SECONDS)
.execute();

.anyMatch(subUser -> subUser.getEmail().equals(email))
).toList();
}, bridge.getWorker()).thenApply(servers -> {
if (servers.isEmpty()) {
return Collections.emptyList();
}

List<PteroServer> pteroServers = new ArrayList<>(servers.size());
for (ApplicationServer server : servers) {
for (ClientServer server : servers) {
Pair<String, String> addressAndNode = Servers.getAddressAndNode(server);

PteroServer targetServer = new PteroServerImpl(
Expand All @@ -125,7 +117,7 @@ public CompletableFuture<Void> setName(@NotNull String name) {
return CompletableFuture.runAsync(() -> {
Try<Optional<ApplicationUser>> catching = Try.catching(() -> {
return bridge.getApplication()
.retrieveUsersByUsername(this.name, true)
.retrieveUsersByEmail(email, true)
.timeout(5, TimeUnit.SECONDS)
.execute()
.stream()
Expand All @@ -149,7 +141,7 @@ public CompletableFuture<Void> setEmail(@NotNull String email) {
return CompletableFuture.runAsync(() -> {
Try<Optional<ApplicationUser>> catching = Try.catching(() -> {
return bridge.getApplication()
.retrieveUsersByUsername(this.name, true)
.retrieveUsersByEmail(email, true)
.timeout(5, TimeUnit.SECONDS)
.execute()
.stream()
Expand All @@ -173,7 +165,7 @@ public CompletableFuture<Void> setPassword(@NotNull String password) {
return CompletableFuture.runAsync(() -> {
Try<Optional<ApplicationUser>> catching = Try.catching(() -> {
return bridge.getApplication()
.retrieveUsersByUsername(this.name, true)
.retrieveUsersByEmail(email, true)
.timeout(5, TimeUnit.SECONDS)
.execute()
.stream()
Expand Down
30 changes: 26 additions & 4 deletions test/src/main/java/net/luxcube/minecraft/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Luiz O. F. Corrêa
Expand Down Expand Up @@ -75,9 +76,6 @@ public void createServer() {
assertNotNull(server, "Server is null");

System.out.println("server.getIdentifier() = " + server.getIdentifier());

while (server.

}

@Test
Expand All @@ -91,7 +89,31 @@ public void fetchAll() {

assertNotNull(pteroServers, "Servers is null");

pteroServers.forEach(pteroServer -> System.out.println("pteroServer.getIdentifier() = " + pteroServer.getIdentifier()));
assertTrue(pteroServers.size() > 0, "Servers is empty");
}

@Test
public void fetchingFromUser() {
PteroUser pteroUser = pteroManager.getUserRepository()
.findUserByUsername("luiz-otavio")
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
}).join();

assertNotNull(pteroUser, "User not found");

List<PteroServer> pteroServers = pteroUser.getServers()
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
}).join();

assertNotNull(pteroServers, "Servers is null");

System.out.println("pteroServers.size() = " + pteroServers.size());

assertTrue(pteroServers.size() > 0, "Servers is empty");
}

@Test
Expand Down

0 comments on commit d9cb029

Please sign in to comment.