Tags are now set in lists.
This commit is contained in:
parent
2067a228aa
commit
df38680226
3 changed files with 50 additions and 56 deletions
|
@ -45,7 +45,8 @@ import org.jsoup.Jsoup
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
||||||
private val tagsKeywords = ArrayList<String>()
|
private val keywords = ArrayList<String>()
|
||||||
|
private val tags = ArrayList<String>()
|
||||||
override val command = Constants.LINK_CMD
|
override val command = Constants.LINK_CMD
|
||||||
override val help = emptyList<String>()
|
override val help = emptyList<String>()
|
||||||
override val isOp = false
|
override val isOp = false
|
||||||
|
@ -53,19 +54,19 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
||||||
override val isVisible = false
|
override val isVisible = false
|
||||||
|
|
||||||
init {
|
init {
|
||||||
tagsKeywords.addAll(keywords.split(", +?| +"))
|
this.keywords.addAll(keywords.split(TAG_MATCH.toRegex()))
|
||||||
Companion.defaultTags = defaultTags
|
tags.addAll(defaultTags.split(TAG_MATCH.toRegex()))
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val LINK_MATCH = "^[hH][tT][tT][pP](|[sS])://.*"
|
const val LINK_MATCH = "^[hH][tT][tT][pP](|[sS])://.*"
|
||||||
|
const val TAG_MATCH = ", *| +"
|
||||||
|
|
||||||
// Entries array
|
// Entries array
|
||||||
private val entries = ArrayList<EntryLink>(0)
|
private val entries = ArrayList<EntryLink>(0)
|
||||||
|
|
||||||
// History/backlogs array
|
// History/backlogs array
|
||||||
private val history = ArrayList<String>(0)
|
private val history = ArrayList<String>(0)
|
||||||
private lateinit var defaultTags: String
|
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val entriesCount
|
val entriesCount
|
||||||
|
@ -130,19 +131,18 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
||||||
val link = cmds[0].trim()
|
val link = cmds[0].trim()
|
||||||
if (!isDupEntry(bot, sender, link, isPrivate)) {
|
if (!isDupEntry(bot, sender, link, isPrivate)) {
|
||||||
val isBackup = saveDayBackup(bot)
|
val isBackup = saveDayBackup(bot)
|
||||||
val tags: StringBuilder = StringBuilder(defaultTags)
|
|
||||||
var title = Constants.NO_TITLE
|
var title = Constants.NO_TITLE
|
||||||
if (cmds.size == 2) {
|
if (cmds.size == 2) {
|
||||||
val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2)
|
val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2)
|
||||||
title = data[0].trim()
|
title = data[0].trim()
|
||||||
if (data.size > 1) {
|
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)
|
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 index: Int = entries.size - 1
|
||||||
val entry: EntryLink = entries[index]
|
val entry: EntryLink = entries[index]
|
||||||
bot.send(EntriesUtils.buildLink(index, entry))
|
bot.send(EntriesUtils.buildLink(index, entry))
|
||||||
|
@ -210,17 +210,17 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun matchTagKeywords(title: String): String {
|
private fun matchTagKeywords(title: String): List<String> {
|
||||||
val matches = ArrayList<String>()
|
val matches = ArrayList<String>()
|
||||||
if (tagsKeywords.isNotEmpty()) {
|
if (keywords.isNotEmpty()) {
|
||||||
for (match in tagsKeywords) {
|
for (match in keywords) {
|
||||||
val m = match.trim()
|
val m = match.trim()
|
||||||
if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
|
if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
|
||||||
matches.add(m)
|
matches.add(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matches.joinToString(" ")
|
return matches
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveDayBackup(bot: Mobibot): Boolean {
|
private fun saveDayBackup(bot: Mobibot): Boolean {
|
||||||
|
|
|
@ -35,9 +35,11 @@ package net.thauvin.erik.mobibot.entries;
|
||||||
import com.rometools.rome.feed.synd.SyndCategory;
|
import com.rometools.rome.feed.synd.SyndCategory;
|
||||||
import com.rometools.rome.feed.synd.SyndCategoryImpl;
|
import com.rometools.rome.feed.synd.SyndCategoryImpl;
|
||||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||||
|
import net.thauvin.erik.mobibot.commands.links.UrlMgr;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -93,7 +95,7 @@ public class EntryLink implements Serializable {
|
||||||
final String nick,
|
final String nick,
|
||||||
final String login,
|
final String login,
|
||||||
final String channel,
|
final String channel,
|
||||||
final String tags) {
|
final List<String> tags) {
|
||||||
this.link = link;
|
this.link = link;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.nick = nick;
|
this.nick = nick;
|
||||||
|
@ -124,8 +126,7 @@ public class EntryLink implements Serializable {
|
||||||
this.nick = nick;
|
this.nick = nick;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.date = new Date(date.getTime());
|
this.date = new Date(date.getTime());
|
||||||
|
this.tags.addAll(tags);
|
||||||
setTags(tags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -345,53 +346,46 @@ public class EntryLink implements Serializable {
|
||||||
*
|
*
|
||||||
* @param tags The space-delimited tags.
|
* @param tags The space-delimited tags.
|
||||||
*/
|
*/
|
||||||
@SuppressFBWarnings("STT_STRING_PARSING_A_FIELD")
|
|
||||||
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
|
|
||||||
public final void setTags(final String tags) {
|
public final void setTags(final String tags) {
|
||||||
if (tags != null) {
|
setTags(Arrays.asList(tags.split(UrlMgr.TAG_MATCH)));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the tags.
|
* Sets the tags.
|
||||||
*
|
*
|
||||||
* @param tags The tags.
|
* @param tags The tags list.
|
||||||
*/
|
*/
|
||||||
final void setTags(final List<SyndCategory> tags) {
|
@SuppressFBWarnings("STT_STRING_PARSING_A_FIELD")
|
||||||
this.tags.addAll(tags);
|
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
|
||||||
|
public final void setTags(final List<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -49,7 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class EntryLinkTest {
|
public class EntryLinkTest {
|
||||||
private final EntryLink entryLink = new EntryLink("https://www.mobitopia.org/", "Mobitopia", "Skynx",
|
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
|
@Test
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue