mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-14 11:48:16 +00:00
[AB-xxx] reworking ban logging to use audit log instead of actively logging or using the banned event
partially fixing broken infraction handling adding CompletableFutureMap to handle futures easier updating user display object to also hold name replaced some references to UserObjects in models with UserDisplay objects
This commit is contained in:
@@ -11,7 +11,6 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||
import dev.sheldan.abstracto.core.service.UserService;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModuleDefinition;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationSlashCommandNames;
|
||||
@@ -51,7 +50,7 @@ public class UnBan extends AbstractConditionableCommand {
|
||||
String userIdStr = (String) parameters.get(0);
|
||||
Long userId = Long.parseLong(userIdStr);
|
||||
return userService.retrieveUserForId(userId)
|
||||
.thenCompose(user -> banService.unBanUserWithNotification(userId, ServerUser.fromMember(commandContext.getAuthor()), commandContext.getGuild()))
|
||||
.thenCompose(user -> banService.unbanUser(commandContext.getGuild(), userId))
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ public class UnBan extends AbstractConditionableCommand {
|
||||
String userIdStr = slashCommandParameterService.getCommandOption(USER_PARAMETER, event, String.class);
|
||||
Long userId = Long.parseLong(userIdStr);
|
||||
return userService.retrieveUserForId(userId)
|
||||
.thenCompose(user -> banService.unBanUserWithNotification(userId, ServerUser.fromMember(event.getMember()), event.getGuild()))
|
||||
.thenCompose(user -> banService.unbanUser(event.getGuild(), userId))
|
||||
.thenCompose(unused -> interactionService.replyEmbed(UN_BAN_RESPONSE, event))
|
||||
.thenApply(interactionHook -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@@ -4,23 +4,17 @@ import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
||||
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserBannedListener;
|
||||
import dev.sheldan.abstracto.core.models.listener.UserBannedModel;
|
||||
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||
import dev.sheldan.abstracto.moderation.config.posttarget.ModerationPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.model.template.listener.UserBannedListenerModel;
|
||||
import dev.sheldan.abstracto.moderation.model.template.listener.UserBannedListenerLogModel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.audit.ActionType;
|
||||
import net.dv8tion.jda.api.audit.AuditLogEntry;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -32,53 +26,20 @@ public class UserBannedListener implements AsyncUserBannedListener {
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Autowired
|
||||
private UserBannedListener self;
|
||||
|
||||
private static final String USER_BANNED_NOTIFICATION_TEMPLATE = "userBanned_listener_notification";
|
||||
public static final String USER_BANNED_NOTIFICATION_TEMPLATE = "userBanned_listener_notification";
|
||||
|
||||
@Override
|
||||
public DefaultListenerResult execute(UserBannedModel model) {
|
||||
model.getGuild()
|
||||
.retrieveAuditLogs()
|
||||
.type(ActionType.BAN)
|
||||
.limit(5)
|
||||
.queue(auditLogEntries -> {
|
||||
if(auditLogEntries.isEmpty()) {
|
||||
log.info("Did not find recent bans in guild {}.", model.getServerId());
|
||||
self.sendBannedNotification(model.getUser(), null, null, model.getServerId());
|
||||
return;
|
||||
}
|
||||
Optional<AuditLogEntry> banEntryOptional = auditLogEntries
|
||||
.stream()
|
||||
.filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == model.getBannedUser().getUserId())
|
||||
.findFirst();
|
||||
if(banEntryOptional.isPresent()) {
|
||||
AuditLogEntry auditLogEntry = banEntryOptional.get();
|
||||
if(!model.getGuild().getJDA().getSelfUser().equals(auditLogEntry.getUser())) {
|
||||
self.sendBannedNotification(model.getUser(), auditLogEntry.getUser(), auditLogEntry.getReason(), model.getServerId());
|
||||
}
|
||||
} else {
|
||||
log.info("Did not find the banned user in the most recent bans for guild {}. Not adding audit log information.", model.getServerId());
|
||||
self.sendBannedNotification(model.getUser(), null, null, model.getServerId());
|
||||
}
|
||||
}, throwable -> {
|
||||
log.error("Retrieving bans for guild {} failed - logging ban regardless.", model.getServerId());
|
||||
self.sendBannedNotification(model.getUser(), null, null, model.getServerId());
|
||||
});
|
||||
return DefaultListenerResult.PROCESSED;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Void> sendBannedNotification(User bannedUser, User banningUser, String reason, Long serverId) {
|
||||
UserBannedListenerModel model = UserBannedListenerModel
|
||||
public DefaultListenerResult execute(UserBannedModel eventModel) {
|
||||
log.info("Notifying about ban of user {} in guild {}.", eventModel.getBannedServerUser().getUserId(), eventModel.getServerId());
|
||||
UserBannedListenerLogModel model = UserBannedListenerLogModel
|
||||
.builder()
|
||||
.bannedUser(bannedUser)
|
||||
.banningUser(banningUser)
|
||||
.reason(reason)
|
||||
.bannedUser(eventModel.getBannedUser() != null ? UserDisplay.fromUser(eventModel.getBannedUser()) : UserDisplay.fromId(eventModel.getBannedServerUser().getUserId()))
|
||||
.banningUser(eventModel.getBanningUser() != null ? UserDisplay.fromUser(eventModel.getBanningUser()) : UserDisplay.fromServerUser(eventModel.getBanningServerUser()))
|
||||
.reason(eventModel.getReason())
|
||||
.build();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_BANNED_NOTIFICATION_TEMPLATE, model, serverId);
|
||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, serverId));
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_BANNED_NOTIFICATION_TEMPLATE, model, eventModel.getServerId());
|
||||
FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, eventModel.getServerId()));
|
||||
return DefaultListenerResult.PROCESSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,30 +4,21 @@ import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
||||
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserUnBannedListener;
|
||||
import dev.sheldan.abstracto.core.models.listener.UserUnBannedModel;
|
||||
import dev.sheldan.abstracto.core.service.FeatureModeService;
|
||||
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||
import dev.sheldan.abstracto.moderation.config.posttarget.ModerationPostTarget;
|
||||
import dev.sheldan.abstracto.moderation.model.template.listener.UserUnBannedListenerModel;
|
||||
import dev.sheldan.abstracto.moderation.model.template.listener.UserUnBannedListenerLogModel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.audit.ActionType;
|
||||
import net.dv8tion.jda.api.audit.AuditLogEntry;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class UserUnBannedListener implements AsyncUserUnBannedListener {
|
||||
@Autowired
|
||||
private FeatureModeService featureModeService;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
@@ -35,56 +26,20 @@ public class UserUnBannedListener implements AsyncUserUnBannedListener {
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Autowired
|
||||
private UserUnBannedListener self;
|
||||
|
||||
private static final String USER_UN_BANNED_NOTIFICATION_TEMPLATE = "userUnBanned_listener_notification";
|
||||
|
||||
@Override
|
||||
public DefaultListenerResult execute(UserUnBannedModel model) {
|
||||
log.info("Notifying about unban of user {} in guild {}.", model.getUnbannedUser().getUserId(), model.getServerId());
|
||||
model.getGuild()
|
||||
.retrieveAuditLogs()
|
||||
.type(ActionType.UNBAN)
|
||||
.limit(5)
|
||||
.queue(auditLogEntries -> {
|
||||
try {
|
||||
if(auditLogEntries.isEmpty()) {
|
||||
log.info("Did not find recent bans in guild {}.", model.getServerId());
|
||||
return;
|
||||
}
|
||||
Optional<AuditLogEntry> banEntryOptional = auditLogEntries
|
||||
.stream()
|
||||
.filter(auditLogEntry -> auditLogEntry.getTargetIdLong() == model.getUnbannedUser().getUserId())
|
||||
.findFirst();
|
||||
if(banEntryOptional.isPresent()) {
|
||||
AuditLogEntry auditLogEntry = banEntryOptional.get();
|
||||
if(!model.getGuild().getJDA().getSelfUser().equals(auditLogEntry.getUser())) {
|
||||
self.sendUnBannedNotification(model.getUser(), auditLogEntry.getUser(), model.getServerId());
|
||||
}
|
||||
} else {
|
||||
log.info("Did not find the un-banned user in the most recent un-bans for guild {}. Not adding audit log information.", model.getServerId());
|
||||
self.sendUnBannedNotification(model.getUser(), null, model.getServerId());
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
log.error("Failed to properly send un ban log with error.", exception);
|
||||
}
|
||||
}, throwable -> {
|
||||
log.error("Failed to retrieve audit log entries for unban log.", throwable);
|
||||
self.sendUnBannedNotification(model.getUser(), null, model.getServerId());
|
||||
});
|
||||
return DefaultListenerResult.PROCESSED;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Void> sendUnBannedNotification(User unbannedUser, User unbanningUser, Long serverId) {
|
||||
UserUnBannedListenerModel model = UserUnBannedListenerModel
|
||||
public DefaultListenerResult execute(UserUnBannedModel eventModel) {
|
||||
log.info("Notifying about unban of user {} in guild {}.", eventModel.getUnBannedServerUser().getUserId(), eventModel.getServerId());
|
||||
UserUnBannedListenerLogModel model = UserUnBannedListenerLogModel
|
||||
.builder()
|
||||
.unBannedUser(unbannedUser)
|
||||
.unBanningUser(unbanningUser)
|
||||
.unBannedUser(eventModel.getUnBannedUser() != null ? UserDisplay.fromUser(eventModel.getUnBannedUser()) : UserDisplay.fromId(eventModel.getUnBannedServerUser().getUserId()))
|
||||
.unBanningUser(eventModel.getUnBanningUser() != null ? UserDisplay.fromUser(eventModel.getUnBanningUser()) : UserDisplay.fromServerUser(eventModel.getUnBanningServerUser()))
|
||||
.reason(eventModel.getReason())
|
||||
.build();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_UN_BANNED_NOTIFICATION_TEMPLATE, model, serverId);
|
||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, serverId));
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_UN_BANNED_NOTIFICATION_TEMPLATE, model, eventModel.getServerId());
|
||||
FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, eventModel.getServerId()));
|
||||
return DefaultListenerResult.PROCESSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,34 +3,32 @@ package dev.sheldan.abstracto.moderation.listener.infraction;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.config.ListenerPriority;
|
||||
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay;
|
||||
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.MemberService;
|
||||
import dev.sheldan.abstracto.core.service.MessageService;
|
||||
import dev.sheldan.abstracto.core.service.UserService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||
import dev.sheldan.abstracto.moderation.listener.InfractionUpdatedDescriptionListener;
|
||||
import dev.sheldan.abstracto.moderation.model.database.Infraction;
|
||||
import dev.sheldan.abstracto.moderation.model.database.InfractionParameter;
|
||||
import dev.sheldan.abstracto.moderation.model.listener.InfractionDescriptionEventModel;
|
||||
import dev.sheldan.abstracto.moderation.model.template.command.BanLog;
|
||||
import dev.sheldan.abstracto.moderation.model.template.listener.UserBannedListenerLogModel;
|
||||
import dev.sheldan.abstracto.moderation.service.BanService;
|
||||
import dev.sheldan.abstracto.moderation.service.BanServiceBean;
|
||||
import dev.sheldan.abstracto.moderation.service.management.InfractionManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static dev.sheldan.abstracto.moderation.listener.UserBannedListener.USER_BANNED_NOTIFICATION_TEMPLATE;
|
||||
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionListener {
|
||||
@@ -45,7 +43,7 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private BanServiceBean banServiceBean;
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private BanReasonUpdatedListener self;
|
||||
@@ -60,7 +58,7 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
||||
public CompletableFuture<DefaultListenerResult> execute(InfractionDescriptionEventModel model) {
|
||||
Infraction infraction = infractionManagementService.loadInfraction(model.getInfractionId());
|
||||
CompletableFuture<User> infractionUser = userService.retrieveUserForId(infraction.getUser().getUserReference().getId());
|
||||
CompletableFuture<Member> creatorUser = memberService.retrieveMemberInServer(ServerUser.fromAUserInAServer(infraction.getInfractionCreator()));
|
||||
CompletableFuture<User> creatorUser = userService.retrieveUserForId(infraction.getInfractionCreator().getUserReference().getId());
|
||||
CompletableFuture<DefaultListenerResult> returningFuture = new CompletableFuture<>();
|
||||
Long infractionId = infraction.getId();
|
||||
CompletableFuture.allOf(infractionUser, creatorUser)
|
||||
@@ -74,26 +72,17 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void handleBanUpdate(InfractionDescriptionEventModel model, CompletableFuture<User> infractionUser, CompletableFuture<Member> infractionCreator, CompletableFuture<DefaultListenerResult> returningFuture) {
|
||||
public void handleBanUpdate(InfractionDescriptionEventModel model, CompletableFuture<User> infractionUser, CompletableFuture<User> infractionCreator, CompletableFuture<DefaultListenerResult> returningFuture) {
|
||||
Infraction infraction = infractionManagementService.loadInfraction(model.getInfractionId());
|
||||
GuildMessageChannel messageChannel = channelService.getMessageChannelFromServer(model.getServerId(), infraction.getLogChannel().getId());
|
||||
Duration deletionDuration = infraction
|
||||
.getParameters()
|
||||
.stream()
|
||||
.filter(infractionParameter -> infractionParameter.getInfractionParameterId().getName().equals(BanService.INFRACTION_PARAMETER_DELETION_DURATION_KEY))
|
||||
.findAny()
|
||||
.map(InfractionParameter::getValue)
|
||||
.map(Duration::parse)
|
||||
.orElse(Duration.ZERO);
|
||||
BanLog banLog = BanLog
|
||||
UserBannedListenerLogModel banLog = UserBannedListenerLogModel
|
||||
.builder()
|
||||
.bannedUser(infractionUser.isCompletedExceptionally() ? null : UserDisplay.fromUser(infractionUser.join()))
|
||||
.banningMember(infractionCreator.isCompletedExceptionally() ? null : MemberDisplay.fromMember(infractionCreator.join()))
|
||||
.deletionDuration(deletionDuration)
|
||||
.banningUser(infractionCreator.isCompletedExceptionally() ? null : UserDisplay.fromUser(infractionCreator.join()))
|
||||
.reason(model.getNewDescription())
|
||||
.build();
|
||||
|
||||
MessageToSend message = banServiceBean.renderBanMessage(banLog, model.getServerId());
|
||||
MessageToSend message = templateService.renderEmbedTemplate(USER_BANNED_NOTIFICATION_TEMPLATE, banLog, model.getServerId());
|
||||
messageService.editMessageInChannel(messageChannel, message, infraction.getLogMessageId())
|
||||
.thenAccept(unused1 -> returningFuture.complete(DefaultListenerResult.PROCESSED))
|
||||
.exceptionally(throwable1 -> {
|
||||
|
||||
@@ -2,30 +2,25 @@ package dev.sheldan.abstracto.moderation.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay;
|
||||
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||
import dev.sheldan.abstracto.core.service.*;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||
import dev.sheldan.abstracto.core.service.MessageService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureConfig;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||
import dev.sheldan.abstracto.moderation.config.posttarget.ModerationPostTarget;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.moderation.model.BanResult;
|
||||
import dev.sheldan.abstracto.moderation.model.database.Infraction;
|
||||
import dev.sheldan.abstracto.moderation.model.template.command.BanLog;
|
||||
import dev.sheldan.abstracto.moderation.model.template.command.BanNotificationModel;
|
||||
import dev.sheldan.abstracto.moderation.model.template.command.UnBanLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.UserSnowflake;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -34,16 +29,11 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public class BanServiceBean implements BanService {
|
||||
|
||||
public static final String BAN_LOG_TEMPLATE = "ban_log";
|
||||
public static final String UN_BAN_LOG_TEMPLATE = "unBan_log";
|
||||
public static final String BAN_NOTIFICATION = "ban_notification";
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Autowired
|
||||
private BanServiceBean self;
|
||||
|
||||
@@ -64,13 +54,6 @@ public class BanServiceBean implements BanService {
|
||||
|
||||
@Override
|
||||
public CompletableFuture<BanResult> banUserWithNotification(ServerUser userToBeBanned, String reason, ServerUser banningUser, Guild guild, Duration deletionDuration) {
|
||||
BanLog banLog = BanLog
|
||||
.builder()
|
||||
.bannedUser(UserDisplay.fromServerUser(userToBeBanned))
|
||||
.banningMember(MemberDisplay.fromServerUser(banningUser))
|
||||
.deletionDuration(deletionDuration)
|
||||
.reason(reason)
|
||||
.build();
|
||||
BanResult[] result = {BanResult.SUCCESSFUL};
|
||||
return sendBanNotification(userToBeBanned, reason, guild)
|
||||
.exceptionally(throwable -> {
|
||||
@@ -78,13 +61,12 @@ public class BanServiceBean implements BanService {
|
||||
return null;
|
||||
})
|
||||
.thenCompose(unused -> banUser(guild, userToBeBanned, deletionDuration, reason))
|
||||
.thenCompose(unused -> sendBanLogMessage(banLog, guild.getIdLong()))
|
||||
.thenAccept(banLogMessage -> self.evaluateAndStoreInfraction(userToBeBanned, guild, reason, banningUser, banLogMessage, deletionDuration))
|
||||
.thenAccept(banLogMessage -> self.evaluateAndStoreInfraction(userToBeBanned, guild, reason, banningUser, deletionDuration))
|
||||
.thenApply(unused -> result[0]);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Long> evaluateAndStoreInfraction(ServerUser user, Guild guild, String reason, ServerUser banningMember, Message banLogMessage, Duration deletionDuration) {
|
||||
public CompletableFuture<Long> evaluateAndStoreInfraction(ServerUser user, Guild guild, String reason, ServerUser banningMember, Duration deletionDuration) {
|
||||
if(featureFlagService.getFeatureFlagValue(ModerationFeatureDefinition.INFRACTIONS, guild.getIdLong())) {
|
||||
Long infractionPoints = configService.getLongValueOrConfigDefault(ModerationFeatureConfig.BAN_INFRACTION_POINTS, guild.getIdLong());
|
||||
AUserInAServer bannedUser = userInServerManagementService.loadOrCreateUser(guild.getIdLong(), user.getUserId());
|
||||
@@ -94,7 +76,7 @@ public class BanServiceBean implements BanService {
|
||||
deletionDuration = Duration.ZERO;
|
||||
}
|
||||
parameters.put(INFRACTION_PARAMETER_DELETION_DURATION_KEY, deletionDuration.toString());
|
||||
return infractionService.createInfractionWithNotification(bannedUser, infractionPoints, BAN_INFRACTION_TYPE, reason, banningUser, parameters, banLogMessage)
|
||||
return infractionService.createInfractionWithNotification(bannedUser, infractionPoints, BAN_INFRACTION_TYPE, reason, banningUser, parameters)
|
||||
.thenApply(Infraction::getId);
|
||||
} else {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
@@ -111,17 +93,6 @@ public class BanServiceBean implements BanService {
|
||||
return messageService.sendMessageToUser(serverUser, message).thenAccept(message1 -> {});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> unBanUserWithNotification(Long userId, ServerUser unBanningMember, Guild guild) {
|
||||
UnBanLog banLog = UnBanLog
|
||||
.builder()
|
||||
.bannedUser(UserDisplay.fromId(userId))
|
||||
.unBanningMember(MemberDisplay.fromServerUser(unBanningMember))
|
||||
.build();
|
||||
return unbanUser(guild, userId)
|
||||
.thenCompose(unused -> self.sendUnBanLogMessage(banLog, guild.getIdLong(), UN_BAN_LOG_TEMPLATE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> banUser(Guild guild, ServerUser userToBeBanned, Duration deletionDuration, String reason) {
|
||||
log.info("Banning user {} in guild {}.", userToBeBanned.getUserId(), guild.getId());
|
||||
@@ -143,20 +114,4 @@ public class BanServiceBean implements BanService {
|
||||
.thenCompose(unused -> unbanUser(guild, user.getUserId()));
|
||||
}
|
||||
|
||||
public CompletableFuture<Message> sendBanLogMessage(BanLog banLog, Long guildId) {
|
||||
MessageToSend banLogMessage = renderBanMessage(banLog, guildId);
|
||||
log.debug("Sending ban log message in guild {}.", guildId);
|
||||
List<CompletableFuture<Message>> messageFutures = postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.BAN_LOG, guildId);
|
||||
return FutureUtils.toSingleFutureGeneric(messageFutures).thenApply(unused -> messageFutures.get(0).join());
|
||||
}
|
||||
|
||||
public MessageToSend renderBanMessage(BanLog banLog, Long guildId) {
|
||||
return templateService.renderEmbedTemplate(BAN_LOG_TEMPLATE, banLog, guildId);
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> sendUnBanLogMessage(UnBanLog banLog, Long guildId, String template) {
|
||||
MessageToSend banLogMessage = templateService.renderEmbedTemplate(template, banLog, guildId);
|
||||
log.debug("Sending unban log message in guild {}.", guildId);
|
||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(banLogMessage, ModerationPostTarget.UN_BAN_LOG, guildId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ public class InfractionServiceBean implements InfractionService {
|
||||
List<Infraction> infractions = infractionManagementService.getActiveInfractionsForUser(aUserInAServer);
|
||||
log.info("Calculating points for user {} in server {} with {} infractions.",
|
||||
aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId(), infractions.size());
|
||||
return infractions.stream().collect(Collectors.summarizingLong(Infraction::getPoints)).getCount();
|
||||
return infractions.stream().collect(Collectors.summarizingLong(Infraction::getPoints)).getSum();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,8 +112,8 @@ public class InfractionServiceBean implements InfractionService {
|
||||
public CompletableFuture<Void> createInfractionNotification(AUserInAServer aUserInAServer, Long points, String type, String description) {
|
||||
Long serverId = aUserInAServer.getServerReference().getId();
|
||||
Long currentPoints = getActiveInfractionPointsForUser(aUserInAServer);
|
||||
Long newPoints = currentPoints + points;
|
||||
Pair<Integer, Integer> levelChange = infractionLevelChanged(serverId, newPoints, currentPoints);
|
||||
Long oldPoints = currentPoints - points;
|
||||
Pair<Integer, Integer> levelChange = infractionLevelChanged(serverId, currentPoints, oldPoints);
|
||||
Integer oldLevel = levelChange.getFirst();
|
||||
Integer newLevel = levelChange.getSecond();
|
||||
if(!oldLevel.equals(newLevel)) {
|
||||
@@ -124,10 +124,10 @@ public class InfractionServiceBean implements InfractionService {
|
||||
.oldLevel(oldLevel)
|
||||
.type(type)
|
||||
.description(description)
|
||||
.oldPoints(currentPoints)
|
||||
.newPoints(newPoints)
|
||||
.oldPoints(oldPoints)
|
||||
.newPoints(currentPoints)
|
||||
.build();
|
||||
infractionLevelChangedListenerManager.sendInfractionLevelChangedEvent(newLevel, oldLevel, newPoints, currentPoints, ServerUser.fromAUserInAServer(aUserInAServer));
|
||||
infractionLevelChangedListenerManager.sendInfractionLevelChangedEvent(newLevel, oldLevel, currentPoints, oldPoints, ServerUser.fromAUserInAServer(aUserInAServer));
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(INFRACTION_NOTIFICATION_TEMPLATE_KEY, model, serverId);
|
||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, InfractionPostTarget.INFRACTION_NOTIFICATION, serverId));
|
||||
} else {
|
||||
|
||||
@@ -32,6 +32,7 @@ abstracto.postTargets.banLog.name=banLog
|
||||
abstracto.postTargets.unBanLog.name=unBanLog
|
||||
abstracto.postTargets.muteLog.name=muteLog
|
||||
abstracto.postTargets.decayLog.name=decayLog
|
||||
abstracto.postTargets.infractionNotification.name=infractionNotification
|
||||
|
||||
abstracto.featureModes.warnDecayLogging.featureName=warnings
|
||||
abstracto.featureModes.warnDecayLogging.mode=warnDecayLogging
|
||||
@@ -49,20 +50,20 @@ abstracto.featureModes.reactionReportActions.featureName=reportReactions
|
||||
abstracto.featureModes.reactionReportActions.mode=reactionReportActions
|
||||
abstracto.featureModes.reactionReportActions.enabled=false
|
||||
|
||||
abstracto.systemConfigs.infractionLvl1.name=infractionLvl1
|
||||
abstracto.systemConfigs.infractionLvl1.longValue=10
|
||||
abstracto.systemConfigs.infractionLevel1.name=infractionLevel1
|
||||
abstracto.systemConfigs.infractionLevel1.longValue=10
|
||||
|
||||
abstracto.systemConfigs.infractionLvl2.name=infractionLvl2
|
||||
abstracto.systemConfigs.infractionLvl2.longValue=20
|
||||
abstracto.systemConfigs.infractionLevel2.name=infractionLevel2
|
||||
abstracto.systemConfigs.infractionLevel2.longValue=20
|
||||
|
||||
abstracto.systemConfigs.infractionLvl3.name=infractionLvl3
|
||||
abstracto.systemConfigs.infractionLvl3.longValue=30
|
||||
abstracto.systemConfigs.infractionLevel3.name=infractionLevel3
|
||||
abstracto.systemConfigs.infractionLevel3.longValue=30
|
||||
|
||||
abstracto.systemConfigs.infractionLvl4.name=infractionLvl4
|
||||
abstracto.systemConfigs.infractionLvl4.longValue=40
|
||||
abstracto.systemConfigs.infractionLevel4.name=infractionLevel4
|
||||
abstracto.systemConfigs.infractionLevel4.longValue=40
|
||||
|
||||
abstracto.systemConfigs.infractionLvl5.name=infractionLvl5
|
||||
abstracto.systemConfigs.infractionLvl5.longValue=50
|
||||
abstracto.systemConfigs.infractionLevel5.name=infractionLevel5
|
||||
abstracto.systemConfigs.infractionLevel5.longValue=50
|
||||
|
||||
abstracto.systemConfigs.infractionLevels.name=infractionLevels
|
||||
abstracto.systemConfigs.infractionLevels.longValue=5
|
||||
|
||||
Reference in New Issue
Block a user