diff --git a/api/src/main/java/net/luxcube/minecraft/server/PteroServer.java b/api/src/main/java/net/luxcube/minecraft/server/PteroServer.java index 978c3ef..7bbd21c 100644 --- a/api/src/main/java/net/luxcube/minecraft/server/PteroServer.java +++ b/api/src/main/java/net/luxcube/minecraft/server/PteroServer.java @@ -86,6 +86,13 @@ public interface PteroServer { */ CompletableFuture start(); + /** + * Check if user can handle/watch the server. + * @param pteroUser The user that will check the state of the server. + * @return A future of the completable state. + */ + CompletableFuture hasPermission(@NotNull PteroUser pteroUser); + /** * Execute the stop command of the server. * @return A future of the completable server. diff --git a/binder/src/main/java/net/luxcube/minecraft/repository/server/ServerRepositoryImpl.java b/binder/src/main/java/net/luxcube/minecraft/repository/server/ServerRepositoryImpl.java index 60a6c3b..13bedd4 100644 --- a/binder/src/main/java/net/luxcube/minecraft/repository/server/ServerRepositoryImpl.java +++ b/binder/src/main/java/net/luxcube/minecraft/repository/server/ServerRepositoryImpl.java @@ -150,39 +150,27 @@ public CompletableFuture deleteServer(@NotNull PteroServer server) @Override public CompletableFuture> retrieveServersByPage(int page, int size) { - return bridge.getApplication() - .retrieveServers() - .cache(false) - .skipTo(Math.max(page, 1)) - .limit(size) - .timeout(10, TimeUnit.SECONDS) - .takeWhileAsync(1, applicationServer -> { - ClientServer server = bridge.getClient() - .retrieveServerByIdentifier(applicationServer.getIdentifier()) - .execute(); - - Utilization utilization = server.retrieveUtilization() - .execute(); - - return utilization.getState() == UtilizationState.RUNNING; - }).thenApply(collection -> { - if (collection.isEmpty()) { - return Collections.emptyList(); - } - - return collection.stream() - .map(applicationServer -> { - Pair addressAndNode = Servers.getAddressAndNode(applicationServer); - - return new PteroServerImpl( - bridge, - applicationServer.getIdentifier(), - addressAndNode.first(), - addressAndNode.second(), - applicationServer.getName(), - applicationServer.getUUID() - ); - }).collect(Collectors.toList()); - }); + return CompletableFuture.supplyAsync(() -> { + return bridge.getApplication() + .retrieveServers() + .skipTo(Math.max(page, 1)) + .limit(size) + .timeout(10, TimeUnit.SECONDS) + .execute(); + }).thenApply(applicationServers -> { + return applicationServers.stream() + .map(server -> { + Pair addressAndNode = Servers.getAddressAndNode(server); + + return new PteroServerImpl( + bridge, + server.getIdentifier(), + addressAndNode.first(), + addressAndNode.second(), + server.getName(), + server.getUUID() + ); + }).collect(Collectors.toList()); + }); } } diff --git a/binder/src/main/java/net/luxcube/minecraft/server/PteroServerImpl.java b/binder/src/main/java/net/luxcube/minecraft/server/PteroServerImpl.java index bec2456..0b42721 100644 --- a/binder/src/main/java/net/luxcube/minecraft/server/PteroServerImpl.java +++ b/binder/src/main/java/net/luxcube/minecraft/server/PteroServerImpl.java @@ -254,6 +254,22 @@ public CompletableFuture start() { }, bridge.getWorker()); } + @Override + public CompletableFuture hasPermission(@NotNull PteroUser pteroUser) { + return CompletableFuture.supplyAsync(() -> { + return bridge.getClient() + .retrieveServerByIdentifier(identifier) + .timeout(5, TimeUnit.SECONDS) + .execute(); + }).thenApply(clientServer -> { + boolean exists = clientServer.getSubusers() + .stream() + .anyMatch(subUser -> subUser.getEmail().equals(pteroUser.getEmail())); + + return exists; + }); + } + @Override public CompletableFuture stop() { PteroLogger.debug("Stopping server %s", identifier); diff --git a/binder/src/main/java/net/luxcube/minecraft/user/PteroUserImpl.java b/binder/src/main/java/net/luxcube/minecraft/user/PteroUserImpl.java index cf48236..ec37eaf 100644 --- a/binder/src/main/java/net/luxcube/minecraft/user/PteroUserImpl.java +++ b/binder/src/main/java/net/luxcube/minecraft/user/PteroUserImpl.java @@ -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; @@ -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 @@ -76,32 +79,21 @@ public PteroUserImpl( @Override public @NotNull CompletableFuture> getServers() { return CompletableFuture.supplyAsync(() -> { - Try> 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 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 pteroServers = new ArrayList<>(servers.size()); - for (ApplicationServer server : servers) { + for (ClientServer server : servers) { Pair addressAndNode = Servers.getAddressAndNode(server); PteroServer targetServer = new PteroServerImpl( @@ -125,7 +117,7 @@ public CompletableFuture setName(@NotNull String name) { return CompletableFuture.runAsync(() -> { Try> catching = Try.catching(() -> { return bridge.getApplication() - .retrieveUsersByUsername(this.name, true) + .retrieveUsersByEmail(email, true) .timeout(5, TimeUnit.SECONDS) .execute() .stream() @@ -149,7 +141,7 @@ public CompletableFuture setEmail(@NotNull String email) { return CompletableFuture.runAsync(() -> { Try> catching = Try.catching(() -> { return bridge.getApplication() - .retrieveUsersByUsername(this.name, true) + .retrieveUsersByEmail(email, true) .timeout(5, TimeUnit.SECONDS) .execute() .stream() @@ -173,7 +165,7 @@ public CompletableFuture setPassword(@NotNull String password) { return CompletableFuture.runAsync(() -> { Try> catching = Try.catching(() -> { return bridge.getApplication() - .retrieveUsersByUsername(this.name, true) + .retrieveUsersByEmail(email, true) .timeout(5, TimeUnit.SECONDS) .execute() .stream() diff --git a/test/src/main/java/net/luxcube/minecraft/ServerTest.java b/test/src/main/java/net/luxcube/minecraft/ServerTest.java index 2130501..52e6272 100644 --- a/test/src/main/java/net/luxcube/minecraft/ServerTest.java +++ b/test/src/main/java/net/luxcube/minecraft/ServerTest.java @@ -5,9 +5,11 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.util.List; 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 @@ -21,7 +23,7 @@ public class ServerTest { public static void setup() { pteroManager = new PteroManagerImpl( "ptla_NZuGwOqmNT8BpCH4hT2LfYBkftWvE989aYhoDixZe2q", - "ptlc_wL9JEOo7b4pVUBRQFhpth8BR0fTIe7LEOd2y4wRjPpY", + "ptlc_fPK6UAWJwLuSbSteEng1dIhc2p4G5pVImrA0k9xuROH", "http://5.249.162.105", 4 ); @@ -76,6 +78,44 @@ public void createServer() { System.out.println("server.getIdentifier() = " + server.getIdentifier()); } + @Test + public void fetchAll() { + List pteroServers = pteroManager.getServerRepository() + .retrieveServersByPage(1, 10) + .exceptionally(throwable -> { + throwable.printStackTrace(); + return null; + }).join(); + + assertNotNull(pteroServers, "Servers is null"); + + 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 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 public void starting() { PteroServer server = pteroManager.getServerRepository()