[AB-297] adding server names to invite deletion log (if available)

This commit is contained in:
Sheldan
2021-09-08 23:04:35 +02:00
parent 5e6999cd45
commit 1d85eb1e7e
2 changed files with 52 additions and 17 deletions

View File

@@ -21,6 +21,9 @@ import dev.sheldan.abstracto.invitefilter.model.template.listener.DeletedInvite;
import dev.sheldan.abstracto.invitefilter.model.template.listener.DeletedInvitesNotificationModel; import dev.sheldan.abstracto.invitefilter.model.template.listener.DeletedInvitesNotificationModel;
import dev.sheldan.abstracto.invitefilter.service.management.AllowedInviteLinkManagement; import dev.sheldan.abstracto.invitefilter.service.management.AllowedInviteLinkManagement;
import dev.sheldan.abstracto.invitefilter.service.management.FilteredInviteLinkManagement; import dev.sheldan.abstracto.invitefilter.service.management.FilteredInviteLinkManagement;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Invite; import net.dv8tion.jda.api.entities.Invite;
@@ -34,7 +37,6 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -222,11 +224,11 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
return Invite.resolve(jda, extractCode(code)).submit(); return Invite.resolve(jda, extractCode(code)).submit();
} }
private void sendDeletionNotification(List<String> codes, Message message) { private CompletableFuture<Void> sendDeletionNotification(List<InviteToDelete> codes, Message message) {
Long serverId = message.getGuild().getIdLong(); Long serverId = message.getGuild().getIdLong();
if(!postTargetService.postTargetDefinedInServer(InviteFilterPostTarget.INVITE_DELETE_LOG, serverId)) { if(!postTargetService.postTargetDefinedInServer(InviteFilterPostTarget.INVITE_DELETE_LOG, serverId)) {
log.info("Post target {} not defined for server {} - not sending invite link deletion notification.", InviteFilterPostTarget.INVITE_DELETE_LOG.getKey(), serverId); log.info("Post target {} not defined for server {} - not sending invite link deletion notification.", InviteFilterPostTarget.INVITE_DELETE_LOG.getKey(), serverId);
return; return CompletableFuture.completedFuture(null);
} }
DeletedInvitesNotificationModel model = DeletedInvitesNotificationModel DeletedInvitesNotificationModel model = DeletedInvitesNotificationModel
.builder() .builder()
@@ -240,21 +242,30 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
codes.size(), serverId, message.getAuthor().getIdLong(), message.getChannel().getIdLong(), message.getIdLong()); codes.size(), serverId, message.getAuthor().getIdLong(), message.getChannel().getIdLong(), message.getIdLong());
MessageToSend messageToSend = templateService.renderEmbedTemplate(INVITE_LINK_DELETED_NOTIFICATION_EMBED_TEMPLATE_KEY, model, message.getGuild().getIdLong()); MessageToSend messageToSend = templateService.renderEmbedTemplate(INVITE_LINK_DELETED_NOTIFICATION_EMBED_TEMPLATE_KEY, model, message.getGuild().getIdLong());
List<CompletableFuture<Message>> messageFutures = postTargetService.sendEmbedInPostTarget(messageToSend, InviteFilterPostTarget.INVITE_DELETE_LOG, serverId); List<CompletableFuture<Message>> messageFutures = postTargetService.sendEmbedInPostTarget(messageToSend, InviteFilterPostTarget.INVITE_DELETE_LOG, serverId);
FutureUtils.toSingleFutureGeneric(messageFutures).thenAccept(unused -> return FutureUtils.toSingleFutureGeneric(messageFutures).thenAccept(unused ->
log.debug("Successfully send notification about deleted invite link in message {}.", message.getIdLong()) log.debug("Successfully send notification about deleted invite link in message {}.", message.getIdLong())
).exceptionally(throwable -> { );
log.error("Failed to send notification about deleted invite link in message {}.", message.getIdLong());
return null;
});
} }
private List<DeletedInvite> groupInvites(List<String> codes) { private List<DeletedInvite> groupInvites(List<InviteToDelete> codes) {
Map<String, String> codeToGuildName = new HashMap<>();
for (InviteToDelete invite: codes) {
if(!codeToGuildName.containsKey(invite.getInviteCode())) {
codeToGuildName.put(invite.getInviteCode(), invite.getGuildName());
}
}
return codes return codes
.stream() .stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .collect(Collectors.groupingBy(InviteToDelete::getInviteCode, Collectors.counting()))
.entrySet() .entrySet()
.stream() .stream()
.map(functionLongEntry -> new DeletedInvite(functionLongEntry.getKey(), functionLongEntry.getValue())) .map(functionLongEntry -> DeletedInvite
.builder()
.code(functionLongEntry.getKey())
.guildName(codeToGuildName.get(functionLongEntry.getKey()))
.count(functionLongEntry.getValue())
.build())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -291,12 +302,17 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
ServerUser author = ServerUser.builder().userId(message.getAuthor().getIdLong()).serverId(message.getGuild().getIdLong()).build(); ServerUser author = ServerUser.builder().userId(message.getAuthor().getIdLong()).serverId(message.getGuild().getIdLong()).build();
boolean toDelete = false; boolean toDelete = false;
Map<Long, String> targetServers = new HashMap<>(); Map<Long, String> targetServers = new HashMap<>();
List<String> deletedInvites = new ArrayList<>(); List<InviteToDelete> deletedInvites = new ArrayList<>();
for (Invite invite : invites) { for (Invite invite : invites) {
if (invite.getType().equals(Invite.InviteType.GUILD) if (invite.getType().equals(Invite.InviteType.GUILD)
&& isCodeFiltered(invite.getGuild().getIdLong(), author)) { && isCodeFiltered(invite.getGuild().getIdLong(), author)) {
toDelete = true; toDelete = true;
deletedInvites.add(invite.getCode()); InviteToDelete inviteToDelete = InviteToDelete
.builder()
.inviteCode(invite.getCode())
.guildName(invite.getGuild().getName())
.build();
deletedInvites.add(inviteToDelete);
targetServers.put(invite.getGuild().getIdLong(), invite.getGuild().getName()); targetServers.put(invite.getGuild().getIdLong(), invite.getGuild().getName());
} }
} }
@@ -310,7 +326,11 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
for(String unresolvedInvite : unResolvedInvites) { for(String unresolvedInvite : unResolvedInvites) {
if(isCodeFiltered(unresolvedInvite, author)) { if(isCodeFiltered(unresolvedInvite, author)) {
toDelete = true; toDelete = true;
deletedInvites.add(unresolvedInvite); InviteToDelete inviteToDelete = InviteToDelete
.builder()
.inviteCode(unresolvedInvite)
.build();
deletedInvites.add(inviteToDelete);
} }
} }
if(toDelete) { if(toDelete) {
@@ -322,7 +342,11 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
} }
boolean sendNotification = featureModeService.featureModeActive(InviteFilterFeatureDefinition.INVITE_FILTER, serverId, InviteFilterMode.FILTER_NOTIFICATIONS); boolean sendNotification = featureModeService.featureModeActive(InviteFilterFeatureDefinition.INVITE_FILTER, serverId, InviteFilterMode.FILTER_NOTIFICATIONS);
if(sendNotification) { if(sendNotification) {
sendDeletionNotification(deletedInvites, message); sendDeletionNotification(deletedInvites, message)
.thenAccept(unused1 -> log.info("Sent invite deletion notification.")).exceptionally(throwable1 -> {
log.error("Failed to send invite deletion notification.");
return null;
});
} }
} }
}).exceptionally(throwable -> { }).exceptionally(throwable -> {
@@ -336,4 +360,12 @@ public class InviteLinkFilterServiceBean implements InviteLinkFilterService {
metricService.registerCounter(MESSAGE_INVITE_FILTERED, "Amount of messages containing an invite filtered"); metricService.registerCounter(MESSAGE_INVITE_FILTERED, "Amount of messages containing an invite filtered");
} }
@Getter
@Setter
@Builder
private static class InviteToDelete {
private String guildName;
private String inviteCode;
}
} }

View File

@@ -1,14 +1,17 @@
package dev.sheldan.abstracto.invitefilter.model.template.listener; package dev.sheldan.abstracto.invitefilter.model.template.listener;
import lombok.AllArgsConstructor; import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Getter @Getter
@Setter @Setter
@AllArgsConstructor @EqualsAndHashCode
@Builder
public class DeletedInvite { public class DeletedInvite {
private String code; private String code;
private String guildName;
private Long count; private Long count;
} }