Skip to content

Commit f682b1e

Browse files
committed
Preload the membercache seperately blocking the main thread.
1 parent f6225c5 commit f682b1e

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

app/src/main/java/net/neoforged/discord/bots/pim/service/IllegalRoleAssignmentService.java

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,94 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import java.util.Objects;
15+
import java.util.stream.Collectors;
1516

1617
public class IllegalRoleAssignmentService
1718
{
1819
private static final Logger LOGGER = LoggerFactory.getLogger(IllegalRoleAssignmentService.class);
1920

20-
private final DBA dba;
21+
private final DBA dba;
2122
@Nullable
2223
private final String loggingChannelId;
2324

24-
public IllegalRoleAssignmentService(final DBA dba, @Nullable final String loggingChannelId) {
25+
public IllegalRoleAssignmentService(final DBA dba, @Nullable final String loggingChannelId)
26+
{
2527
this.dba = dba;
2628
this.loggingChannelId = loggingChannelId;
2729
}
2830

29-
public void onStartup(JDA bot) {
31+
public void onStartup(JDA bot)
32+
{
3033
LOGGER.info("Startup role assignment invalidation triggered...");
3134
var roleConfigurations = dba.getRoleConfigurations();
32-
if (roleConfigurations.isEmpty()) {
35+
if (roleConfigurations.isEmpty())
36+
{
3337
LOGGER.warn("No role configurations found to invalidate.");
3438
return;
35-
} else {
39+
}
40+
else
41+
{
3642
LOGGER.info("Processing: {} roles...", roleConfigurations.size());
3743
}
3844

45+
var guilds = roleConfigurations
46+
.stream()
47+
.map(r -> r.guildId)
48+
.collect(Collectors.toSet());
49+
50+
for (final Long guildId : guilds)
51+
{
52+
var guild = bot.getGuildById(guildId);
53+
if (guild == null)
54+
{
55+
LOGGER.info("Guild with id: {} does not exist!", guildId);
56+
return;
57+
}
58+
59+
LOGGER.info("Loading membercache for: {}...", guild.getName());
60+
61+
//Normally we don't call get on this, but we are 100% sure not on the event thread
62+
//as this is invoked from the main thread!
63+
//So we hold the main thread while we load the members of the guild
64+
//We need to do this so that the member cache is filled
65+
guild.loadMembers().get();
66+
}
67+
3968
roleConfigurations.forEach(roleConfig -> {
4069
LOGGER.info("Validating role configuration: {}", roleConfig.name);
4170
var guild = bot.getGuildById(roleConfig.guildId);
42-
if (guild == null) {
71+
if (guild == null)
72+
{
4373
LOGGER.info("Guild with id: {} does not exist!", roleConfig.guildId);
4474
return;
4575
}
46-
guild.loadMembers().onSuccess(members -> {
47-
var roles = guild.getRolesByName(roleConfig.name, false);
48-
if (roles.isEmpty()) {
49-
LOGGER.error("Could not find roles in guild: {} with name: {}", guild.getName(), roleConfig.name);
50-
return;
51-
} else {
52-
LOGGER.info("Checking: {} active roles for invalidation...", roles.size());
53-
}
5476

55-
roles.forEach(role -> {
56-
LOGGER.info("Invalidating role: {}", role.getName());
57-
58-
var membersWithRole = guild.getMembersWithRoles(role);
59-
if (membersWithRole.isEmpty()) {
60-
LOGGER.info("No members found with role: {}", role.getName());
61-
} else {
62-
LOGGER.warn("Checking: {} for validation...", membersWithRole.size());
63-
}
64-
membersWithRole.forEach(member -> {
65-
LOGGER.info("Checking member: {} for role: {} in validation...", member.getEffectiveName(), role.getName());
66-
checkAndHandle(member.getUser(), role, guild);
67-
});
77+
var roles = guild.getRolesByName(roleConfig.name, false);
78+
if (roles.isEmpty())
79+
{
80+
LOGGER.error("Could not find roles in guild: {} with name: {}", guild.getName(), roleConfig.name);
81+
return;
82+
}
83+
else
84+
{
85+
LOGGER.info("Checking: {} active roles for invalidation...", roles.size());
86+
}
87+
88+
roles.forEach(role -> {
89+
LOGGER.info("Invalidating role: {}", role.getName());
90+
91+
var membersWithRole = guild.getMembersWithRoles(role);
92+
if (membersWithRole.isEmpty())
93+
{
94+
LOGGER.info("No members found with role: {}", role.getName());
95+
}
96+
else
97+
{
98+
LOGGER.warn("Checking: {} for validation...", membersWithRole.size());
99+
}
100+
membersWithRole.forEach(member -> {
101+
LOGGER.info("Checking member: {} for role: {} in validation...", member.getEffectiveName(), role.getName());
102+
checkAndHandle(member.getUser(), role, guild);
68103
});
69104
});
70105
});
@@ -81,7 +116,8 @@ public void onRoleConfigurationUpserted(final Role role, final Guild guild)
81116
LOGGER.info("Role configuration upsertion invalidation completed.");
82117
}
83118

84-
public void checkAndHandle(User user, Role role, Guild guild) {
119+
public void checkAndHandle(User user, Role role, Guild guild)
120+
{
85121
LOGGER.info("Checking role assignment for: {} role: {} in: {}", user.getName(), role.getName(), guild.getName());
86122
if (!isManagedRole(role))
87123
{
@@ -102,7 +138,8 @@ public void checkAndHandle(User user, Role role, Guild guild) {
102138
LOGGER.warn("Removed unauthorized role from user: {} ({}). Role: {}", user.getIdLong(), user.getName(), role.getName());
103139
}
104140
);
105-
if (loggingChannelId != null) {
141+
if (loggingChannelId != null)
142+
{
106143
Objects.requireNonNull(guild.getTextChannelById(loggingChannelId))
107144
.sendMessageEmbeds(new EmbedBuilder()
108145
.setTitle("User: " + user.getName() + " tried to add protected role!")
@@ -113,15 +150,18 @@ public void checkAndHandle(User user, Role role, Guild guild) {
113150
}
114151
}
115152

116-
private boolean isManagedRole(Role role) {
153+
private boolean isManagedRole(Role role)
154+
{
117155
return dba.getRoleConfiguration(role.getName(), role.getGuild().getIdLong()) != null;
118156
}
119157

120-
private boolean wasApproved(User user, Role role) {
158+
private boolean wasApproved(User user, Role role)
159+
{
121160
var openRequests = dba.getOpenRemovalJobs();
122161
for (final RoleRemovalJob request : openRequests)
123162
{
124-
if (request.roleId == role.getIdLong() && request.userId == user.getIdLong()) {
163+
if (request.roleId == role.getIdLong() && request.userId == user.getIdLong())
164+
{
125165
return true;
126166
}
127167
}

0 commit comments

Comments
 (0)