Skip to content

Commit 737e456

Browse files
committed
Fixed some bugs revolving around bad titles
* /ask command with too long title * implicit ask with bad title (file upload or invisible spaces, ...)
1 parent 2c386dc commit 737e456

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
1717
import org.togetherjava.tjbot.config.Config;
1818

19+
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MAX;
20+
import static org.togetherjava.tjbot.commands.help.HelpSystemHelper.TITLE_COMPACT_LENGTH_MIN;
21+
1922
/**
2023
* Implements the {@code /ask} command, which is the main way of asking questions. The command can
2124
* only be used in the staging channel.
22-
*
25+
* <p>
2326
* Upon use, it will create a new thread for the question and invite all helpers interested in the
2427
* given category to it. It will also introduce the user to the system and give a quick explanation
2528
* message.
26-
*
29+
* <p>
2730
* The other way to ask questions is by {@link ImplicitAskListener}.
28-
*
31+
* <p>
2932
* Example usage:
30-
*
33+
*
3134
* <pre>
3235
* {@code
3336
* /ask title: How to send emails? category: Frameworks
@@ -76,6 +79,10 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
7679
return;
7780
}
7881

82+
if (!handleIsValidTitle(title, event)) {
83+
return;
84+
}
85+
7986
TextChannel helpStagingChannel = event.getTextChannel();
8087
helpStagingChannel.createThreadChannel("[%s] %s".formatted(category, title))
8188
.flatMap(threadChannel -> handleEvent(event, threadChannel, event.getMember(), title,
@@ -96,6 +103,20 @@ private boolean handleIsStagingChannel(@NotNull IReplyCallback event) {
96103
return false;
97104
}
98105

106+
private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyCallback event) {
107+
if (HelpSystemHelper.isTitleValid(title)) {
108+
return true;
109+
}
110+
111+
event.reply(
112+
"Sorry, but the titel length (after removal of special characters) has to be between %d and %d."
113+
.formatted(TITLE_COMPACT_LENGTH_MIN, TITLE_COMPACT_LENGTH_MAX))
114+
.setEphemeral(true)
115+
.queue();
116+
117+
return false;
118+
}
119+
99120
private @NotNull RestAction<Message> handleEvent(@NotNull IReplyCallback event,
100121
@NotNull ThreadChannel threadChannel, @NotNull Member author, @NotNull String title,
101122
@NotNull String category) {

application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public final class HelpSystemHelper {
3434
private static final Pattern EXTRACT_CATEGORY_TITLE_PATTERN =
3535
Pattern.compile("(?:\\[(?<%s>.+)] )?(?<%s>.+)".formatted(CATEGORY_GROUP, TITLE_GROUP));
3636

37+
private static final Pattern TITLE_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W");
38+
static final int TITLE_COMPACT_LENGTH_MIN = 2;
39+
static final int TITLE_COMPACT_LENGTH_MAX = 80;
40+
3741
private final Predicate<String> isOverviewChannelName;
3842
private final String overviewChannelPattern;
3943
private final Predicate<String> isStagingChannelName;
@@ -175,4 +179,11 @@ boolean isStagingChannelName(@NotNull String channelName) {
175179
String getStagingChannelPattern() {
176180
return stagingChannelPattern;
177181
}
182+
183+
static boolean isTitleValid(@NotNull CharSequence title) {
184+
String titleCompact = TITLE_COMPACT_REMOVAL_PATTERN.matcher(title).replaceAll("");
185+
186+
return titleCompact.length() >= TITLE_COMPACT_LENGTH_MIN
187+
&& titleCompact.length() <= TITLE_COMPACT_LENGTH_MAX;
188+
}
178189
}

application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
* <p>
2828
* Listens to plain messages in the staging channel, picks them up and transfers them into a proper
2929
* question thread.
30-
*
30+
* <p>
3131
* The system can handle spam appropriately and will not create multiple threads for each message.
32-
*
32+
* <p>
3333
* For example:
34-
*
34+
*
3535
* <pre>
3636
* {@code
3737
* John sends: How to send emails?
@@ -128,17 +128,22 @@ private Optional<HelpThread> getLastHelpThreadIfOnCooldown(long userId) {
128128
}
129129

130130
private static @NotNull String createTitle(@NotNull String message) {
131+
String titleCandidate;
131132
if (message.length() < TITLE_MAX_LENGTH) {
132-
return message;
133-
}
134-
// Attempt to end at the last word before hitting the limit
135-
// e.g. "[foo bar] baz" for a limit somewhere in between "baz"
136-
int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH);
137-
if (lastWordEnd == -1) {
138-
lastWordEnd = TITLE_MAX_LENGTH;
133+
titleCandidate = message;
134+
} else {
135+
// Attempt to end at the last word before hitting the limit
136+
// e.g. "[foo bar] baz" for a limit somewhere in between "baz"
137+
int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH);
138+
if (lastWordEnd == -1) {
139+
lastWordEnd = TITLE_MAX_LENGTH;
140+
}
141+
142+
titleCandidate = message.substring(0, lastWordEnd);
139143
}
140144

141-
return message.substring(0, lastWordEnd);
145+
return HelpSystemHelper.isTitleValid(titleCandidate) ? titleCandidate : "Untitled";
146+
142147
}
143148

144149
private @NotNull RestAction<?> handleEvent(@NotNull ThreadChannel threadChannel,

0 commit comments

Comments
 (0)