From 2424dc3961ce0657762a37ba3bbf19248fd8c755 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 7 Apr 2023 14:24:43 -0400 Subject: [PATCH] Brings the findCitedUsers function to BaseTextNote Event with cache. --- .../com/vitorpamplona/amethyst/model/Note.kt | 41 +------------------ .../service/model/BaseTextNoteEvent.kt | 24 ++++++++++- 2 files changed, 23 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index d8f633116..063a40e90 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -57,7 +57,7 @@ open class Note(val idHex: String) { val channelHex = (event as? ChannelMessageEvent)?.channel() ?: (event as? ChannelMetadataEvent)?.channel() - ?: (event as? ChannelCreateEvent)?.let { it.id } + ?: (event as? ChannelCreateEvent)?.id return channelHex?.let { LocalCache.checkGetOrCreateChannel(it) } } @@ -270,45 +270,6 @@ open class Note(val idHex: String) { ) } - fun directlyCiteUsersHex(): Set { - val matcher = tagSearch.matcher(event?.content() ?: "") - val returningList = mutableSetOf() - while (matcher.find()) { - try { - val tag = matcher.group(1)?.let { event?.tags()?.get(it.toInt()) } - if (tag != null && tag[0] == "p") { - returningList.add(tag[1]) - } - } catch (e: Exception) { - } - } - return returningList - } - - fun directlyCiteUsers(): Set { - val matcher = tagSearch.matcher(event?.content() ?: "") - val returningList = mutableSetOf() - while (matcher.find()) { - try { - val tag = matcher.group(1)?.let { event?.tags()?.get(it.toInt()) } - if (tag != null && tag[0] == "p") { - LocalCache.checkGetOrCreateUser(tag[1])?.let { - returningList.add(it) - } - } - } catch (e: Exception) { - } - } - return returningList - } - - fun directlyCites(userProfile: User): Boolean { - return author == userProfile || - (userProfile in directlyCiteUsers()) || - (event is ReactionEvent && replyTo?.lastOrNull()?.directlyCites(userProfile) == true) || - (event is RepostEvent && replyTo?.lastOrNull()?.directlyCites(userProfile) == true) - } - fun isNewThread(): Boolean { return event is RepostEvent || replyTo == null || replyTo?.size == 0 } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/model/BaseTextNoteEvent.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/model/BaseTextNoteEvent.kt index c401ff260..031c2b1cf 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/model/BaseTextNoteEvent.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/model/BaseTextNoteEvent.kt @@ -15,6 +15,26 @@ open class BaseTextNoteEvent( fun mentions() = taggedUsers() open fun replyTos() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) } + private var citedUsersCache: Set? = null + + fun citedUsers(): Set { + citedUsersCache?.let { return it } + + val matcher = tagSearch.matcher(content) + val returningList = mutableSetOf() + while (matcher.find()) { + try { + val tag = matcher.group(1)?.let { tags[it.toInt()] } + if (tag != null && tag.size > 1 && tag[0] == "p") { + returningList.add(tag[1]) + } + } catch (e: Exception) { + } + } + citedUsersCache = returningList + return returningList + } + fun findCitations(): Set { var citations = mutableSetOf() // Removes citations from replies: @@ -22,10 +42,10 @@ open class BaseTextNoteEvent( while (matcher.find()) { try { val tag = matcher.group(1)?.let { tags[it.toInt()] } - if (tag != null && tag[0] == "e") { + if (tag != null && tag.size > 1 && tag[0] == "e") { citations.add(tag[1]) } - if (tag != null && tag[0] == "a") { + if (tag != null && tag.size > 1 && tag[0] == "a") { citations.add(tag[1]) } } catch (e: Exception) {