From 5d9f1fba29bea987b082938bff65c4f2f38a18cd Mon Sep 17 00:00:00 2001 From: dluvian Date: Tue, 20 Feb 2024 13:03:15 +0800 Subject: [PATCH 1/2] Exclude Nip11Document from Proguard --- app/proguard-rules.pro | 1 + 1 file changed, 1 insertion(+) diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 455e1ca8..0ea16cdb 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -24,6 +24,7 @@ -keep class com.dluvian.nozzle.model.nostr.Event { ; } -keep class com.dluvian.nozzle.model.nostr.Metadata { ; } -keep class com.dluvian.nozzle.model.nostr.nip05.Nip05Response { ; } +-keep class com.dluvian.nozzle.model.nostr.nip11.Nip11Document { ; } # Please add these rules to your existing keep rules in order to suppress warnings. # This is generated automatically by the Android Gradle plugin. From 2113ed03f0c5545cee289c888ee8cc77ca655c97 Mon Sep 17 00:00:00 2001 From: dluvian Date: Tue, 20 Feb 2024 14:15:40 +0800 Subject: [PATCH 2/2] Debounce contact list updates --- .../data/profileFollower/ProfileFollower.kt | 34 +++++++++++++------ .../dluvian/nozzle/data/room/dao/Nip65Dao.kt | 4 +-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/dluvian/nozzle/data/profileFollower/ProfileFollower.kt b/app/src/main/java/com/dluvian/nozzle/data/profileFollower/ProfileFollower.kt index 29f1b57e..7c2bde5a 100644 --- a/app/src/main/java/com/dluvian/nozzle/data/profileFollower/ProfileFollower.kt +++ b/app/src/main/java/com/dluvian/nozzle/data/profileFollower/ProfileFollower.kt @@ -3,18 +3,18 @@ package com.dluvian.nozzle.data.profileFollower import android.util.Log import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf +import com.dluvian.nozzle.data.WAIT_TIME import com.dluvian.nozzle.data.nostr.INostrService import com.dluvian.nozzle.data.provider.IPubkeyProvider import com.dluvian.nozzle.data.provider.IRelayProvider import com.dluvian.nozzle.data.room.dao.ContactDao import com.dluvian.nozzle.data.room.entity.ContactEntity +import com.dluvian.nozzle.data.utils.getCurrentTimeInSeconds import com.dluvian.nozzle.model.Pubkey import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.util.concurrent.CancellationException @@ -28,13 +28,8 @@ class ProfileFollower( ) : IProfileFollower { private val scope = CoroutineScope(Dispatchers.IO) private val followProcesses: MutableMap = mutableMapOf() - private val forcedFollowState = mutableStateOf(emptyMap()) - val pubkeyState = pubkeyProvider.getActivePubkeyStateFlow() - .onEach local@{ forcedFollowState.value = emptyMap() } - .stateIn(scope, SharingStarted.Eagerly, "") - override fun follow(pubkeyToFollow: Pubkey) { putForcedFollowState(pubkey = pubkeyToFollow, isFollowed = true) @@ -85,10 +80,27 @@ class ProfileFollower( .apply { this[pubkey] = isFollowed } } - private suspend fun updateContactList(personalPubkey: String) { - val contactPubkeys = contactDao.listContactPubkeys(pubkey = personalPubkey) + private var updateJob: Job? = null + private suspend fun updateContactList(personalPubkey: Pubkey) { + contactDao.updateTime(pubkey = personalPubkey, createdAt = getCurrentTimeInSeconds()) + updateJob?.cancel(CancellationException("Cancel to prevent spamming relay")) + updateJob = scope.launch { + var jobs: List + do { + delay(WAIT_TIME) + jobs = followProcesses.values.toList() + } while (jobs.any { it.isActive }) + updateContactListOverNostr(pubkey = pubkeyProvider.getActivePubkey()) + } + updateJob?.invokeOnCompletion { + Log.i(TAG, "Completed contact list update. Error = ${it?.localizedMessage}") + } + } + + private suspend fun updateContactListOverNostr(pubkey: Pubkey) { val event = nostrService.updateContactList( - contactPubkeys = contactPubkeys, relays = relayProvider.getWriteRelays() + contactPubkeys = contactDao.listContactPubkeys(pubkey = pubkey), + relays = relayProvider.getWriteRelays() ) contactDao.updateTime(pubkey = event.pubkey, createdAt = event.createdAt) } diff --git a/app/src/main/java/com/dluvian/nozzle/data/room/dao/Nip65Dao.kt b/app/src/main/java/com/dluvian/nozzle/data/room/dao/Nip65Dao.kt index d969cff9..7dd54235 100644 --- a/app/src/main/java/com/dluvian/nozzle/data/room/dao/Nip65Dao.kt +++ b/app/src/main/java/com/dluvian/nozzle/data/room/dao/Nip65Dao.kt @@ -93,7 +93,7 @@ interface Nip65Dao { "WHERE isRead = '1' " + "AND pubkey = :pubkey" ) - suspend fun getReadRelaysOfPubkey(pubkey: String): List + suspend fun getReadRelaysOfPubkey(pubkey: String): List @Query( "SELECT url " + @@ -101,7 +101,7 @@ interface Nip65Dao { "WHERE isWrite = '1' " + "AND pubkey = :pubkey" ) - suspend fun getWriteRelaysOfPubkey(pubkey: String): List + suspend fun getWriteRelaysOfPubkey(pubkey: String): List @Query( "SELECT url, isRead, isWrite " +