From c8fb1a2a0321a4ba24e9e6ee9235292104c7f971 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Fri, 28 Nov 2025 07:22:25 -0300 Subject: [PATCH] Fix fetching of profile metadata - Only request profile metadata if it hasn't been updated in the last 24 hours and hasn't been checked in the last 15 minutes. - Add getters for `LastCheck` and `LastMetadataUpdate` in `LocalPreferences`. - Fix logic for handling EOSE messages to correctly update the last check timestamp for the specific account. --- .../nostrsigner/LocalPreferences.kt | 4 ++++ .../nostrsigner/service/ProfileSubscription.kt | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt b/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt index 89ec4438..56d71fb9 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt @@ -150,6 +150,8 @@ object LocalPreferences { } } + fun getLastCheck(context: Context, npub: String): Long = sharedPrefs(context, npub).getLong(PrefKeys.LAST_CHECK.key, 0) + fun setLastCheck(context: Context, npub: String, time: Long) { sharedPrefs(context, npub).edit { apply { @@ -158,6 +160,8 @@ object LocalPreferences { } } + fun getLastMetadataUpdate(context: Context, npub: String): Long = sharedPrefs(context, npub).getLong(PrefKeys.LAST_METADATA_UPDATE.key, 0) + fun setLastMetadataUpdate(context: Context, npub: String, time: Long) { sharedPrefs(context, npub).edit { apply { diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt index 5054f423..89eeac25 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt @@ -21,7 +21,6 @@ package com.greenart7c3.nostrsigner.service import android.content.Context -import com.greenart7c3.nostrsigner.AccountInfo import com.greenart7c3.nostrsigner.Amber import com.greenart7c3.nostrsigner.LocalPreferences import com.greenart7c3.nostrsigner.models.Account @@ -48,7 +47,6 @@ class ProfileSubscription( val scope: CoroutineScope, ) : IRelayClientListener { private val subIds = mutableMapOf() - private val monitoringAccounts: List = emptyList() init { // listens until the app crashes. @@ -57,8 +55,12 @@ class ProfileSubscription( override fun onIncomingMessage(relay: IRelayClient, msgStr: String, msg: Message) { if (msg is EoseMessage) { - monitoringAccounts.forEach { - LocalPreferences.setLastCheck(Amber.instance, it.npub, TimeUtils.now()) + scope.launch { + LocalPreferences.allAccounts(appContext).forEach { + if (msg.subId == subIds[it.hexKey]) { + LocalPreferences.setLastCheck(Amber.instance, it.npub, TimeUtils.now()) + } + } } } if (msg is EventMessage) { @@ -105,7 +107,13 @@ class ProfileSubscription( if (!subIds.containsKey(it.hexKey)) { subIds[it.hexKey] = UUID.randomUUID().toString() } - client.openReqSubscription(subIds[it.hexKey]!!, createProfileFilter(it)) + val lastMetaData = LocalPreferences.getLastMetadataUpdate(appContext, it.npub) + val lastCheck = LocalPreferences.getLastCheck(appContext, it.npub) + val oneDayAgo = TimeUtils.oneDayAgo() + val fifteenMinutesAgo = TimeUtils.fifteenMinutesAgo() + if ((lastMetaData == 0L || oneDayAgo > lastMetaData) && (lastCheck == 0L || fifteenMinutesAgo > lastCheck)) { + client.openReqSubscription(subIds[it.hexKey]!!, createProfileFilter(it)) + } } }