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

@@ -138,7 +138,10 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
}
private List<AUserInAServer> getUsersExcept(List<Long> users, AUserInAServer author) {
return users.stream().filter(user -> !user.equals(author.getUserInServerId())).map(aLong -> userInServerManagementService.loadUser(aLong)).collect(Collectors.toList());
return users.stream().filter(user -> !user.equals(author.getUserInServerId())).map(aLong -> {
Optional<AUserInAServer> aUserInAServer = userInServerManagementService.loadUser(aLong);
return aUserInAServer.get();
}).collect(Collectors.toList());
}
@Override

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.template.listener.MessageEmbeddedModel;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
@@ -112,31 +113,38 @@ public class MessageEmbedServiceBean implements MessageEmbedService {
@Override
@Transactional
public void embedLink(CachedMessage cachedMessage, TextChannel target, Long userEmbeddingUserInServerId, Message embeddingMessage) {
AUserInAServer cause = userInServerManagementService.loadUser(userEmbeddingUserInServerId);
MessageEmbeddedModel messageEmbeddedModel = buildTemplateParameter(embeddingMessage, cachedMessage);
MessageToSend embed = templateService.renderEmbedTemplate(MESSAGE_EMBED_TEMPLATE, messageEmbeddedModel);
List<CompletableFuture<Message>> completableFutures = channelService.sendMessageToSendToChannel(embed, target);
log.trace("Embedding message {} from channel {} from server {}, because of user {}", cachedMessage.getMessageId(),
cachedMessage.getChannelId(), cachedMessage.getServerId(), cause.getUserReference().getId());
Long userInServerId = cause.getUserInServerId();
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
try {
Message createdMessage = completableFutures.get(0).get();
messageEmbedPostManagementService.createMessageEmbed(cachedMessage, createdMessage, userInServerManagementService.loadUser(userInServerId));
messageService.addReactionToMessage(REMOVAL_EMOTE, cachedMessage.getServerId(), createdMessage);
} catch (InterruptedException | ExecutionException e) {
log.error("Failed to post message embed.", e);
}
}).exceptionally(throwable -> {
log.error("Failed to send message for embedding the link for message {} in channel {} in server {}",
cachedMessage.getMessageId(), cachedMessage.getChannelId(), cachedMessage.getServerId(), throwable);
return null;
});
Optional<AUserInAServer> causeOpt = userInServerManagementService.loadUser(userEmbeddingUserInServerId);
if(causeOpt.isPresent()) {
AUserInAServer cause = causeOpt.get();
MessageEmbeddedModel messageEmbeddedModel = buildTemplateParameter(embeddingMessage, cachedMessage);
MessageToSend embed = templateService.renderEmbedTemplate(MESSAGE_EMBED_TEMPLATE, messageEmbeddedModel);
List<CompletableFuture<Message>> completableFutures = channelService.sendMessageToSendToChannel(embed, target);
log.trace("Embedding message {} from channel {} from server {}, because of user {}", cachedMessage.getMessageId(),
cachedMessage.getChannelId(), cachedMessage.getServerId(), cause.getUserReference().getId());
Long userInServerId = cause.getUserInServerId();
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
try {
Message createdMessage = completableFutures.get(0).get();
Optional<AUserInAServer> innerCauseOpt = userInServerManagementService.loadUser(userInServerId);
innerCauseOpt.ifPresent(aUserInAServer -> {
messageEmbedPostManagementService.createMessageEmbed(cachedMessage, createdMessage, aUserInAServer);
messageService.addReactionToMessage(REMOVAL_EMOTE, cachedMessage.getServerId(), createdMessage);
});
} catch (InterruptedException | ExecutionException e) {
log.error("Failed to post message embed.", e);
}
}).exceptionally(throwable -> {
log.error("Failed to send message for embedding the link for message {} in channel {} in server {}",
cachedMessage.getMessageId(), cachedMessage.getChannelId(), cachedMessage.getServerId(), throwable);
return null;
});
}
}
private MessageEmbeddedModel buildTemplateParameter(Message message, CachedMessage embeddedMessage) {
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
Optional<AChannel> channelOpt = channelManagementService.loadChannel(message.getChannel().getIdLong());
AServer server = serverManagementService.loadOrCreate(message.getGuild().getIdLong());
AUserInAServer user = userInServerManagementService.loadUser(message.getMember());
Member author = botService.getMemberInServer(embeddedMessage.getServerId(), embeddedMessage.getAuthorId());
@@ -145,6 +153,7 @@ public class MessageEmbedServiceBean implements MessageEmbedService {
if(textChannelFromServer.isPresent()) {
sourceChannel = textChannelFromServer.get();
}
AChannel channel = channelOpt.orElseThrow(() -> new ChannelNotFoundException(message.getChannel().getIdLong(), server.getId()));
return MessageEmbeddedModel
.builder()
.channel(channel)

View File

@@ -59,7 +59,7 @@ public class RemindServiceBean implements ReminderService {
@Override
public void createReminderInForUser(AUserInAServer user, String remindText, Duration remindIn, ReminderModel reminderModel) {
AChannel channel = channelManagementService.loadChannel(reminderModel.getChannel().getId());
AChannel channel = reminderModel.getChannel();
AServerAChannelAUser aServerAChannelAUser = AServerAChannelAUser
.builder()
.user(user.getUserReference())
@@ -98,7 +98,7 @@ public class RemindServiceBean implements ReminderService {
@Override
@Transactional
public void executeReminder(Long reminderId) {
Reminder reminderToRemindFor = reminderManagementService.loadReminder(reminderId);
Reminder reminderToRemindFor = reminderManagementService.loadReminder(reminderId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find reminder with id %s", reminderId)));
if(reminderToRemindFor.isReminded()) {
return;
}

View File

@@ -1,5 +1,7 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
@@ -101,10 +103,10 @@ public class StarboardServiceBean implements StarboardService {
@Transactional
public void persistPost(CachedMessage message, List<Long> userExceptAuthorIds, List<CompletableFuture<Message>> completableFutures, Long starboardChannelId, Long starredUserId, Long userReactingId) {
AUserInAServer innerStarredUser = userInServerManagementService.loadUser(starredUserId);
AUserInAServer innerUserReacting = userInServerManagementService.loadUser(userReactingId);
AUserInAServer innerStarredUser = userInServerManagementService.loadUser(starredUserId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find user with id %s", starredUserId)));
AUserInAServer innerUserReacting = userInServerManagementService.loadUser(userReactingId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Cannnot find user with id %s", userReactingId)));
try {
AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId);
AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId).orElseThrow(() -> new ChannelNotFoundException(starboardChannelId, message.getServerId()));
Message message1 = completableFutures.get(0).get();
AServerAChannelMessage aServerAChannelMessage = AServerAChannelMessage
.builder()
@@ -114,7 +116,7 @@ public class StarboardServiceBean implements StarboardService {
.build();
StarboardPost starboardPost = starboardPostManagementService.createStarboardPost(message, innerStarredUser, innerUserReacting, aServerAChannelMessage);
userExceptAuthorIds.forEach(aLong -> {
AUserInAServer user = userInServerManagementService.loadUser(aLong);
AUserInAServer user = userInServerManagementService.loadUser(aLong).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find user with id %s", aLong)));
starboardPostReactorManagementService.addReactor(starboardPost, user);
});
} catch (InterruptedException | ExecutionException e) {

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.service.BotService;
@@ -64,7 +65,7 @@ public class SuggestionServiceBean implements SuggestionService {
if(guildById != null) {
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SUGGESTIONS_TARGET, guildId);
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
Suggestion innerSuggestion = suggestionManagementService.getSuggestion(suggestionId);
Suggestion innerSuggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId)));
try {
Message message = completableFutures.get(0).get();
suggestionManagementService.setPostedMessage(innerSuggestion, message);
@@ -84,7 +85,7 @@ public class SuggestionServiceBean implements SuggestionService {
@Override
public void acceptSuggestion(Long suggestionId, String text, SuggestionLog suggestionLog) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId);
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId)));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.ACCEPTED);
updateSuggestion(text, suggestionLog, suggestion);
}
@@ -129,7 +130,7 @@ public class SuggestionServiceBean implements SuggestionService {
@Override
public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId);
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new AbstractoRunTimeException(String.format("Could not find suggestion with id %s", suggestionId)));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.REJECTED);
updateSuggestion(text, log, suggestion);
}

View File

@@ -9,6 +9,7 @@ import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
@Component
public class ReminderManagementServiceBean implements ReminderManagementService {
@@ -34,8 +35,8 @@ public class ReminderManagementServiceBean implements ReminderManagementService
}
@Override
public Reminder loadReminder(Long reminderId) {
return reminderRepository.getOne(reminderId);
public Optional<Reminder> loadReminder(Long reminderId) {
return reminderRepository.findById(reminderId);
}
@Override

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
@@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.Optional;
@Component
public class SuggestionManagementServiceBean implements SuggestionManagementService {
@@ -50,16 +52,21 @@ public class SuggestionManagementServiceBean implements SuggestionManagementServ
}
@Override
public Suggestion getSuggestion(Long suggestionId) {
return suggestionRepository.getOne(suggestionId);
public Optional<Suggestion> getSuggestion(Long suggestionId) {
return suggestionRepository.findById(suggestionId);
}
@Override
public void setPostedMessage(Suggestion suggestion, Message message) {
suggestion.setMessageId(message.getIdLong());
AChannel channel = channelManagementService.loadChannel(message.getTextChannel().getIdLong());
suggestion.setChannel(channel);
long channelId = message.getTextChannel().getIdLong();
Optional<AChannel> channelOptional = channelManagementService.loadChannel(channelId);
if(channelOptional.isPresent()) {
suggestion.setChannel(channelOptional.get());
} else {
throw new ChannelNotFoundException(channelId, suggestion.getServer().getId());
}
AServer server = serverManagementService.loadOrCreate(message.getGuild().getIdLong());
suggestion.setServer(server);
suggestionRepository.save(suggestion);