Added more tests.
This commit is contained in:
parent
bc75d1eb73
commit
2963e747be
6 changed files with 264 additions and 35 deletions
|
@ -50,6 +50,8 @@ class Recap : AbstractCommand() {
|
|||
override val isVisible = true
|
||||
|
||||
companion object {
|
||||
const val MAX_RECAPS = 10
|
||||
|
||||
@JvmField
|
||||
val recaps = mutableListOf<String>()
|
||||
|
||||
|
@ -62,7 +64,7 @@ class Recap : AbstractCommand() {
|
|||
LocalDateTime.now(Clock.systemUTC()).toUtcDateTime()
|
||||
+ " - $sender" + (if (isAction) " " else ": ") + message
|
||||
)
|
||||
if (recaps.size > 10) {
|
||||
if (recaps.size > MAX_RECAPS) {
|
||||
recaps.removeFirst()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,8 @@ import net.thauvin.erik.mobibot.Utils.today
|
|||
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
|
||||
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink
|
||||
import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLinkLabel
|
||||
import net.thauvin.erik.mobibot.entries.EntryLink
|
||||
import net.thauvin.erik.mobibot.modules.Twitter
|
||||
import org.jsoup.Jsoup
|
||||
|
@ -124,7 +125,7 @@ class LinksMgr : AbstractCommand() {
|
|||
val entry = EntryLink(link, title, sender, login, channel, tags)
|
||||
entries.links.add(entry)
|
||||
val index = entries.links.lastIndexOf(entry)
|
||||
event.sendMessage(EntriesUtils.buildLink(index, entry))
|
||||
event.sendMessage(buildLink(index, entry))
|
||||
|
||||
pinboard.addPin(event.bot().serverHostname, entry)
|
||||
|
||||
|
@ -135,7 +136,7 @@ class LinksMgr : AbstractCommand() {
|
|||
|
||||
if (Constants.NO_TITLE == entry.title) {
|
||||
event.sendMessage("Please specify a title, by typing:")
|
||||
event.sendMessage(helpFormat("${EntriesUtils.buildLinkLabel(index)}:|This is the title"))
|
||||
event.sendMessage(helpFormat("${buildLinkLabel(index)}:|This is the title"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -147,7 +148,7 @@ class LinksMgr : AbstractCommand() {
|
|||
return message.matches(LINK_MATCH.toRegex())
|
||||
}
|
||||
|
||||
private fun fetchTitle(link: String): String {
|
||||
internal fun fetchTitle(link: String): String {
|
||||
try {
|
||||
val html = Jsoup.connect(link)
|
||||
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0")
|
||||
|
@ -164,18 +165,19 @@ class LinksMgr : AbstractCommand() {
|
|||
|
||||
private fun isDupEntry(link: String, event: GenericMessageEvent): Boolean {
|
||||
synchronized(entries) {
|
||||
for (i in entries.links.indices) {
|
||||
if (link == entries.links[i].link) {
|
||||
val entry: EntryLink = entries.links[i]
|
||||
event.sendMessage("Duplicate".bold() + " >> " + EntriesUtils.buildLink(i, entry))
|
||||
return true
|
||||
}
|
||||
return try {
|
||||
val match = entries.links.single { it.link == link }
|
||||
event.sendMessage(
|
||||
"Duplicate".bold() + " >> " + buildLink(entries.links.indexOf(match), match)
|
||||
)
|
||||
true
|
||||
} catch (ignore: NoSuchElementException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun matchTagKeywords(title: String, tags: MutableList<String>) {
|
||||
internal fun matchTagKeywords(title: String, tags: MutableList<String>) {
|
||||
for (match in keywords) {
|
||||
val m = Regex.escape(match)
|
||||
if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
|
||||
|
|
|
@ -60,56 +60,60 @@ class View : AbstractCommand() {
|
|||
|
||||
override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) {
|
||||
if (entries.links.isNotEmpty()) {
|
||||
showPosts(args, event)
|
||||
val p = parseArgs(args)
|
||||
showPosts(p.first, p.second, event)
|
||||
} else {
|
||||
event.sendMessage("There is currently nothing to view. Why don't you post something?")
|
||||
}
|
||||
}
|
||||
|
||||
private fun showPosts(args: String, event: GenericMessageEvent) {
|
||||
val max = entries.links.size
|
||||
var lcArgs = args.lowercase()
|
||||
var i = 0
|
||||
if (lcArgs.isEmpty() && max > maxEntries) {
|
||||
i = max - maxEntries
|
||||
internal fun parseArgs(args: String): Pair<Int, String> {
|
||||
var query = args.lowercase().trim()
|
||||
var start = 0
|
||||
if (query.isEmpty() && entries.links.size > maxEntries) {
|
||||
start = entries.links.size - maxEntries
|
||||
}
|
||||
if (lcArgs.matches("^\\d+(| .*)".toRegex())) {
|
||||
val split = lcArgs.split(" ", limit = 2)
|
||||
if (query.matches("^\\d+(| .*)".toRegex())) { // view [<start>] [<query>]
|
||||
val split = query.split(" ", limit = 2)
|
||||
try {
|
||||
i = split[0].toInt() - 1
|
||||
lcArgs = if (split.size == 2) {
|
||||
start = split[0].toInt() - 1
|
||||
query = if (split.size == 2) {
|
||||
split[1].trim()
|
||||
} else {
|
||||
""
|
||||
}
|
||||
if (i > max) {
|
||||
i = 0
|
||||
if (start > entries.links.size) {
|
||||
start = 0
|
||||
}
|
||||
} catch (ignore: NumberFormatException) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
return Pair(start, query)
|
||||
}
|
||||
|
||||
private fun showPosts(start: Int, query: String, event: GenericMessageEvent) {
|
||||
var index = start
|
||||
var entry: EntryLink
|
||||
var sent = 0
|
||||
while (i < max && sent < maxEntries) {
|
||||
entry = entries.links[i]
|
||||
if (lcArgs.isNotBlank()) {
|
||||
if (entry.matches(lcArgs)) {
|
||||
event.sendMessage(EntriesUtils.buildLink(i, entry, true))
|
||||
while (index < entries.links.size && sent < maxEntries) {
|
||||
entry = entries.links[index]
|
||||
if (query.isNotBlank()) {
|
||||
if (entry.matches(query)) {
|
||||
event.sendMessage(EntriesUtils.buildLink(index, entry, true))
|
||||
sent++
|
||||
}
|
||||
} else {
|
||||
event.sendMessage(EntriesUtils.buildLink(i, entry, true))
|
||||
event.sendMessage(EntriesUtils.buildLink(index, entry, true))
|
||||
sent++
|
||||
}
|
||||
i++
|
||||
if (sent == maxEntries && i < max) {
|
||||
index++
|
||||
if (sent == maxEntries && index < entries.links.size) {
|
||||
event.sendMessage("To view more, try: ")
|
||||
event.sendMessage(
|
||||
helpFormat(
|
||||
buildCmdSyntax(
|
||||
"%c $name ${i + 1} $lcArgs",
|
||||
"%c $name ${index + 1} $query",
|
||||
event.bot().nick,
|
||||
event is PrivateMessageEvent
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue