Optimized Links Manager.

This commit is contained in:
Erik C. Thauvin 2020-12-04 01:44:16 -08:00
parent 2d24f5fc97
commit 0e8db3440f
9 changed files with 52 additions and 56 deletions

View file

@ -11,6 +11,7 @@
<ID>LongParameterList:Comment.kt$Comment$( bot: Mobibot, sender: String, isOp: Boolean, entry: EntryLink, index: Int, commentIndex: Int )</ID> <ID>LongParameterList:Comment.kt$Comment$( bot: Mobibot, sender: String, isOp: Boolean, entry: EntryLink, index: Int, commentIndex: Int )</ID>
<ID>LongParameterList:Comment.kt$Comment$(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int)</ID> <ID>LongParameterList:Comment.kt$Comment$(bot: Mobibot, cmd: String, sender: String, entry: EntryLink, index: Int, commentIndex: Int)</ID>
<ID>LongParameterList:Twitter.kt$Twitter.Companion$( consumerKey: String?, consumerSecret: String?, token: String?, tokenSecret: String?, handle: String?, message: String, isDm: Boolean )</ID> <ID>LongParameterList:Twitter.kt$Twitter.Companion$( consumerKey: String?, consumerSecret: String?, token: String?, tokenSecret: String?, handle: String?, message: String, isDm: Boolean )</ID>
<ID>MagicNumber:AddLog.kt$AddLog$4</ID>
<ID>MagicNumber:Comment.kt$Comment$3</ID> <ID>MagicNumber:Comment.kt$Comment$3</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$11</ID> <ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$11</ID>
<ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$3</ID> <ID>MagicNumber:CurrencyConverter.kt$CurrencyConverter$3</ID>

View file

@ -33,8 +33,8 @@
package net.thauvin.erik.mobibot.commands package net.thauvin.erik.mobibot.commands
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.addHistory
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.getHistory import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.history
import net.thauvin.erik.mobibot.entries.EntriesMgr import net.thauvin.erik.mobibot.entries.EntriesMgr
import java.io.File import java.io.File
@ -52,15 +52,18 @@ class AddLog(bot: Mobibot) : AbstractCommand(bot) {
isOp: Boolean, isOp: Boolean,
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (isOp && args.isNotBlank()) { if (isOp) {
if (args.isNotBlank()) {
// e.g: 2014-04-01 // e.g: 2014-04-01
val backlog = File("${bot.logsDir}$args${EntriesMgr.XML_EXT}") val backlog = File("${bot.logsDir}$args${EntriesMgr.XML_EXT}")
if (backlog.exists()) { if (backlog.exists()) {
addHistory(0, args) history.add(0, args)
bot.send(sender, getHistory().toString(), isPrivate)
} else { } else {
bot.send(sender, "The specified log could not be found.", isPrivate) bot.send(sender, "The specified log could not be found.", isPrivate)
} return
}
}
bot.sendList(sender, history, 4, isPrivate, false)
} }
} }
} }

View file

