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.
This commit is contained in:
greenart7c3
2025-11-28 07:22:25 -03:00
parent e9fddd7014
commit c8fb1a2a03
2 changed files with 17 additions and 5 deletions
@@ -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 {
@@ -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<String, String>()
private val monitoringAccounts: List<AccountInfo> = 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))
}
}
}