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:
@@ -5,13 +5,14 @@ import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
|||||||
import dev.sheldan.abstracto.core.listener.async.jda.AsyncLeaveListener;
|
import dev.sheldan.abstracto.core.listener.async.jda.AsyncLeaveListener;
|
||||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
import dev.sheldan.abstracto.core.models.listener.MemberLeaveModel;
|
import dev.sheldan.abstracto.core.models.listener.MemberLeaveModel;
|
||||||
import dev.sheldan.abstracto.core.service.MemberService;
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||||
import dev.sheldan.abstracto.logging.config.LoggingFeatureDefinition;
|
import dev.sheldan.abstracto.logging.config.LoggingFeatureDefinition;
|
||||||
import dev.sheldan.abstracto.logging.config.LoggingPostTarget;
|
import dev.sheldan.abstracto.logging.config.LoggingPostTarget;
|
||||||
|
import dev.sheldan.abstracto.logging.model.template.MemberLeaveLogModel;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -40,10 +41,10 @@ public class LeaveLogger implements AsyncLeaveListener {
|
|||||||
.userId(listenerModel.getUser().getIdLong())
|
.userId(listenerModel.getUser().getIdLong())
|
||||||
.serverId(listenerModel.getServerId())
|
.serverId(listenerModel.getServerId())
|
||||||
.build();
|
.build();
|
||||||
MemberLeaveModel model = MemberLeaveModel
|
MemberLeaveLogModel model = MemberLeaveLogModel
|
||||||
.builder()
|
.builder()
|
||||||
.leavingUser(leavingUser)
|
.leavingUser(leavingUser)
|
||||||
.user(listenerModel.getUser())
|
.user(UserDisplay.fromUser(listenerModel.getUser()))
|
||||||
.build();
|
.build();
|
||||||
log.debug("Logging leave event for user {} in server {}.", listenerModel.getUser().getIdLong(), listenerModel.getServerId());
|
log.debug("Logging leave event for user {} in server {}.", listenerModel.getUser().getIdLong(), listenerModel.getServerId());
|
||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_LEAVE_TEMPLATE, model, listenerModel.getServerId());
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_LEAVE_TEMPLATE, model, listenerModel.getServerId());
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
package dev.sheldan.abstracto.logging.model.template;
|
package dev.sheldan.abstracto.logging.model.template;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class MemberLeaveLogModel {
|
public class MemberLeaveLogModel {
|
||||||
private Member member;
|
private ServerUser leavingUser;
|
||||||
|
private UserDisplay user;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.interaction.slash.parameter.SlashCommandParameterService;
|
||||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
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.core.service.UserService;
|
||||||
import dev.sheldan.abstracto.moderation.config.ModerationModuleDefinition;
|
import dev.sheldan.abstracto.moderation.config.ModerationModuleDefinition;
|
||||||
import dev.sheldan.abstracto.moderation.config.ModerationSlashCommandNames;
|
import dev.sheldan.abstracto.moderation.config.ModerationSlashCommandNames;
|
||||||
@@ -51,7 +50,7 @@ public class UnBan extends AbstractConditionableCommand {
|
|||||||
String userIdStr = (String) parameters.get(0);
|
String userIdStr = (String) parameters.get(0);
|
||||||
Long userId = Long.parseLong(userIdStr);
|
Long userId = Long.parseLong(userIdStr);
|
||||||
return userService.retrieveUserForId(userId)
|
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());
|
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +59,7 @@ public class UnBan extends AbstractConditionableCommand {
|
|||||||
String userIdStr = slashCommandParameterService.getCommandOption(USER_PARAMETER, event, String.class);
|
String userIdStr = slashCommandParameterService.getCommandOption(USER_PARAMETER, event, String.class);
|
||||||
Long userId = Long.parseLong(userIdStr);
|
Long userId = Long.parseLong(userIdStr);
|
||||||
return userService.retrieveUserForId(userId)
|
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))
|
.thenCompose(unused -> interactionService.replyEmbed(UN_BAN_RESPONSE, event))
|
||||||
.thenApply(interactionHook -> CommandResult.fromSuccess());
|
.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.DefaultListenerResult;
|
||||||
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserBannedListener;
|
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserBannedListener;
|
||||||
import dev.sheldan.abstracto.core.models.listener.UserBannedModel;
|
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.service.PostTargetService;
|
||||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||||
import dev.sheldan.abstracto.moderation.config.posttarget.ModerationPostTarget;
|
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 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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -32,53 +26,20 @@ public class UserBannedListener implements AsyncUserBannedListener {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostTargetService postTargetService;
|
private PostTargetService postTargetService;
|
||||||
|
|
||||||
@Autowired
|
public static final String USER_BANNED_NOTIFICATION_TEMPLATE = "userBanned_listener_notification";
|
||||||
private UserBannedListener self;
|
|
||||||
|
|
||||||
private static final String USER_BANNED_NOTIFICATION_TEMPLATE = "userBanned_listener_notification";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultListenerResult execute(UserBannedModel model) {
|
public DefaultListenerResult execute(UserBannedModel eventModel) {
|
||||||
model.getGuild()
|
log.info("Notifying about ban of user {} in guild {}.", eventModel.getBannedServerUser().getUserId(), eventModel.getServerId());
|
||||||
.retrieveAuditLogs()
|
UserBannedListenerLogModel model = UserBannedListenerLogModel
|
||||||
.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
|
|
||||||
.builder()
|
.builder()
|
||||||
.bannedUser(bannedUser)
|
.bannedUser(eventModel.getBannedUser() != null ? UserDisplay.fromUser(eventModel.getBannedUser()) : UserDisplay.fromId(eventModel.getBannedServerUser().getUserId()))
|
||||||
.banningUser(banningUser)
|
.banningUser(eventModel.getBanningUser() != null ? UserDisplay.fromUser(eventModel.getBanningUser()) : UserDisplay.fromServerUser(eventModel.getBanningServerUser()))
|
||||||
.reason(reason)
|
.reason(eventModel.getReason())
|
||||||
.build();
|
.build();
|
||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_BANNED_NOTIFICATION_TEMPLATE, model, serverId);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_BANNED_NOTIFICATION_TEMPLATE, model, eventModel.getServerId());
|
||||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, serverId));
|
FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, eventModel.getServerId()));
|
||||||
|
return DefaultListenerResult.PROCESSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.DefaultListenerResult;
|
||||||
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserUnBannedListener;
|
import dev.sheldan.abstracto.core.listener.async.jda.AsyncUserUnBannedListener;
|
||||||
import dev.sheldan.abstracto.core.models.listener.UserUnBannedModel;
|
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.service.PostTargetService;
|
||||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
||||||
import dev.sheldan.abstracto.moderation.config.posttarget.ModerationPostTarget;
|
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 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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class UserUnBannedListener implements AsyncUserUnBannedListener {
|
public class UserUnBannedListener implements AsyncUserUnBannedListener {
|
||||||
@Autowired
|
|
||||||
private FeatureModeService featureModeService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TemplateService templateService;
|
private TemplateService templateService;
|
||||||
@@ -35,56 +26,20 @@ public class UserUnBannedListener implements AsyncUserUnBannedListener {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostTargetService postTargetService;
|
private PostTargetService postTargetService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserUnBannedListener self;
|
|
||||||
|
|
||||||
private static final String USER_UN_BANNED_NOTIFICATION_TEMPLATE = "userUnBanned_listener_notification";
|
private static final String USER_UN_BANNED_NOTIFICATION_TEMPLATE = "userUnBanned_listener_notification";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultListenerResult execute(UserUnBannedModel model) {
|
public DefaultListenerResult execute(UserUnBannedModel eventModel) {
|
||||||
log.info("Notifying about unban of user {} in guild {}.", model.getUnbannedUser().getUserId(), model.getServerId());
|
log.info("Notifying about unban of user {} in guild {}.", eventModel.getUnBannedServerUser().getUserId(), eventModel.getServerId());
|
||||||
model.getGuild()
|
UserUnBannedListenerLogModel model = UserUnBannedListenerLogModel
|
||||||
.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
|
|
||||||
.builder()
|
.builder()
|
||||||
.unBannedUser(unbannedUser)
|
.unBannedUser(eventModel.getUnBannedUser() != null ? UserDisplay.fromUser(eventModel.getUnBannedUser()) : UserDisplay.fromId(eventModel.getUnBannedServerUser().getUserId()))
|
||||||
.unBanningUser(unbanningUser)
|
.unBanningUser(eventModel.getUnBanningUser() != null ? UserDisplay.fromUser(eventModel.getUnBanningUser()) : UserDisplay.fromServerUser(eventModel.getUnBanningServerUser()))
|
||||||
|
.reason(eventModel.getReason())
|
||||||
.build();
|
.build();
|
||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_UN_BANNED_NOTIFICATION_TEMPLATE, model, serverId);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(USER_UN_BANNED_NOTIFICATION_TEMPLATE, model, eventModel.getServerId());
|
||||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, serverId));
|
FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationPostTarget.BAN_LOG, eventModel.getServerId()));
|
||||||
|
return DefaultListenerResult.PROCESSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.FeatureDefinition;
|
||||||
import dev.sheldan.abstracto.core.config.ListenerPriority;
|
import dev.sheldan.abstracto.core.config.ListenerPriority;
|
||||||
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
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.models.template.display.UserDisplay;
|
||||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||||
import dev.sheldan.abstracto.core.service.MemberService;
|
import dev.sheldan.abstracto.core.service.MemberService;
|
||||||
import dev.sheldan.abstracto.core.service.MessageService;
|
import dev.sheldan.abstracto.core.service.MessageService;
|
||||||
import dev.sheldan.abstracto.core.service.UserService;
|
import dev.sheldan.abstracto.core.service.UserService;
|
||||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
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.config.feature.ModerationFeatureDefinition;
|
||||||
import dev.sheldan.abstracto.moderation.listener.InfractionUpdatedDescriptionListener;
|
import dev.sheldan.abstracto.moderation.listener.InfractionUpdatedDescriptionListener;
|
||||||
import dev.sheldan.abstracto.moderation.model.database.Infraction;
|
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.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.BanService;
|
||||||
import dev.sheldan.abstracto.moderation.service.BanServiceBean;
|
|
||||||
import dev.sheldan.abstracto.moderation.service.management.InfractionManagementService;
|
import dev.sheldan.abstracto.moderation.service.management.InfractionManagementService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.User;
|
||||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import static dev.sheldan.abstracto.moderation.listener.UserBannedListener.USER_BANNED_NOTIFICATION_TEMPLATE;
|
||||||
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionListener {
|
public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionListener {
|
||||||
@@ -45,7 +43,7 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
|||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BanServiceBean banServiceBean;
|
private TemplateService templateService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BanReasonUpdatedListener self;
|
private BanReasonUpdatedListener self;
|
||||||
@@ -60,7 +58,7 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
|||||||
public CompletableFuture<DefaultListenerResult> execute(InfractionDescriptionEventModel model) {
|
public CompletableFuture<DefaultListenerResult> execute(InfractionDescriptionEventModel model) {
|
||||||
Infraction infraction = infractionManagementService.loadInfraction(model.getInfractionId());
|
Infraction infraction = infractionManagementService.loadInfraction(model.getInfractionId());
|
||||||
CompletableFuture<User> infractionUser = userService.retrieveUserForId(infraction.getUser().getUserReference().getId());
|
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<>();
|
CompletableFuture<DefaultListenerResult> returningFuture = new CompletableFuture<>();
|
||||||
Long infractionId = infraction.getId();
|
Long infractionId = infraction.getId();
|
||||||
CompletableFuture.allOf(infractionUser, creatorUser)
|
CompletableFuture.allOf(infractionUser, creatorUser)
|
||||||
@@ -74,26 +72,17 @@ public class BanReasonUpdatedListener implements InfractionUpdatedDescriptionLis
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@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());
|
Infraction infraction = infractionManagementService.loadInfraction(model.getInfractionId());
|
||||||
GuildMessageChannel messageChannel = channelService.getMessageChannelFromServer(model.getServerId(), infraction.getLogChannel().getId());
|
GuildMessageChannel messageChannel = channelService.getMessageChannelFromServer(model.getServerId(), infraction.getLogChannel().getId());
|
||||||
Duration deletionDuration = infraction
|
UserBannedListenerLogModel banLog = UserBannedListenerLogModel
|
||||||
.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
|
|
||||||
.builder()
|
.builder()
|
||||||
.bannedUser(infractionUser.isCompletedExceptionally() ? null : UserDisplay.fromUser(infractionUser.join()))
|
.bannedUser(infractionUser.isCompletedExceptionally() ? null : UserDisplay.fromUser(infractionUser.join()))
|
||||||
.banningMember(infractionCreator.isCompletedExceptionally() ? null : MemberDisplay.fromMember(infractionCreator.join()))
|
.banningUser(infractionCreator.isCompletedExceptionally() ? null : UserDisplay.fromUser(infractionCreator.join()))
|
||||||
.deletionDuration(deletionDuration)
|
|
||||||
.reason(model.getNewDescription())
|
.reason(model.getNewDescription())
|
||||||
.build();
|
.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())
|
messageService.editMessageInChannel(messageChannel, message, infraction.getLogMessageId())
|
||||||
.thenAccept(unused1 -> returningFuture.complete(DefaultListenerResult.PROCESSED))
|
.thenAccept(unused1 -> returningFuture.complete(DefaultListenerResult.PROCESSED))
|
||||||
.exceptionally(throwable1 -> {
|
.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.ServerUser;
|
||||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay;
|
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||||
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||||
import dev.sheldan.abstracto.core.service.*;
|
import dev.sheldan.abstracto.core.service.MessageService;
|
||||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
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.ModerationFeatureConfig;
|
||||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureDefinition;
|
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.BanResult;
|
||||||
import dev.sheldan.abstracto.moderation.model.database.Infraction;
|
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.BanNotificationModel;
|
||||||
import dev.sheldan.abstracto.moderation.model.template.command.UnBanLog;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -34,16 +29,11 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class BanServiceBean implements BanService {
|
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";
|
public static final String BAN_NOTIFICATION = "ban_notification";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TemplateService templateService;
|
private TemplateService templateService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private PostTargetService postTargetService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BanServiceBean self;
|
private BanServiceBean self;
|
||||||
|
|
||||||
@@ -64,13 +54,6 @@ public class BanServiceBean implements BanService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<BanResult> banUserWithNotification(ServerUser userToBeBanned, String reason, ServerUser banningUser, Guild guild, Duration deletionDuration) {
|
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};
|
BanResult[] result = {BanResult.SUCCESSFUL};
|
||||||
return sendBanNotification(userToBeBanned, reason, guild)
|
return sendBanNotification(userToBeBanned, reason, guild)
|
||||||
.exceptionally(throwable -> {
|
.exceptionally(throwable -> {
|
||||||
@@ -78,13 +61,12 @@ public class BanServiceBean implements BanService {
|
|||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.thenCompose(unused -> banUser(guild, userToBeBanned, deletionDuration, reason))
|
.thenCompose(unused -> banUser(guild, userToBeBanned, deletionDuration, reason))
|
||||||
.thenCompose(unused -> sendBanLogMessage(banLog, guild.getIdLong()))
|
.thenAccept(banLogMessage -> self.evaluateAndStoreInfraction(userToBeBanned, guild, reason, banningUser, deletionDuration))
|
||||||
.thenAccept(banLogMessage -> self.evaluateAndStoreInfraction(userToBeBanned, guild, reason, banningUser, banLogMessage, deletionDuration))
|
|
||||||
.thenApply(unused -> result[0]);
|
.thenApply(unused -> result[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@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())) {
|
if(featureFlagService.getFeatureFlagValue(ModerationFeatureDefinition.INFRACTIONS, guild.getIdLong())) {
|
||||||
Long infractionPoints = configService.getLongValueOrConfigDefault(ModerationFeatureConfig.BAN_INFRACTION_POINTS, guild.getIdLong());
|
Long infractionPoints = configService.getLongValueOrConfigDefault(ModerationFeatureConfig.BAN_INFRACTION_POINTS, guild.getIdLong());
|
||||||
AUserInAServer bannedUser = userInServerManagementService.loadOrCreateUser(guild.getIdLong(), user.getUserId());
|
AUserInAServer bannedUser = userInServerManagementService.loadOrCreateUser(guild.getIdLong(), user.getUserId());
|
||||||
@@ -94,7 +76,7 @@ public class BanServiceBean implements BanService {
|
|||||||
deletionDuration = Duration.ZERO;
|
deletionDuration = Duration.ZERO;
|
||||||
}
|
}
|
||||||
parameters.put(INFRACTION_PARAMETER_DELETION_DURATION_KEY, deletionDuration.toString());
|
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);
|
.thenApply(Infraction::getId);
|
||||||
} else {
|
} else {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
@@ -111,17 +93,6 @@ public class BanServiceBean implements BanService {
|
|||||||
return messageService.sendMessageToUser(serverUser, message).thenAccept(message1 -> {});
|
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
|
@Override
|
||||||
public CompletableFuture<Void> banUser(Guild guild, ServerUser userToBeBanned, Duration deletionDuration, String reason) {
|
public CompletableFuture<Void> banUser(Guild guild, ServerUser userToBeBanned, Duration deletionDuration, String reason) {
|
||||||
log.info("Banning user {} in guild {}.", userToBeBanned.getUserId(), guild.getId());
|
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()));
|
.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);
|
List<Infraction> infractions = infractionManagementService.getActiveInfractionsForUser(aUserInAServer);
|
||||||
log.info("Calculating points for user {} in server {} with {} infractions.",
|
log.info("Calculating points for user {} in server {} with {} infractions.",
|
||||||
aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId(), infractions.size());
|
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
|
@Override
|
||||||
@@ -112,8 +112,8 @@ public class InfractionServiceBean implements InfractionService {
|
|||||||
public CompletableFuture<Void> createInfractionNotification(AUserInAServer aUserInAServer, Long points, String type, String description) {
|
public CompletableFuture<Void> createInfractionNotification(AUserInAServer aUserInAServer, Long points, String type, String description) {
|
||||||
Long serverId = aUserInAServer.getServerReference().getId();
|
Long serverId = aUserInAServer.getServerReference().getId();
|
||||||
Long currentPoints = getActiveInfractionPointsForUser(aUserInAServer);
|
Long currentPoints = getActiveInfractionPointsForUser(aUserInAServer);
|
||||||
Long newPoints = currentPoints + points;
|
Long oldPoints = currentPoints - points;
|
||||||
Pair<Integer, Integer> levelChange = infractionLevelChanged(serverId, newPoints, currentPoints);
|
Pair<Integer, Integer> levelChange = infractionLevelChanged(serverId, currentPoints, oldPoints);
|
||||||
Integer oldLevel = levelChange.getFirst();
|
Integer oldLevel = levelChange.getFirst();
|
||||||
Integer newLevel = levelChange.getSecond();
|
Integer newLevel = levelChange.getSecond();
|
||||||
if(!oldLevel.equals(newLevel)) {
|
if(!oldLevel.equals(newLevel)) {
|
||||||
@@ -124,10 +124,10 @@ public class InfractionServiceBean implements InfractionService {
|
|||||||
.oldLevel(oldLevel)
|
.oldLevel(oldLevel)
|
||||||
.type(type)
|
.type(type)
|
||||||
.description(description)
|
.description(description)
|
||||||
.oldPoints(currentPoints)
|
.oldPoints(oldPoints)
|
||||||
.newPoints(newPoints)
|
.newPoints(currentPoints)
|
||||||
.build();
|
.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);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(INFRACTION_NOTIFICATION_TEMPLATE_KEY, model, serverId);
|
||||||
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, InfractionPostTarget.INFRACTION_NOTIFICATION, serverId));
|
return FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, InfractionPostTarget.INFRACTION_NOTIFICATION, serverId));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ abstracto.postTargets.banLog.name=banLog
|
|||||||
abstracto.postTargets.unBanLog.name=unBanLog
|
abstracto.postTargets.unBanLog.name=unBanLog
|
||||||
abstracto.postTargets.muteLog.name=muteLog
|
abstracto.postTargets.muteLog.name=muteLog
|
||||||
abstracto.postTargets.decayLog.name=decayLog
|
abstracto.postTargets.decayLog.name=decayLog
|
||||||
|
abstracto.postTargets.infractionNotification.name=infractionNotification
|
||||||
|
|
||||||
abstracto.featureModes.warnDecayLogging.featureName=warnings
|
abstracto.featureModes.warnDecayLogging.featureName=warnings
|
||||||
abstracto.featureModes.warnDecayLogging.mode=warnDecayLogging
|
abstracto.featureModes.warnDecayLogging.mode=warnDecayLogging
|
||||||
@@ -49,20 +50,20 @@ abstracto.featureModes.reactionReportActions.featureName=reportReactions
|
|||||||
abstracto.featureModes.reactionReportActions.mode=reactionReportActions
|
abstracto.featureModes.reactionReportActions.mode=reactionReportActions
|
||||||
abstracto.featureModes.reactionReportActions.enabled=false
|
abstracto.featureModes.reactionReportActions.enabled=false
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLvl1.name=infractionLvl1
|
abstracto.systemConfigs.infractionLevel1.name=infractionLevel1
|
||||||
abstracto.systemConfigs.infractionLvl1.longValue=10
|
abstracto.systemConfigs.infractionLevel1.longValue=10
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLvl2.name=infractionLvl2
|
abstracto.systemConfigs.infractionLevel2.name=infractionLevel2
|
||||||
abstracto.systemConfigs.infractionLvl2.longValue=20
|
abstracto.systemConfigs.infractionLevel2.longValue=20
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLvl3.name=infractionLvl3
|
abstracto.systemConfigs.infractionLevel3.name=infractionLevel3
|
||||||
abstracto.systemConfigs.infractionLvl3.longValue=30
|
abstracto.systemConfigs.infractionLevel3.longValue=30
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLvl4.name=infractionLvl4
|
abstracto.systemConfigs.infractionLevel4.name=infractionLevel4
|
||||||
abstracto.systemConfigs.infractionLvl4.longValue=40
|
abstracto.systemConfigs.infractionLevel4.longValue=40
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLvl5.name=infractionLvl5
|
abstracto.systemConfigs.infractionLevel5.name=infractionLevel5
|
||||||
abstracto.systemConfigs.infractionLvl5.longValue=50
|
abstracto.systemConfigs.infractionLevel5.longValue=50
|
||||||
|
|
||||||
abstracto.systemConfigs.infractionLevels.name=infractionLevels
|
abstracto.systemConfigs.infractionLevels.name=infractionLevels
|
||||||
abstracto.systemConfigs.infractionLevels.longValue=5
|
abstracto.systemConfigs.infractionLevels.longValue=5
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
package dev.sheldan.abstracto.moderation.model.template.listener;
|
package dev.sheldan.abstracto.moderation.model.template.listener;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class UserBannedListenerModel {
|
public class UserBannedListenerLogModel {
|
||||||
private User bannedUser;
|
private UserDisplay bannedUser;
|
||||||
private String reason;
|
private String reason;
|
||||||
private User banningUser;
|
private UserDisplay banningUser;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package dev.sheldan.abstracto.moderation.model.template.listener;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
public class UserUnBannedListenerLogModel {
|
||||||
|
private UserDisplay unBannedUser;
|
||||||
|
private String reason;
|
||||||
|
private UserDisplay unBanningUser;
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package dev.sheldan.abstracto.moderation.model.template.listener;
|
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import net.dv8tion.jda.api.entities.User;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@Builder
|
|
||||||
public class UserUnBannedListenerModel {
|
|
||||||
private User unBannedUser;
|
|
||||||
private User unBanningUser;
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ public interface BanService {
|
|||||||
String BAN_INFRACTION_TYPE = "ban";
|
String BAN_INFRACTION_TYPE = "ban";
|
||||||
String INFRACTION_PARAMETER_DELETION_DURATION_KEY = "DELETION_DURATION";
|
String INFRACTION_PARAMETER_DELETION_DURATION_KEY = "DELETION_DURATION";
|
||||||
CompletableFuture<BanResult> banUserWithNotification(ServerUser userToBeBanned, String reason, ServerUser banningUser, Guild guild, Duration deletionDuration);
|
CompletableFuture<BanResult> banUserWithNotification(ServerUser userToBeBanned, String reason, ServerUser banningUser, Guild guild, Duration deletionDuration);
|
||||||
CompletableFuture<Void> unBanUserWithNotification(Long userId, ServerUser unBanningMember, Guild guild);
|
|
||||||
CompletableFuture<Void> banUser(Guild guild, ServerUser userToBeBanned, Duration deletionDuration, String reason);
|
CompletableFuture<Void> banUser(Guild guild, ServerUser userToBeBanned, Duration deletionDuration, String reason);
|
||||||
CompletableFuture<Void> unbanUser(Guild guild, Long userId);
|
CompletableFuture<Void> unbanUser(Guild guild, Long userId);
|
||||||
CompletableFuture<Void> softBanUser(Guild guild, ServerUser user, Duration delDays);
|
CompletableFuture<Void> softBanUser(Guild guild, ServerUser user, Duration delDays);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import dev.sheldan.abstracto.core.models.FullGuild;
|
|||||||
import dev.sheldan.abstracto.core.models.FullUserInServer;
|
import dev.sheldan.abstracto.core.models.FullUserInServer;
|
||||||
import dev.sheldan.abstracto.core.models.UndoActionInstance;
|
import dev.sheldan.abstracto.core.models.UndoActionInstance;
|
||||||
import dev.sheldan.abstracto.core.models.database.*;
|
import dev.sheldan.abstracto.core.models.database.*;
|
||||||
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
import dev.sheldan.abstracto.core.service.*;
|
import dev.sheldan.abstracto.core.service.*;
|
||||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||||
@@ -914,7 +915,7 @@ public class ModMailThreadServiceBean implements ModMailThreadService {
|
|||||||
.build();
|
.build();
|
||||||
Long modmailThreadId = modMailThread.getId();
|
Long modmailThreadId = modMailThread.getId();
|
||||||
return userService.retrieveUserForId(modMailThread.getUser().getUserReference().getId()).thenApply(user -> {
|
return userService.retrieveUserForId(modMailThread.getUser().getUserReference().getId()).thenApply(user -> {
|
||||||
headerModel.setUser(user);
|
headerModel.setUser(UserDisplay.fromUser(user));
|
||||||
return self.sendClosingHeader(headerModel, modmailThreadId).get(0);
|
return self.sendClosingHeader(headerModel, modmailThreadId).get(0);
|
||||||
}).thenCompose(Function.identity());
|
}).thenCompose(Function.identity());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package dev.sheldan.abstracto.modmail.model.template;
|
package dev.sheldan.abstracto.modmail.model.template;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
|
||||||
import dev.sheldan.abstracto.modmail.model.database.ModMailThread;
|
import dev.sheldan.abstracto.modmail.model.database.ModMailThread;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -39,7 +39,7 @@ public class ModMailClosingHeaderModel {
|
|||||||
|
|
||||||
private Member closingMember;
|
private Member closingMember;
|
||||||
private Boolean silently;
|
private Boolean silently;
|
||||||
private User user;
|
private UserDisplay user;
|
||||||
private Long serverId;
|
private Long serverId;
|
||||||
private Long modmailThreadId;
|
private Long modmailThreadId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ package dev.sheldan.abstracto.core.listener.async.jda;
|
|||||||
import dev.sheldan.abstracto.core.listener.ListenerService;
|
import dev.sheldan.abstracto.core.listener.ListenerService;
|
||||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
import dev.sheldan.abstracto.core.models.listener.UserBannedModel;
|
import dev.sheldan.abstracto.core.models.listener.UserBannedModel;
|
||||||
|
import dev.sheldan.abstracto.core.service.UserService;
|
||||||
|
import dev.sheldan.abstracto.core.utils.CompletableFutureMap;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.events.guild.GuildBanEvent;
|
import net.dv8tion.jda.api.audit.ActionType;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
@@ -12,11 +16,13 @@ import org.springframework.core.task.TaskExecutor;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AsyncUserBannedListenerBean extends ListenerAdapter {
|
public class AsyncUserBannedListenerBean extends ListenerAdapter {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private List<AsyncUserBannedListener> listenerList;
|
private List<AsyncUserBannedListener> listenerList;
|
||||||
|
|
||||||
@@ -27,25 +33,48 @@ public class AsyncUserBannedListenerBean extends ListenerAdapter {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ListenerService listenerService;
|
private ListenerService listenerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGuildBan(@Nonnull GuildBanEvent event) {
|
public void onGuildAuditLogEntryCreate(@Nonnull GuildAuditLogEntryCreateEvent event) {
|
||||||
if(listenerList == null) return;
|
if(listenerList == null) return;
|
||||||
UserBannedModel model = getModel(event);
|
if(event.getEntry().getType().equals(ActionType.BAN)) {
|
||||||
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
log.info("Handling ban audit log entry created for user {} in server {}.", event.getEntry().getTargetIdLong(), event.getGuild().getIdLong());
|
||||||
|
CompletableFutureMap<Long, User> longUserCompletableFutureMap = userService.retrieveUsersMapped(Arrays.asList(event.getEntry().getTargetIdLong(), event.getEntry().getUserIdLong()));
|
||||||
|
longUserCompletableFutureMap.getMainFuture().thenAccept(avoid -> {
|
||||||
|
User bannedUser = longUserCompletableFutureMap.getElement(event.getEntry().getTargetIdLong());
|
||||||
|
User banningUser = longUserCompletableFutureMap.getElement(event.getEntry().getUserIdLong());
|
||||||
|
UserBannedModel model = getModel(event, bannedUser, banningUser);
|
||||||
|
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
||||||
|
}).exceptionally(throwable -> {
|
||||||
|
log.warn("Failed to fetch users {} or {} for banned event.", event.getEntry().getTargetIdLong(), event.getEntry().getUserIdLong(), throwable);
|
||||||
|
UserBannedModel model = getModel(event, null, null);
|
||||||
|
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserBannedModel getModel(GuildBanEvent event) {
|
private UserBannedModel getModel(GuildAuditLogEntryCreateEvent event, User bannedUser, User banningUser) {
|
||||||
ServerUser serverUser = ServerUser
|
ServerUser bannedServerUser = ServerUser
|
||||||
.builder()
|
.builder()
|
||||||
.serverId(event.getGuild().getIdLong())
|
.serverId(event.getGuild().getIdLong())
|
||||||
.userId(event.getUser().getIdLong())
|
.userId(event.getEntry().getTargetIdLong())
|
||||||
.isBot(event.getUser().isBot())
|
.build();
|
||||||
|
ServerUser banningServerUser = ServerUser
|
||||||
|
.builder()
|
||||||
|
.serverId(event.getGuild().getIdLong())
|
||||||
|
.userId(event.getEntry().getUserIdLong())
|
||||||
.build();
|
.build();
|
||||||
return UserBannedModel
|
return UserBannedModel
|
||||||
.builder()
|
.builder()
|
||||||
.bannedUser(serverUser)
|
.bannedServerUser(bannedServerUser)
|
||||||
|
.banningUser(banningUser)
|
||||||
|
.banningServerUser(banningServerUser)
|
||||||
.guild(event.getGuild())
|
.guild(event.getGuild())
|
||||||
.user(event.getUser())
|
.reason(event.getEntry().getReason())
|
||||||
|
.bannedUser(bannedUser)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ package dev.sheldan.abstracto.core.listener.async.jda;
|
|||||||
import dev.sheldan.abstracto.core.listener.ListenerService;
|
import dev.sheldan.abstracto.core.listener.ListenerService;
|
||||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
import dev.sheldan.abstracto.core.models.listener.UserUnBannedModel;
|
import dev.sheldan.abstracto.core.models.listener.UserUnBannedModel;
|
||||||
|
import dev.sheldan.abstracto.core.service.UserService;
|
||||||
|
import dev.sheldan.abstracto.core.utils.CompletableFutureMap;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.events.guild.GuildUnbanEvent;
|
import net.dv8tion.jda.api.audit.ActionType;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
@@ -12,6 +16,7 @@ import org.springframework.core.task.TaskExecutor;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -27,25 +32,48 @@ public class AsyncUserUnBannedListenerBean extends ListenerAdapter {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ListenerService listenerService;
|
private ListenerService listenerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGuildUnban(@Nonnull GuildUnbanEvent event) {
|
public void onGuildAuditLogEntryCreate(@Nonnull GuildAuditLogEntryCreateEvent event) {
|
||||||
if(listenerList == null) return;
|
if(listenerList == null) return;
|
||||||
UserUnBannedModel model = getModel(event);
|
if(event.getEntry().getType().equals(ActionType.UNBAN)) {
|
||||||
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
log.info("Handling unBan audit log entry created for user {} in server {}.", event.getEntry().getTargetIdLong(), event.getGuild().getIdLong());
|
||||||
|
CompletableFutureMap<Long, User> longUserCompletableFutureMap = userService.retrieveUsersMapped(Arrays.asList(event.getEntry().getTargetIdLong(), event.getEntry().getUserIdLong()));
|
||||||
|
longUserCompletableFutureMap.getMainFuture().thenAccept(avoid -> {
|
||||||
|
User unBannedUser = longUserCompletableFutureMap.getElement(event.getEntry().getTargetIdLong());
|
||||||
|
User unBanningUser = longUserCompletableFutureMap.getElement(event.getEntry().getUserIdLong());
|
||||||
|
UserUnBannedModel model = getModel(event, unBannedUser, unBanningUser);
|
||||||
|
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
||||||
|
}).exceptionally(throwable -> {
|
||||||
|
log.warn("Failed to fetch users {} or {} for unbanned event.", event.getEntry().getTargetIdLong(), event.getEntry().getUserIdLong(), throwable);
|
||||||
|
UserUnBannedModel model = getModel(event, null, null);
|
||||||
|
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, leaveListenerExecutor));
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserUnBannedModel getModel(GuildUnbanEvent event) {
|
private UserUnBannedModel getModel(GuildAuditLogEntryCreateEvent event, User unBannedUser, User unBanningUser) {
|
||||||
ServerUser serverUser = ServerUser
|
ServerUser unBannedServerUser = ServerUser
|
||||||
.builder()
|
.builder()
|
||||||
.serverId(event.getGuild().getIdLong())
|
.serverId(event.getGuild().getIdLong())
|
||||||
.userId(event.getUser().getIdLong())
|
.userId(event.getEntry().getTargetIdLong())
|
||||||
.isBot(event.getUser().isBot())
|
.build();
|
||||||
|
ServerUser unBanningServerUser = ServerUser
|
||||||
|
.builder()
|
||||||
|
.serverId(event.getGuild().getIdLong())
|
||||||
|
.userId(event.getEntry().getUserIdLong())
|
||||||
.build();
|
.build();
|
||||||
return UserUnBannedModel
|
return UserUnBannedModel
|
||||||
.builder()
|
.builder()
|
||||||
.unbannedUser(serverUser)
|
.unBannedServerUser(unBannedServerUser)
|
||||||
|
.unBanningUser(unBanningUser)
|
||||||
|
.unBanningServerUser(unBanningServerUser)
|
||||||
.guild(event.getGuild())
|
.guild(event.getGuild())
|
||||||
.user(event.getUser())
|
.reason(event.getEntry().getReason())
|
||||||
|
.unBannedUser(unBannedUser)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.core.service;
|
package dev.sheldan.abstracto.core.service;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
|
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
|
||||||
|
import dev.sheldan.abstracto.core.utils.CompletableFutureMap;
|
||||||
import net.dv8tion.jda.api.entities.SelfUser;
|
import net.dv8tion.jda.api.entities.SelfUser;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -8,6 +9,7 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -30,6 +32,12 @@ public class UserServiceBean implements UserService {
|
|||||||
return new CompletableFutureList<>(userFutures);
|
return new CompletableFutureList<>(userFutures);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFutureMap<Long, User> retrieveUsersMapped(List<Long> ids) {
|
||||||
|
return new CompletableFutureMap<>(ids.stream()
|
||||||
|
.collect(Collectors.toMap(Function.identity(), this::retrieveUserForId)));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SelfUser getSelfUser() {
|
public SelfUser getSelfUser() {
|
||||||
return botService.getInstance().getSelfUser();
|
return botService.getInstance().getSelfUser();
|
||||||
|
|||||||
@@ -1,21 +1,51 @@
|
|||||||
package dev.sheldan.abstracto.core.models.frontend;
|
package dev.sheldan.abstracto.core.models.frontend;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
|
import dev.sheldan.abstracto.core.utils.MemberUtils;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Builder
|
@Builder
|
||||||
public class UserDisplay {
|
public class UserDisplay {
|
||||||
private String avatarUrl;
|
private String avatarUrl;
|
||||||
private String name;
|
private String name;
|
||||||
private String id;
|
private String discriminator;
|
||||||
|
private String userMention;
|
||||||
|
private Long id;
|
||||||
|
|
||||||
public static UserDisplay fromMember(Member member) {
|
public static UserDisplay fromMember(Member member) {
|
||||||
return builder()
|
return builder()
|
||||||
.avatarUrl(member.getEffectiveAvatarUrl())
|
.avatarUrl(member.getEffectiveAvatarUrl())
|
||||||
.name(member.getEffectiveName())
|
.name(member.getEffectiveName())
|
||||||
.id(member.getId())
|
.userMention(MemberUtils.getUserAsMention(member.getIdLong()))
|
||||||
|
.id(member.getIdLong())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserDisplay fromUser(User user) {
|
||||||
|
return builder()
|
||||||
|
.discriminator(user.getDiscriminator())
|
||||||
|
.avatarUrl(user.getEffectiveAvatarUrl())
|
||||||
|
.name(user.getEffectiveName())
|
||||||
|
.userMention(MemberUtils.getUserAsMention(user.getIdLong()))
|
||||||
|
.id(user.getIdLong())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserDisplay fromServerUser(ServerUser user) {
|
||||||
|
return builder()
|
||||||
|
.userMention(MemberUtils.getUserAsMention(user.getUserId()))
|
||||||
|
.id(user.getUserId())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserDisplay fromId(Long id) {
|
||||||
|
return builder()
|
||||||
|
.userMention(MemberUtils.getUserAsMention(id))
|
||||||
|
.id(id)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,12 @@ import net.dv8tion.jda.api.entities.User;
|
|||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class UserBannedModel implements FeatureAwareListenerModel {
|
public class UserBannedModel implements FeatureAwareListenerModel {
|
||||||
private ServerUser bannedUser;
|
private ServerUser bannedServerUser;
|
||||||
private User user;
|
private ServerUser banningServerUser;
|
||||||
|
private User bannedUser;
|
||||||
|
private User banningUser;
|
||||||
private Guild guild;
|
private Guild guild;
|
||||||
|
private String reason;
|
||||||
@Override
|
@Override
|
||||||
public Long getServerId() {
|
public Long getServerId() {
|
||||||
return guild.getIdLong();
|
return guild.getIdLong();
|
||||||
|
|||||||
@@ -12,9 +12,12 @@ import net.dv8tion.jda.api.entities.User;
|
|||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class UserUnBannedModel implements FeatureAwareListenerModel {
|
public class UserUnBannedModel implements FeatureAwareListenerModel {
|
||||||
private ServerUser unbannedUser;
|
private ServerUser unBannedServerUser;
|
||||||
private User user;
|
private ServerUser unBanningServerUser;
|
||||||
|
private User unBannedUser;
|
||||||
|
private User unBanningUser;
|
||||||
private Guild guild;
|
private Guild guild;
|
||||||
|
private String reason;
|
||||||
@Override
|
@Override
|
||||||
public Long getServerId() {
|
public Long getServerId() {
|
||||||
return guild.getIdLong();
|
return guild.getIdLong();
|
||||||
|
|||||||
@@ -11,14 +11,18 @@ import net.dv8tion.jda.api.entities.User;
|
|||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class UserDisplay {
|
public class UserDisplay {
|
||||||
private Long userId;
|
private Long id;
|
||||||
private String userMention;
|
private String userMention;
|
||||||
|
private String discriminator;
|
||||||
|
private String name;
|
||||||
|
|
||||||
public static UserDisplay fromUser(User user) {
|
public static UserDisplay fromUser(User user) {
|
||||||
return UserDisplay
|
return UserDisplay
|
||||||
.builder()
|
.builder()
|
||||||
.userMention(MemberUtils.getUserAsMention(user.getIdLong()))
|
.userMention(MemberUtils.getUserAsMention(user.getIdLong()))
|
||||||
.userId(user.getIdLong())
|
.name(user.getEffectiveName())
|
||||||
|
.discriminator(user.getDiscriminator())
|
||||||
|
.id(user.getIdLong())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +30,7 @@ public class UserDisplay {
|
|||||||
return UserDisplay
|
return UserDisplay
|
||||||
.builder()
|
.builder()
|
||||||
.userMention(MemberUtils.getUserAsMention(serverUser.getUserId()))
|
.userMention(MemberUtils.getUserAsMention(serverUser.getUserId()))
|
||||||
.userId(serverUser.getUserId())
|
.id(serverUser.getUserId())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +38,7 @@ public class UserDisplay {
|
|||||||
return UserDisplay
|
return UserDisplay
|
||||||
.builder()
|
.builder()
|
||||||
.userMention(MemberUtils.getUserAsMention(id))
|
.userMention(MemberUtils.getUserAsMention(id))
|
||||||
.userId(id)
|
.id(id)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.core.service;
|
package dev.sheldan.abstracto.core.service;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
|
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
|
||||||
|
import dev.sheldan.abstracto.core.utils.CompletableFutureMap;
|
||||||
import net.dv8tion.jda.api.entities.SelfUser;
|
import net.dv8tion.jda.api.entities.SelfUser;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
|
||||||
@@ -10,5 +11,6 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
public interface UserService {
|
public interface UserService {
|
||||||
CompletableFuture<User> retrieveUserForId(Long id);
|
CompletableFuture<User> retrieveUserForId(Long id);
|
||||||
CompletableFutureList<User> retrieveUsers(List<Long> ids);
|
CompletableFutureList<User> retrieveUsers(List<Long> ids);
|
||||||
|
CompletableFutureMap<Long, User> retrieveUsersMapped(List<Long> ids);
|
||||||
SelfUser getSelfUser();
|
SelfUser getSelfUser();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package dev.sheldan.abstracto.core.utils;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Slf4j
|
||||||
|
public class CompletableFutureMap<L, T> {
|
||||||
|
private final CompletableFuture<Void> mainFuture;
|
||||||
|
private final Map<L, CompletableFuture<T>> futures;
|
||||||
|
|
||||||
|
public CompletableFutureMap(Map<L ,CompletableFuture<T>> futures) {
|
||||||
|
this.mainFuture = CompletableFuture.allOf(futures.values().toArray(new CompletableFuture[0]));
|
||||||
|
this.futures = futures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> getObjects() {
|
||||||
|
List<T> result = new ArrayList<>();
|
||||||
|
futures.values().forEach(future -> {
|
||||||
|
if(!future.isCompletedExceptionally()) {
|
||||||
|
result.add(future.join());
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
future.join();
|
||||||
|
} catch (Exception exception) {
|
||||||
|
log.warn("Future completed with exception.", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getElement(L key) {
|
||||||
|
if(!getFutures().containsKey(key)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
CompletableFuture<T> future = getFutures().get(key);
|
||||||
|
if(!future.isCompletedExceptionally()) {
|
||||||
|
return future.join();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user