Skip to content
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

refactor method for generating title of forum post #932

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Changes from all 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 @@ -39,10 +39,9 @@ public final class TransferQuestionCommand extends BotCommandAdapter
private static final String MODAL_TITLE_ID = "transferID";
private static final String MODAL_INPUT_ID = "transferQuestion";
private static final String MODAL_TAG = "tags";
private static final int TITLE_GUESS_MAX_LENGTH = 50;
private static final int TITLE_MAX_LENGTH = 50;
private static final Pattern TITLE_GUESS_COMPACT_REMOVAL_PATTERN = Pattern.compile("\\W");
private static final int TITLE_GUESS_COMPACT_LENGTH_MIN = 2;
private static final int TITLE_GUESS_COMPACT_LENGTH_MAX = 30;
private static final int TITLE_MIN_LENGTH = 3;
private static final Color EMBED_COLOR = new Color(50, 164, 168);
private final Predicate<String> isHelpForumName;
private final List<String> tags;
Expand Down Expand Up @@ -76,8 +75,8 @@ public void onMessageContext(MessageContextInteractionEvent event) {
String mostCommonTag = tags.get(0);

TextInput modalTitle = TextInput.create(MODAL_TITLE_ID, "Title", TextInputStyle.SHORT)
.setMaxLength(70)
.setMinLength(4)
.setMaxLength(TITLE_MAX_LENGTH)
.setMinLength(TITLE_MIN_LENGTH)
.setPlaceholder("Describe the question in short")
.setValue(createTitle(originalMessage))
.build();
Expand Down Expand Up @@ -123,14 +122,14 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
}

private static String createTitle(String message) {
if (message.length() >= TITLE_GUESS_MAX_LENGTH) {
int lastWordEnd = message.lastIndexOf(' ', TITLE_GUESS_MAX_LENGTH);
if (message.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = message.lastIndexOf(' ', TITLE_MAX_LENGTH);

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

message = message.substring(0, lastWordEnd);
message = message.substring(0, lastWordEnd).replace('\n', ' ');
}

return isTitleValid(message) ? message : "Untitled";
Expand All @@ -139,8 +138,8 @@ private static String createTitle(String message) {
private static boolean isTitleValid(CharSequence title) {
String titleCompact = TITLE_GUESS_COMPACT_REMOVAL_PATTERN.matcher(title).replaceAll("");

return titleCompact.length() >= TITLE_GUESS_COMPACT_LENGTH_MIN
&& titleCompact.length() <= TITLE_GUESS_COMPACT_LENGTH_MAX;
return titleCompact.length() >= TITLE_MIN_LENGTH
&& titleCompact.length() <= TITLE_MAX_LENGTH;
}

private RestAction<ForumPost> createForumPost(ModalInteractionEvent event, User originalUser) {
Expand Down
Loading