From c3c19ebb49a035fd8ffb5adcba305f4f96907f2c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 1 Feb 2023 13:51:03 -0300 Subject: [PATCH] Only showing notifications if it directly cites the account holder --- .../com/vitorpamplona/amethyst/model/Note.kt | 29 +++++++++++++++++++ .../service/NostrNotificationDataSource.kt | 5 +++- 2 files changed, 33 insertions(+), 1 deletion(-) 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 e70832230..e94a8e019 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -2,6 +2,8 @@ package com.vitorpamplona.amethyst.model import androidx.lifecycle.LiveData import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource +import com.vitorpamplona.amethyst.service.model.ReactionEvent +import com.vitorpamplona.amethyst.service.model.RepostEvent import com.vitorpamplona.amethyst.ui.note.toShortenHex import fr.acinq.secp256k1.Hex import java.time.Instant @@ -14,8 +16,12 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch import com.vitorpamplona.amethyst.service.relays.Relay +import java.util.regex.Matcher +import java.util.regex.Pattern import nostr.postr.events.Event +val tagSearch = Pattern.compile("(?:\\s|\\A)\\#\\[([0-9]+)\\]") + class Note(val idHex: String) { // These fields are always available. // They are immutable @@ -117,6 +123,29 @@ class Note(val idHex: String) { } } + fun directlyCiteUsers(): Set { + val matcher = tagSearch.matcher(event?.content ?: "") + val returningList = mutableSetOf() + while (matcher.find()) { + try { + val tag = event?.tags?.get(matcher.group(1).toInt()) + if (tag != null && tag[0] == "p") { + returningList.add(LocalCache.getOrCreateUser(tag[1])) + } + } 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) + } + // Observers line up here. val live: NoteLiveData = NoteLiveData(this) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrNotificationDataSource.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrNotificationDataSource.kt index b40f53ac3..ed5440d8f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrNotificationDataSource.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrNotificationDataSource.kt @@ -4,6 +4,7 @@ import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent +import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.service.model.RepostEvent import nostr.postr.JsonFilter @@ -25,7 +26,9 @@ object NostrNotificationDataSource: NostrDataSource("NotificationFeed") { } return filtered.filter { - it.event !is ChannelCreateEvent && it.event !is ChannelMetadataEvent + it.event !is ChannelCreateEvent + && it.event !is ChannelMetadataEvent + && it.directlyCites(account.userProfile()) }.sortedBy { it.event?.createdAt }.reversed() }