From df386802262310dcdb62d781b7996693a29cf564 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 31 Mar 2020 22:36:08 -0700 Subject: [PATCH] Tags are now set in lists. --- .../erik/mobibot/commands/links/UrlMgr.kt | 24 +++--- .../erik/mobibot/entries/EntryLink.java | 80 +++++++++---------- .../erik/mobibot/entries/EntryLinkTest.java | 2 +- 3 files changed, 50 insertions(+), 56 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt index 1daf17e..de2db08 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/links/UrlMgr.kt @@ -45,7 +45,8 @@ import org.jsoup.Jsoup import java.io.IOException class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { - private val tagsKeywords = ArrayList() + private val keywords = ArrayList() + private val tags = ArrayList() override val command = Constants.LINK_CMD override val help = emptyList() override val isOp = false @@ -53,19 +54,19 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { override val isVisible = false init { - tagsKeywords.addAll(keywords.split(", +?| +")) - Companion.defaultTags = defaultTags + this.keywords.addAll(keywords.split(TAG_MATCH.toRegex())) + tags.addAll(defaultTags.split(TAG_MATCH.toRegex())) } companion object { const val LINK_MATCH = "^[hH][tT][tT][pP](|[sS])://.*" + const val TAG_MATCH = ", *| +" // Entries array private val entries = ArrayList(0) // History/backlogs array private val history = ArrayList(0) - private lateinit var defaultTags: String @JvmStatic val entriesCount @@ -130,19 +131,18 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { val link = cmds[0].trim() if (!isDupEntry(bot, sender, link, isPrivate)) { val isBackup = saveDayBackup(bot) - val tags: StringBuilder = StringBuilder(defaultTags) var title = Constants.NO_TITLE if (cmds.size == 2) { val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2) title = data[0].trim() if (data.size > 1) { - tags.append(' ').append(data[1].trim()) + tags.addAll(data[1].split(TAG_MATCH.toRegex())) } } - tags.append(matchTagKeywords(title)) title = fetchTitle(link, title) + tags.addAll(matchTagKeywords(title)) - entries.add(EntryLink(link, title, sender, login, bot.channel, tags.toString())) + entries.add(EntryLink(link, title, sender, login, bot.channel, tags)) val index: Int = entries.size - 1 val entry: EntryLink = entries[index] bot.send(EntriesUtils.buildLink(index, entry)) @@ -210,17 +210,17 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() { return false } - private fun matchTagKeywords(title: String): String { + private fun matchTagKeywords(title: String): List { val matches = ArrayList() - if (tagsKeywords.isNotEmpty()) { - for (match in tagsKeywords) { + if (keywords.isNotEmpty()) { + for (match in keywords) { val m = match.trim() if (title.matches("(?i).*\\b$m\\b.*".toRegex())) { matches.add(m) } } } - return matches.joinToString(" ") + return matches } private fun saveDayBackup(bot: Mobibot): Boolean { diff --git a/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java b/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java index af6986a..26f098a 100644 --- a/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java +++ b/src/main/java/net/thauvin/erik/mobibot/entries/EntryLink.java @@ -35,9 +35,11 @@ package net.thauvin.erik.mobibot.entries; import com.rometools.rome.feed.synd.SyndCategory; import com.rometools.rome.feed.synd.SyndCategoryImpl; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import net.thauvin.erik.mobibot.commands.links.UrlMgr; import org.apache.commons.lang3.StringUtils; import java.io.Serializable; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -93,7 +95,7 @@ public class EntryLink implements Serializable { final String nick, final String login, final String channel, - final String tags) { + final List tags) { this.link = link; this.title = title; this.nick = nick; @@ -124,8 +126,7 @@ public class EntryLink implements Serializable { this.nick = nick; this.channel = channel; this.date = new Date(date.getTime()); - - setTags(tags); + this.tags.addAll(tags); } /** @@ -345,53 +346,46 @@ public class EntryLink implements Serializable { * * @param tags The space-delimited tags. */ - @SuppressFBWarnings("STT_STRING_PARSING_A_FIELD") - @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public final void setTags(final String tags) { - if (tags != null) { - final String[] parts = tags.replace(", ", " ").replace(',', ' ').split(" "); - - SyndCategoryImpl tag; - String part; - char mod; - - for (final String rawPart : parts) { - part = rawPart.trim(); - - if (part.length() >= 2) { - tag = new SyndCategoryImpl(); - tag.setName(StringUtils.lowerCase(part.substring(1))); - - mod = part.charAt(0); - - if (mod == '-') { - // Don't remove the channel tag, if any - if (!channel.substring(1).equals(tag.getName())) { - this.tags.remove(tag); - } - } else if (mod == '+') { - if (!this.tags.contains(tag)) { - this.tags.add(tag); - } - } else { - tag.setName(StringUtils.lowerCase(part.trim())); - - if (!this.tags.contains(tag)) { - this.tags.add(tag); - } - } - } - } - } + setTags(Arrays.asList(tags.split(UrlMgr.TAG_MATCH))); } /** * Sets the tags. * - * @param tags The tags. + * @param tags The tags list. */ - final void setTags(final List tags) { - this.tags.addAll(tags); + @SuppressFBWarnings("STT_STRING_PARSING_A_FIELD") + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + public final void setTags(final List tags) { + if (!tags.isEmpty()) { + SyndCategoryImpl category; + + for (final String tag : tags) { + if (!tag.isBlank()) { + final String t = StringUtils.lowerCase(tag); + final char mod = t.charAt(0); + if (mod == '-') { + // Don't remove the channel tag + if (!channel.substring(1).equals(t.substring(1))) { + category = new SyndCategoryImpl(); + category.setName(t.substring(1)); + this.tags.remove(category); + } + } else { + category = new SyndCategoryImpl(); + if (mod == '+') { + category.setName(t.substring(1)); + } else { + category.setName(t); + } + if (!this.tags.contains(category)) { + this.tags.add(category); + } + } + } + } + } } /** diff --git a/src/test/java/net/thauvin/erik/mobibot/entries/EntryLinkTest.java b/src/test/java/net/thauvin/erik/mobibot/entries/EntryLinkTest.java index cc4ad92..9c32598 100644 --- a/src/test/java/net/thauvin/erik/mobibot/entries/EntryLinkTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/entries/EntryLinkTest.java @@ -49,7 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class EntryLinkTest { private final EntryLink entryLink = new EntryLink("https://www.mobitopia.org/", "Mobitopia", "Skynx", - "JimH", "#mobitopia", "tag1, tag2,tag3 TAG4 tag5"); + "JimH", "#mobitopia", List.of("tag1", "tag2", "tag3", "TAG4", "Tag5")); @Test