kopia lustrzana https://github.com/vitorpamplona/amethyst
Loads basic information for all the accounts in the app.
rodzic
393fbd0359
commit
b2b7cba352
|
@ -35,6 +35,8 @@ import com.vitorpamplona.quartz.signers.NostrSignerExternal
|
||||||
import com.vitorpamplona.quartz.signers.NostrSignerInternal
|
import com.vitorpamplona.quartz.signers.NostrSignerInternal
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
@ -99,6 +101,7 @@ object LocalPreferences {
|
||||||
|
|
||||||
private var _currentAccount: String? = null
|
private var _currentAccount: String? = null
|
||||||
private var _savedAccounts: List<AccountInfo>? = null
|
private var _savedAccounts: List<AccountInfo>? = null
|
||||||
|
private var _cachedAccounts: MutableMap<String, Account?> = mutableMapOf()
|
||||||
|
|
||||||
suspend fun currentAccount(): String? {
|
suspend fun currentAccount(): String? {
|
||||||
if (_currentAccount == null) {
|
if (_currentAccount == null) {
|
||||||
|
@ -286,9 +289,9 @@ object LocalPreferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun loadCurrentAccountFromEncryptedStorage(): Account? {
|
suspend fun loadCurrentAccountFromEncryptedStorage(): Account? {
|
||||||
val acc = loadCurrentAccountFromEncryptedStorage(currentAccount())
|
return currentAccount()?.let {
|
||||||
acc?.registerObservers()
|
loadCurrentAccountFromEncryptedStorage(it)
|
||||||
return acc
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun migrateOldSharedSettings(): Settings? {
|
suspend fun migrateOldSharedSettings(): Settings? {
|
||||||
|
@ -376,7 +379,23 @@ object LocalPreferences {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun loadCurrentAccountFromEncryptedStorage(npub: String?): Account? = withContext(Dispatchers.IO) {
|
val mutex = Mutex()
|
||||||
|
suspend fun loadCurrentAccountFromEncryptedStorage(npub: String): Account? = withContext(Dispatchers.IO) {
|
||||||
|
mutex.withLock {
|
||||||
|
if (_cachedAccounts.containsKey(npub)) {
|
||||||
|
return@withContext _cachedAccounts.get(npub)
|
||||||
|
}
|
||||||
|
|
||||||
|
val account = innerLoadCurrentAccountFromEncryptedStorage(npub)
|
||||||
|
account?.registerObservers()
|
||||||
|
|
||||||
|
_cachedAccounts.put(npub, account)
|
||||||
|
|
||||||
|
return@withContext account
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun innerLoadCurrentAccountFromEncryptedStorage(npub: String?): Account? = withContext(Dispatchers.IO) {
|
||||||
checkNotInMainThread()
|
checkNotInMainThread()
|
||||||
|
|
||||||
return@withContext with(encryptedPreferences(npub)) {
|
return@withContext with(encryptedPreferences(npub)) {
|
||||||
|
|
|
@ -28,7 +28,9 @@ import com.vitorpamplona.amethyst.service.NostrThreadDataSource
|
||||||
import com.vitorpamplona.amethyst.service.NostrUserProfileDataSource
|
import com.vitorpamplona.amethyst.service.NostrUserProfileDataSource
|
||||||
import com.vitorpamplona.amethyst.service.NostrVideoDataSource
|
import com.vitorpamplona.amethyst.service.NostrVideoDataSource
|
||||||
import com.vitorpamplona.amethyst.service.relays.Client
|
import com.vitorpamplona.amethyst.service.relays.Client
|
||||||
|
import com.vitorpamplona.quartz.encoders.bechToBytes
|
||||||
import com.vitorpamplona.quartz.encoders.decodePublicKeyAsHexOrNull
|
import com.vitorpamplona.quartz.encoders.decodePublicKeyAsHexOrNull
|
||||||
|
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
@ -78,6 +80,13 @@ class ServiceManager {
|
||||||
|
|
||||||
// start services
|
// start services
|
||||||
NostrAccountDataSource.account = myAccount
|
NostrAccountDataSource.account = myAccount
|
||||||
|
NostrAccountDataSource.otherAccounts = LocalPreferences.allSavedAccounts().mapNotNull {
|
||||||
|
try {
|
||||||
|
it.npub.bechToBytes().toHexKey()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
NostrHomeDataSource.account = myAccount
|
NostrHomeDataSource.account = myAccount
|
||||||
NostrChatroomListDataSource.account = myAccount
|
NostrChatroomListDataSource.account = myAccount
|
||||||
NostrVideoDataSource.account = myAccount
|
NostrVideoDataSource.account = myAccount
|
||||||
|
|
|
@ -10,6 +10,7 @@ import com.vitorpamplona.amethyst.service.relays.EOSETime
|
||||||
import com.vitorpamplona.amethyst.service.relays.JsonFilter
|
import com.vitorpamplona.amethyst.service.relays.JsonFilter
|
||||||
import com.vitorpamplona.amethyst.service.relays.Relay
|
import com.vitorpamplona.amethyst.service.relays.Relay
|
||||||
import com.vitorpamplona.amethyst.service.relays.TypedFilter
|
import com.vitorpamplona.amethyst.service.relays.TypedFilter
|
||||||
|
import com.vitorpamplona.quartz.encoders.HexKey
|
||||||
import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent
|
import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent
|
||||||
import com.vitorpamplona.quartz.events.BadgeAwardEvent
|
import com.vitorpamplona.quartz.events.BadgeAwardEvent
|
||||||
import com.vitorpamplona.quartz.events.BadgeProfilesEvent
|
import com.vitorpamplona.quartz.events.BadgeProfilesEvent
|
||||||
|
@ -38,6 +39,7 @@ import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
// TODO: Migrate this to a property of AccountVi
|
// TODO: Migrate this to a property of AccountVi
|
||||||
object NostrAccountDataSource : NostrDataSource("AccountData") {
|
object NostrAccountDataSource : NostrDataSource("AccountData") {
|
||||||
lateinit var account: Account
|
lateinit var account: Account
|
||||||
|
var otherAccounts = listOf<HexKey>()
|
||||||
|
|
||||||
val latestEOSEs = EOSEAccount()
|
val latestEOSEs = EOSEAccount()
|
||||||
val hasLoadedTheBasics = mutableMapOf<User, Boolean>()
|
val hasLoadedTheBasics = mutableMapOf<User, Boolean>()
|
||||||
|
@ -75,6 +77,18 @@ object NostrAccountDataSource : NostrDataSource("AccountData") {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createOtherAccountsBaseFilter(): TypedFilter? {
|
||||||
|
if (otherAccounts.isEmpty()) return null
|
||||||
|
return TypedFilter(
|
||||||
|
types = COMMON_FEED_TYPES,
|
||||||
|
filter = JsonFilter(
|
||||||
|
kinds = listOf(MetadataEvent.kind, ContactListEvent.kind, AdvertisedRelayListEvent.kind, MuteListEvent.kind, PeopleListEvent.kind),
|
||||||
|
authors = otherAccounts.filter { it != account.userProfile().pubkeyHex },
|
||||||
|
limit = 100
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun createAccountAcceptedAwardsFilter(): TypedFilter {
|
fun createAccountAcceptedAwardsFilter(): TypedFilter {
|
||||||
return TypedFilter(
|
return TypedFilter(
|
||||||
types = COMMON_FEED_TYPES,
|
types = COMMON_FEED_TYPES,
|
||||||
|
@ -218,7 +232,7 @@ object NostrAccountDataSource : NostrDataSource("AccountData") {
|
||||||
override fun updateChannelFilters() {
|
override fun updateChannelFilters() {
|
||||||
return if (hasLoadedTheBasics[account.userProfile()] != null) {
|
return if (hasLoadedTheBasics[account.userProfile()] != null) {
|
||||||
// gets everthing about the user logged in
|
// gets everthing about the user logged in
|
||||||
accountChannel.typedFilters = listOf(
|
accountChannel.typedFilters = listOfNotNull(
|
||||||
createAccountMetadataFilter(),
|
createAccountMetadataFilter(),
|
||||||
createAccountContactListFilter(),
|
createAccountContactListFilter(),
|
||||||
createAccountRelayListFilter(),
|
createAccountRelayListFilter(),
|
||||||
|
@ -227,7 +241,8 @@ object NostrAccountDataSource : NostrDataSource("AccountData") {
|
||||||
createAccountReportsFilter(),
|
createAccountReportsFilter(),
|
||||||
createAccountAcceptedAwardsFilter(),
|
createAccountAcceptedAwardsFilter(),
|
||||||
createAccountBookmarkListFilter(),
|
createAccountBookmarkListFilter(),
|
||||||
createAccountLastPostsListFilter()
|
createAccountLastPostsListFilter(),
|
||||||
|
createOtherAccountsBaseFilter()
|
||||||
).ifEmpty { null }
|
).ifEmpty { null }
|
||||||
} else {
|
} else {
|
||||||
// just the basics.
|
// just the basics.
|
||||||
|
|
|
@ -116,7 +116,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
|
||||||
// old event being re-broadcast
|
// old event being re-broadcast
|
||||||
if (event.createdAt < TimeUtils.fiveMinutesAgo()) return
|
if (event.createdAt < TimeUtils.fiveMinutesAgo()) return
|
||||||
|
|
||||||
if (acc != null && acc.userProfile().pubkeyHex == event.verifiedRecipientPubKey()) {
|
if (acc.userProfile().pubkeyHex == event.verifiedRecipientPubKey()) {
|
||||||
val followingKeySet = acc.followingKeySet()
|
val followingKeySet = acc.followingKeySet()
|
||||||
|
|
||||||
val knownChatrooms = acc.userProfile().privateChatrooms.keys.filter {
|
val knownChatrooms = acc.userProfile().privateChatrooms.keys.filter {
|
||||||
|
|
Ładowanie…
Reference in New Issue