From 05aedf2067ee64db9cf68fa03b94c56a6e0c6edb Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 27 Dec 2024 00:53:19 +0200 Subject: [PATCH] Handle attachment permissions in mirroring --- .../DiscordMessageMirroringModule.java | 48 +++++++++++-------- .../common/util/DiscordPermissionUtil.java | 2 + 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/discord/DiscordMessageMirroringModule.java b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/discord/DiscordMessageMirroringModule.java index 4f029478..99363de0 100644 --- a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/discord/DiscordMessageMirroringModule.java +++ b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/discord/DiscordMessageMirroringModule.java @@ -188,19 +188,42 @@ public class DiscordMessageMirroringModule extends AbstractModule { } channelIdsHandled.add(channelId); + GuildMessageChannel channel = (GuildMessageChannel) mirrorChannel.getAsJDAMessageChannel(); + MirroringConfig config = target.config; MirroringConfig.AttachmentConfig attachmentConfig = config.attachments; + int attachmentMaxSize = attachmentConfig.maximumSizeKb * 1000; + + boolean embedAttachments = attachmentConfig.embedAttachments; + boolean attachAttachments = attachmentMaxSize >= 0; + boolean hasAttachments = !attachments.isEmpty() && (embedAttachments || attachAttachments); + + String missingPermissions = DiscordPermissionUtil.missingPermissionsString( + channel, + Permission.VIEW_CHANNEL, + Permission.MANAGE_WEBHOOKS, + embedAttachments ? Permission.MESSAGE_EMBED_LINKS : null, + attachAttachments ? Permission.MESSAGE_ATTACH_FILES : null + ); + if (missingPermissions != null) { + logger().error("Failed to mirror message to " + describeChannel(mirrorChannel) + ": " + missingPermissions); + continue; + } SendableDiscordMessage.Builder messageBuilder = convert(event.getMessage(), mirrorChannel, config); - if (!attachmentEmbed.getFields().isEmpty() && attachmentConfig.embedAttachments) { + if (messageBuilder.isEmpty() && !hasAttachments) { + logger().debug("Nothing to mirror to " + mirrorChannel + ", skipping"); + return; + } + + if (embedAttachments && !attachmentEmbed.getFields().isEmpty()) { messageBuilder.addEmbed(attachmentEmbed.build()); } - int maxSize = attachmentConfig.maximumSizeKb * 1000; List streams = new ArrayList<>(); - if (!attachments.isEmpty() && maxSize >= 0) { + if (attachAttachments) { attachments.forEach((attachment, bytes) -> { - if (bytes != null && (maxSize == 0 || attachment.sizeBytes() <= maxSize)) { + if (bytes != null && (attachmentMaxSize == 0 || attachment.sizeBytes() <= attachmentMaxSize)) { InputStream stream = new ByteArrayInputStream(bytes); streams.add(stream); messageBuilder.addAttachment(stream, attachment.fileName()); @@ -208,23 +231,6 @@ public class DiscordMessageMirroringModule extends AbstractModule { }); } - if (messageBuilder.isEmpty()) { - logger().debug("Nothing to mirror to " + mirrorChannel + ", skipping"); - for (InputStream stream : streams) { - try { - stream.close(); - } catch (IOException ignored) {} - } - return; - } - - GuildMessageChannel channel = (GuildMessageChannel) mirrorChannel.getAsJDAMessageChannel(); - String missingPermissions = DiscordPermissionUtil.missingPermissionsString(channel, Permission.VIEW_CHANNEL, Permission.MANAGE_WEBHOOKS); - if (missingPermissions != null) { - logger().error("Failed to mirror message to " + describeChannel(mirrorChannel) + ": " + missingPermissions); - continue; - } - CompletableFuture future = mirrorChannel.sendMessage(messageBuilder.build()) .thenApply(msg -> new MirroredMessage(msg, config)); diff --git a/common/src/main/java/com/discordsrv/common/util/DiscordPermissionUtil.java b/common/src/main/java/com/discordsrv/common/util/DiscordPermissionUtil.java index c2e73c9c..5ce1f817 100644 --- a/common/src/main/java/com/discordsrv/common/util/DiscordPermissionUtil.java +++ b/common/src/main/java/com/discordsrv/common/util/DiscordPermissionUtil.java @@ -51,6 +51,7 @@ public final class DiscordPermissionUtil { } EnumSet missingPermissions = EnumSet.noneOf(Permission.class); for (Permission permission : permissions) { + if (permission == null) continue; if (!channel.getGuild().getSelfMember().hasPermission(channel, permission)) { missingPermissions.add(permission); } @@ -70,6 +71,7 @@ public final class DiscordPermissionUtil { public static EnumSet getMissingPermissions(Guild guild, Collection permissions) { EnumSet missingPermissions = EnumSet.noneOf(Permission.class); for (Permission permission : permissions) { + if (permission == null) continue; if (!guild.getSelfMember().hasPermission(permission)) { missingPermissions.add(permission); }