Skip to content

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

billpapat
Copy link

@billpapat billpapat commented Jun 28, 2025

fix: Media forwarding #1243

  • forwarded messages that contain media are not deleted.
  • Introduced a 1-second delay to prevent the unintended deletion of subsequent forwarded comments.
  • Improved URL pattern recognition.

@billpapat billpapat requested a review from a team as a code owner June 28, 2025 20:54
@christolis
Copy link
Member

Great job, I'll code review this in the morning.

@christolis christolis self-assigned this Jun 29, 2025
Comment on lines 34 to 35
private final ConcurrentHashMap<Long, Long> lastValidForwardedMediaMessageTime =
new ConcurrentHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use Instant instead of raw Longs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}

private MessageCreateData createNotificationMessage(Message message) {
String originalMessageContent = message.getContentRaw();
if (originalMessageContent.trim().isEmpty()) {
originalMessageContent = "(Original message had no visible text content)";
Copy link
Member

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.

Copy link
Author

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

Comment on lines 57 to 103
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;
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi-done!

- Changed TimeUnit implementation to java.time.Instant for grace period.
- Cleaned up and improved readability of the onMessageReceived method.
@billpapat billpapat force-pushed the fix/forward-to-media-channel branch from 810b1d5 to 2742573 Compare June 29, 2025 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants