From fe6ddf267d907934d0e1f609da902b4e5bddc59f Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 14 Feb 2022 22:15:16 -0800 Subject: [PATCH] Added toLinkLabel extension function. --- .idea/runConfigurations.xml | 10 ---------- .../erik/mobibot/commands/links/Comment.kt | 6 +++--- .../erik/mobibot/commands/links/LinksMgr.kt | 4 ++-- .../erik/mobibot/commands/links/Posting.kt | 5 +++-- .../erik/mobibot/entries/EntriesUtils.kt | 18 +++++++++--------- .../thauvin/erik/mobibot/modules/Twitter.kt | 6 +++--- .../erik/mobibot/entries/EntriesUtilsTest.kt | 4 ++-- 7 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 .idea/runConfigurations.xml diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Comment.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Comment.kt index 59f31c8..10916de 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Comment.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Comment.kt @@ -39,7 +39,7 @@ import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment -import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLinkLabel +import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import net.thauvin.erik.mobibot.entries.EntryLink import org.pircbotx.hooks.types.GenericMessageEvent @@ -97,7 +97,7 @@ class Comment : AbstractCommand() { } override fun matches(message: String): Boolean { - return message.matches("^${Constants.LINK_CMD}[0-9]+\\.[0-9]+:.*".toRegex()) + return message.matches("^${Constants.LINK_CMD}\\d+\\.\\d+:.*".toRegex()) } private fun changeAuthor( @@ -127,7 +127,7 @@ class Comment : AbstractCommand() { ) { if (isChannelOp(channel, event) || event.user.nick == entry.getComment(commentIndex).nick) { entry.deleteComment(commentIndex) - event.sendMessage("Comment ${buildLinkLabel(entryIndex)}.${commentIndex + 1} removed.") + event.sendMessage("Comment ${entryIndex.toLinkLabel()}.${commentIndex + 1} removed.") LinksMgr.entries.save() } else { event.sendMessage("Please ask a channel op to delete this comment for you.") diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt index 8485be0..58efd89 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/LinksMgr.kt @@ -43,7 +43,7 @@ import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.Ignore.Companion.isNotIgnored import net.thauvin.erik.mobibot.entries.Entries import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink -import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLinkLabel +import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.modules.Twitter import org.jsoup.Jsoup @@ -147,7 +147,7 @@ class LinksMgr : AbstractCommand() { if (Constants.NO_TITLE == entry.title) { event.sendMessage("Please specify a title, by typing:") - event.sendMessage(helpFormat("${buildLinkLabel(index)}:|This is the title")) + event.sendMessage(helpFormat("${index.toLinkLabel()}:|This is the title")) } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Posting.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Posting.kt index 670daeb..bc91b97 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Posting.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/Posting.kt @@ -41,6 +41,7 @@ import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries import net.thauvin.erik.mobibot.entries.EntriesUtils +import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import net.thauvin.erik.mobibot.entries.EntryLink import org.pircbotx.hooks.types.GenericMessageEvent @@ -86,7 +87,7 @@ class Posting : AbstractCommand() { } override fun matches(message: String): Boolean { - return message.matches("${Constants.LINK_CMD}[0-9]+:.*".toRegex()) + return message.matches("${Constants.LINK_CMD}\\d+:.*".toRegex()) } private fun addComment(cmd: String, entryIndex: Int, event: GenericMessageEvent) { @@ -141,7 +142,7 @@ class Posting : AbstractCommand() { LinksMgr.pinboard.deletePin(entry) LinksMgr.twitter.removeEntry(index) entries.links.removeAt(index) - event.sendMessage("Entry ${EntriesUtils.buildLinkLabel(index)} removed.") + event.sendMessage("Entry ${index.toLinkLabel()} removed.") entries.save() } else { event.sendMessage("Please ask a channel op to remove this entry for you.") diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtils.kt index 023ec86..2b96dac 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtils.kt @@ -39,18 +39,12 @@ import net.thauvin.erik.mobibot.Utils.green * Entries utilities. */ object EntriesUtils { - /** - * Build link label based on its index. e.g: L1 - */ - @JvmStatic - fun buildLinkLabel(index: Int): String = Constants.LINK_CMD + (index + 1) - /** * Builds an entry's comment for display on the channel. */ @JvmStatic fun buildComment(entryIndex: Int, commentIndex: Int, comment: EntryComment): String = - ("${buildLinkLabel(entryIndex)}.${commentIndex + 1}: [${comment.nick}] ${comment.comment}") + ("${entryIndex.toLinkLabel()}.${commentIndex + 1}: [${comment.nick}] ${comment.comment}") /** * Builds an entry's link for display on the channel. @@ -58,7 +52,7 @@ object EntriesUtils { @JvmStatic @JvmOverloads fun buildLink(entryIndex: Int, entry: EntryLink, isView: Boolean = false): String { - val buff = StringBuilder().append(buildLinkLabel(entryIndex)).append(": ") + val buff = StringBuilder().append(entryIndex.toLinkLabel()).append(": ") .append('[').append(entry.nick).append(']') if (isView && entry.comments.isNotEmpty()) { buff.append("[+").append(entry.comments.size).append(']') @@ -80,5 +74,11 @@ object EntriesUtils { */ @JvmStatic fun buildTags(entryIndex: Int, entry: EntryLink): String = - buildLinkLabel(entryIndex) + "${Constants.TAG_CMD}: " + entry.pinboardTags.replace(",", ", ") + entryIndex.toLinkLabel() + "${Constants.TAG_CMD}: " + entry.pinboardTags.replace(",", ", ") + + /** + * Build link label based on its index. e.g: L1 + */ + @JvmStatic + fun Int.toLinkLabel(): String = Constants.LINK_CMD + (this + 1) } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt index 74f0ba0..3c0f212 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt @@ -37,7 +37,7 @@ import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.TwitterTimer import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.commands.links.LinksMgr -import net.thauvin.erik.mobibot.entries.EntriesUtils +import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import org.pircbotx.hooks.types.GenericMessageEvent import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -135,7 +135,7 @@ class Twitter : ThreadedModule() { launch { try { if (logger.isDebugEnabled) { - logger.debug("Posting {} to Twitter.", EntriesUtils.buildLinkLabel(index)) + logger.debug("Posting {} to Twitter.", index.toLinkLabel()) } post(message = msg, isDm = false) } catch (e: ModuleException) { @@ -151,7 +151,7 @@ class Twitter : ThreadedModule() { if (isAutoPost) { addEntry(index) if (logger.isDebugEnabled) { - logger.debug("Scheduling {} for posting on Twitter.", EntriesUtils.buildLinkLabel(index)) + logger.debug("Scheduling {} for posting on Twitter.", index.toLinkLabel()) } timer.schedule(TwitterTimer(this, index), Constants.TIMER_DELAY * 60L * 1000L) } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt index 3b39be4..9f4821d 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt @@ -38,8 +38,8 @@ import assertk.assertions.isEqualTo import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink -import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLinkLabel import net.thauvin.erik.mobibot.entries.EntriesUtils.buildTags +import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import org.testng.annotations.Test class EntriesUtilsTest { @@ -61,7 +61,7 @@ class EntriesUtilsTest { @Test fun buildLinkLabelTest() { - assertThat(buildLinkLabel(1)).isEqualTo("${Constants.LINK_CMD}2") + assertThat(1.toLinkLabel()).isEqualTo("${Constants.LINK_CMD}2") } @Test