Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Route for getting all Members of a Group #13

Open
Carlos17Kopra opened this issue Jan 6, 2024 · 0 comments
Open

New Route for getting all Members of a Group #13

Carlos17Kopra opened this issue Jan 6, 2024 · 0 comments

Comments

@Carlos17Kopra
Copy link

Carlos17Kopra commented Jan 6, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant