mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-10 07:23:32 +02:00
Refactor LocalCache.isValidHexNpub()
This commit is contained in:
@@ -5,37 +5,10 @@ import androidx.lifecycle.LiveData
|
|||||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.vitorpamplona.amethyst.service.model.ATag
|
import com.vitorpamplona.amethyst.service.model.*
|
||||||
import com.vitorpamplona.amethyst.service.model.BadgeAwardEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.BadgeDefinitionEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.BadgeProfilesEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelHideMessageEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMuteUserEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ContactListEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.DeletionEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.Event
|
|
||||||
import com.vitorpamplona.amethyst.service.model.LnZapEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.LnZapRequestEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.LongTextNoteEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.MetadataEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.PrivateDmEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ReactionEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.RecommendRelayEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ReportEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.RepostEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.TextNoteEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.relays.Relay
|
import com.vitorpamplona.amethyst.service.relays.Relay
|
||||||
import fr.acinq.secp256k1.Hex
|
import fr.acinq.secp256k1.Hex
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.NonCancellable
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import nostr.postr.toNpub
|
import nostr.postr.toNpub
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
@@ -57,13 +30,10 @@ object LocalCache {
|
|||||||
val addressables = ConcurrentHashMap<String, AddressableNote>()
|
val addressables = ConcurrentHashMap<String, AddressableNote>()
|
||||||
|
|
||||||
fun checkGetOrCreateUser(key: String): User? {
|
fun checkGetOrCreateUser(key: String): User? {
|
||||||
return try {
|
if (isValidHexNpub(key)) {
|
||||||
checkIfValidHex(key)
|
return getOrCreateUser(key)
|
||||||
getOrCreateUser(key)
|
|
||||||
} catch (e: IllegalArgumentException) {
|
|
||||||
Log.e("LocalCache", "Invalid Key to create user: $key", e)
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@@ -79,13 +49,10 @@ object LocalCache {
|
|||||||
if (ATag.isATag(key)) {
|
if (ATag.isATag(key)) {
|
||||||
return checkGetOrCreateAddressableNote(key)
|
return checkGetOrCreateAddressableNote(key)
|
||||||
}
|
}
|
||||||
return try {
|
if (isValidHexNpub(key)) {
|
||||||
checkIfValidHex(key)
|
return getOrCreateNote(key)
|
||||||
getOrCreateNote(key)
|
|
||||||
} catch (e: IllegalArgumentException) {
|
|
||||||
Log.e("LocalCache", "Invalid Key to create note: $key", e)
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@@ -98,17 +65,20 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun checkGetOrCreateChannel(key: String): Channel? {
|
fun checkGetOrCreateChannel(key: String): Channel? {
|
||||||
return try {
|
if (isValidHexNpub(key)) {
|
||||||
checkIfValidHex(key)
|
return getOrCreateChannel(key)
|
||||||
getOrCreateChannel(key)
|
|
||||||
} catch (e: IllegalArgumentException) {
|
|
||||||
Log.e("LocalCache", "Invalid Key to create channel: $key", e)
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkIfValidHex(key: String) {
|
private fun isValidHexNpub(key: String): Boolean {
|
||||||
|
return try {
|
||||||
Hex.decode(key).toNpub()
|
Hex.decode(key).toNpub()
|
||||||
|
true
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
Log.e("LocalCache", "Invalid Key to create user: $key", e)
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
Reference in New Issue
Block a user