mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-26 23:17:31 +00:00
[AB-57] [AB-61] reworked commands and services to work with completable futures and moved the database operations to the very last operation so we have transaction safety in more areas
added some cache annotations to the default repository functions reworked how the undo cations are processed within commands, they are executed in a post command listener when the state is error added a counter id to generate ids to be unique within servers, changed a few tables to be unique within a server added future utils class for wrapping a list of futures into one moved abstracto tables to separate schema in the installer refactored experience gain to work with more futures and delayed database access
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -32,7 +33,7 @@ public class Ban extends AbstractConditionableCommand {
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Member member = (Member) parameters.get(0);
|
||||
@@ -43,8 +44,8 @@ public class Ban extends AbstractConditionableCommand {
|
||||
banLogModel.setBannedUser(member);
|
||||
banLogModel.setBanningUser(commandContext.getAuthor());
|
||||
banLogModel.setReason(reason);
|
||||
banService.banMember(member, reason, banLogModel);
|
||||
return CommandResult.fromSuccess();
|
||||
return banService.banMember(member, reason, banLogModel)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,6 +58,7 @@ public class Ban extends AbstractConditionableCommand {
|
||||
.name("ban")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class BanId extends AbstractConditionableCommand {
|
||||
@@ -27,7 +28,7 @@ public class BanId extends AbstractConditionableCommand {
|
||||
private BanService banService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Long userId = (Long) parameters.get(0);
|
||||
@@ -37,9 +38,8 @@ public class BanId extends AbstractConditionableCommand {
|
||||
banLogModel.setBannedUserId(userId);
|
||||
banLogModel.setBanningUser(commandContext.getAuthor());
|
||||
banLogModel.setReason(reason);
|
||||
banService.banMember(commandContext.getGuild().getIdLong(), userId, reason, banLogModel);
|
||||
|
||||
return CommandResult.fromSuccess();
|
||||
return banService.banMember(commandContext.getGuild().getIdLong(), userId, reason, banLogModel)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,6 +52,7 @@ public class BanId extends AbstractConditionableCommand {
|
||||
.name("banId")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class DecayAllWarnings extends AbstractConditionableCommand {
|
||||
@@ -23,12 +24,12 @@ public class DecayAllWarnings extends AbstractConditionableCommand {
|
||||
private WarnService warnService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
boolean logWarnings = !parameters.isEmpty() ? (Boolean) parameters.get(0) : Boolean.FALSE;
|
||||
warnService.decayAllWarningsForServer(commandContext.getUserInitiatedContext().getServer(), logWarnings);
|
||||
return CommandResult.fromSuccess();
|
||||
return warnService.decayAllWarningsForServer(commandContext.getUserInitiatedContext().getServer(), logWarnings)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,6 +42,7 @@ public class DecayAllWarnings extends AbstractConditionableCommand {
|
||||
.name("decayAllWarnings")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class DecayWarnings extends AbstractConditionableCommand {
|
||||
@@ -23,9 +24,9 @@ public class DecayWarnings extends AbstractConditionableCommand {
|
||||
private WarnService warnService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
warnService.decayWarningsForServer(commandContext.getUserInitiatedContext().getServer());
|
||||
return CommandResult.fromSuccess();
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
return warnService.decayWarningsForServer(commandContext.getUserInitiatedContext().getServer())
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,6 +37,7 @@ public class DecayWarnings extends AbstractConditionableCommand {
|
||||
.name("decayWarnings")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -29,12 +29,8 @@ public class DeleteWarning extends AbstractConditionableCommand {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
Long warnId = (Long) commandContext.getParameters().getParameters().get(0);
|
||||
Optional<Warning> optional = warnManagementService.findById(warnId);
|
||||
optional.ifPresent(warning -> {
|
||||
if(warning.getWarnedUser().getServerReference().getId().equals(commandContext.getUserInitiatedContext().getServer().getId())) {
|
||||
warnManagementService.deleteWarning(warning);
|
||||
}
|
||||
});
|
||||
Optional<Warning> optional = warnManagementService.findById(warnId, commandContext.getGuild().getIdLong());
|
||||
optional.ifPresent(warning -> warnManagementService.deleteWarning(warning));
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class Kick extends AbstractConditionableCommand {
|
||||
@@ -30,19 +31,18 @@ public class Kick extends AbstractConditionableCommand {
|
||||
private KickServiceBean kickService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Member member = (Member) parameters.get(0);
|
||||
String defaultReason = templateService.renderSimpleTemplate(KICK_DEFAULT_REASON_TEMPLATE);
|
||||
String reason = parameters.size() == 2 ? (String) parameters.get(1) : defaultReason;
|
||||
|
||||
KickLogModel kickLogModel = (KickLogModel) ContextConverter.fromCommandContext(commandContext, KickLogModel.class);
|
||||
KickLogModel kickLogModel = (KickLogModel) ContextConverter.slimFromCommandContext(commandContext, KickLogModel.class);
|
||||
kickLogModel.setKickedUser(member);
|
||||
kickLogModel.setKickingUser(commandContext.getAuthor());
|
||||
kickLogModel.setReason(reason);
|
||||
kickService.kickMember(member, reason, kickLogModel);
|
||||
return CommandResult.fromSuccess();
|
||||
return kickService.kickMember(member, reason, kickLogModel)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -33,16 +33,15 @@ public class Purge extends AbstractConditionableCommand {
|
||||
private ExceptionUtils exceptionUtils;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
Integer amountOfMessages = (Integer) commandContext.getParameters().getParameters().get(0);
|
||||
Member memberToPurgeMessagesOf = null;
|
||||
if(commandContext.getParameters().getParameters().size() == 2) {
|
||||
memberToPurgeMessagesOf = (Member) commandContext.getParameters().getParameters().get(1);
|
||||
}
|
||||
CompletableFuture<Void> future = purgeService.purgeMessagesInChannel(amountOfMessages, commandContext.getChannel(), commandContext.getMessage(), memberToPurgeMessagesOf);
|
||||
future.whenComplete((aVoid, throwable) -> exceptionUtils.handleExceptionIfTemplatable(throwable, commandContext.getChannel()));
|
||||
return CommandResult.fromSelfDestruct();
|
||||
return purgeService.purgeMessagesInChannel(amountOfMessages, commandContext.getChannel(), commandContext.getMessage(), memberToPurgeMessagesOf)
|
||||
.thenApply(aVoid -> CommandResult.fromSelfDestruct());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,6 +54,7 @@ public class Purge extends AbstractConditionableCommand {
|
||||
.name("purge")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class SlowMode extends AbstractConditionableCommand {
|
||||
@@ -26,7 +27,7 @@ public class SlowMode extends AbstractConditionableCommand {
|
||||
private SlowModeService slowModeService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
TextChannel channel;
|
||||
String durationString = (String) commandContext.getParameters().getParameters().get(0);
|
||||
@@ -41,8 +42,8 @@ public class SlowMode extends AbstractConditionableCommand {
|
||||
} else {
|
||||
channel = commandContext.getChannel();
|
||||
}
|
||||
slowModeService.setSlowMode(channel, duration);
|
||||
return CommandResult.fromSuccess();
|
||||
return slowModeService.setSlowMode(channel, duration)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,6 +56,7 @@ public class SlowMode extends AbstractConditionableCommand {
|
||||
.name("slowmode")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -12,6 +12,7 @@ import dev.sheldan.abstracto.core.models.FullUserInServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModule;
|
||||
import dev.sheldan.abstracto.moderation.config.features.ModerationFeatures;
|
||||
import dev.sheldan.abstracto.moderation.converter.UserNotesConverter;
|
||||
@@ -25,6 +26,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class UserNotes extends AbstractConditionableCommand {
|
||||
@@ -45,7 +47,7 @@ public class UserNotes extends AbstractConditionableCommand {
|
||||
private UserNotesConverter userNotesConverter;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
List<UserNote> userNotes;
|
||||
@@ -65,8 +67,8 @@ public class UserNotes extends AbstractConditionableCommand {
|
||||
userNotes = userNoteManagementService.loadNotesForServer(commandContext.getUserInitiatedContext().getServer());
|
||||
}
|
||||
model.setUserNotes(userNotesConverter.fromNotes(userNotes));
|
||||
channelService.sendEmbedTemplateInChannel(USER_NOTES_RESPONSE_TEMPLATE, model, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(USER_NOTES_RESPONSE_TEMPLATE, model, commandContext.getChannel()))
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,6 +81,7 @@ public class UserNotes extends AbstractConditionableCommand {
|
||||
.name("userNotes")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -9,7 +9,7 @@ import dev.sheldan.abstracto.core.command.execution.*;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModule;
|
||||
import dev.sheldan.abstracto.moderation.config.features.ModerationFeatures;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.WarnLog;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.WarnContext;
|
||||
import dev.sheldan.abstracto.moderation.service.WarnService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -19,6 +19,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -32,19 +33,17 @@ public class Warn extends AbstractConditionableCommand {
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Member member = (Member) parameters.get(0);
|
||||
String defaultReason = templateService.renderSimpleTemplate(WARN_DEFAULT_REASON_TEMPLATE);
|
||||
String reason = parameters.size() == 2 ? (String) parameters.get(1) : defaultReason;
|
||||
WarnLog warnLogModel = (WarnLog) ContextConverter.fromCommandContext(commandContext, WarnLog.class);
|
||||
warnLogModel.setWarnedUser(member);
|
||||
warnLogModel.setMessage(commandContext.getMessage());
|
||||
WarnContext warnLogModel = (WarnContext) ContextConverter.slimFromCommandContext(commandContext, WarnContext.class);
|
||||
warnLogModel.setReason(reason);
|
||||
warnLogModel.setWarningUser(commandContext.getAuthor());
|
||||
warnService.warnUserWithLog(member, commandContext.getAuthor(), reason, warnLogModel, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
warnLogModel.setWarnedMember(member);
|
||||
return warnService.warnUserWithLog(warnLogModel)
|
||||
.thenApply(warning -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,6 +56,7 @@ public class Warn extends AbstractConditionableCommand {
|
||||
.name("warn")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -7,11 +7,11 @@ import dev.sheldan.abstracto.core.command.config.HelpInfo;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModule;
|
||||
import dev.sheldan.abstracto.moderation.config.features.ModerationFeatures;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.MuteLog;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.MuteContext;
|
||||
import dev.sheldan.abstracto.moderation.service.MuteService;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -21,6 +21,7 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class Mute extends AbstractConditionableCommand {
|
||||
@@ -29,18 +30,31 @@ public class Mute extends AbstractConditionableCommand {
|
||||
private MuteService muteService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Member member = (Member) parameters.get(0);
|
||||
Duration duration = (Duration) parameters.get(1);
|
||||
String reason = (String) parameters.get(2);
|
||||
MuteLog muteLogModel = (MuteLog) ContextConverter.fromCommandContext(commandContext, MuteLog.class);
|
||||
muteLogModel.setMessage(commandContext.getMessage());
|
||||
muteLogModel.setMutedUser(member);
|
||||
muteLogModel.setMutingUser(commandContext.getAuthor());
|
||||
muteService.muteMemberWithLog(member, commandContext.getAuthor(), reason, Instant.now().plus(duration), muteLogModel, commandContext.getMessage());
|
||||
return CommandResult.fromSuccess();
|
||||
ServerChannelMessage context = ServerChannelMessage
|
||||
.builder()
|
||||
.serverId(commandContext.getGuild().getIdLong())
|
||||
.channelId(commandContext.getChannel().getIdLong())
|
||||
.messageId(commandContext.getMessage().getIdLong())
|
||||
.build();
|
||||
MuteContext muteLogModel = MuteContext
|
||||
.builder()
|
||||
.muteDate(Instant.now())
|
||||
.muteTargetDate(Instant.now().plus(duration))
|
||||
.mutedUser(member)
|
||||
.reason(reason)
|
||||
.contextChannel(commandContext.getChannel())
|
||||
.message(commandContext.getMessage())
|
||||
.mutingUser(commandContext.getAuthor())
|
||||
.context(context)
|
||||
.build();
|
||||
return muteService.muteMemberWithLog(muteLogModel)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,6 +68,7 @@ public class Mute extends AbstractConditionableCommand {
|
||||
.name("mute")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.causesReaction(true)
|
||||
.supportsEmbedException(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -7,9 +7,10 @@ import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModule;
|
||||
import dev.sheldan.abstracto.moderation.config.features.ModerationFeatures;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Mute;
|
||||
import dev.sheldan.abstracto.moderation.service.MuteService;
|
||||
import dev.sheldan.abstracto.moderation.service.management.MuteManagementService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
@@ -19,6 +20,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class UnMute extends AbstractConditionableCommand {
|
||||
@@ -33,19 +35,18 @@ public class UnMute extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private UserInServerManagementService userInServerManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Member member = (Member) parameters.get(0);
|
||||
if(!muteManagementService.hasActiveMute(member)) {
|
||||
return CommandResult.fromError(templateService.renderSimpleTemplate(NO_ACTIVE_MUTE));
|
||||
}
|
||||
Mute mute = muteManagementService.getAMuteOf(member);
|
||||
muteService.unMuteUser(mute);
|
||||
muteService.cancelUnMuteJob(mute);
|
||||
muteService.completelyUnMuteMember(member);
|
||||
return CommandResult.fromSuccess();
|
||||
AUserInAServer userToUnMute = userInServerManagementService.loadUser(member);
|
||||
return muteService.unMuteUser(userToUnMute).thenApply(aVoid ->
|
||||
CommandResult.fromSuccess()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,6 +58,7 @@ public class UnMute extends AbstractConditionableCommand {
|
||||
.name("unMute")
|
||||
.module(ModerationModule.MODERATION)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package dev.sheldan.abstracto.moderation.job;
|
||||
|
||||
import dev.sheldan.abstracto.moderation.service.MuteService;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.DisallowConcurrentExecution;
|
||||
import org.quartz.JobExecutionContext;
|
||||
@@ -14,9 +16,12 @@ import org.springframework.stereotype.Component;
|
||||
@DisallowConcurrentExecution
|
||||
@Component
|
||||
@PersistJobDataAfterExecution
|
||||
@Getter
|
||||
@Setter
|
||||
public class UnMuteJob extends QuartzJobBean {
|
||||
|
||||
private Long muteId;
|
||||
private Long serverId;
|
||||
|
||||
@Autowired
|
||||
private MuteService muteService;
|
||||
@@ -24,14 +29,7 @@ public class UnMuteJob extends QuartzJobBean {
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
||||
log.info("Executing unMute job for mute {}", muteId);
|
||||
muteService.endMute(muteId);
|
||||
muteService.endMute(muteId, serverId);
|
||||
}
|
||||
|
||||
public Long getMuteId() {
|
||||
return muteId;
|
||||
}
|
||||
|
||||
public void setMuteId(Long muteId) {
|
||||
this.muteId = muteId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package dev.sheldan.abstracto.moderation.repository;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Mute;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.QueryHint;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface MuteRepository extends JpaRepository<Mute, Long> {
|
||||
@@ -18,5 +20,9 @@ public interface MuteRepository extends JpaRepository<Mute, Long> {
|
||||
Mute findTopByMutedUserAndMuteEndedFalse(AUserInAServer userInAServer);
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
List<Mute> findAllByMutedUserAndMuteEndedFalseOrderByIdDesc(AUserInAServer aUserInAServer);
|
||||
List<Mute> findAllByMutedUserAndMuteEndedFalseOrderByMuteId_IdDesc(AUserInAServer aUserInAServer);
|
||||
|
||||
@NotNull
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
Optional<Mute> findByMuteId_IdAndMuteId_ServerId(Long muteId, Long serverId);
|
||||
}
|
||||
|
||||
@@ -18,5 +18,6 @@ public interface MuteRoleRepository extends JpaRepository<MuteRole, Long> {
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
List<MuteRole> findAllByRoleServer(AServer server);
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
boolean existsByRoleServer(AServer server);
|
||||
}
|
||||
|
||||
@@ -4,13 +4,23 @@ import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.moderation.models.database.UserNote;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.QueryHint;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserNoteRepository extends JpaRepository<UserNote, Long> {
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
List<UserNote> findByUser(AUserInAServer aUserInAServer);
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
List<UserNote> findByUser_ServerReference(AServer server);
|
||||
|
||||
@Override
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
boolean existsById(@NonNull Long aLong);
|
||||
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@ package dev.sheldan.abstracto.moderation.repository;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Warning;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.QueryHint;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface WarnRepository extends JpaRepository<Warning, Long> {
|
||||
@@ -25,7 +28,15 @@ public interface WarnRepository extends JpaRepository<Warning, Long> {
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
Long countByWarnedUserAndDecayedFalse(AUserInAServer aUserInAServer);
|
||||
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
List<Warning> findByWarnedUser(AUserInAServer aUserInAServer);
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
Optional<Warning> findById(@NonNull Long aLong);
|
||||
|
||||
@NotNull
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
Optional<Warning> findByWarnId_IdAndWarnId_ServerId(Long warnId, Long serverId);
|
||||
}
|
||||
|
||||
@@ -4,16 +4,20 @@ import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
|
||||
import dev.sheldan.abstracto.core.models.context.ServerContext;
|
||||
import dev.sheldan.abstracto.core.service.BotService;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.posttargets.ModerationPostTarget;
|
||||
import dev.sheldan.abstracto.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.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 org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -32,31 +36,33 @@ public class BanServiceBean implements BanService {
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Override
|
||||
public void banMember(Member member, String reason, ServerContext banLog) {
|
||||
this.banUser(member.getGuild(), member.getIdLong(), reason);
|
||||
public CompletableFuture<Void> banMember(Member member, String reason, ServerContext banLog) {
|
||||
CompletableFuture<Void> banFuture = banUser(member.getGuild(), member.getIdLong(), reason);
|
||||
MessageToSend banLogMessage = templateService.renderEmbedTemplate(BAN_LOG_TEMPLATE, banLog);
|
||||
postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.BAN_LOG, member.getGuild().getIdLong());
|
||||
List<CompletableFuture<Message>> notificationFutures = postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.BAN_LOG, member.getGuild().getIdLong());
|
||||
return CompletableFuture.allOf(banFuture, FutureUtils.toSingleFutureGeneric(notificationFutures));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void banMember(Long guildId, Long userId, String reason, ServerContext banIdLog) {
|
||||
banUser(guildId, userId, reason);
|
||||
public CompletableFuture<Void> banMember(Long guildId, Long userId, String reason, ServerContext banIdLog) {
|
||||
CompletableFuture<Void> banFuture = banUser(guildId, userId, reason);
|
||||
MessageToSend banLogMessage = templateService.renderEmbedTemplate(BAN_ID_LOG_TEMPLATE, banIdLog);
|
||||
postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.BAN_LOG, guildId);
|
||||
List<CompletableFuture<Message>> notificationFutures = postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.BAN_LOG, guildId);
|
||||
return CompletableFuture.allOf(banFuture, FutureUtils.toSingleFutureGeneric(notificationFutures));
|
||||
}
|
||||
|
||||
private void banUser(Long guildId, Long userId, String reason) {
|
||||
private CompletableFuture<Void> banUser(Long guildId, Long userId, String reason) {
|
||||
Optional<Guild> guildByIdOptional = botService.getGuildById(guildId);
|
||||
if(guildByIdOptional.isPresent()) {
|
||||
log.info("Banning user {} in guild {}.", userId, guildId);
|
||||
banUser(guildByIdOptional.get(), userId, reason);
|
||||
return banUser(guildByIdOptional.get(), userId, reason);
|
||||
} else {
|
||||
log.warn("Guild {} not found. Not able to ban user {}", guildId, userId);
|
||||
throw new GuildNotFoundException(guildId);
|
||||
}
|
||||
}
|
||||
|
||||
private void banUser(Guild guild, Long userId, String reason) {
|
||||
guild.ban(userId.toString(), 0, reason).queue();
|
||||
private CompletableFuture<Void> banUser(Guild guild, Long userId, String reason) {
|
||||
return guild.ban(userId.toString(), 0, reason).submit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.moderation.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.posttargets.ModerationPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.KickLogModel;
|
||||
import dev.sheldan.abstracto.templating.model.MessageToSend;
|
||||
@@ -11,6 +12,8 @@ import net.dv8tion.jda.api.entities.Member;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class KickServiceBean implements KickService {
|
||||
@@ -24,15 +27,16 @@ public class KickServiceBean implements KickService {
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Override
|
||||
public void kickMember(Member member, String reason, KickLogModel kickLogModel) {
|
||||
public CompletableFuture<Void> kickMember(Member member, String reason, KickLogModel kickLogModel) {
|
||||
Guild guild = member.getGuild();
|
||||
log.info("Kicking user {} from guild {}", member.getUser().getIdLong(), guild.getIdLong());
|
||||
guild.kick(member, reason).queue();
|
||||
this.sendKickLog(kickLogModel);
|
||||
CompletableFuture<Void> kickFuture = guild.kick(member, reason).submit();
|
||||
CompletableFuture<Void> logFuture = this.sendKickLog(kickLogModel);
|
||||
return CompletableFuture.allOf(kickFuture, logFuture);
|
||||
}
|
||||
|
||||
private void sendKickLog(KickLogModel kickLogModel) {
|
||||
private CompletableFuture<Void> sendKickLog(KickLogModel kickLogModel) {
|
||||
MessageToSend warnLogMessage = templateService.renderEmbedTemplate(KICK_LOG_TEMPLATE, kickLogModel);
|
||||
postTargetService.sendEmbedInPostTarget(warnLogMessage, ModerationPostTarget.KICK_LOG, kickLogModel.getServer().getId());
|
||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(warnLogMessage, ModerationPostTarget.KICK_LOG, kickLogModel.getGuild().getIdLong()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,21 @@ package dev.sheldan.abstracto.moderation.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
|
||||
import dev.sheldan.abstracto.core.models.FullUserInServer;
|
||||
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.*;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.posttargets.MutingPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.exception.MuteRoleNotSetupException;
|
||||
import dev.sheldan.abstracto.moderation.exception.NoMuteFoundException;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Mute;
|
||||
import dev.sheldan.abstracto.moderation.models.database.MuteRole;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.MuteLog;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.MuteContext;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.MuteNotification;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.UnMuteLog;
|
||||
import dev.sheldan.abstracto.moderation.service.management.MuteManagementService;
|
||||
@@ -30,9 +34,11 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -68,7 +74,7 @@ public class MuteServiceBean implements MuteService {
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Autowired
|
||||
private MuteService self;
|
||||
private MuteServiceBean self;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("unmuteScheduler")
|
||||
@@ -77,12 +83,19 @@ public class MuteServiceBean implements MuteService {
|
||||
@Autowired
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Autowired
|
||||
private CounterService counterService;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
public static final String MUTE_LOG_TEMPLATE = "mute_log";
|
||||
public static final String UN_MUTE_LOG_TEMPLATE = "unmute_log";
|
||||
public static final String MUTE_NOTIFICATION_TEMPLATE = "mute_notification";
|
||||
public static final String MUTE_COUNTER_KEY = "MUTES";
|
||||
|
||||
@Override
|
||||
public Mute muteMember(Member memberToMute, Member mutingMember, String reason, Instant unMuteDate, Message message) {
|
||||
public CompletableFuture<Void> muteMember(Member memberToMute, Member mutingMember, String reason, Instant unMuteDate, ServerChannelMessage message) {
|
||||
FullUserInServer mutedUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(userInServerManagementService.loadUser(memberToMute))
|
||||
@@ -94,11 +107,11 @@ public class MuteServiceBean implements MuteService {
|
||||
.aUserInAServer(userInServerManagementService.loadUser(mutingMember))
|
||||
.member(mutingMember)
|
||||
.build();
|
||||
return muteUser(mutedUser, mutingUser, reason, unMuteDate, message);
|
||||
return muteUserInServer(mutedUser, mutingUser, reason, unMuteDate, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mute muteAUserInAServer(AUserInAServer userBeingMuted, AUserInAServer userMuting, String reason, Instant unMuteDate, Message message) {
|
||||
public CompletableFuture<Void> muteAUserInAServer(AUserInAServer userBeingMuted, AUserInAServer userMuting, String reason, Instant unMuteDate, ServerChannelMessage message) {
|
||||
FullUserInServer mutedUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(userBeingMuted)
|
||||
@@ -110,11 +123,11 @@ public class MuteServiceBean implements MuteService {
|
||||
.aUserInAServer(userMuting)
|
||||
.member(botService.getMemberInServer(userMuting))
|
||||
.build();
|
||||
return muteUser(mutedUser, mutingUser, reason, unMuteDate, message);
|
||||
return muteUserInServer(mutedUser, mutingUser, reason, unMuteDate, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mute muteUser(FullUserInServer userBeingMuted, FullUserInServer userMuting, String reason, Instant unMuteDate, Message message) {
|
||||
public CompletableFuture<Void> muteUserInServer(FullUserInServer userBeingMuted, FullUserInServer userMuting, String reason, Instant unMuteDate, ServerChannelMessage message) {
|
||||
AServer serverBeingMutedIn = userBeingMuted.getAUserInAServer().getServerReference();
|
||||
if(!muteRoleManagementService.muteRoleForServerExists(serverBeingMutedIn)) {
|
||||
log.error("Mute role for server {} has not been setup.", serverBeingMutedIn.getId());
|
||||
@@ -123,64 +136,73 @@ public class MuteServiceBean implements MuteService {
|
||||
Member memberBeingMuted = userBeingMuted.getMember();
|
||||
log.info("User {} mutes user {} until {}",
|
||||
memberBeingMuted.getIdLong(), userMuting.getMember().getIdLong(), unMuteDate);
|
||||
if(message != null) {
|
||||
log.trace("because of message {} in channel {} in server {}", message.getId(), message.getChannel().getId(), message.getGuild().getId());
|
||||
if(message.getMessageId() != null) {
|
||||
log.trace("because of message {} in channel {} in server {}", message.getMessageId(), message.getChannelId(), message.getServerId());
|
||||
} else {
|
||||
log.trace("This mute was not triggered by a message.");
|
||||
}
|
||||
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>();
|
||||
AUserInAServer userInServerBeingMuted = userBeingMuted.getAUserInAServer();
|
||||
applyMuteRole(userInServerBeingMuted);
|
||||
Mute mute = createMuteObject(userMuting, reason, unMuteDate, message, userInServerBeingMuted);
|
||||
futures.add(applyMuteRole(userInServerBeingMuted));
|
||||
Guild guild = memberBeingMuted.getGuild();
|
||||
if(memberBeingMuted.getVoiceState() != null && memberBeingMuted.getVoiceState().getChannel() != null) {
|
||||
guild.kickVoiceMember(memberBeingMuted).queue();
|
||||
futures.add(guild.kickVoiceMember(memberBeingMuted).submit());
|
||||
}
|
||||
sendMuteNotification(message, memberBeingMuted, mute, guild);
|
||||
|
||||
String triggerKey = startUnMuteJobFor(unMuteDate, mute);
|
||||
mute.setTriggerKey(triggerKey);
|
||||
muteManagementService.saveMute(mute);
|
||||
return mute;
|
||||
MuteNotification muteNotification = MuteNotification
|
||||
.builder()
|
||||
.muteTargetDate(unMuteDate)
|
||||
.reason(reason)
|
||||
.serverName(guild.getName())
|
||||
.build();
|
||||
futures.add(sendMuteNotification(message, memberBeingMuted, muteNotification));
|
||||
return FutureUtils.toSingleFutureGeneric(futures);
|
||||
}
|
||||
|
||||
private void sendMuteNotification(Message message, Member memberBeingMuted, Mute mute, Guild guild) {
|
||||
private CompletableFuture<Void> sendMuteNotification(ServerChannelMessage message, Member memberBeingMuted, MuteNotification muteNotification) {
|
||||
log.trace("Notifying the user about the mute.");
|
||||
MuteNotification muteNotification = MuteNotification.builder().mute(mute).serverName(guild.getName()).build();
|
||||
CompletableFuture<Void> notificationFuture = new CompletableFuture<>();
|
||||
String muteNotificationMessage = templateService.renderTemplate(MUTE_NOTIFICATION_TEMPLATE, muteNotification);
|
||||
MessageChannel textChannel = message != null ? message.getChannel() : null;
|
||||
messageService.sendMessageToUser(memberBeingMuted.getUser(), muteNotificationMessage, textChannel);
|
||||
CompletableFuture<Message> messageCompletableFuture = messageService.sendMessageToUser(memberBeingMuted.getUser(), muteNotificationMessage);
|
||||
messageCompletableFuture.exceptionally(throwable -> {
|
||||
TextChannel feedBackChannel = botService.getTextChannelFromServer(message.getServerId(), message.getChannelId());
|
||||
feedBackChannel.sendMessage(throwable.getMessage()).submit().whenComplete((exceptionMessage, innerThrowable) ->
|
||||
notificationFuture.complete(null)
|
||||
);
|
||||
return null;
|
||||
});
|
||||
messageCompletableFuture.thenAccept(message1 ->
|
||||
notificationFuture.complete(null)
|
||||
);
|
||||
return notificationFuture;
|
||||
}
|
||||
|
||||
private Mute createMuteObject(FullUserInServer userMuting, String reason, Instant unMuteDate, Message message, AUserInAServer userInServerBeingMuted) {
|
||||
AServerAChannelMessage origin = null;
|
||||
if(message != null) {
|
||||
long channelId = message.getChannel().getIdLong();
|
||||
AChannel channel = channelManagementService.loadChannel(channelId);
|
||||
origin = AServerAChannelMessage
|
||||
.builder()
|
||||
.channel(channel)
|
||||
.server(channel.getServer())
|
||||
.messageId(message.getIdLong())
|
||||
.build();
|
||||
}
|
||||
return muteManagementService.createMute(userInServerBeingMuted, userMuting.getAUserInAServer(), reason, unMuteDate, origin);
|
||||
private void createMuteObject(MuteContext muteContext, String triggerKey) {
|
||||
AChannel channel = channelManagementService.loadChannel(muteContext.getContext().getChannelId());
|
||||
AServerAChannelMessage origin = AServerAChannelMessage
|
||||
.builder()
|
||||
.channel(channel)
|
||||
.server(channel.getServer())
|
||||
.messageId(muteContext.getContext().getMessageId())
|
||||
.build();
|
||||
AUserInAServer userInServerBeingMuted = userInServerManagementService.loadUser(muteContext.getMutedUser());
|
||||
AUserInAServer userInServerMuting = userInServerManagementService.loadUser(muteContext.getMutedUser());
|
||||
muteManagementService.createMute(userInServerBeingMuted, userInServerMuting, muteContext.getReason(), muteContext.getMuteTargetDate(), origin, triggerKey, muteContext.getMuteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyMuteRole(AUserInAServer aUserInAServer) {
|
||||
public CompletableFuture<Void> applyMuteRole(AUserInAServer aUserInAServer) {
|
||||
MuteRole muteRole = muteRoleManagementService.retrieveMuteRoleForServer(aUserInAServer.getServerReference());
|
||||
roleService.addRoleToUser(aUserInAServer, muteRole.getRole());
|
||||
return roleService.addRoleToUserFuture(aUserInAServer, muteRole.getRole());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String startUnMuteJobFor(Instant unMuteDate, Mute mute) {
|
||||
public String startUnMuteJobFor(Instant unMuteDate, Long muteId, Long serverId) {
|
||||
Duration muteDuration = Duration.between(Instant.now(), unMuteDate);
|
||||
if(muteDuration.getSeconds() < 60) {
|
||||
log.trace("Directly scheduling the unMute, because it was below the threshold.");
|
||||
unMuteScheduler.schedule(() -> {
|
||||
try {
|
||||
self.endMute(mute.getId());
|
||||
self.endMute(muteId, serverId);
|
||||
} catch (Exception exception) {
|
||||
log.error("Failed to remind immediately.", exception);
|
||||
}
|
||||
@@ -189,7 +211,8 @@ public class MuteServiceBean implements MuteService {
|
||||
} else {
|
||||
log.trace("Starting scheduled job to execute unMute.");
|
||||
JobDataMap parameters = new JobDataMap();
|
||||
parameters.putAsString("muteId", mute.getId());
|
||||
parameters.putAsString("muteId", muteId);
|
||||
parameters.putAsString("serverId", serverId);
|
||||
return schedulerService.executeJobWithParametersOnce("unMuteJob", "moderation", parameters, Date.from(unMuteDate));
|
||||
}
|
||||
}
|
||||
@@ -202,61 +225,101 @@ public class MuteServiceBean implements MuteService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void muteMemberWithLog(Member memberToMute, Member memberMuting, String reason, Instant unMuteDate, MuteLog muteLog, Message message) {
|
||||
public CompletableFuture<Void> muteMemberWithLog(MuteContext context) {
|
||||
log.trace("Muting member with sending a mute log");
|
||||
Mute mute = muteMember(memberToMute, memberMuting, reason, unMuteDate, message);
|
||||
muteLog.setMute(mute);
|
||||
sendMuteLog(muteLog);
|
||||
AServer server = serverManagementService.loadOrCreate(context.getContext().getServerId());
|
||||
Long nextCounterValue = counterService.getNextCounterValue(server, MUTE_COUNTER_KEY);
|
||||
context.setMuteId(nextCounterValue);
|
||||
CompletableFuture<Void> mutingFuture = muteMember(context.getMutedUser(), context.getMutingUser(), context.getReason(), context.getMuteTargetDate(), context.getContext());
|
||||
CompletableFuture<Void> muteLogFuture = sendMuteLog(context);
|
||||
return CompletableFuture.allOf(mutingFuture, muteLogFuture).thenAccept(aVoid ->
|
||||
self.persistMute(context)
|
||||
);
|
||||
}
|
||||
|
||||
private void sendMuteLog(MuteLog muteLogModel) {
|
||||
@Transactional
|
||||
public void persistMute(MuteContext context) {
|
||||
String triggerKey = startUnMuteJobFor(context.getMuteTargetDate(), context.getMuteId(), context.getContext().getServerId());
|
||||
createMuteObject(context, triggerKey);
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> sendMuteLog(MuteContext muteLogModel) {
|
||||
log.trace("Sending mute log to the mute posttarget");
|
||||
MessageToSend message = templateService.renderEmbedTemplate(MUTE_LOG_TEMPLATE, muteLogModel);
|
||||
postTargetService.sendEmbedInPostTarget(message, MutingPostTarget.MUTE_LOG, muteLogModel.getServer().getId());
|
||||
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(message, MutingPostTarget.MUTE_LOG, muteLogModel.getContext().getServerId());
|
||||
return FutureUtils.toSingleFutureGeneric(completableFutures);
|
||||
}
|
||||
|
||||
private void sendUnMuteLog(UnMuteLog muteLogModel) {
|
||||
private CompletableFuture<Void> sendUnMuteLog(UnMuteLog muteLogModel) {
|
||||
log.trace("Sending unMute log to the mute posttarget");
|
||||
MessageToSend message = templateService.renderEmbedTemplate(UN_MUTE_LOG_TEMPLATE, muteLogModel);
|
||||
postTargetService.sendEmbedInPostTarget(message, MutingPostTarget.MUTE_LOG, muteLogModel.getServer().getId());
|
||||
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(message, MutingPostTarget.MUTE_LOG, muteLogModel.getServer().getId());
|
||||
return FutureUtils.toSingleFutureGeneric(completableFutures);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void unMuteUser(Mute mute) {
|
||||
if(Boolean.TRUE.equals(mute.getMuteEnded())) {
|
||||
log.info("Mute {} has ended already, user {} does not need to be unMuted anymore.", mute.getId(), mute.getMutedUser().getUserReference().getId());
|
||||
return;
|
||||
public CompletableFuture<Void> unMuteUser(AUserInAServer aUserInAServer) {
|
||||
if(muteManagementService.hasActiveMute(aUserInAServer)) {
|
||||
throw new NoMuteFoundException();
|
||||
}
|
||||
AServer mutingServer = mute.getMutingServer();
|
||||
Mute mute = muteManagementService.getAMuteOf(aUserInAServer);
|
||||
if(Boolean.TRUE.equals(mute.getMuteEnded())) {
|
||||
log.info("Mute {} has ended already, user {} does not need to be unMuted anymore.", mute.getMuteId().getId(), mute.getMutedUser().getUserReference().getId());
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
return endMute(mute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> endMute(Mute mute) {
|
||||
Long muteId = mute.getMuteId().getId();
|
||||
AServer mutingServer = mute.getServer();
|
||||
log.info("UnMuting {} in server {}", mute.getMutedUser().getUserReference().getId(), mutingServer.getId());
|
||||
MuteRole muteRole = muteRoleManagementService.retrieveMuteRoleForServer(mutingServer);
|
||||
log.trace("Using the mute role {} mapping to role {}", muteRole.getId(), muteRole.getRole().getId());
|
||||
Guild guild = botService.getGuildByIdNullable(mute.getMutingServer().getId());
|
||||
Guild guild = botService.getGuildByIdNullable(mutingServer.getId());
|
||||
CompletableFuture<Void> roleRemovalFuture;
|
||||
if(botService.isUserInGuild(guild, mute.getMutedUser())) {
|
||||
roleService.removeRoleFromUser(mute.getMutedUser(), muteRole.getRole());
|
||||
roleRemovalFuture = roleService.removeRoleFromUserFuture(mute.getMutedUser(), muteRole.getRole());
|
||||
} else {
|
||||
roleRemovalFuture = CompletableFuture.completedFuture(null);
|
||||
log.info("User to unMute left the guild.");
|
||||
}
|
||||
Long serverId = mutingServer.getId();
|
||||
UnMuteLog unMuteLog = UnMuteLog
|
||||
.builder()
|
||||
.mute(mute)
|
||||
.mutingUser(botService.getMemberInServer(mute.getMutingUser()))
|
||||
.unMutedUser(botService.getMemberInServer(mute.getMutedUser()))
|
||||
.guild(guild)
|
||||
.server(mute.getMutingServer())
|
||||
.server(mutingServer)
|
||||
.build();
|
||||
sendUnMuteLog(unMuteLog);
|
||||
mute.setMuteEnded(true);
|
||||
muteManagementService.saveMute(mute);
|
||||
CompletableFuture<Void> notificationFuture = sendUnMuteLog(unMuteLog);
|
||||
return CompletableFuture.allOf(roleRemovalFuture, notificationFuture).thenAccept(aVoid ->
|
||||
self.endMuteInDatabase(muteId, serverId)
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void endMuteInDatabase(Long muteId, Long serverId) {
|
||||
Optional<Mute> muteOptional = muteManagementService.findMute(muteId, serverId);
|
||||
muteOptional.ifPresent(mute ->
|
||||
completelyUnMuteUser(mute.getMutedUser())
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void endMute(Long muteId) {
|
||||
public CompletableFuture<Void> endMute(Long muteId, Long serverId) {
|
||||
log.info("UnMuting the mute {}", muteId);
|
||||
Optional<Mute> mute = muteManagementService.findMute(muteId);
|
||||
mute.ifPresent(this::unMuteUser);
|
||||
Optional<Mute> muteOptional = muteManagementService.findMute(muteId, serverId);
|
||||
if(muteOptional.isPresent()) {
|
||||
return endMute(muteOptional.get());
|
||||
} else {
|
||||
throw new NoMuteFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -19,26 +20,26 @@ public class SlowModeServiceBean implements SlowModeService {
|
||||
private BotService botService;
|
||||
|
||||
@Override
|
||||
public void setSlowMode(TextChannel channel, Duration duration) {
|
||||
public CompletableFuture<Void> setSlowMode(TextChannel channel, Duration duration) {
|
||||
log.info("Setting slow mode to {} in channel {} in server {}", duration.toString(), channel.getIdLong(), channel.getGuild().getId());
|
||||
long seconds = duration.getSeconds();
|
||||
if(seconds > TextChannel.MAX_SLOWMODE) {
|
||||
throw new IllegalArgumentException("Slow mode duration must be < " + TextChannel.MAX_SLOWMODE + " seconds.");
|
||||
}
|
||||
channel.getManager().setSlowmode((int) seconds).queue();
|
||||
return channel.getManager().setSlowmode((int) seconds).submit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableSlowMode(TextChannel channel) {
|
||||
setSlowMode(channel, Duration.ZERO);
|
||||
public CompletableFuture<Void> disableSlowMode(TextChannel channel) {
|
||||
return setSlowMode(channel, Duration.ZERO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSlowMode(AChannel channel, Duration duration) {
|
||||
public CompletableFuture<Void> setSlowMode(AChannel channel, Duration duration) {
|
||||
Optional<TextChannel> textChannelOptional = botService.getTextChannelFromServerOptional(channel.getServer().getId(), channel.getId());
|
||||
if(textChannelOptional.isPresent()) {
|
||||
TextChannel textChannel = textChannelOptional.get();
|
||||
this.setSlowMode(textChannel, duration);
|
||||
return this.setSlowMode(textChannel, duration);
|
||||
} else {
|
||||
throw new ChannelNotFoundException(channel.getId());
|
||||
}
|
||||
|
||||
@@ -1,26 +1,25 @@
|
||||
package dev.sheldan.abstracto.moderation.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.FullUserInServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.service.MessageService;
|
||||
import dev.sheldan.abstracto.core.service.*;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.features.WarningDecayFeature;
|
||||
import dev.sheldan.abstracto.moderation.config.posttargets.WarnDecayPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.config.posttargets.WarningPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.models.template.job.WarnDecayLogModel;
|
||||
import dev.sheldan.abstracto.moderation.models.template.job.WarnDecayWarning;
|
||||
import dev.sheldan.abstracto.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.WarnLog;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.WarnContext;
|
||||
import dev.sheldan.abstracto.moderation.models.template.commands.WarnNotification;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Warning;
|
||||
import dev.sheldan.abstracto.moderation.service.management.WarnManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.BotService;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -29,6 +28,8 @@ import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -55,73 +56,88 @@ public class WarnServiceBean implements WarnService {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
@Autowired
|
||||
private CounterService counterService;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Autowired
|
||||
private WarnServiceBean self;
|
||||
|
||||
public static final String WARN_LOG_TEMPLATE = "warn_log";
|
||||
public static final String WARN_NOTIFICATION_TEMPLATE = "warn_notification";
|
||||
public static final String WARNINGS_COUNTER_KEY = "WARNINGS";
|
||||
public static final String WARN_DECAY_LOG_TEMPLATE_KEY = "warn_decay_log";
|
||||
|
||||
@Override
|
||||
public Warning warnUser(AUserInAServer warnedAUserInAServer, AUserInAServer warningAUserInAServer, String reason, MessageChannel feedbackChannel) {
|
||||
FullUserInServer warnedUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(warnedAUserInAServer)
|
||||
.member(botService.getMemberInServer(warnedAUserInAServer))
|
||||
.build();
|
||||
|
||||
FullUserInServer warningUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(warningAUserInAServer)
|
||||
.member(botService.getMemberInServer(warningAUserInAServer))
|
||||
.build();
|
||||
return warnFullUser(warnedUser, warningUser, reason, feedbackChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Warning warnMember(Member warnedMember, Member warningMember, String reason, MessageChannel feedbackChannel) {
|
||||
FullUserInServer warnedUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(userInServerManagementService.loadUser(warnedMember))
|
||||
.member(warnedMember)
|
||||
.build();
|
||||
|
||||
FullUserInServer warningUser = FullUserInServer
|
||||
.builder()
|
||||
.aUserInAServer(userInServerManagementService.loadUser(warningMember))
|
||||
.member(warningMember)
|
||||
.build();
|
||||
return warnFullUser(warnedUser, warningUser, reason, feedbackChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Warning warnFullUser(FullUserInServer warnedMember, FullUserInServer warningMember, String reason, MessageChannel feedbackChannel) {
|
||||
Guild guild = warnedMember.getMember().getGuild();
|
||||
log.info("User {} is warning {} in server {}", warnedMember.getMember().getId(), warningMember.getMember().getId(), guild.getIdLong());
|
||||
Warning warning = warnManagementService.createWarning(warnedMember.getAUserInAServer(), warningMember.getAUserInAServer(), reason);
|
||||
WarnNotification warnNotification = WarnNotification.builder().warning(warning).serverName(guild.getName()).build();
|
||||
public CompletableFuture<Void> notifyAndLogFullUserWarning(WarnContext context) {
|
||||
AServer server = serverManagementService.loadOrCreate(context.getGuild().getIdLong());
|
||||
Long warningId = counterService.getNextCounterValue(server, WARNINGS_COUNTER_KEY);
|
||||
context.setWarnId(warningId);
|
||||
Member warnedMember = context.getWarnedMember();
|
||||
Member warningMember = context.getMember();
|
||||
Guild guild = warnedMember.getGuild();
|
||||
log.info("User {} is warning {} in server {}", warnedMember.getId(), warningMember.getId(), guild.getIdLong());
|
||||
WarnNotification warnNotification = WarnNotification.builder().reason(context.getReason()).warnId(warningId).serverName(guild.getName()).build();
|
||||
String warnNotificationMessage = templateService.renderTemplate(WARN_NOTIFICATION_TEMPLATE, warnNotification);
|
||||
messageService.sendMessageToUser(warnedMember.getMember().getUser(), warnNotificationMessage, feedbackChannel);
|
||||
return warning;
|
||||
List<CompletableFuture<Message>> futures = new ArrayList<>();
|
||||
futures.add(messageService.sendMessageToUser(warnedMember.getUser(), warnNotificationMessage));
|
||||
MessageToSend message = templateService.renderEmbedTemplate(WARN_LOG_TEMPLATE, context);
|
||||
futures.addAll(postTargetService.sendEmbedInPostTarget(message, WarningPostTarget.WARN_LOG, context.getGuild().getIdLong()));
|
||||
|
||||
return FutureUtils.toSingleFutureGeneric(futures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Warning warnUserWithLog(Member warnedMember, Member warningMember, String reason, WarnLog warnLog, MessageChannel feedbackChannel) {
|
||||
Warning warning = warnMember(warnedMember, warningMember, reason, feedbackChannel);
|
||||
warnLog.setWarning(warning);
|
||||
this.sendWarnLog(warnLog);
|
||||
return warning;
|
||||
public CompletableFuture<Void> warnUserWithLog(WarnContext context) {
|
||||
return notifyAndLogFullUserWarning(context).thenAccept(aVoid ->
|
||||
self.persistWarning(context)
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void persistWarning(WarnContext context) {
|
||||
AUserInAServer warnedUser = userInServerManagementService.loadUser(context.getWarnedMember());
|
||||
AUserInAServer warningUser = userInServerManagementService.loadUser(context.getMember());
|
||||
warnManagementService.createWarning(warnedUser, warningUser, context.getReason(), context.getWarnId());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void decayWarningsForServer(AServer server) {
|
||||
public CompletableFuture<Void> decayWarningsForServer(AServer server) {
|
||||
Long days = configService.getLongValue(WarningDecayFeature.DECAY_DAYS_KEY, server.getId());
|
||||
Instant cutOffDay = Instant.now().minus(days, ChronoUnit.DAYS);
|
||||
List<Warning> warningsToDecay = warnManagementService.getActiveWarningsInServerOlderThan(server, cutOffDay);
|
||||
decayWarnings(warningsToDecay);
|
||||
logDecayedWarnings(server, warningsToDecay);
|
||||
List<Long> warningIds = flattenWarnings(warningsToDecay);
|
||||
Long serverId = server.getId();
|
||||
return logDecayedWarnings(server, warningsToDecay).thenAccept(aVoid ->
|
||||
self.decayWarnings(warningIds, serverId)
|
||||
);
|
||||
}
|
||||
|
||||
private void decayWarnings(List<Warning> warningsToDecay) {
|
||||
@NotNull
|
||||
private List<Long> flattenWarnings(List<Warning> warningsToDecay) {
|
||||
List<Long> warningIds = new ArrayList<>();
|
||||
warningsToDecay.forEach(warning ->
|
||||
warningIds.add(warning.getWarnId().getId())
|
||||
);
|
||||
return warningIds;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void decayWarnings(List<Long> warningIds, Long serverId) {
|
||||
Instant now = Instant.now();
|
||||
warningsToDecay.forEach(warning -> decayWarning(warning, now));
|
||||
warningIds.forEach(warningId -> {
|
||||
Optional<Warning> warningOptional = warnManagementService.findById(warningId, serverId);
|
||||
warningOptional.ifPresent(warning ->
|
||||
decayWarning(warning, now)
|
||||
);
|
||||
if(!warningOptional.isPresent()) {
|
||||
log.warn("Warning with id {} in server {} not found. Was not decayed.", warningId, serverId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,7 +146,7 @@ public class WarnServiceBean implements WarnService {
|
||||
warning.setDecayed(true);
|
||||
}
|
||||
|
||||
private void logDecayedWarnings(AServer server, List<Warning> warningsToDecay) {
|
||||
private CompletableFuture<Void> logDecayedWarnings(AServer server, List<Warning> warningsToDecay) {
|
||||
List<WarnDecayWarning> warnDecayWarnings = new ArrayList<>();
|
||||
warningsToDecay.forEach(warning -> {
|
||||
WarnDecayWarning warnDecayWarning = WarnDecayWarning
|
||||
@@ -147,21 +163,23 @@ public class WarnServiceBean implements WarnService {
|
||||
.server(server)
|
||||
.warnings(warnDecayWarnings)
|
||||
.build();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("warn_decay_log", warnDecayLogModel);
|
||||
postTargetService.sendEmbedInPostTarget(messageToSend, WarnDecayPostTarget.DECAY_LOG, server.getId());
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(WARN_DECAY_LOG_TEMPLATE_KEY, warnDecayLogModel);
|
||||
List<CompletableFuture<Message>> messageFutures = postTargetService.sendEmbedInPostTarget(messageToSend, WarnDecayPostTarget.DECAY_LOG, server.getId());
|
||||
return FutureUtils.toSingleFutureGeneric(messageFutures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decayAllWarningsForServer(AServer server, boolean logWarnings) {
|
||||
public CompletableFuture<Void> decayAllWarningsForServer(AServer server, boolean logWarnings) {
|
||||
List<Warning> warningsToDecay = warnManagementService.getActiveWarningsInServerOlderThan(server, Instant.now());
|
||||
decayWarnings(warningsToDecay);
|
||||
List<Long> warnIds = flattenWarnings(warningsToDecay);
|
||||
Long serverId = server.getId();
|
||||
if(logWarnings) {
|
||||
logDecayedWarnings(server, warningsToDecay);
|
||||
return logDecayedWarnings(server, warningsToDecay).thenAccept(aVoid ->
|
||||
self.decayWarnings(warnIds, serverId)
|
||||
);
|
||||
} else {
|
||||
decayWarnings(warnIds, serverId);
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendWarnLog(WarnLog warnLogModel) {
|
||||
MessageToSend message = templateService.renderEmbedTemplate(WARN_LOG_TEMPLATE, warnLogModel);
|
||||
postTargetService.sendEmbedInPostTarget(message, WarningPostTarget.WARN_LOG, warnLogModel.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.moderation.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
|
||||
import dev.sheldan.abstracto.core.models.ServerSpecificId;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Mute;
|
||||
@@ -25,18 +26,21 @@ public class MuteManagementServiceBean implements MuteManagementService {
|
||||
private UserInServerManagementService userInServerManagementService;
|
||||
|
||||
@Override
|
||||
public Mute createMute(AUserInAServer mutedUser, AUserInAServer mutingUser, String reason, Instant unMuteDate, AServerAChannelMessage muteMessage) {
|
||||
public Mute createMute(AUserInAServer mutedUser, AUserInAServer mutingUser, String reason, Instant unMuteDate, AServerAChannelMessage muteMessage, String triggerKey, Long muteId) {
|
||||
log.trace("Creating mute for user {} executed by user {} in server {}, user will be un-muted at {}",
|
||||
mutedUser.getUserReference().getId(), mutingUser.getUserReference().getId(), mutedUser.getServerReference().getId(), unMuteDate);
|
||||
ServerSpecificId id = new ServerSpecificId(muteMessage.getServer().getId(), muteId);
|
||||
Mute mute = Mute
|
||||
.builder()
|
||||
.mutedUser(mutedUser)
|
||||
.mutingUser(mutingUser)
|
||||
.muteTargetDate(unMuteDate)
|
||||
.mutingServer(mutedUser.getServerReference())
|
||||
.server(mutedUser.getServerReference())
|
||||
.mutingChannel(muteMessage.getChannel())
|
||||
.messageId(muteMessage.getMessageId())
|
||||
.reason(reason)
|
||||
.triggerKey(triggerKey)
|
||||
.muteId(id)
|
||||
.muteEnded(false)
|
||||
.build();
|
||||
muteRepository.save(mute);
|
||||
@@ -44,8 +48,8 @@ public class MuteManagementServiceBean implements MuteManagementService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Mute> findMute(Long muteId) {
|
||||
return muteRepository.findById(muteId);
|
||||
public Optional<Mute> findMute(Long muteId, Long serverId) {
|
||||
return muteRepository.findByMuteId_IdAndMuteId_ServerId(muteId, serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,7 +80,7 @@ public class MuteManagementServiceBean implements MuteManagementService {
|
||||
|
||||
@Override
|
||||
public List<Mute> getAllMutesOf(AUserInAServer aUserInAServer) {
|
||||
return muteRepository.findAllByMutedUserAndMuteEndedFalseOrderByIdDesc(aUserInAServer);
|
||||
return muteRepository.findAllByMutedUserAndMuteEndedFalseOrderByMuteId_IdDesc(aUserInAServer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package dev.sheldan.abstracto.moderation.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.ServerSpecificId;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.CounterService;
|
||||
import dev.sheldan.abstracto.moderation.models.database.UserNote;
|
||||
import dev.sheldan.abstracto.moderation.repository.UserNoteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -15,11 +17,20 @@ public class UserNoteManagementServiceBean implements UserNoteManagementService
|
||||
@Autowired
|
||||
private UserNoteRepository userNoteRepository;
|
||||
|
||||
@Autowired
|
||||
private CounterService counterService;
|
||||
|
||||
public static final String USER_NOTE_COUNTER_KEY = "USER_NOTES";
|
||||
|
||||
@Override
|
||||
public UserNote createUserNote(AUserInAServer aUserInAServer, String note) {
|
||||
Long id = counterService.getNextCounterValue(aUserInAServer.getServerReference(), USER_NOTE_COUNTER_KEY);
|
||||
ServerSpecificId userNoteId = new ServerSpecificId(aUserInAServer.getServerReference().getId(), id);
|
||||
UserNote newNote = UserNote
|
||||
.builder()
|
||||
.note(note)
|
||||
.userNoteId(userNoteId)
|
||||
.server(aUserInAServer.getServerReference())
|
||||
.user(aUserInAServer)
|
||||
.build();
|
||||
userNoteRepository.save(newNote);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.moderation.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.ServerSpecificId;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.moderation.models.database.Warning;
|
||||
import dev.sheldan.abstracto.moderation.repository.WarnRepository;
|
||||
@@ -18,12 +19,15 @@ public class WarnManagementServiceBean implements WarnManagementService {
|
||||
private WarnRepository warnRepository;
|
||||
|
||||
@Override
|
||||
public Warning createWarning(AUserInAServer warnedAUser, AUserInAServer warningAUser, String reason) {
|
||||
public Warning createWarning(AUserInAServer warnedAUser, AUserInAServer warningAUser, String reason, Long warnId) {
|
||||
ServerSpecificId warningId = new ServerSpecificId(warnId, warningAUser.getServerReference().getId());
|
||||
Warning warning = Warning.builder()
|
||||
.reason(reason)
|
||||
.warnedUser(warnedAUser)
|
||||
.warningUser(warningAUser)
|
||||
.warnDate(Instant.now())
|
||||
.server(warningAUser.getServerReference())
|
||||
.warnId(warningId)
|
||||
.decayed(false)
|
||||
.build();
|
||||
warnRepository.save(warning);
|
||||
@@ -56,8 +60,8 @@ public class WarnManagementServiceBean implements WarnManagementService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Warning> findById(Long id) {
|
||||
return warnRepository.findById(id);
|
||||
public Optional<Warning> findById(Long id, Long serverId) {
|
||||
return warnRepository.findByWarnId_IdAndWarnId_ServerId(id, serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<column name="muting_channel" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="muting_server" type="BIGINT">
|
||||
<column name="server_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="mute_ended" type="BOOLEAN"/>
|
||||
@@ -44,8 +44,8 @@
|
||||
<changeSet author="Sheldan" id="mute-fk_mute_muted_user">
|
||||
<addForeignKeyConstraint baseColumnNames="muted_user" baseTableName="mute" constraintName="fk_mute_muted_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
|
||||
</changeSet>
|
||||
<changeSet author="Sheldan" id="mute-fk_mute_muting_server">
|
||||
<addForeignKeyConstraint baseColumnNames="muting_server" baseTableName="mute" constraintName="fk_mute_muting_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
|
||||
<changeSet author="Sheldan" id="mute-fk_mute_server_id">
|
||||
<addForeignKeyConstraint baseColumnNames="server_id" baseTableName="mute" constraintName="fk_mute_server_id" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
@@ -16,11 +16,17 @@
|
||||
<column name="note_user" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="server_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
</changeSet>
|
||||
|
||||
<changeSet author="Sheldan" id="user_note-fk_user_note_user">
|
||||
<addForeignKeyConstraint baseColumnNames="note_user" baseTableName="user_note" constraintName="fk_user_note_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
|
||||
</changeSet>
|
||||
<changeSet author="Sheldan" id="user_note-fk_user_note_server_id">
|
||||
<addForeignKeyConstraint baseColumnNames="server_id" baseTableName="user_note" constraintName="fk_user_note_server_id" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
@@ -23,6 +23,9 @@
|
||||
<column name="warning_user_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="server_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
</changeSet>
|
||||
|
||||
@@ -33,4 +36,8 @@
|
||||
<changeSet author="Sheldan" id="warning-fk_warning_warning_user">
|
||||
<addForeignKeyConstraint baseColumnNames="warning_user_id" baseTableName="warning" constraintName="fk_warning_warning_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
|
||||
</changeSet>
|
||||
<changeSet author="Sheldan" id="warning-fk_warning_server_id">
|
||||
<addForeignKeyConstraint baseColumnNames="server_id" baseTableName="warning" constraintName="fk_warning_server_id" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
Reference in New Issue
Block a user