added possibility to define the text which is used for notifying the user when closing a thread

This commit is contained in:
Sheldan
2020-05-20 01:13:45 +02:00
parent 1e667686d3
commit e2f71ef7a3
4 changed files with 31 additions and 14 deletions

View File

@@ -4,17 +4,25 @@ import dev.sheldan.abstracto.core.listener.ServerConfigListener;
import dev.sheldan.abstracto.core.models.database.AServer; import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ConfigManagementService; import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
import dev.sheldan.abstracto.modmail.service.ModMailThreadServiceBean; import dev.sheldan.abstracto.modmail.service.ModMailThreadServiceBean;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static dev.sheldan.abstracto.modmail.service.ModMailThreadServiceBean.MODMAIL_CLOSING_MESSAGE_TEXT;
@Component @Component
public class ModMailConfigListener implements ServerConfigListener { public class ModMailConfigListener implements ServerConfigListener {
@Autowired @Autowired
private ConfigManagementService configService; private ConfigManagementService configService;
@Autowired
private TemplateService templateService;
@Override @Override
public void updateServerConfig(AServer server) { public void updateServerConfig(AServer server) {
configService.createIfNotExists(server.getId(), ModMailThreadServiceBean.MODMAIL_CATEGORY, 0L); configService.createIfNotExists(server.getId(), ModMailThreadServiceBean.MODMAIL_CATEGORY, 0L);
configService.createIfNotExists(server.getId(), MODMAIL_CLOSING_MESSAGE_TEXT, templateService.renderSimpleTemplate("modmail_closing_user_message_description"));
} }
} }

View File

@@ -39,6 +39,7 @@ import java.util.concurrent.ExecutionException;
@Slf4j @Slf4j
public class ModMailThreadServiceBean implements ModMailThreadService { public class ModMailThreadServiceBean implements ModMailThreadService {
public static final String MODMAIL_CLOSING_MESSAGE_TEXT = "modMailClosingText";
public static final String MODMAIL_CATEGORY = "modmailCategory"; public static final String MODMAIL_CATEGORY = "modmailCategory";
@Autowired @Autowired
private ModMailThreadManagementService modMailThreadManagementService; private ModMailThreadManagementService modMailThreadManagementService;
@@ -129,19 +130,25 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
try { try {
ModMailThread thread = createThreadObject(channel, aUserInAServer); ModMailThread thread = createThreadObject(channel, aUserInAServer);
sendModMailHeader(channel, aUserInAServer, undoActions); sendModMailHeader(channel, aUserInAServer, undoActions);
CompletableFuture<Void> future;
if(initialMessage != null){ if(initialMessage != null){
self.sendUserReply(channel, thread, initialMessage); future = self.sendUserReply(channel, thread, initialMessage);
} else {
future = CompletableFuture.completedFuture(null);
} }
future.thenAccept(aVoid -> {
if(userInitiated) { if(userInitiated) {
sendModMailNotification(aUserInAServer, thread, undoActions); self.sendModMailNotification(aUserInAServer, thread, undoActions);
} }
});
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to perform mod mail thread setup.", e); log.error("Failed to perform mod mail thread setup.", e);
undoActionService.performActions(undoActions); undoActionService.performActions(undoActions);
} }
} }
private void sendModMailNotification(FullUser aUserInAServer, ModMailThread thread, List<UndoActionInstance> undoActions) { @Transactional
public void sendModMailNotification(FullUser aUserInAServer, ModMailThread thread, List<UndoActionInstance> undoActions) {
List<ModMailRole> rolesToPing = modMailRoleManagementService.getRolesForServer(thread.getServer()); List<ModMailRole> rolesToPing = modMailRoleManagementService.getRolesForServer(thread.getServer());
ModMailNotificationModel modMailNotificationModel = ModMailNotificationModel ModMailNotificationModel modMailNotificationModel = ModMailNotificationModel
.builder() .builder()
@@ -259,7 +266,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
} }
} }
public void sendUserReply(TextChannel textChannel, ModMailThread modMailThread, Message message) { public CompletableFuture<Void> sendUserReply(TextChannel textChannel, ModMailThread modMailThread, Message message) {
Long modMailThreadId = modMailThread.getId(); Long modMailThreadId = modMailThread.getId();
FullUser fullUser = FullUser FullUser fullUser = FullUser
.builder() .builder()
@@ -286,7 +293,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.build(); .build();
MessageToSend messageToSend = templateService.renderEmbedTemplate("modmail_user_message", modMailUserReplyModel); MessageToSend messageToSend = templateService.renderEmbedTemplate("modmail_user_message", modMailUserReplyModel);
List<CompletableFuture<Message>> completableFutures = channelService.sendMessageToSendToChannel(messageToSend, textChannel); List<CompletableFuture<Message>> completableFutures = channelService.sendMessageToSendToChannel(messageToSend, textChannel);
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> { return CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
self.postProcessSendMessages(modMailThreadId, message, completableFutures); self.postProcessSendMessages(modMailThreadId, message, completableFutures);
}); });
@@ -433,7 +440,10 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
List<CompletableFuture<Message>> messageFutures = new ArrayList<>(); List<CompletableFuture<Message>> messageFutures = new ArrayList<>();
if(notifyUser){ if(notifyUser){
log.trace("Notifying user {}", user.getIdLong()); log.trace("Notifying user {}", user.getIdLong());
messageFutures.addAll(channelService.sendTemplateInChannel("modmail_closing_user_message", new Object(), privateChannel)); HashMap<String, String> closingMessage = new HashMap<>();
String defaultValue = templateService.renderSimpleTemplate("modmail_closing_user_message_description");
closingMessage.put("closingMessage", configService.getStringValue(MODMAIL_CLOSING_MESSAGE_TEXT, modMailThread.getServer().getId(), defaultValue));
messageFutures.addAll(channelService.sendTemplateInChannel("modmail_closing_user_message", closingMessage , privateChannel));
} else { } else {
log.trace("*Not* notifying user {}", user.getIdLong()); log.trace("*Not* notifying user {}", user.getIdLong());
messageFutures.add(CompletableFuture.completedFuture(null)); messageFutures.add(CompletableFuture.completedFuture(null));

View File

@@ -4,5 +4,5 @@
"g": 0, "g": 0,
"b": 255 "b": 255
}, },
"description": "<#include "modmail_closing_user_message_description">" "description": "${closingMessage}"
} }

View File

@@ -14,6 +14,10 @@ This feature enables users to contact the moderation of the server in a private
Feature key: `modmail` Feature key: `modmail`
==== Relevant system configuration
`modmailCategory`:: The category on the server which is used to hold the text channels representing the threads
`modMailClosingText`::The text being used when notifying the user when a thread is closed.
==== Post targets ==== Post targets
`modmailPing`:: Will be used to send the notification when a new thread is opened. `modmailPing`:: Will be used to send the notification when a new thread is opened.
`modmailLog`:: Will be used to log the interactions when a thread is closed. `modmailLog`:: Will be used to log the interactions when a thread is closed.
@@ -68,8 +72,3 @@ Close a thread without logging
* Description: Closes the thread without notifying the user and without logging the messages. * Description: Closes the thread without notifying the user and without logging the messages.
* Mode Restriction: This command is only available when mod mail is in the mode `log`. * Mode Restriction: This command is only available when mod mail is in the mode `log`.
=== Mod mail thread logging
==== Post targets