mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-16 20:29:08 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Member;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Responsible for creating/updating/retrieving mutes in the database.
|
||||
@@ -26,9 +27,9 @@ public interface MuteManagementService {
|
||||
/**
|
||||
* Finds the mute from the database by the given ID.
|
||||
* @param muteId The id of the mute to search for
|
||||
* @return The found {@link Mute}, the first access fails, if the entity was not found
|
||||
* @return An optional containing a {@link Mute} if the ID exists, and null otherwise
|
||||
*/
|
||||
Mute findMute(Long muteId);
|
||||
Optional<Mute> findMute(Long muteId);
|
||||
|
||||
/**
|
||||
* Saves the given mute to the database.
|
||||
|
||||
Reference in New Issue
Block a user