[AB-347] adding support for multiple and generic attachments to modmail

fixing editing messages with multiple embeds
fixing message limit also bein imposed on pure embed count
This commit is contained in:
Sheldan
2021-12-25 16:29:23 +01:00
parent cc898b27bb
commit 986b65a1e4
6 changed files with 54 additions and 7 deletions

View File

@@ -28,8 +28,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@Component @Component
@Slf4j @Slf4j
@@ -106,11 +109,24 @@ public class ModMailMessageEditedListener implements AsyncMessageUpdatedListener
.aUserInAServer(modMailMessage.getThreadReference().getUser()) .aUserInAServer(modMailMessage.getThreadReference().getUser())
.member(targetMember) .member(targetMember)
.build(); .build();
List<String> imageUrls = loadedMessage
.getAttachments()
.stream()
.filter(Message.Attachment::isImage)
.map(Message.Attachment::getProxyUrl)
.collect(Collectors.toList());
Map<String, String> otherAttachments = loadedMessage
.getAttachments()
.stream()
.filter(attachment -> !attachment.isImage())
.collect(Collectors.toMap(Message.Attachment::getFileName, Message.Attachment::getUrl));
ModMailModeratorReplyModel.ModMailModeratorReplyModelBuilder modMailModeratorReplyModelBuilder = ModMailModeratorReplyModel ModMailModeratorReplyModel.ModMailModeratorReplyModelBuilder modMailModeratorReplyModelBuilder = ModMailModeratorReplyModel
.builder() .builder()
.text(newText) .text(newText)
.modMailThread(modMailMessage.getThreadReference()) .modMailThread(modMailMessage.getThreadReference())
.postedMessage(loadedMessage) .postedMessage(loadedMessage)
.attachedImageUrls(imageUrls)
.remainingAttachments(otherAttachments)
.anonymous(modMailMessage.getAnonymous()) .anonymous(modMailMessage.getAnonymous())
.threadUser(fullThreadUser); .threadUser(fullThreadUser);
if(modMailMessage.getAnonymous()) { if(modMailMessage.getAnonymous()) {

View File

@@ -498,10 +498,23 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.map(CompletableFuture::join) .map(CompletableFuture::join)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
List<String> imageUrls = messageFromUser
.getAttachments()
.stream()
.filter(Message.Attachment::isImage)
.map(Message.Attachment::getProxyUrl)
.collect(Collectors.toList());
Map<String, String> otherAttachments = messageFromUser
.getAttachments()
.stream()
.filter(attachment -> !attachment.isImage())
.collect(Collectors.toMap(Message.Attachment::getFileName, Message.Attachment::getUrl));
ModMailUserReplyModel modMailUserReplyModel = ModMailUserReplyModel ModMailUserReplyModel modMailUserReplyModel = ModMailUserReplyModel
.builder() .builder()
.postedMessage(messageFromUser) .postedMessage(messageFromUser)
.member(member) .member(member)
.attachedImageUrls(imageUrls)
.remainingAttachments(otherAttachments)
.subscribers(subscribers) .subscribers(subscribers)
.build(); .build();
MessageToSend messageToSend = templateService.renderEmbedTemplate("modmail_user_message", modMailUserReplyModel, textChannel.getGuild().getIdLong()); MessageToSend messageToSend = templateService.renderEmbedTemplate("modmail_user_message", modMailUserReplyModel, textChannel.getGuild().getIdLong());
@@ -568,11 +581,24 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
.aUserInAServer(modMailThread.getUser()) .aUserInAServer(modMailThread.getUser())
.member(targetMember) .member(targetMember)
.build(); .build();
List<String> imageUrls = replyCommandMessage
.getAttachments()
.stream()
.filter(Message.Attachment::isImage)
.map(Message.Attachment::getProxyUrl)
.collect(Collectors.toList());
Map<String, String> otherAttachments = replyCommandMessage
.getAttachments()
.stream()
.filter(attachment -> !attachment.isImage())
.collect(Collectors.toMap(Message.Attachment::getFileName, Message.Attachment::getUrl));
ModMailModeratorReplyModel.ModMailModeratorReplyModelBuilder modMailModeratorReplyModelBuilder = ModMailModeratorReplyModel ModMailModeratorReplyModel.ModMailModeratorReplyModelBuilder modMailModeratorReplyModelBuilder = ModMailModeratorReplyModel
.builder() .builder()
.text(text) .text(text)
.modMailThread(modMailThread) .modMailThread(modMailThread)
.postedMessage(replyCommandMessage) .postedMessage(replyCommandMessage)
.remainingAttachments(otherAttachments)
.attachedImageUrls(imageUrls)
.anonymous(anonymous) .anonymous(anonymous)
.threadUser(fullThreadUser); .threadUser(fullThreadUser);
if(anonymous) { if(anonymous) {
@@ -798,7 +824,8 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
return modMailMessage.getCreatedMessageInChannel().equals(message.getIdLong()); return modMailMessage.getCreatedMessageInChannel().equals(message.getIdLong());
} }
}) })
.findFirst().orElseThrow(() -> new AbstractoRunTimeException("Could not find desired message in list of messages in thread. This should not happen, as we just retrieved them from the same place.")); .findFirst()
.orElseThrow(() -> new AbstractoRunTimeException("Could not find desired message in list of messages in thread. This should not happen, as we just retrieved them from the same place."));
User author = authors.getOrDefault(modmailMessage.getAuthor().getUserReference().getId(), message.getJDA().getSelfUser()); User author = authors.getOrDefault(modmailMessage.getAuthor().getUserReference().getId(), message.getJDA().getSelfUser());
ModMailLoggedMessageModel modMailLoggedMessageModel = ModMailLoggedMessageModel modMailLoggedMessageModel =
ModMailLoggedMessageModel ModMailLoggedMessageModel

