-
- Notifications
You must be signed in to change notification settings - Fork 104
Only allow media posts in media channels #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zabuzard merged 17 commits into Together-Java:develop from Mom0aut:feature/only_allow_media-posts_in_#memes Aug 28, 2022
Merged
Changes from 6 commits
Commits
Show all changes
17 commits Select commit Hold shift + click to select a range
ea5499f added MemeListener checks if the sent message in Meme Channel contain…
Mom0aut b335d09 added MemeListenerTests
Mom0aut ee6c9c1 added Review Feedback
Mom0aut 1470cf4 Merge branch 'develop' into feature/only_allow_media-posts_in_#memes
Mom0aut ce26a69 CodeFactor does not allow escaped Unicode
Mom0aut 5b07c7e fixed typo
Mom0aut e8d06e2 aded Review Feedback
Mom0aut 10af88f added Review Feedback
Mom0aut e82f416 added Review Feedback
Mom0aut 5ac5231 added Review Feedback
Mom0aut 4c39446 added Review Feedback
Mom0aut 3567c04 added Review Feedback
Mom0aut fd164b4 added Review Feedback
Mom0aut e7311ff added Review Feedback
Mom0aut 41f38d7 added Review Feedback
Mom0aut 11945a6 Improved the unit tests
Zabuzard 88f73bc Use raw message when forwarding
Zabuzard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -44,5 +44,6 @@ | |
| "Other" | ||
| ], | ||
| "categoryRoleSuffix": " - Helper" | ||
| } | ||
| }, | ||
| "mediaOnlyChannelPattern": "memes" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions 65 application/src/main/java/org/togetherjava/tjbot/commands/meme/MemeListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.togetherjava.tjbot.commands.meme; | ||
| | ||
| import net.dv8tion.jda.api.JDA; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
| import net.dv8tion.jda.api.requests.RestAction; | ||
| import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.togetherjava.tjbot.commands.MessageReceiverAdapter; | ||
| import org.togetherjava.tjbot.config.Config; | ||
| | ||
| import java.util.regex.Pattern; | ||
| | ||
| /** | ||
| * Listener that receives all sent messages from the Meme channel, checks if the message has media | ||
| * attched. | ||
| * <p> | ||
| * If there was no media attached, delete the messagen and send the User a DM, telling what they did | ||
| * wrong. | ||
| */ | ||
| public final class MemeListener extends MessageReceiverAdapter { | ||
Mom0aut marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| /** | ||
| * Creates a MemeListener to receive all message sent in Memes channel. | ||
| * | ||
| * @param config the config to use for this | ||
| */ | ||
| public MemeListener(@NotNull Config config) { | ||
| super(Pattern.compile(config.getMediaOnlyChannelPattern())); | ||
| } | ||
| | ||
| | ||
Mom0aut marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| @Override | ||
| public void onMessageReceived(@NotNull MessageReceivedEvent event) { | ||
| if (event.getAuthor().isBot() || event.isWebhookMessage()) { | ||
| return; | ||
| } | ||
| boolean messageHasNoMediaAttached = messageHasNoMediaAttached(event); | ||
| if (messageHasNoMediaAttached) { | ||
| deleteMessage(event).flatMap(any -> dmUser(event)).queue(); | ||
| } | ||
| } | ||
| | ||
| private static boolean messageHasNoMediaAttached(MessageReceivedEvent event) { | ||
| Message message = event.getMessage(); | ||
| return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty(); | ||
| } | ||
| | ||
| private AuditableRestAction<Void> deleteMessage(@NotNull MessageReceivedEvent event) { | ||
| return event.getMessage().delete(); | ||
| } | ||
| | ||
| private RestAction<Message> dmUser(@NotNull MessageReceivedEvent event) { | ||
| return dmUser(event.getMessage(), event.getAuthor().getIdLong(), event.getJDA()); | ||
| } | ||
| | ||
| private RestAction<Message> dmUser(Message sentMessage, long userId, @NotNull JDA jda) { | ||
| String contentDisplay = sentMessage.getContentDisplay(); | ||
| String dmMessage = | ||
| ("Hey there, your were posting a Meme without a Media attached: '%s' please attach some media (URL or other Media) to your message") | ||
Mom0aut marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| .formatted(contentDisplay); | ||
| return jda.openPrivateChannelById(userId) | ||
| .flatMap(channel -> channel.sendMessage(dmMessage)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions 89 application/src/test/java/org/togetherjava/tjbot/commands/meme/MemeListenerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package org.togetherjava.tjbot.commands.meme; | ||
| | ||
| import net.dv8tion.jda.api.JDA; | ||
| import net.dv8tion.jda.api.entities.*; | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
| import net.dv8tion.jda.api.requests.RestAction; | ||
| import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; | ||
| import net.dv8tion.jda.internal.JDAImpl; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.togetherjava.tjbot.config.Config; | ||
| | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| | ||
| class MemeListenerTest { | ||
| | ||
| private final JDA api = mock(JDA.class); | ||
| | ||
| private final JDAImpl jda = mock(JDAImpl.class); | ||
| | ||
| private final Message message = mock(Message.class); | ||
| private final long responseNumber = 1L; | ||
| private final User user = mock(User.class); | ||
| | ||
| private MemeListener memeListener; | ||
| | ||
| @BeforeEach | ||
| void setUp() { | ||
| Config config = mock(Config.class); | ||
| when(config.getMediaOnlyChannelPattern()).thenReturn("memes"); | ||
| memeListener = new MemeListener(config); | ||
| } | ||
| | ||
| @Test | ||
| void validMemePostWithAttachment() { | ||
| Message.Attachment attachment = new Message.Attachment(1L, null, null, "TEST", | ||
| "Test ContentType", null, 1, 1, 1, false, jda); | ||
| List<Message.Attachment> attachments = Collections.singletonList(attachment); | ||
| | ||
| when(message.getAuthor()).thenReturn(user); | ||
| when(message.getAttachments()).thenReturn(attachments); | ||
| when(user.isBot()).thenReturn(false); | ||
| | ||
| MessageReceivedEvent messageReceivedEvent = | ||
| new MessageReceivedEvent(api, responseNumber, message); | ||
| memeListener.onMessageReceived(messageReceivedEvent); | ||
| } | ||
| | ||
| @Test | ||
| void validMemePostWithUrlEmbedded() { | ||
| MessageEmbed messageEmbed = new MessageEmbed("https://9gag.com/gag/a61A238", "Test", "Test", | ||
| EmbedType.LINK, null, 1, null, null, null, null, null, null, null); | ||
| List<MessageEmbed> messageEmbeds = Collections.singletonList(messageEmbed); | ||
| | ||
| when(message.getAuthor()).thenReturn(user); | ||
| when(message.getEmbeds()).thenReturn(messageEmbeds); | ||
| when(user.isBot()).thenReturn(false); | ||
| | ||
| MessageReceivedEvent messageReceivedEvent = | ||
| new MessageReceivedEvent(api, responseNumber, message); | ||
| memeListener.onMessageReceived(messageReceivedEvent); | ||
| } | ||
| | ||
| @Test | ||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| void unvalidMemePostWithOnlyText() { | ||
| AuditableRestAction<Void> auditableRestAction = mock(AuditableRestAction.class); | ||
| RestAction<PrivateChannel> restActionPrivateChannel = mock(RestAction.class); | ||
| RestAction restActionMessage = mock(RestAction.class); | ||
| | ||
| when(message.getAuthor()).thenReturn(user); | ||
| when(message.getAttachments()).thenReturn(Collections.emptyList()); | ||
| when(message.delete()).thenReturn(auditableRestAction); | ||
| when(user.isBot()).thenReturn(false); | ||
| when(api.openPrivateChannelById(0)).thenReturn(restActionPrivateChannel); | ||
| when(restActionPrivateChannel.flatMap(any())).thenReturn(restActionMessage); | ||
| when(auditableRestAction.flatMap(any())).thenReturn(restActionMessage); | ||
| | ||
| MessageReceivedEvent messageReceivedEvent = | ||
| new MessageReceivedEvent(api, responseNumber, message); | ||
| memeListener.onMessageReceived(messageReceivedEvent); | ||
| } | ||
| | ||
| } |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.