Skip to content

fix: Handle empty message content for suggestion threads #1265

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 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
public final class SuggestionsUpDownVoter extends MessageReceiverAdapter {
private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class);
private static final int TITLE_MAX_LENGTH = 60;
private static final Emoji FALLBACK_UP_VOTE = Emoji.fromUnicode("👍");
private static final Emoji FALLBACK_DOWN_VOTE = Emoji.fromUnicode("👎");

Expand Down Expand Up @@ -55,19 +54,10 @@ public void onMessageReceived(MessageReceivedEvent event) {
}

private static void createThread(Message message) {
String title = message.getContentRaw();
ThreadTitle threadTitle = ThreadTitle.withFallback(message.getContentRaw(),
message.getAuthor().getName() + "'s suggestions");

if (title.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH);

if (lastWordEnd == -1) {
lastWordEnd = TITLE_MAX_LENGTH;
}

title = title.substring(0, lastWordEnd);
}

message.createThreadChannel(title).queue();
message.createThreadChannel(threadTitle.value()).queue();
}

private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.togetherjava.tjbot.features.basic;

/**
* Represents a thread title, enforcing a maximum length of 60 characters. If an initial title
* exceeds this limit, it's truncated at the last word boundary before or at the 60-character mark
* to prevent cutting words mid-sentence. If no space is found, it truncates at 60 characters.
* Provides a static factory method `withFallback` to create a ThreadTitle, using a fallback title
* if the primary title is empty.
*/
public record ThreadTitle(String value) {

private static final int TITLE_MAX_LENGTH = 60;

public ThreadTitle(String value) {
String threadTitle;
if (value.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = value.lastIndexOf(' ', TITLE_MAX_LENGTH);

if (lastWordEnd == -1) {
lastWordEnd = TITLE_MAX_LENGTH;
}

threadTitle = value.substring(0, lastWordEnd);
} else {
threadTitle = value;
}

this.value = threadTitle;
}

public static ThreadTitle withFallback(String primary, String fallback) {
if (!primary.isEmpty()) {
return new ThreadTitle(primary);
} else {
return new ThreadTitle(fallback);
}
}

@Override
public String toString() {
return value;
}

}
Loading