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
|
||||
|
||||
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 help = emptyList<String>()
|
||||
override val isOp = false
|
||||
|
@ -53,19 +54,19 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
|||
override val isVisible = false
|
||||
|
||||
init {
|
||||
tagsKeywords.addAll(keywords.split(", +?| +"))
|
||||
Companion.defaultTags = defaultTags
|
||||
this.keywords.addAll(keywords.split(TAG_MATCH.toRegex()))
|
||||
tags.addAll(defaultTags.split(TAG_MATCH.toRegex()))
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LINK_MATCH = "^[hH][tT][tT][pP](|[sS])://.*"
|
||||
const val TAG_MATCH = ", *| +"
|
||||
|
||||
// Entries array
|
||||
private val entries = ArrayList<EntryLink>(0)
|
||||
|
||||
// History/backlogs array
|
||||
private val history = ArrayList<String>(0)
|
||||
private lateinit var defaultTags: String
|
||||
|
||||
@JvmStatic
|
||||
val entriesCount
|
||||
|
@ -130,19 +131,18 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
|||
val link = cmds[0].trim()
|
||||
if (!isDupEntry(bot, sender, link, isPrivate)) {
|
||||
val isBackup = saveDayBackup(bot)
|
||||
val tags: StringBuilder = StringBuilder(defaultTags)
|
||||
var title = Constants.NO_TITLE
|
||||
if (cmds.size == 2) {
|
||||
val data = cmds[1].trim().split("${Tags.COMMAND}:", limit = 2)
|
||||
title = data[0].trim()
|
||||
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)
|
||||
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 entry: EntryLink = entries[index]
|
||||
bot.send(EntriesUtils.buildLink(index, entry))
|
||||
|
@ -210,17 +210,17 @@ class UrlMgr(defaultTags: String, keywords: String) : AbstractCommand() {
|
|||
return false
|
||||
}
|
||||
|
||||
private fun matchTagKeywords(title: String): String {
|
||||
private fun matchTagKeywords(title: String): List<String> {
|
||||
val matches = ArrayList<String>()
|
||||
if (tagsKeywords.isNotEmpty()) {
|
||||
for (match in tagsKeywords) {
|
||||
if (keywords.isNotEmpty()) {
|
||||
for (match in keywords) {
|
||||
val m = match.trim()
|
||||
if (title.matches("(?i).*\\b$m\\b.*".toRegex())) {
|
||||
matches.add(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches.joinToString(" ")
|
||||
return matches
|
||||
}
|
||||
|
||||
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.SyndCategoryImpl;
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import net.thauvin.erik.mobibot.commands.links.UrlMgr;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
@ -93,7 +95,7 @@ public class EntryLink implements Serializable {
|
|||
final String nick,
|
||||
final String login,
|
||||
final String channel,
|
||||
final String tags) {
|
||||
final List<String> tags) {
|
||||
this.link = link;
|
||||
this.title = title;
|
||||
this.nick = nick;
|
||||
|
@ -124,8 +126,7 @@ public class EntryLink implements Serializable {
|
|||
this.nick = nick;
|
||||
this.channel = channel;
|
||||
this.date = new Date(date.getTime());
|
||||
|
||||
setTags(tags);
|
||||
this.tags.addAll(tags);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -345,53 +346,46 @@ public class EntryLink implements Serializable {
|
|||
*
|
||||
* @param tags The space-delimited tags.
|
||||
*/
|
||||
@SuppressFBWarnings("STT_STRING_PARSING_A_FIELD")
|
||||
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
|
||||
public final void setTags(final String tags) {
|
||||
if (tags != null) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
setTags(Arrays.asList(tags.split(UrlMgr.TAG_MATCH)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the tags.
|
||||
*
|
||||
* @param tags The tags.
|
||||
* @param tags The tags list.
|
||||
*/
|
||||
final void setTags(final List<SyndCategory> tags) {
|
||||
this.tags.addAll(tags);
|
||||
@SuppressFBWarnings("STT_STRING_PARSING_A_FIELD")
|
||||
@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 {
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue