Skip to content

Commit 6397008

Browse files
committed
fix: allow forwarded messages with media in media-only channels
1 parent 3292631 commit 6397008

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

application/src/main/java/org/togetherjava/tjbot/features/mediaonly/MediaOnlyChannelListener.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.dv8tion.jda.api.entities.Message;
55
import net.dv8tion.jda.api.entities.MessageEmbed;
66
import net.dv8tion.jda.api.entities.MessageType;
7+
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
78
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
89
import net.dv8tion.jda.api.requests.RestAction;
910
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
@@ -13,6 +14,7 @@
1314
import org.togetherjava.tjbot.features.MessageReceiverAdapter;
1415

1516
import java.awt.Color;
17+
import java.util.List;
1618
import java.util.concurrent.TimeUnit;
1719
import java.util.regex.Pattern;
1820

@@ -51,9 +53,49 @@ public void onMessageReceived(MessageReceivedEvent event) {
5153
}
5254
}
5355

56+
// private boolean messageHasNoMediaAttached(Message message) {
57+
// return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty()
58+
// && !message.getContentRaw().contains("http");
59+
// }
60+
61+
/**
62+
* Checks whether the given message has no media attached.
63+
* <p>
64+
* A message is considered to have media if it contains attachments, embeds,
65+
* or a URL in its text content. For forwarded messages, the snapshots are also
66+
* checked for media.
67+
*
68+
* @param message the message to check
69+
* @return {@code true} if the message has no media, {@code false} otherwise
70+
*/
5471
private boolean messageHasNoMediaAttached(Message message) {
55-
return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty()
56-
&& !message.getContentRaw().contains("http");
72+
if (hasMedia(message.getAttachments(), message.getEmbeds(), message.getContentRaw())) {
73+
return false;
74+
}
75+
// checks forwarded snapshots
76+
for (MessageSnapshot snapshot : message.getMessageSnapshots()) {
77+
if (hasMedia(snapshot.getAttachments(), snapshot.getEmbeds(), snapshot.getContentRaw())) {
78+
return false;
79+
}
80+
}
81+
return true;
82+
}
83+
/**
84+
* Checks whether the given content contains any media.
85+
* <p>
86+
* Media is considered present if there are attachments, embeds,
87+
* or a URL (identified by {@code "http"}) in the text content.
88+
*
89+
* @param attachments the attachments of the message or snapshot
90+
* @param embeds the embeds of the message or snapshot
91+
* @param content the raw text content of the message or snapshot
92+
*/
93+
94+
private boolean hasMedia(List<Message.Attachment> attachments,
95+
List<MessageEmbed> embeds, String content) {
96+
return !attachments.isEmpty()
97+
|| !embeds.isEmpty()
98+
|| content.contains("http");
5799
}
58100

59101
private MessageCreateData createNotificationMessage(Message message) {

application/src/test/java/org/togetherjava/tjbot/features/mediaonly/MediaOnlyChannelListenerTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.dv8tion.jda.api.entities.Message;
55
import net.dv8tion.jda.api.entities.MessageEmbed;
66
import net.dv8tion.jda.api.entities.channel.ChannelType;
7+
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
78
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
89
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
910
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
@@ -112,4 +113,49 @@ private MessageReceivedEvent sendMessage(MessageCreateData message,
112113
mediaOnlyChannelListener.onMessageReceived(event);
113114
return event;
114115
}
116+
117+
118+
@Test
119+
void keepsForwardedMessageWithAttachment() {
120+
// GIVEN a forwarded message that contains an attachment inside the snapshot
121+
MessageCreateData message = new MessageCreateBuilder().setContent("any").build();
122+
123+
MessageSnapshot snapshot = mock(MessageSnapshot.class);
124+
when(snapshot.getAttachments()).thenReturn(List.of(mock(Message.Attachment.class)));
125+
when(snapshot.getEmbeds()).thenReturn(List.of());
126+
when(snapshot.getContentRaw()).thenReturn("");
127+
128+
// WHEN sending the forwarded message
129+
MessageReceivedEvent event = sendMessageWithSnapshots(message, List.of(snapshot));
130+
131+
// THEN it does not get deleted
132+
verify(event.getMessage(), never()).delete();
133+
}
134+
135+
@Test
136+
void deletesForwardedMessageWithoutMedia() {
137+
// GIVEN a forwarded message that contains no media inside the snapshot
138+
MessageCreateData message = new MessageCreateBuilder().setContent("any").build();
139+
140+
MessageSnapshot snapshot = mock(MessageSnapshot.class);
141+
when(snapshot.getAttachments()).thenReturn(List.of());
142+
when(snapshot.getEmbeds()).thenReturn(List.of());
143+
when(snapshot.getContentRaw()).thenReturn("just some text, no media");
144+
145+
// WHEN sending the forwarded message
146+
MessageReceivedEvent event = sendMessageWithSnapshots(message, List.of(snapshot));
147+
148+
// THEN it gets deleted
149+
verify(event.getMessage()).delete();
150+
}
151+
152+
// Добавить этот вспомогательный метод рядом с существующим sendMessage():
153+
private MessageReceivedEvent sendMessageWithSnapshots(MessageCreateData message,
154+
List<MessageSnapshot> snapshots) {
155+
MessageReceivedEvent event =
156+
jdaTester.createMessageReceiveEvent(message, List.of(), ChannelType.TEXT);
157+
when(event.getMessage().getMessageSnapshots()).thenReturn(snapshots);
158+
mediaOnlyChannelListener.onMessageReceived(event);
159+
return event;
160+
}
115161
}

0 commit comments

Comments
 (0)