refactored channel exception to have a unified interface to form the exception message

replaced getOne with findById in order to get optionals and handle those
some places still have the general abstracto run time exception
This commit is contained in:
Sheldan
2020-05-13 12:49:08 +02:00
parent 5755d033eb
commit c429aa882b
52 changed files with 501 additions and 344 deletions

View File

@@ -1,5 +1,7 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.FullUser;
import dev.sheldan.abstracto.core.models.database.AChannel;
@@ -33,6 +35,7 @@ import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -129,7 +132,9 @@ public class MuteServiceBean implements MuteService {
applyMuteRole(userInServerBeingMuted);
AServerAChannelMessage origin = null;
if(message != null) {
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
long channelId = message.getChannel().getIdLong();
Optional<AChannel> channelOpt = channelManagementService.loadChannel(channelId);
AChannel channel = channelOpt.orElseThrow(() -> new ChannelNotFoundException(channelId, userInServerBeingMuted.getServerReference().getId()));
origin = AServerAChannelMessage
.builder()
.channel(channel)
@@ -213,7 +218,8 @@ public class MuteServiceBean implements MuteService {
@Transactional
public void unmuteUser(Mute mute) {
AServer mutingServer = mute.getMutingServer();
Mute updatedMute = muteManagementService.findMute(mute.getId());
Optional<Mute> updatedMuteOpt = muteManagementService.findMute(mute.getId());
Mute updatedMute = updatedMuteOpt.orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannot find mute with id %s", mute.getId())));
// we do not store any reference to the instant unmutes (<=60sec), so we cannot cancel it
// but if the person gets unmuted immediately, via command, this might still execute of the instant unmute
// so we need to load the mute, and check if the mute was unmuted already, because the mute object we have at
@@ -248,8 +254,8 @@ public class MuteServiceBean implements MuteService {
@Transactional
public void endMute(Long muteId) {
log.info("Unmuting the mute {}", muteId);
Mute mute = muteManagementService.findMute(muteId);
unmuteUser(mute);
Optional<Mute> mute = muteManagementService.findMute(muteId);
mute.ifPresent(this::unmuteUser);
}
@Override

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.exception.ChannelException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.service.BotService;
import lombok.extern.slf4j.Slf4j;
@@ -40,7 +40,7 @@ public class SlowModeServiceBean implements SlowModeService {
TextChannel textChannel = textChannelOptional.get();
this.setSlowMode(textChannel, duration);
} else {
throw new ChannelException(String.format("Channel %s not found in guild %s", channel.getId(), channel.getServer().getId()));
throw new ChannelNotFoundException(channel.getId(), channel.getServer().getId());
}
}
}

View File

@@ -12,6 +12,7 @@ import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
@Component
@Slf4j
@@ -44,8 +45,8 @@ public class MuteManagementServiceBean implements MuteManagementService {
}
@Override
public Mute findMute(Long muteId) {
return muteRepository.getOne(muteId);
public Optional<Mute> findMute(Long muteId) {
return muteRepository.findById(muteId);
}
@Override