Skip to content

Commit 03ea888

Browse files
billpapatchristolisbowbahdoe
authored
fix: Handle empty message content for suggestion threads (#1265)
* fix: handle empty message content for thread titles Ensures a fallback thread title is provided when the original message content is empty. The thread title will now default to the message author's name in such cases, preventing empty or uninformative thread titles. Co-authored-by: billpapat <[email protected]> Co-authored-by: Chris Sdogkos <[email protected]> Mentored-by: Chris Sdogkos <[email protected]> * style: apply gradle task `spotlessApply` CI/CD is failing without this commit. Co-authored-by: billpapat <[email protected]> Co-authored-by: Chris Sdogkos <[email protected]> Mentored-by: Chris Sdogkos <[email protected]> * fix: thread titles #1265 1) Created channels without title will default to "username's suggestions" 2) Change String threadTitle to final 3) ThreadTitle logic implemented in a helper method Co-authored-by: Ethan McCue <[email protected]> * docs(ThreadTitle): add JavaDocs and rename param Addresses code review by @christolis. Co-authorized-by: Chris Sdogkos <[email protected]> Signed-off-by: Chris Sdogkos <[email protected]> Signed-off-by: billpapat <[email protected]> * Changes as per requested by Zabuzard * Changes as per requested by Zabuzard * Changes as per requested by Taz03 --------- Signed-off-by: Chris Sdogkos <[email protected]> Signed-off-by: billpapat <[email protected]> Co-authored-by: Chris Sdogkos <[email protected]> Co-authored-by: Ethan McCue <[email protected]>
1 parent 2e1fb91 commit 03ea888

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
public final class SuggestionsUpDownVoter extends MessageReceiverAdapter {
2525
private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class);
26-
private static final int TITLE_MAX_LENGTH = 60;
2726
private static final Emoji FALLBACK_UP_VOTE = Emoji.fromUnicode("👍");
2827
private static final Emoji FALLBACK_DOWN_VOTE = Emoji.fromUnicode("👎");
28+
private static final int THREAD_TITLE_MAX_LENGTH = 60;
2929

3030
private final SuggestionsConfig config;
3131

@@ -55,19 +55,33 @@ public void onMessageReceived(MessageReceivedEvent event) {
5555
}
5656

5757
private static void createThread(Message message) {
58-
String title = message.getContentRaw();
58+
String threadTitle = generateThreadTitle(message);
59+
message.createThreadChannel(threadTitle).queue();
60+
}
5961

60-
if (title.length() >= TITLE_MAX_LENGTH) {
61-
int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH);
62+
/**
63+
* Generates a title for the given message. The maximum length of the title is
64+
* {@value #THREAD_TITLE_MAX_LENGTH}.
65+
*
66+
* @param message The message for which to generate the title.
67+
* @return The generated and truncated thread title.
68+
*/
69+
private static String generateThreadTitle(Message message) {
70+
String primaryTitle = message.getContentStripped();
71+
String fallbackTitle = message.getAuthor().getEffectiveName() + "'s suggestion";
72+
String title = primaryTitle.isEmpty() ? fallbackTitle : primaryTitle;
6273

63-
if (lastWordEnd == -1) {
64-
lastWordEnd = TITLE_MAX_LENGTH;
65-
}
74+
if (title.length() <= THREAD_TITLE_MAX_LENGTH) {
75+
return title;
76+
}
77+
78+
int lastWordEnd = title.lastIndexOf(' ', THREAD_TITLE_MAX_LENGTH);
6679

67-
title = title.substring(0, lastWordEnd);
80+
if (lastWordEnd == -1) {
81+
return title.substring(0, THREAD_TITLE_MAX_LENGTH);
6882
}
6983

70-
message.createThreadChannel(title).queue();
84+
return title.substring(0, lastWordEnd);
7185
}
7286

7387
private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild,

0 commit comments

Comments
 (0)