[AB-275] fixing channel groups not being able to be found, because of upper/lowercase

adding performance improvements for experience listener
adding logging to when a user gains a level
fixing creating a user experience instance in the join listener, which was not persisted
This commit is contained in:
Sheldan
2021-05-29 01:09:14 +02:00
parent f3bb9b9a69
commit 148c25da4c
7 changed files with 33 additions and 17 deletions

View File

@@ -16,6 +16,8 @@ import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* If a {@link Member member} joins, this {@link JoinListener listener} retrieves the previously stored {@link AUserExperience experience} and gives the
* member the necessary {@link net.dv8tion.jda.api.entities.Role role} according to the current configuration, if any
@@ -36,10 +38,10 @@ public class JoiningUserRoleListener implements AsyncJoinListener {
@Override
public DefaultListenerResult execute(MemberJoinModel model) {
AUserInAServer userInAServer = userInServerManagementService.loadOrCreateUser(model.getServerId(), model.getJoiningUser().getUserId());
AUserExperience userExperience = userExperienceManagementService.findUserInServer(userInAServer);
if(userExperience != null) {
Optional<AUserExperience> userExperienceOptional = userExperienceManagementService.findByUserInServerIdOptional(userInAServer.getUserInServerId());
if(userExperienceOptional.isPresent()) {
log.info("User {} joined {} with previous experience. Setting up experience role again (if necessary).", model.getJoiningUser().getUserId(), model.getServerId());
userExperienceService.syncForSingleUser(userExperience).thenAccept(result ->
userExperienceService.syncForSingleUser(userExperienceOptional.get()).thenAccept(result ->
log.info("Finished re-assigning experience for re-joining user {} in server {}.", model.getJoiningUser().getUserId(), model.getServerId())
);
} else {

View File

@@ -290,7 +290,7 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
@Transactional
public void persistExperienceChanges(List<ExperienceGainResult> resultFutures) {
// we do have the _value_ of the level, but we require the actual instance
List<AExperienceLevel> levels = experienceLevelManagementService.getLevelConfig();
Map<Integer, AExperienceLevel> levels = experienceLevelManagementService.getLevelConfigAsMap();
log.info("Storing {} experience gain results.", resultFutures.size());
HashMap<Long, List<AExperienceRole>> serverRoleMapping = new HashMap<>();
resultFutures.forEach(experienceGainResult -> {
@@ -305,11 +305,13 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
userExperience.setMessageCount(experienceGainResult.getNewMessageCount());
userExperience.setExperience(experienceGainResult.getNewExperience());
// only search the levels if the level changed, or if there is no level currently set
boolean userExperienceHasLevel = userExperience.getCurrentLevel() != null;
if(!userExperienceHasLevel || !userExperience.getCurrentLevel().getLevel().equals(experienceGainResult.getNewLevel())) {
Optional<AExperienceLevel> foundLevel = levels.stream().filter(level -> level.getLevel().equals(experienceGainResult.getNewLevel())).findFirst();
if(foundLevel.isPresent()) {
userExperience.setCurrentLevel(foundLevel.get());
AExperienceLevel currentLevel = userExperience.getCurrentLevel();
boolean userExperienceHasLevel = currentLevel != null;
if(!userExperienceHasLevel || !currentLevel.getLevel().equals(experienceGainResult.getNewLevel())) {
AExperienceLevel foundLevel = levels.get(experienceGainResult.getNewLevel());
if(foundLevel != null) {
log.info("User {} in server {} changed the level. Old level {}. New level {}.", experienceGainResult.getUserInServerId(), experienceGainResult.getServerId(), currentLevel.getLevel(), experienceGainResult.getNewLevel());
userExperience.setCurrentLevel(foundLevel);
} else {
log.warn("User {} was present, but no level matching the calculation result {} could be found.", userExperience.getUser().getUserReference().getId(), experienceGainResult.getNewLevel());
}

View File

@@ -7,7 +7,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
public class ExperienceLevelManagementServiceBean implements ExperienceLevelManagementService {
@@ -44,4 +47,9 @@ public class ExperienceLevelManagementServiceBean implements ExperienceLevelMana
public List<AExperienceLevel> getLevelConfig() {
return experienceLevelRepository.findAll();
}
@Override
public Map<Integer, AExperienceLevel> getLevelConfigAsMap() {
return getLevelConfig().stream().collect(Collectors.toMap(AExperienceLevel::getLevel, Function.identity()));
}
}

View File

@@ -18,6 +18,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import static org.mockito.Mockito.*;
@@ -48,19 +49,21 @@ public class JoiningUserRoleListenerTest {
private static final Long SERVER_ID = 1L;
private static final Long USER_ID = 2L;
private static final Long USER_IN_SERVER_ID = 3L;
@Before
public void setup() {
when(serverUser.getUserId()).thenReturn(USER_ID);
when(model.getJoiningUser()).thenReturn(serverUser);
when(model.getServerId()).thenReturn(SERVER_ID);
when(aUserInAServer.getUserInServerId()).thenReturn(USER_IN_SERVER_ID);
when(userInServerManagementService.loadOrCreateUser(SERVER_ID, USER_ID)).thenReturn(aUserInAServer);
}
@Test
public void testUserWithExperienceRejoining() {
AUserExperience experience = Mockito.mock(AUserExperience.class);
when(userExperienceManagementService.findUserInServer(aUserInAServer)).thenReturn(experience);
when(userExperienceManagementService.findByUserInServerIdOptional(USER_IN_SERVER_ID)).thenReturn(Optional.of(experience));
when(userExperienceService.syncForSingleUser(experience)).thenReturn(CompletableFuture.completedFuture(null));
DefaultListenerResult result = testUnit.execute(model);
Assert.assertEquals(DefaultListenerResult.PROCESSED, result);
@@ -68,7 +71,7 @@ public class JoiningUserRoleListenerTest {
@Test
public void testUserWithOutExperienceRejoining() {
when(userExperienceManagementService.findUserInServer(aUserInAServer)).thenReturn(null);
when(userExperienceManagementService.findByUserInServerIdOptional(USER_IN_SERVER_ID)).thenReturn(Optional.empty());
testUnit.execute(model);
verify(userExperienceService, times(0)).syncForSingleUser(any());
}

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.experience.service.management;
import dev.sheldan.abstracto.experience.model.database.AExperienceLevel;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
@@ -37,4 +38,5 @@ public interface ExperienceLevelManagementService {
* @return A list of {@link AExperienceLevel levels} representing the currently active configuration.
*/
List<AExperienceLevel> getLevelConfig();
Map<Integer, AExperienceLevel> getLevelConfigAsMap();
}

View File

@@ -15,7 +15,7 @@ public interface ChannelGroupRepository extends JpaRepository<AChannelGroup, Lon
Optional<AChannelGroup> findByGroupNameIgnoreCaseAndServer(String name, AServer server);
Optional<AChannelGroup> findByGroupNameAndServerAndChannelGroupType_GroupTypeKey(String name, AServer server, String groupTyeKey);
Optional<AChannelGroup> findByGroupNameIgnoreCaseAndServerAndChannelGroupType_GroupTypeKey(String name, AServer server, String groupTyeKey);
List<AChannelGroup> findByServerAndChannelGroupType_GroupTypeKey(AServer server, String groupTyeKey);

View File

@@ -139,14 +139,13 @@ public class ChannelGroupManagementServiceBean implements ChannelGroupManagement
@Override
public AChannelGroup findByNameAndServerAndType(String name, AServer server, String expectedType) {
String lowerName = name.toLowerCase();
Optional<AChannelGroup> channelOptional = channelGroupRepository.findByGroupNameAndServerAndChannelGroupType_GroupTypeKey(lowerName, server, expectedType);
Optional<AChannelGroup> channelOptional = channelGroupRepository.findByGroupNameIgnoreCaseAndServerAndChannelGroupType_GroupTypeKey(name, server, expectedType);
return channelOptional.orElseThrow(() -> {
if(channelGroupRepository.existsByGroupNameIgnoreCaseAndServer(lowerName, server)) {
return new ChannelGroupIncorrectTypeException(name.toLowerCase(), expectedType);
if(channelGroupRepository.existsByGroupNameIgnoreCaseAndServer(name, server)) {
return new ChannelGroupIncorrectTypeException(name, expectedType);
} else {
List<String> channelGroupNames = extractChannelGroupNames(findAllInServerWithType(server.getId(), expectedType));
return new ChannelGroupNotFoundException(name.toLowerCase(), channelGroupNames);
return new ChannelGroupNotFoundException(name, channelGroupNames);
}
});
}