LuckPerms / rest-api

A REST API for LuckPerms.
MIT License
31 stars 8 forks source link

New Route for getting all Members of a Group #13

Open Carlos17Kopra opened 10 months ago

Carlos17Kopra commented 10 months ago

Hey there! I had the issue, that I needed all Members of a Group without "request spaming" my Server. Since its an open Source Project, I've added it.

What have I done

First, I added a new Interface InheritingMembersController

package me.lucko.luckperms.extension.rest.controller;

import io.javalin.http.Context;

public interface InheritingMembersController {

    // GET /<type>/{id}/members
    void getGroupMembers(Context ctx) throws Exception;

}

After that, I implemented the Interface in GroupController

@Override
//GET /group/{id}/members
public void getGroupMembers(Context ctx) throws Exception {
    String groupName = ctx.pathParam("id");
    List<UUID> membersInGroup = new ArrayList<>();

    CompletableFuture<Set<UUID>> usersFuture = userManager.getUniqueUsers();
    ctx.future(usersFuture);
    Set<UUID> uuids = usersFuture.get();
    for(UUID uuid : uuids){
        CompletableFuture<User> loadFuture = loadUserCached(uuid);
        ctx.future(loadFuture);
        User user = loadFuture.get();
        if(user == null) continue;
        if(user.getPrimaryGroup().equals(groupName)){
            membersInGroup.add(uuid);
        }
    }
    ctx.json(membersInGroup);
}

Also I had to add the UserManager from Luckperms

private final UserManager userManager;
public GroupController(GroupManager groupManager, UserManager userManager, MessagingService messagingService, ObjectMapper objectMapper) {
    this.groupManager = groupManager;
    this.messagingService = messagingService;
    this.objectMapper = objectMapper;
    this.userManager = userManager;
}

FInally, I had to modify the RestServer Class In the _setupRoutes_I had to modify the Constructorcall for the GroupManager

GroupController groupController = new GroupController(luckPerms.getGroupManager(), luckPerms.getUserManager(), messagingService, this.objectMapper);

And in the setupControllerRoutes I've added

if(controller instanceof InheritingMembersController groupController){
    get("members", groupController::getGroupMembers);
}

I would love to see a route like this in the Official API, since that would be such a nice Feature!

Great work to you guys with this API :) If you have any questions or improvements I would love to hear about them!