Added toLinkLabel extension function.

This commit is contained in:
Erik C. Thauvin 2022-02-14 22:15:16 -08:00
parent 0cf98c6ca4
commit fe6ddf267d
7 changed files with 22 additions and 31 deletions

View file

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View file

@ -39,7 +39,7 @@ import net.thauvin.erik.mobibot.Utils.isChannelOp
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment 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 net.thauvin.erik.mobibot.entries.EntryLink
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
@ -97,7 +97,7 @@ class Comment : AbstractCommand() {
} }
override fun matches(message: String): Boolean { 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( private fun changeAuthor(
@ -127,7 +127,7 @@ class Comment : AbstractCommand() {
) { ) {
if (isChannelOp(channel, event) || event.user.nick == entry.getComment(commentIndex).nick) { if (isChannelOp(channel, event) || event.user.nick == entry.getComment(commentIndex).nick) {
entry.deleteComment(commentIndex) entry.deleteComment(commentIndex)
event.sendMessage("Comment ${buildLinkLabel(entryIndex)}.${commentIndex + 1} removed.") event.sendMessage("Comment ${entryIndex.toLinkLabel()}.${commentIndex + 1} removed.")
LinksMgr.entries.save() LinksMgr.entries.save()
} else { } else {
event.sendMessage("Please ask a channel op to delete this comment for you.") event.sendMessage("Please ask a channel op to delete this comment for you.")

View file

@ -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.commands.Ignore.Companion.isNotIgnored
import net.thauvin.erik.mobibot.entries.Entries import net.thauvin.erik.mobibot.entries.Entries
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink 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.entries.EntryLink
import net.thauvin.erik.mobibot.modules.Twitter import net.thauvin.erik.mobibot.modules.Twitter
import org.jsoup.Jsoup import org.jsoup.Jsoup
@ -147,7 +147,7 @@ class LinksMgr : AbstractCommand() {
if (Constants.NO_TITLE == entry.title) { if (Constants.NO_TITLE == entry.title) {
event.sendMessage("Please specify a title, by typing:") 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"))
} }
} }
} }

View file

@ -41,6 +41,7 @@ import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel
import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.entries.EntryLink
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
@ -86,7 +87,7 @@ class Posting : AbstractCommand() {
} }
override fun matches(message: String): Boolean { 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) { private fun addComment(cmd: String, entryIndex: Int, event: GenericMessageEvent) {
@ -141,7 +142,7 @@ class Posting : AbstractCommand() {
LinksMgr.pinboard.deletePin(entry) LinksMgr.pinboard.deletePin(entry)
LinksMgr.twitter.removeEntry(index) LinksMgr.twitter.removeEntry(index)
entries.links.removeAt(index) entries.links.removeAt(index)
event.sendMessage("Entry ${EntriesUtils.buildLinkLabel(index)} removed.") event.sendMessage("Entry ${index.toLinkLabel()} removed.")
entries.save() entries.save()
} else { } else {
event.sendMessage("Please ask a channel op to remove this entry for you.") event.sendMessage("Please ask a channel op to remove this entry for you.")

View file

@ -39,18 +39,12 @@ import net.thauvin.erik.mobibot.Utils.green
* Entries utilities. * Entries utilities.
*/ */
object EntriesUtils { 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. * Builds an entry's comment for display on the channel.
*/ */
@JvmStatic @JvmStatic
fun buildComment(entryIndex: Int, commentIndex: Int, comment: EntryComment): String = 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. * Builds an entry's link for display on the channel.
@ -58,7 +52,7 @@ object EntriesUtils {
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun buildLink(entryIndex: Int, entry: EntryLink, isView: Boolean = false): String { 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(']') .append('[').append(entry.nick).append(']')
if (isView && entry.comments.isNotEmpty()) { if (isView && entry.comments.isNotEmpty()) {
buff.append("[+").append(entry.comments.size).append(']') buff.append("[+").append(entry.comments.size).append(']')
@ -80,5 +74,11 @@ object EntriesUtils {
*/ */
@JvmStatic @JvmStatic
fun buildTags(entryIndex: Int, entry: EntryLink): String = 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)
} }

View file

@ -37,7 +37,7 @@ import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.TwitterTimer import net.thauvin.erik.mobibot.TwitterTimer
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.commands.links.LinksMgr 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.pircbotx.hooks.types.GenericMessageEvent
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -135,7 +135,7 @@ class Twitter : ThreadedModule() {
launch { launch {
try { try {
if (logger.isDebugEnabled) { if (logger.isDebugEnabled) {
logger.debug("Posting {} to Twitter.", EntriesUtils.buildLinkLabel(index)) logger.debug("Posting {} to Twitter.", index.toLinkLabel())
} }
post(message = msg, isDm = false) post(message = msg, isDm = false)
} catch (e: ModuleException) { } catch (e: ModuleException) {
@ -151,7 +151,7 @@ class Twitter : ThreadedModule() {
if (isAutoPost) { if (isAutoPost) {
addEntry(index) addEntry(index)
if (logger.isDebugEnabled) { 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) timer.schedule(TwitterTimer(this, index), Constants.TIMER_DELAY * 60L * 1000L)
} }

View file

@ -38,8 +38,8 @@ import assertk.assertions.isEqualTo
import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink 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.buildTags
import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel
import org.testng.annotations.Test import org.testng.annotations.Test
class EntriesUtilsTest { class EntriesUtilsTest {
@ -61,7 +61,7 @@ class EntriesUtilsTest {
@Test @Test
fun buildLinkLabelTest() { fun buildLinkLabelTest() {
assertThat(buildLinkLabel(1)).isEqualTo("${Constants.LINK_CMD}2") assertThat(1.toLinkLabel()).isEqualTo("${Constants.LINK_CMD}2")
} }
@Test @Test