Skip to content

Commit

Permalink
Merge pull request #9 from luiz-otavio/staging
Browse files Browse the repository at this point in the history
Resolve fetching method of user's server
  • Loading branch information
luiz-otavio authored Dec 7, 2022
2 parents cf52ed1 + d9cb029 commit d5dd712
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public interface PteroServer {
*/
CompletableFuture<Void> 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<Boolean> hasPermission(@NotNull PteroUser pteroUser);

/**
* Execute the stop command of the server.
* @return A future of the completable server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,27 @@ public CompletableFuture<PteroServer> deleteServer(@NotNull PteroServer server)

@Override
public CompletableFuture<List<PteroServer>> 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<String, String> 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<String, String> addressAndNode = Servers.getAddressAndNode(server);

return new PteroServerImpl(
bridge,
server.getIdentifier(),
addressAndNode.first(),
addressAndNode.second(),
server.getName(),
server.getUUID()
);
}).collect(Collectors.toList());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ public CompletableFuture<Void> start() {
}, bridge.getWorker());
}

@Override
public CompletableFuture<Boolean> 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<Void> stop() {
PteroLogger.debug("Stopping server %s", identifier);
Expand Down
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
42 changes: 41 additions & 1 deletion test/src/main/java/net/luxcube/minecraft/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
);
Expand Down Expand Up @@ -76,6 +78,44 @@ public void createServer() {
System.out.println("server.getIdentifier() = " + server.getIdentifier());
}

@Test
public void fetchAll() {
List<PteroServer> 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<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
public void starting() {
PteroServer server = pteroManager.getServerRepository()
Expand Down

0 comments on commit d5dd712

Please sign in to comment.