Skip to content

Commit 08da794

Browse files
billpapatbowbahdoe
andcommitted
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 <5004262+bowbahdoe@users.noreply.github.com>
1 parent 629ac8c commit 08da794

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
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("👎");
2928

@@ -55,26 +54,10 @@ public void onMessageReceived(MessageReceivedEvent event) {
5554
}
5655

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

59-
String threadTitle;
60-
String messageContent = message.getContentRaw();
61-
62-
if (messageContent.isEmpty()) {
63-
threadTitle = message.getAuthor().getName();
64-
} else if (messageContent.length() >= TITLE_MAX_LENGTH) {
65-
int lastWordEnd = messageContent.lastIndexOf(' ', TITLE_MAX_LENGTH);
66-
67-
if (lastWordEnd == -1) {
68-
lastWordEnd = TITLE_MAX_LENGTH;
69-
}
70-
71-
threadTitle = messageContent.substring(0, lastWordEnd);
72-
} else {
73-
74-
threadTitle = messageContent;
75-
}
76-
77-
message.createThreadChannel(threadTitle).queue();
60+
message.createThreadChannel(threadTitle.value()).queue();
7861
}
7962

8063
private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
package org.togetherjava.tjbot.features.basic;
3+
4+
public record ThreadTitle(String value) {
5+
6+
private static final int TITLE_MAX_LENGTH = 60;
7+
8+
public ThreadTitle(String value) {
9+
String threadTitle;
10+
if (value.length() >= TITLE_MAX_LENGTH) {
11+
int lastWordEnd = value.lastIndexOf(' ', TITLE_MAX_LENGTH);
12+
13+
if (lastWordEnd == -1) {
14+
lastWordEnd = TITLE_MAX_LENGTH;
15+
}
16+
17+
threadTitle = value.substring(0, lastWordEnd);
18+
} else {
19+
threadTitle = value;
20+
}
21+
22+
this.value = threadTitle;
23+
}
24+
25+
public static ThreadTitle withFallback(String threadtitle, String fallback) {
26+
if (!threadtitle.isEmpty()) {
27+
return new ThreadTitle(threadtitle);
28+
} else {
29+
return new ThreadTitle(fallback);
30+
}
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return value;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)