View File

@@ -8,6 +8,9 @@ import lombok.Setter;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import java.util.List;
import java.util.Map;
/** /**
* Model used to render the response by staff members to the DM channel with the user * Model used to render the response by staff members to the DM channel with the user
*/ */
@@ -36,6 +39,8 @@ public class ModMailModeratorReplyModel {
* Whether or not the reply should be shown anonymous * Whether or not the reply should be shown anonymous
*/ */
private Boolean anonymous; private Boolean anonymous;
private List<String> attachedImageUrls;
private Map<String, String> remainingAttachments;
/** /**
* The {@link ModMailThread} to reply to * The {@link ModMailThread} to reply to
*/ */

View File

@@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Model used to render the response by the user to the mod mail thread channel. * Model used to render the response by the user to the mod mail thread channel.
@@ -23,6 +24,8 @@ public class ModMailUserReplyModel {
* The {@link Message} which was posted, which contains all the possible information * The {@link Message} which was posted, which contains all the possible information
*/ */
private Message postedMessage; private Message postedMessage;
private List<String> attachedImageUrls;
private Map<String, String> remainingAttachments;
/** /**
* List of {@link Member} which are registered as subscribers for a particular mod mail thread and will be pinged * List of {@link Member} which are registered as subscribers for a particular mod mail thread and will be pinged
* when the user sends a new message * when the user sends a new message

View File

@@ -314,12 +314,12 @@ public class ChannelServiceBean implements ChannelService {
messageAction = channel.editMessageById(messageId, messageToSend.getMessages().get(0)); messageAction = channel.editMessageById(messageId, messageToSend.getMessages().get(0));
if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) { if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) {
log.debug("Also editing the embed for message {}.", messageId); log.debug("Also editing the embed for message {}.", messageId);
messageAction = messageAction.setEmbeds(messageToSend.getEmbeds().get(0)); messageAction = messageAction.setEmbeds(messageToSend.getEmbeds());
} }
} else { } else {
log.debug("Editing message {} with new embeds.", messageId); log.debug("Editing message {} with new embeds.", messageId);
if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) { if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) {
messageAction = channel.editMessageEmbedsById(messageId, messageToSend.getEmbeds().get(0)); messageAction = channel.editMessageEmbedsById(messageId, messageToSend.getEmbeds());
} else { } else {
throw new IllegalArgumentException("Message to send did not contain anything to send."); throw new IllegalArgumentException("Message to send did not contain anything to send.");
} }

View File

@@ -201,10 +201,6 @@ public class TemplateServiceBean implements TemplateService {
if(messageConfiguration.getMessageConfig() != null && messageConfiguration.getMessageConfig().getMessageLimit() != null) { if(messageConfiguration.getMessageConfig() != null && messageConfiguration.getMessageConfig().getMessageLimit() != null) {
messageLimit = Math.min(messageLimit, messageConfiguration.getMessageConfig().getMessageLimit()); messageLimit = Math.min(messageLimit, messageConfiguration.getMessageConfig().getMessageLimit());
} }
if(embeds.size() > messageLimit) {
log.info("Limiting size of embeds. Max allowed: {}, currently: {}.", messageLimit, embeds.size());
embeds.subList(messageLimit.intValue(), embeds.size()).clear();
}
if(messages.size() > messageLimit) { if(messages.size() > messageLimit) {
log.info("Limiting size of messages. Max allowed: {}, currently: {}.", messageLimit, messages.size()); log.info("Limiting size of messages. Max allowed: {}, currently: {}.", messageLimit, messages.size());
messages.subList(messageLimit.intValue(), messages.size()).clear(); messages.subList(messageLimit.intValue(), messages.size()).clear();