-
-
Notifications
You must be signed in to change notification settings - Fork 91
Allow forwarding of media, to media channels #1266
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
base: develop
Are you sure you want to change the base?
Allow forwarding of media, to media channels #1266
Conversation
Great job, I'll code review this in the morning. |
...cation/src/main/java/org/togetherjava/tjbot/features/mediaonly/MediaOnlyChannelListener.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
private MessageCreateData createNotificationMessage(Message message) { | ||
String originalMessageContent = message.getContentRaw(); | ||
if (originalMessageContent.trim().isEmpty()) { | ||
originalMessageContent = "(Original message had no visible text content)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that text is written with a developer as target audience, not the user. id rewrite it as See image:
or similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by that. It checks for .isEmpty() meaning I cannot return "See image:" Since there is not one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what i mean is that if this is a user facing text, its not written in a user friendly way.
long userId = event.getAuthor().getIdLong(); | ||
|
||
boolean isForwardedWithMedia = | ||
!message.getMessageSnapshots().isEmpty() && !messageHasNoMediaAttached(message); | ||
|
||
if (isForwardedWithMedia) { | ||
lastValidForwardedMediaMessageTime.put(userId, System.currentTimeMillis()); | ||
return; | ||
} | ||
|
||
boolean isNormalMediaUpload = | ||
message.getMessageSnapshots().isEmpty() && !messageHasNoMediaAttached(message); | ||
if (isNormalMediaUpload) { | ||
return; | ||
} | ||
|
||
Long lastForwardedMediaTime = lastValidForwardedMediaMessageTime.get(userId); | ||
long gracePeriodMillis = TimeUnit.SECONDS.toMillis(1); | ||
|
||
if (lastForwardedMediaTime != null | ||
&& (System.currentTimeMillis() - lastForwardedMediaTime) <= gracePeriodMillis) { | ||
lastValidForwardedMediaMessageTime.remove(userId); | ||
return; | ||
} | ||
|
||
message.delete().queue(deleteSuccess -> dmUser(message).queue(dmSuccess -> { | ||
}, dmFailure -> tempNotifyUserInChannel(message)), | ||
deleteFailure -> tempNotifyUserInChannel(message)); | ||
} | ||
|
||
private boolean messageHasNoMediaAttached(Message message) { | ||
return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty() | ||
&& !message.getContentRaw().contains("http"); | ||
if (!message.getAttachments().isEmpty() || !message.getEmbeds().isEmpty() | ||
|| MEDIA_URL_PATTERN.matcher(message.getContentRaw()).matches()) { | ||
return false; | ||
} | ||
|
||
if (!message.getMessageSnapshots().isEmpty()) { | ||
for (MessageSnapshot snapshot : message.getMessageSnapshots()) { | ||
if (!snapshot.getAttachments().isEmpty() || !snapshot.getEmbeds().isEmpty() | ||
|| MEDIA_URL_PATTERN.matcher(snapshot.getContentRaw()).matches()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im not happy with how this logic is currently structured.
the main structure should be:
if (!is there any media?) {
delete it
} else {
forward it
}
the details of detecting media should be in a helper method. and the details of that cache handling as well.
i also dont quite get the point of the cache thing. what is it used for? and how does the UX look like if they run into it?
for the cache topic u should use high level classes like Duration
and Instant
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semi-done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bot previously failed to recognize forwarded media such as images, voice messages, and GIFs, processing them as plain text and subsequently deleting them. The updated version's handling of such media is illustrated in the accompanying images.
If the forwarded message contains no media (i.e., it's plain text), it will be automatically deleted, and the user will be notified as usual.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay. and what is the role of the grace period thing now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imagine a user tries to forward a media file (like an image or video) to a "media-only" Discord channel. Discord might process upcoming (potential) additional comment added by the sender as message without content (text).
Without a grace period: If the bot immediately checks after the forwarded message, the added comment would be incorrectly deleted even though the user did forward an image or video to post media.
Discord's forwarding system allows for the addition of extra comment (text) that is being sent separately than the embed, meaning it will be deleted by the bot for no-media (we do not want forwarded media without context). That is what the grace period is for, waits 1 second after forwarded media (that have passed the messageHasNoMediaAttached) to allow for the comment to not be deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im having a super hard time following ur explanation. it sounds like ur saying there are multiple messages involved. when a user sends an image its a single message isnt it.
my advice is to please work more with screenshots or gifs/videos. make it visual and it will be easy to understand 👍
- Changed TimeUnit implementation to java.time.Instant for grace period. - Cleaned up and improved readability of the onMessageReceived method.
810b1d5
to
2742573
Compare
Conditionally add the embed only if the original message content is NOT empty
+ " Hey there, you posted a message without media (image, video, link) in a media-only channel. Please see the description of the channel for details and then repost with media attached, thanks 😀"); | ||
|
||
// Conditionally add the embed only if the original message content is NOT empty | ||
if (!originalMessageContent.trim().isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.isBlank is probably what you want here
better way to implement that behavior, and motivation.
fix: Media forwarding #1243