Skip to content
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 @@ -32,9 +32,11 @@
import java.io.InputStream;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Helper class offering certain methods used by the help system.
Expand All @@ -48,6 +50,11 @@ public final class HelpSystemHelper {

private final Predicate<String> isHelpForumName;
private final String helpForumPattern;
/**
* Compares categories by how common they are, ascending. I.e., the most uncommon or specific
* category comes first.
*/
private final Comparator<ForumTag> byCategoryCommonnessAsc;
private final Set<String> categories;
private final Set<String> threadActivityTagNames;
private final String categoryRoleSuffix;
Expand All @@ -66,9 +73,18 @@ public HelpSystemHelper(Config config, Database database) {
helpForumPattern = helpConfig.getHelpForumPattern();
isHelpForumName = Pattern.compile(helpForumPattern).asMatchPredicate();

categories = new HashSet<>(helpConfig.getCategories());
List<String> categoriesList = helpConfig.getCategories();
categories = new HashSet<>(categoriesList);
categoryRoleSuffix = helpConfig.getCategoryRoleSuffix();

Map<String, Integer> categoryToCommonDesc = IntStream.range(0, categoriesList.size())
.boxed()
.collect(Collectors.toMap(categoriesList::get, Function.identity()));
byCategoryCommonnessAsc = Comparator
.<ForumTag>comparingInt(
tag -> categoryToCommonDesc.getOrDefault(tag.getName(), categories.size()))
.reversed();

threadActivityTagNames = Arrays.stream(ThreadActivity.values())
.map(ThreadActivity::getTagName)
.collect(Collectors.toSet());
Expand Down Expand Up @@ -171,12 +187,12 @@ Optional<ForumTag> getActivityTagOfChannel(ThreadChannel channel) {
return getFirstMatchingTagOfChannel(threadActivityTagNames, channel);
}

private static Optional<ForumTag> getFirstMatchingTagOfChannel(Set<String> tagNamesToMatch,
private Optional<ForumTag> getFirstMatchingTagOfChannel(Set<String> tagNamesToMatch,
ThreadChannel channel) {
return channel.getAppliedTags()
.stream()
.filter(tag -> tagNamesToMatch.contains(tag.getName()))
.findFirst();
.min(byCategoryCommonnessAsc);
}

RestAction<Void> changeChannelCategory(ThreadChannel channel, String category) {
Expand All @@ -187,8 +203,8 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
return changeMatchingTagOfChannel(activity.getTagName(), threadActivityTagNames, channel);
}

private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
Set<String> tagNamesToMatch, ThreadChannel channel) {
private RestAction<Void> changeMatchingTagOfChannel(String tagName, Set<String> tagNamesToMatch,
ThreadChannel channel) {
List<ForumTag> tags = new ArrayList<>(channel.getAppliedTags());

Optional<ForumTag> currentTag = getFirstMatchingTagOfChannel(tagNamesToMatch, channel);
Expand Down