@ -93,7 +93,7 @@ public class Info extends AbstractCommand {
info.append(Utils.uptime(ManagementFactory.getRuntimeMXBean().getUptime())) info.append(Utils.uptime(ManagementFactory.getRuntimeMXBean().getUptime()))
.append(" [Entries: ") .append(" [Entries: ")
.append(LinksMgr.getEntriesCount()); .append(LinksMgr.entries.size());
if (isOp) { if (isOp) {
if (getBot().getTell().isEnabled()) { if (getBot().getTell().isEnabled()) {

View file

@ -68,8 +68,8 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
val cmds = args.substring(1).split("[.:]".toRegex(), 3) val cmds = args.substring(1).split("[.:]".toRegex(), 3)
val index = cmds[0].toInt() - 1 val index = cmds[0].toInt() - 1
if (index < LinksMgr.entriesCount) { if (index < LinksMgr.entries.size) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = LinksMgr.entries[index]
val commentIndex = cmds[1].toInt() - 1 val commentIndex = cmds[1].toInt() - 1
if (commentIndex < entry.comments.size) { if (commentIndex < entry.comments.size) {
when (val cmd = cmds[2].trim()) { when (val cmd = cmds[2].trim()) {

View file

@ -40,7 +40,6 @@ import net.thauvin.erik.mobibot.commands.Ignore
import net.thauvin.erik.mobibot.entries.EntriesMgr import net.thauvin.erik.mobibot.entries.EntriesMgr
import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.entries.EntryLink
import org.apache.logging.log4j.LogManager
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.IOException import java.io.IOException
@ -65,23 +64,22 @@ class LinksMgr(bot: Mobibot) : AbstractCommand(bot) {
const val TAG_MATCH = ", *| +" const val TAG_MATCH = ", *| +"
// Entries array // Entries array
private val entries = ArrayList<EntryLink>(0) @JvmField
val entries = ArrayList<EntryLink>(0)
// History/backlogs array // History/backlogs array
private val history = ArrayList<String>(0) @JvmField
val history = ArrayList<String>(0)
@JvmStatic
val entriesCount
get() = entries.size
@JvmStatic @JvmStatic
var startDate: String = Utils.today() var startDate: String = Utils.today()
private set private set
@JvmStatic // @JvmStatic
fun addHistory(index: Int, entry: String) { // fun addHistory(index: Int, entry: String) {
history.add(index, entry) // history.add(index, entry)
} // }
/** /**
* Saves the entries. * Saves the entries.
@ -93,20 +91,15 @@ class LinksMgr(bot: Mobibot) : AbstractCommand(bot) {
EntriesMgr.saveEntries(bot, entries, history, isDayBackup) EntriesMgr.saveEntries(bot, entries, history, isDayBackup)
} }
@JvmStatic // @JvmStatic
fun removeEntry(index: Int) { // fun removeEntry(index: Int) {
entries.removeAt(index) // entries.removeAt(index)
} // }
//
@JvmStatic // @JvmStatic
fun getEntry(index: Int): EntryLink { // fun getEntry(index: Int): EntryLink {
return entries[index] // return entries[index]
} // }
@JvmStatic
fun getHistory(): List<String> {
return history
}
@JvmStatic @JvmStatic
fun startup(current: String, backlogs: String, channel: String) { fun startup(current: String, backlogs: String, channel: String) {

View file

@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
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.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.entries.EntryLink
@ -67,7 +68,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
val cmds = args.substring(1).split(":", limit = 2) val cmds = args.substring(1).split(":", limit = 2)
val index = cmds[0].toInt() - 1 val index = cmds[0].toInt() - 1
if (index < LinksMgr.entriesCount) { if (index < entries.size) {
when (val cmd = cmds[1].trim()) { when (val cmd = cmds[1].trim()) {
"" -> showEntry(index) "" -> showEntry(index)
"-" -> removeEntry(sender, login, isOp, index) // L1:- "-" -> removeEntry(sender, login, isOp, index) // L1:-
@ -88,7 +89,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
} }
private fun addComment(cmd: String, sender: String, index: Int) { private fun addComment(cmd: String, sender: String, index: Int) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = entries[index]
val commentIndex = entry.addComment(cmd, sender) val commentIndex = entry.addComment(cmd, sender)
val comment = entry.getComment(commentIndex) val comment = entry.getComment(commentIndex)
bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false) bot.send(sender, EntriesUtils.buildComment(index, commentIndex, comment), false)
@ -97,7 +98,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
private fun changeTitle(cmd: String, index: Int) { private fun changeTitle(cmd: String, index: Int) {
if (cmd.length > 1) { if (cmd.length > 1) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = entries[index]
entry.title = cmd.substring(1).trim() entry.title = cmd.substring(1).trim()
bot.updatePin(entry.link, entry) bot.updatePin(entry.link, entry)
bot.send(EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
@ -106,7 +107,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
} }
private fun changeUrl(cmd: String, login: String, isOp: Boolean, index: Int) { private fun changeUrl(cmd: String, login: String, isOp: Boolean, index: Int) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = entries[index]
if (entry.login == login || isOp) { if (entry.login == login || isOp) {
val link = cmd.substring(1) val link = cmd.substring(1)
if (link.matches(LinksMgr.LINK_MATCH.toRegex())) { if (link.matches(LinksMgr.LINK_MATCH.toRegex())) {
@ -122,7 +123,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
private fun changeAuthor(cmd: String, sender: String, isOp: Boolean, index: Int) { private fun changeAuthor(cmd: String, sender: String, isOp: Boolean, index: Int) {
if (isOp) { if (isOp) {
if (cmd.length > 1) { if (cmd.length > 1) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = entries[index]
entry.nick = cmd.substring(1) entry.nick = cmd.substring(1)
bot.send(EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
LinksMgr.saveEntries(bot, false) LinksMgr.saveEntries(bot, false)
@ -133,10 +134,10 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
} }
private fun removeEntry(sender: String, login: String, isOp: Boolean, index: Int) { private fun removeEntry(sender: String, login: String, isOp: Boolean, index: Int) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = LinksMgr.entries[index]
if (entry.login == login || isOp) { if (entry.login == login || isOp) {
bot.deletePin(index, entry) bot.deletePin(index, entry)
LinksMgr.removeEntry(index) entries.removeAt(index)
bot.send("Entry ${EntriesUtils.buildLinkCmd(index)} removed.") bot.send("Entry ${EntriesUtils.buildLinkCmd(index)} removed.")
LinksMgr.saveEntries(bot, false) LinksMgr.saveEntries(bot, false)
} else { } else {
@ -145,7 +146,7 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
} }
private fun showEntry(index: Int) { private fun showEntry(index: Int) {
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = entries[index]
bot.send(EntriesUtils.buildLink(index, entry)) bot.send(EntriesUtils.buildLink(index, entry))
if (entry.hasTags()) { if (entry.hasTags()) {
bot.send(EntriesUtils.buildTags(index, entry)) bot.send(EntriesUtils.buildTags(index, entry))

View file

@ -63,9 +63,9 @@ class Tags(bot: Mobibot) : AbstractCommand(bot) {
val cmds = args.substring(1).split("T:", limit = 2) val cmds = args.substring(1).split("T:", limit = 2)
val index = cmds[0].toInt() - 1 val index = cmds[0].toInt() - 1
if (index < LinksMgr.entriesCount) { if (index < LinksMgr.entries.size) {
val cmd = cmds[1].trim() val cmd = cmds[1].trim()
val entry: EntryLink = LinksMgr.getEntry(index) val entry: EntryLink = LinksMgr.entries[index]
if (cmd.isNotEmpty()) { if (cmd.isNotEmpty()) {
if (entry.login == login || isOp) { if (entry.login == login || isOp) {
entry.setTags(cmd) entry.setTags(cmd)

View file

@ -35,8 +35,7 @@ package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entriesCount import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.getEntry
import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.entries.EntryLink
@ -62,7 +61,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
isOp: Boolean, isOp: Boolean,
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (entriesCount != 0) { if (entries.size != 0) {
showPosts(bot, args, sender) showPosts(bot, args, sender)
} else { } else {
bot.send(sender, "There is currently nothing to view. Why don't you post something?", isPrivate) bot.send(sender, "There is currently nothing to view. Why don't you post something?", isPrivate)
@ -70,7 +69,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
} }
private fun showPosts(bot: Mobibot, args: String, sender: String) { private fun showPosts(bot: Mobibot, args: String, sender: String) {
val max = entriesCount val max = entries.size
var lcArgs = args.toLowerCase() var lcArgs = args.toLowerCase()
var i = 0 var i = 0
if (lcArgs.isEmpty() && max > maxEntries) { if (lcArgs.isEmpty() && max > maxEntries) {
@ -96,7 +95,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
var entry: EntryLink var entry: EntryLink
var sent = 0 var sent = 0
while (i < max && sent < maxEntries) { while (i < max && sent < maxEntries) {
entry = getEntry(i) entry = entries[i]
if (lcArgs.isNotBlank()) { if (lcArgs.isNotBlank()) {
if (entry.matches(lcArgs)) { if (entry.matches(lcArgs)) {
bot.send(sender, EntriesUtils.buildLink(i, entry, true), false) bot.send(sender, EntriesUtils.buildLink(i, entry, true), false)

View file

@ -35,8 +35,7 @@ import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.TwitterTimer import net.thauvin.erik.mobibot.TwitterTimer
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entriesCount import net.thauvin.erik.mobibot.commands.links.LinksMgr
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.getEntry
import net.thauvin.erik.mobibot.entries.EntriesUtils import net.thauvin.erik.mobibot.entries.EntriesUtils
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.NoticeMessage import net.thauvin.erik.mobibot.msg.NoticeMessage
@ -125,8 +124,8 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
*/ */
fun postEntry(index: Int) { fun postEntry(index: Int) {
with(bot) { with(bot) {
if (isAutoPost && hasEntry(index) && entriesCount >= index) { if (isAutoPost && hasEntry(index) && LinksMgr.entries.size >= index) {
val entry = getEntry(index) val entry = LinksMgr.entries[index]
val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel" val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel"
Thread { Thread {
try { try {