-
-
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. |
private final ConcurrentHashMap<Long, Long> lastValidForwardedMediaMessageTime = | ||
new ConcurrentHashMap<>(); |
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.
please use Instant
instead of raw Long
s
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.
Done!
} | ||
|
||
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
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!
- Changed TimeUnit implementation to java.time.Instant for grace period. - Cleaned up and improved readability of the onMessageReceived method.
810b1d5
to
2742573
Compare
fix: Media forwarding #1243