Skip to content

Commit c176c32

Browse files
committed
fixed issues with multiple tags and tag limit
1 parent 0b79d4a commit c176c32

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
190190

191191
private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
192192
Set<String> tagNamesToMatch, ThreadChannel channel) {
193-
Collection<ForumTag> tags = new ArrayList<>(channel.getAppliedTags());
193+
List<ForumTag> tags = new ArrayList<>(channel.getAppliedTags());
194194

195195
Optional<ForumTag> currentTag = getFirstMatchingTagOfChannel(tagNamesToMatch, channel);
196196
if (currentTag.isPresent()) {
@@ -203,10 +203,24 @@ private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
203203
}
204204

205205
ForumTag nextTag = requireTag(tagName, channel.getParentChannel().asForumChannel());
206-
tags.add(nextTag);
206+
// In case the tag was already there, but not in front, we first remove it
207+
tags.remove(nextTag);
208+
209+
if (tags.size() >= ForumChannel.MAX_POST_TAGS) {
210+
// If still at max size, remove last to make place for the new tag.
211+
// The last tag is the least important.
212+
// NOTE In practice, this can happen if the user selected 5 categories and
213+
// the bot then tries to add the activity tag
214+
tags.remove(tags.size() - 1);
215+
}
216+
217+
Collection<ForumTag> nextTags = new ArrayList<>(tags.size());
218+
// Tag should be in front, to take priority over others
219+
nextTags.add(nextTag);
220+
nextTags.addAll(tags);
207221

208222
List<ForumTagSnowflake> tagSnowflakes =
209-
tags.stream().map(ForumTag::getIdLong).map(ForumTagSnowflake::fromId).toList();
223+
nextTags.stream().map(ForumTag::getIdLong).map(ForumTagSnowflake::fromId).toList();
210224
return channel.getManager().setAppliedTags(tagSnowflakes);
211225
}
212226

0 commit comments

Comments
 (0)