Skip to content

Commit 44f6cc5

Browse files
committed
filtering in API
1 parent 869c55d commit 44f6cc5

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/main/java/net/discordjug/javabot/api/routes/leaderboard/qotw/QOTWLeaderboardController.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.discordjug.javabot.util.Pair;
1111
import net.dv8tion.jda.api.JDA;
1212
import net.dv8tion.jda.api.entities.Guild;
13+
import net.dv8tion.jda.api.entities.User;
1314

1415
import org.springframework.beans.factory.annotation.Autowired;
1516
import org.springframework.http.HttpStatus;
@@ -68,15 +69,21 @@ public ResponseEntity<List<QOTWUserData>> getQOTWLeaderboard(
6869
if (members == null || members.isEmpty()) {
6970
List<QOTWAccount> topAccounts = pointsService.getTopAccounts(PAGE_AMOUNT, page);
7071
members = topAccounts.stream()
71-
.map(account -> QOTWUserData.of(
72-
account,
73-
jda.retrieveUserById(account.getUserId()).complete(),
74-
//this can be inaccurate for later pages with multiple users having the same score on the previous page
75-
//specifically, it counts all users on previous pages as strictly higher in the leaderboard
76-
pointsService.getQOTWRank(account.getUserId(), topAccounts)+(page-1)*PAGE_AMOUNT))
72+
.map(account -> new Pair<>(account, jda.retrieveUserById(account.getUserId()).complete()))
73+
.filter(pair -> guild.isMember(pair.second()))
74+
.map(pair -> createAPIAccount(pair.first(), pair.second(), topAccounts, page))
7775
.toList();
7876
getCache().put(new Pair<>(guild.getIdLong(), page), members);
7977
}
8078
return new ResponseEntity<>(members, HttpStatus.OK);
8179
}
80+
81+
private QOTWUserData createAPIAccount(QOTWAccount account, User user, List<QOTWAccount> topAccounts, int page) {
82+
return QOTWUserData.of(
83+
account,
84+
user,
85+
//this can be inaccurate for later pages with multiple users having the same score on the previous page
86+
//specifically, it counts all users on previous pages as strictly higher in the leaderboard
87+
pointsService.getQOTWRank(account.getUserId(), topAccounts)+(page-1)*PAGE_AMOUNT);
88+
}
8289
}

0 commit comments

Comments
 (0)