From b5b8b62a16d8237fdcce54428805ee0a217d48e5 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 1 Sep 2023 13:51:23 -0400 Subject: [PATCH] Only trigger notification of events within 5minutes from the current time (avoids annoying notifications when rebroadcasting events) --- .../notifications/EventNotificationConsumer.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt index de61cadcf..9b740b2f6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/notifications/EventNotificationConsumer.kt @@ -19,6 +19,7 @@ import com.vitorpamplona.quartz.events.LnZapEvent import com.vitorpamplona.quartz.events.LnZapRequestEvent import com.vitorpamplona.quartz.events.PrivateDmEvent import com.vitorpamplona.quartz.events.SealedGossipEvent +import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.collections.immutable.persistentSetOf import java.math.BigDecimal @@ -75,7 +76,11 @@ class EventNotificationConsumer(private val applicationContext: Context) { if (acc != null && acc.userProfile().pubkeyHex == giftWrap.recipientPubKey()) { val chatEvent = unwrapAndConsume(giftWrap, account = acc) - if (chatEvent is ChatMessageEvent && acc.keyPair.privKey != null && chatEvent.pubKey != acc.userProfile().pubkeyHex) { + if (chatEvent is ChatMessageEvent && // must be messages, not any other event + acc.keyPair.privKey != null && // can't decrypt + chatEvent.createdAt > TimeUtils.fiveMinutesAgo() && // old event being re-broadcasted + chatEvent.pubKey != acc.userProfile().pubkeyHex // from the user + ) { val chatNote = LocalCache.notes[chatEvent.id] ?: return val chatRoom = chatEvent.chatroomKey(acc.keyPair.pubKey.toHexKey()) @@ -101,6 +106,9 @@ class EventNotificationConsumer(private val applicationContext: Context) { private fun notify(event: PrivateDmEvent) { val note = LocalCache.notes[event.id] ?: return + // old event being re-broadcast + if (event.createdAt < TimeUtils.fiveMinutesAgo()) return + LocalPreferences.allSavedAccounts().forEach { val acc = LocalPreferences.loadFromEncryptedStorage(it.npub) @@ -130,6 +138,9 @@ class EventNotificationConsumer(private val applicationContext: Context) { private fun notify(event: LnZapEvent) { val noteZapEvent = LocalCache.notes[event.id] ?: return + // old event being re-broadcast + if (event.createdAt < TimeUtils.fiveMinutesAgo()) return + val noteZapRequest = event.zapRequest?.id?.let { LocalCache.checkGetOrCreateNote(it) } ?: return val noteZapped = event.zappedPost().firstOrNull()?.let { LocalCache.checkGetOrCreateNote(it) }