mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-14 03:45:57 +00:00
[AB-358] upgrading to JDA 5
removal of jda-utils adding message context commands [AB-360] fixing confirmation buttons being triggered by somebody not the author
This commit is contained in:
@@ -43,7 +43,11 @@ public class SlowMode extends AbstractConditionableCommand {
|
||||
throw new EntityGuildMismatchException();
|
||||
}
|
||||
} else {
|
||||
channel = commandContext.getChannel();
|
||||
if(commandContext.getChannel() instanceof TextChannel) {
|
||||
channel = (TextChannel) commandContext.getChannel();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not a text channel.");
|
||||
}
|
||||
}
|
||||
return slowModeService.setSlowMode(channel, duration)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
|
||||
@@ -28,10 +28,7 @@ import dev.sheldan.abstracto.scheduling.service.SchedulerService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -159,7 +156,7 @@ public class MuteServiceBean implements MuteService {
|
||||
String muteNotificationMessage = templateService.renderTemplate(MUTE_NOTIFICATION_TEMPLATE, muteNotification, message.getServerId());
|
||||
CompletableFuture<Message> messageCompletableFuture = messageService.sendMessageToUser(memberBeingMuted.getUser(), muteNotificationMessage);
|
||||
messageCompletableFuture.exceptionally(throwable -> {
|
||||
TextChannel feedBackChannel = channelService.getTextChannelFromServer(message.getServerId(), message.getChannelId());
|
||||
GuildMessageChannel feedBackChannel = channelService.getMessageChannelFromServer(message.getServerId(), message.getChannelId());
|
||||
channelService.sendTextToChannel(throwable.getMessage(), feedBackChannel).whenComplete((exceptionMessage, innerThrowable) -> {
|
||||
notificationFuture.complete(null);
|
||||
log.info("Successfully notified user {} in server {} about mute.", memberBeingMuted.getId(), memberBeingMuted.getGuild().getId());
|
||||
|
||||
@@ -9,10 +9,7 @@ import dev.sheldan.abstracto.moderation.model.template.command.PurgeStatusUpdate
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageHistory;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.utils.MiscUtil;
|
||||
import net.dv8tion.jda.api.utils.TimeUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -51,11 +48,11 @@ public class PurgeServiceBean implements PurgeService {
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> purgeMessagesInChannel(Integer amountToDelete, TextChannel channel, Long startId, Member purgedMember) {
|
||||
public CompletableFuture<Void> purgeMessagesInChannel(Integer amountToDelete, GuildMessageChannel channel, Long startId, Member purgedMember) {
|
||||
return purgeMessages(amountToDelete, channel, startId, purgedMember, amountToDelete, 0, 0L);
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> purgeMessages(Integer amountToDelete, TextChannel channel, Long startId, Member purgedMember, Integer totalCount, Integer currentCount, Long statusMessageId) {
|
||||
private CompletableFuture<Void> purgeMessages(Integer amountToDelete, GuildMessageChannel channel, Long startId, Member purgedMember, Integer totalCount, Integer currentCount, Long statusMessageId) {
|
||||
|
||||
int toDeleteInThisIteration;
|
||||
if(amountToDelete >= PURGE_MAX_MESSAGES){
|
||||
@@ -106,9 +103,12 @@ public class PurgeServiceBean implements PurgeService {
|
||||
return CompletableFuture.allOf(retrievalFuture, deletionFuture);
|
||||
}
|
||||
|
||||
private void bulkDeleteMessages(TextChannel channel, CompletableFuture<Void> deletionFuture, List<Message> messagesToDeleteNow, Consumer<Void> consumer) {
|
||||
private void bulkDeleteMessages(GuildMessageChannel channel, CompletableFuture<Void> deletionFuture, List<Message> messagesToDeleteNow, Consumer<Void> consumer) {
|
||||
try {
|
||||
channelService.deleteMessagesInChannel(channel, messagesToDeleteNow).queue(consumer, deletionFuture::completeExceptionally);
|
||||
channelService.deleteMessagesInChannel(channel, messagesToDeleteNow).thenAccept(consumer).exceptionally(throwable -> {
|
||||
deletionFuture.completeExceptionally(throwable);
|
||||
return null;
|
||||
});
|
||||
} catch (IllegalArgumentException e) {
|
||||
channelService.sendTextToChannel(e.getMessage(), channel);
|
||||
log.warn("Failed to bulk delete, message was most likely too old to delete by bulk.", e);
|
||||
@@ -116,7 +116,7 @@ public class PurgeServiceBean implements PurgeService {
|
||||
}
|
||||
}
|
||||
|
||||
private CompletableFuture<Long> getOrCreatedStatusMessage(TextChannel channel, Integer totalCount, Long statusMessageId) {
|
||||
private CompletableFuture<Long> getOrCreatedStatusMessage(GuildMessageChannel channel, Integer totalCount, Long statusMessageId) {
|
||||
CompletableFuture<Long> statusMessageFuture;
|
||||
if(statusMessageId == 0) {
|
||||
log.debug("Creating new status message in channel {} in server {} because of puring.", channel.getIdLong(), channel.getGuild().getId());
|
||||
@@ -151,7 +151,7 @@ public class PurgeServiceBean implements PurgeService {
|
||||
return messagesToDeleteNow;
|
||||
}
|
||||
|
||||
private Consumer<Void> deletionConsumer(Integer amountToDelete, TextChannel channel, Member purgedMember, Integer totalCount, Integer currentCount, CompletableFuture<Void> deletionFuture, Long currentStatusMessageId, Message earliestMessage) {
|
||||
private Consumer<Void> deletionConsumer(Integer amountToDelete, GuildMessageChannel channel, Member purgedMember, Integer totalCount, Integer currentCount, CompletableFuture<Void> deletionFuture, Long currentStatusMessageId, Message earliestMessage) {
|
||||
return aVoid -> {
|
||||
if (amountToDelete >= 1) {
|
||||
log.debug("Still more than 1 message to delete. Continuing.");
|
||||
@@ -181,7 +181,7 @@ public class PurgeServiceBean implements PurgeService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> purgeMessagesInChannel(Integer count, TextChannel channel, Message origin, Member purgingRestriction) {
|
||||
public CompletableFuture<Void> purgeMessagesInChannel(Integer count, GuildMessageChannel channel, Message origin, Member purgingRestriction) {
|
||||
return purgeMessagesInChannel(count, channel, origin.getIdLong(), purgingRestriction);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import dev.sheldan.abstracto.moderation.model.template.listener.ReportReactionNo
|
||||
import dev.sheldan.abstracto.moderation.service.management.ModerationUserManagementService;
|
||||
import dev.sheldan.abstracto.moderation.service.management.ReactionReportManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -92,7 +93,7 @@ public class ReactionReportServiceBean implements ReactionReportService {
|
||||
ReactionReport report = recentReportOptional.get();
|
||||
log.info("Report is already present in channel {} with message {}. Updating field.", report.getReportChannel().getId(), report.getReportMessageId());
|
||||
report.setReportCount(report.getReportCount() + 1);
|
||||
TextChannel reportTextChannel = channelService.getTextChannelFromServer(serverId, report.getReportChannel().getId());
|
||||
GuildMessageChannel reportTextChannel = channelService.getMessageChannelFromServer(serverId, report.getReportChannel().getId());
|
||||
return channelService.editFieldValueInMessage(reportTextChannel, report.getReportMessageId(), 0, report.getReportCount().toString())
|
||||
.thenAccept(message -> self.updateModerationUserReportCooldown(reporter));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.sheldan.abstracto.core.exception.ChannelNotInGuildException;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -35,7 +35,7 @@ public class ReactionReportManagementServiceBean implements ReactionReportManage
|
||||
|
||||
@Override
|
||||
public ReactionReport createReactionReport(CachedMessage reportedMessage, Message reportMessage) {
|
||||
AChannel reportChannel = channelManagementService.loadChannel(reportMessage.getTextChannel());
|
||||
AChannel reportChannel = channelManagementService.loadChannel(reportMessage.getChannel());
|
||||
AChannel reportedChannel = channelManagementService.loadChannel(reportedMessage.getChannelId());
|
||||
AUserInAServer reportedUser = userInServerManagementService.loadOrCreateUser(reportedMessage.getAuthorAsServerUser());
|
||||
ReactionReport report = ReactionReport
|
||||
|
||||
@@ -32,7 +32,7 @@ public class SlowModeTest {
|
||||
public void testExecuteSlowModeWithDurationCurrentChannel() {
|
||||
String duration = "1m";
|
||||
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(duration));
|
||||
when(slowModeService.setSlowMode(parameters.getChannel(), Duration.ofMinutes(1))).thenReturn(CompletableFuture.completedFuture(null));
|
||||
when(slowModeService.setSlowMode((TextChannel) parameters.getChannel(), Duration.ofMinutes(1))).thenReturn(CompletableFuture.completedFuture(null));
|
||||
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class SlowModeTest {
|
||||
public void testDisableSlowModeCurrentChannel() {
|
||||
String duration = "off";
|
||||
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(duration));
|
||||
when(slowModeService.setSlowMode(parameters.getChannel(), Duration.ZERO)).thenReturn(CompletableFuture.completedFuture(null));
|
||||
when(slowModeService.setSlowMode((TextChannel) parameters.getChannel(), Duration.ZERO)).thenReturn(CompletableFuture.completedFuture(null));
|
||||
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
|
||||
}
|
||||
|
||||
@@ -1,300 +0,0 @@
|
||||
package dev.sheldan.abstracto.moderation.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.metric.service.MetricService;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.MessageService;
|
||||
import dev.sheldan.abstracto.moderation.exception.NoMessageFoundException;
|
||||
import dev.sheldan.abstracto.moderation.model.template.command.PurgeStatusUpdateModel;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||
import net.dv8tion.jda.api.utils.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PurgeServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private PurgeServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private ChannelService channelService;
|
||||
|
||||
@Mock
|
||||
private MessageService messageService;
|
||||
|
||||
@Mock
|
||||
private TemplateService templateService;
|
||||
|
||||
@Mock
|
||||
private MetricService metricService;
|
||||
|
||||
@Mock
|
||||
private TextChannel textChannel;
|
||||
|
||||
@Mock
|
||||
private Member purgedMember;
|
||||
|
||||
private static final Long START_MESSAGE_ID = 4L;
|
||||
private static final Long STATUS_MESSAGE_ID = 7L;
|
||||
private static final Long AUTHOR_ID = 17L;
|
||||
|
||||
@Mock
|
||||
private User messageAuthor;
|
||||
|
||||
@Mock
|
||||
private Message firstMessage;
|
||||
|
||||
@Mock
|
||||
private Message secondMessage;
|
||||
|
||||
@Mock
|
||||
private Message thirdMessage;
|
||||
|
||||
@Mock
|
||||
private Message fourthMessage;
|
||||
|
||||
@Mock
|
||||
private MessageHistory history;
|
||||
|
||||
@Mock
|
||||
private MessageToSend firstStatusUpdateMessage;
|
||||
|
||||
@Mock
|
||||
private RestAction deleteMessagesAction;
|
||||
|
||||
@Mock
|
||||
private AuditableRestAction deleteStatusAction;
|
||||
|
||||
@Mock
|
||||
private Guild guild;
|
||||
|
||||
private static final Long SERVER_ID = 1L;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
when(textChannel.getGuild()).thenReturn(guild);
|
||||
when(guild.getId()).thenReturn(SERVER_ID.toString());
|
||||
when(guild.getIdLong()).thenReturn(SERVER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeMessageViaStartMessage() {
|
||||
Integer amountToDelete = 50;
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
setupOneMessageBatch(getDeletableMessageId(), getDeletableMessageId());
|
||||
|
||||
Message messageToStartOffAT = Mockito.mock(Message.class);
|
||||
when(messageToStartOffAT.getIdLong()).thenReturn(START_MESSAGE_ID);
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, messageToStartOffAT, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertNull(throwable));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(1)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
verify(metricService, times(1)).incrementCounter(any(), eq((long) amountToDelete));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeMessageNotNoUser() {
|
||||
Integer amountToDelete = 50;
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
when(firstMessage.getId()).thenReturn(getDeletableMessageId().toString());
|
||||
when(secondMessage.getId()).thenReturn(getDeletableMessageId().toString());
|
||||
|
||||
setupFirstMessageHistoryMocks();
|
||||
setupStatusMessageMocks();
|
||||
mockQueueDoubleVoidConsumer(deleteMessagesAction);
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, null);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertNull(throwable));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(1)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
verify(metricService, times(1)).incrementCounter(any(), eq((long) amountToDelete));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeSingleMessage() {
|
||||
Integer amountToDelete = 50;
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
when(firstMessage.getId()).thenReturn(getDeletableMessageId().toString());
|
||||
when(firstMessage.getAuthor()).thenReturn(messageAuthor);
|
||||
setupMembersWithAuthorId();
|
||||
|
||||
List<Message> messagesToDelete = Arrays.asList(firstMessage);
|
||||
when(history.getRetrievedHistory()).thenReturn(messagesToDelete);
|
||||
setupStatusMessageMocks();
|
||||
AuditableRestAction auditableRestAction = Mockito.mock(AuditableRestAction.class);
|
||||
when(messageService.deleteMessageWithAction(firstMessage)).thenReturn(auditableRestAction);
|
||||
mockQueueDoubleVoidConsumer(auditableRestAction);
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertNull(throwable));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(1)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
verify(metricService, times(1)).incrementCounter(any(), eq((long) amountToDelete));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeMessagesInTwoIterationsSecondIterationsTooOld() {
|
||||
Integer amountToDelete = 150;
|
||||
Long latestDeletedMessageId = getDeletableMessageId();
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete - 50)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
MessageHistory secondHistory = Mockito.mock(MessageHistory.class);
|
||||
when(channelService.getHistoryOfChannel(textChannel, latestDeletedMessageId, 50)).thenReturn(CompletableFuture.completedFuture(secondHistory));
|
||||
|
||||
when(secondMessage.getIdLong()).thenReturn(latestDeletedMessageId);
|
||||
when(thirdMessage.getId()).thenReturn(getNotDeletableMessageId().toString());
|
||||
when(fourthMessage.getId()).thenReturn(getNotDeletableMessageId().toString());
|
||||
|
||||
setupOneMessageBatch(getDeletableMessageId(), latestDeletedMessageId);
|
||||
|
||||
List<Message> secondMessagesToDelete = Arrays.asList(thirdMessage, fourthMessage);
|
||||
when(secondHistory.getRetrievedHistory()).thenReturn(secondMessagesToDelete);
|
||||
|
||||
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertTrue(throwable.getCause() instanceof NoMessageFoundException));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(1)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
ArgumentCaptor<Long> deleted = ArgumentCaptor.forClass(Long.class);
|
||||
verify(metricService, times(2)).incrementCounter(any(), deleted.capture());
|
||||
List<Long> capturedValues = deleted.getAllValues();
|
||||
Assert.assertEquals(2, capturedValues.size());
|
||||
Assert.assertEquals(100, capturedValues.get(0).longValue());
|
||||
Assert.assertEquals(50, capturedValues.get(1).longValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPurgeMessagesInTwoIterations() {
|
||||
Integer amountToDelete = 150;
|
||||
Long latestDeletedMessageId = getDeletableMessageId();
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete - 50)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
MessageHistory secondHistory = Mockito.mock(MessageHistory.class);
|
||||
when(channelService.getHistoryOfChannel(textChannel, latestDeletedMessageId, 50)).thenReturn(CompletableFuture.completedFuture(secondHistory));
|
||||
|
||||
when(secondMessage.getIdLong()).thenReturn(latestDeletedMessageId);
|
||||
|
||||
setupOneMessageBatch(getDeletableMessageId(), latestDeletedMessageId);
|
||||
|
||||
setupFirstMessages(thirdMessage, getDeletableMessageId(), fourthMessage, latestDeletedMessageId, messageAuthor);
|
||||
|
||||
RestAction secondDeleteMessagesAction = Mockito.mock(RestAction.class);
|
||||
List<Message> secondMessagesToDelete = Arrays.asList(thirdMessage, fourthMessage);
|
||||
when(secondHistory.getRetrievedHistory()).thenReturn(secondMessagesToDelete);
|
||||
when(channelService.deleteMessagesInChannel(textChannel, secondMessagesToDelete)).thenReturn(secondDeleteMessagesAction);
|
||||
|
||||
|
||||
mockQueueDoubleVoidConsumer(secondDeleteMessagesAction);
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertNull(throwable));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(2)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
ArgumentCaptor<Long> deleted = ArgumentCaptor.forClass(Long.class);
|
||||
verify(metricService, times(2)).incrementCounter(any(), deleted.capture());
|
||||
List<Long> capturedValues = deleted.getAllValues();
|
||||
Assert.assertEquals(2, capturedValues.size());
|
||||
Assert.assertEquals(100, capturedValues.get(0).longValue());
|
||||
Assert.assertEquals(50, capturedValues.get(1).longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeMessagesInOneIteration() {
|
||||
Integer amountToDelete = 50;
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
setupOneMessageBatch(getDeletableMessageId(), getDeletableMessageId());
|
||||
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertNull(throwable));
|
||||
verify(deleteStatusAction, times(1)).queueAfter(5, TimeUnit.SECONDS);
|
||||
verify(messageService, times(1)).updateStatusMessage(eq(textChannel), anyLong(), any());
|
||||
verify(metricService, times(1)).incrementCounter(any(), eq((long) amountToDelete));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPurgeTooOldMessage() {
|
||||
Integer amountToDelete = 50;
|
||||
when(channelService.getHistoryOfChannel(textChannel, START_MESSAGE_ID, amountToDelete)).thenReturn(CompletableFuture.completedFuture(history));
|
||||
|
||||
when(firstMessage.getId()).thenReturn(getNotDeletableMessageId().toString());
|
||||
|
||||
when(history.getRetrievedHistory()).thenReturn(Arrays.asList(firstMessage));
|
||||
setupStatusMessageMocks();
|
||||
CompletableFuture<Void> futures = testUnit.purgeMessagesInChannel(amountToDelete, textChannel, START_MESSAGE_ID, purgedMember);
|
||||
futures.whenComplete((aVoid, throwable) -> Assert.assertTrue(throwable.getCause() instanceof NoMessageFoundException));
|
||||
}
|
||||
|
||||
private void setupOneMessageBatch(Long deletableMessageId, Long deletableMessageId2) {
|
||||
setupFirstMessages(firstMessage, deletableMessageId, secondMessage, deletableMessageId2, messageAuthor);
|
||||
setupMembersWithAuthorId();
|
||||
setupFirstMessageHistoryMocks();
|
||||
mockQueueDoubleVoidConsumer(deleteMessagesAction);
|
||||
setupStatusMessageMocks();
|
||||
}
|
||||
|
||||
public void mockQueueDoubleVoidConsumer(RestAction action) {
|
||||
doAnswer(invocationOnMock -> {
|
||||
Object consumerObj = invocationOnMock.getArguments()[0];
|
||||
if(consumerObj instanceof Consumer) {
|
||||
Consumer<Void> consumer = (Consumer) consumerObj;
|
||||
consumer.accept(null);
|
||||
}
|
||||
return null;
|
||||
}).when(action).queue(any(Consumer.class), any(Consumer.class));
|
||||
}
|
||||
|
||||
private void setupFirstMessageHistoryMocks() {
|
||||
List<Message> messagesToDelete = Arrays.asList(firstMessage, secondMessage);
|
||||
when(history.getRetrievedHistory()).thenReturn(messagesToDelete);
|
||||
when(channelService.deleteMessagesInChannel(textChannel, messagesToDelete)).thenReturn(deleteMessagesAction);
|
||||
}
|
||||
|
||||
private void setupStatusMessageMocks() {
|
||||
when(templateService.renderTemplateToMessageToSend(eq("purge_status_update"), any(PurgeStatusUpdateModel.class), eq(SERVER_ID))).thenReturn(firstStatusUpdateMessage);
|
||||
when(messageService.createStatusMessageId(firstStatusUpdateMessage, textChannel)).thenReturn(CompletableFuture.completedFuture(STATUS_MESSAGE_ID));
|
||||
when(textChannel.deleteMessageById(STATUS_MESSAGE_ID)).thenReturn(deleteStatusAction);
|
||||
}
|
||||
|
||||
private void setupMembersWithAuthorId() {
|
||||
when(messageAuthor.getIdLong()).thenReturn(AUTHOR_ID);
|
||||
when(purgedMember.getIdLong()).thenReturn(AUTHOR_ID);
|
||||
}
|
||||
|
||||
private void setupFirstMessages(Message firstMessageToMock, Long firstMessageId, Message secondMessageToMock, Long secondMessageId, User author) {
|
||||
when(firstMessageToMock.getId()).thenReturn(firstMessageId.toString());
|
||||
when(firstMessageToMock.getAuthor()).thenReturn(author);
|
||||
when(secondMessageToMock.getId()).thenReturn(secondMessageId.toString());
|
||||
when(secondMessageToMock.getAuthor()).thenReturn(author);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Long getDeletableMessageId() {
|
||||
return TimeUtil.getDiscordTimestamp((System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000)));
|
||||
}
|
||||
|
||||
private Long getNotDeletableMessageId() {
|
||||
return TimeUtil.getDiscordTimestamp((System.currentTimeMillis() - (21 * 24 * 60 * 60 * 1000)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.managers.ChannelManager;
|
||||
import net.dv8tion.jda.api.managers.channel.ChannelManager;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
|
||||
Reference in New Issue
Block a user