added concept of undo actions

used undo actions to delete the mod mail thread channel, if it already reached a certain state when initially setting up the mod mail thread
This commit is contained in:
Sheldan
2020-05-09 00:19:19 +02:00
parent 271ea7364a
commit 379db1ff73
8 changed files with 129 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ import com.jagrosh.jdautilities.menu.ButtonMenu;
import dev.sheldan.abstracto.core.exception.PostTargetException;
import dev.sheldan.abstracto.core.models.FullGuild;
import dev.sheldan.abstracto.core.models.FullUser;
import dev.sheldan.abstracto.core.models.UndoActionInstance;
import dev.sheldan.abstracto.core.models.database.*;
import dev.sheldan.abstracto.core.service.*;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
@@ -83,6 +84,9 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
@Autowired
private ModMailRoleManagementService modMailRoleManagementService;
@Autowired
private UndoActionService undoActionService;
@Autowired
private ModMailSubscriberManagementService modMailSubscriberManagementService;
@@ -102,7 +106,9 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
CompletableFuture<TextChannel> textChannel = channelService.createTextChannel(user.getName() + user.getDiscriminator(), aUserInAServer.getAUserInAServer().getServerReference(), categoryId);
textChannel.thenAccept(channel -> {
self.performModMailThreadSetup(aUserInAServer, channel, userInitiated);
List<UndoActionInstance> undoActions = new ArrayList<>();
undoActions.add(UndoActionInstance.getChannelDeleteAction(channel.getIdLong(), aUserInAServer.getAUserInAServer().getServerReference().getId()));
self.performModMailThreadSetup(aUserInAServer, channel, userInitiated, undoActions);
}).exceptionally(throwable -> {
log.error("Failed to create mod mail thread", throwable);
sendModMailFailure("modmail_exception_failed_to_create_mod_mail_thread", aUserInAServer.getAUserInAServer(), null, feedBackChannel, throwable);
@@ -111,15 +117,20 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
}
@Transactional
public void performModMailThreadSetup(FullUser aUserInAServer, TextChannel channel, Boolean userInitiated) {
ModMailThread thread = createThreadObject(channel, aUserInAServer);
sendModMailHeader(channel, aUserInAServer);
if(userInitiated) {
sendModMailNotification(aUserInAServer, thread);
public void performModMailThreadSetup(FullUser aUserInAServer, TextChannel channel, Boolean userInitiated, List<UndoActionInstance> undoActions) {
try {
ModMailThread thread = createThreadObject(channel, aUserInAServer);
sendModMailHeader(channel, aUserInAServer, undoActions);
if(userInitiated) {
sendModMailNotification(aUserInAServer, thread, undoActions);
}
} catch (Exception e) {
log.error("Failed to perform mod mail thread setup.", e);
undoActionService.performActions(undoActions);
}
}
private void sendModMailNotification(FullUser aUserInAServer, ModMailThread thread) {
private void sendModMailNotification(FullUser aUserInAServer, ModMailThread thread, List<UndoActionInstance> undoActions) {
List<ModMailRole> rolesToPing = modMailRoleManagementService.getRolesForServer(thread.getServer());
ModMailNotificationModel modMailNotificationModel = ModMailNotificationModel
.builder()
@@ -128,7 +139,13 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.roles(rolesToPing)
.build();
MessageToSend messageToSend = templateService.renderEmbedTemplate("modmail_notification_message", modMailNotificationModel);
postTargetService.sendEmbedInPostTarget(messageToSend, "modmailping", thread.getServer().getId());
List<CompletableFuture<Message>> modmailping = postTargetService.sendEmbedInPostTarget(messageToSend, "modmailping", thread.getServer().getId());
CompletableFuture.allOf(modmailping.toArray(new CompletableFuture[0])).whenComplete((aVoid, throwable) -> {
if(throwable != null) {
log.error("Failed to send mod mail thread notification ping.", throwable);
undoActionService.performActions(undoActions);
}
});
}
public ModMailThread createThreadObject(TextChannel channel, FullUser user) {
@@ -203,7 +220,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
}
private void sendModMailHeader(TextChannel channel, FullUser aUserInAServer) {
private void sendModMailHeader(TextChannel channel, FullUser aUserInAServer, List<UndoActionInstance> undoActions) {
ModMailThread latestThread = modMailThreadManagementService.getLatestModMailThread(aUserInAServer.getAUserInAServer());
List<ModMailThread> oldThreads = modMailThreadManagementService.getModMailThreadForUser(aUserInAServer.getAUserInAServer());
ModMailThreaderHeader header = ModMailThreaderHeader
@@ -212,7 +229,13 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.latestModMailThread(latestThread)
.pastModMailThreadCount((long)oldThreads.size())
.build();
channelService.sendTemplateInChannel("modmail_thread_header", header, channel);
List<CompletableFuture<Message>> messages = channelService.sendTemplateInChannel("modmail_thread_header", header, channel);
CompletableFuture.allOf(messages.toArray(new CompletableFuture[0])).whenComplete((aVoid, throwable)-> {
if(throwable != null) {
log.error("Failed to send mod mail header for for user {}.", aUserInAServer.getAUserInAServer().getUserReference().getId(), throwable);
undoActionService.performActions(undoActions);
}
});
}
@Override

View File

@@ -168,11 +168,16 @@ public class ChannelServiceBean implements ChannelService {
@Override
public CompletableFuture<Void> deleteTextChannel(AChannel channel) {
TextChannel textChannelById = botService.getInstance().getTextChannelById(channel.getId());
return deleteTextChannel(channel.getServer().getId(), channel.getId());
}
@Override
public CompletableFuture<Void> deleteTextChannel(Long serverId, Long channelId) {
TextChannel textChannelById = botService.getInstance().getTextChannelById(channelId);
if(textChannelById != null) {
return textChannelById.delete().submit();
}
throw new ChannelException(String.format("Failed to delete channel %s in server %s", channel.getId(), channel.getServer().getId()));
throw new ChannelException(String.format("Failed to delete channel %s in server %s", channelId, serverId));
}
@Override

View File

@@ -0,0 +1,43 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.UndoActionException;
import dev.sheldan.abstracto.core.models.UndoAction;
import dev.sheldan.abstracto.core.models.UndoActionInstance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Slf4j
public class UndoActionServiceBean implements UndoActionService {
@Autowired
private BotService botService;
@Autowired
private ChannelService channelService;
@Override
public void performActions(List<UndoActionInstance> actionsToPerform) {
actionsToPerform.forEach(undoActionInstance -> {
UndoAction action = undoActionInstance.getAction();
switch (action) {
case DELETE_CHANNEL:
if(undoActionInstance.getIds().size() != 2) {
throw new UndoActionException("Not the correct amount of ides provided for the channel deletion undo action");
}
deleteChannel(undoActionInstance.getIds().get(0), undoActionInstance.getIds().get(1));
break;
}
});
}
private void deleteChannel(Long serverId, Long channelId) {
channelService.deleteTextChannel(serverId, channelId).exceptionally((throwable) -> {
log.error("Failed to execute undo action channel delete for channel {} in server {}", channelId, serverId, throwable);
return null;
});
}
}

View File

@@ -0,0 +1,7 @@
package dev.sheldan.abstracto.core.exception;
public class UndoActionException extends AbstractoRunTimeException {
public UndoActionException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,5 @@
package dev.sheldan.abstracto.core.models;
public enum UndoAction {
DELETE_CHANNEL, DELETE_MESSAGE
}

View File

@@ -0,0 +1,24 @@
package dev.sheldan.abstracto.core.models;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.Arrays;
import java.util.List;
@Getter
@Builder
@Setter
public class UndoActionInstance {
private List<Long> ids;
private UndoAction action;
public static UndoActionInstance getChannelDeleteAction(Long channelId, Long serverId) {
return UndoActionInstance
.builder()
.action(UndoAction.DELETE_CHANNEL)
.ids(Arrays.asList(serverId, channelId))
.build();
}
}

View File

@@ -27,6 +27,7 @@ public interface ChannelService {
void editMessageInAChannel(MessageToSend messageToSend, AChannel channel, Long messageId);
void editMessageInAChannel(MessageToSend messageToSend, MessageChannel channel, Long messageId);
CompletableFuture<Void> deleteTextChannel(AChannel channel);
CompletableFuture<Void> deleteTextChannel(Long serverId, Long channelId);
List<CompletableFuture<Message>> sendTemplateInChannel(String templateKey, Object model, MessageChannel channel);
CompletableFuture<TextChannel> createTextChannel(String name, AServer server, Long categoryId);

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.UndoActionInstance;
import java.util.List;
public interface UndoActionService {
void performActions(List<UndoActionInstance> actionsToPerform);
}