Moves added charts after NIP19 uris to being nullable.

This commit is contained in:
Vitor Pamplona
2024-04-08 18:32:37 -04:00
parent 776a23c256
commit bbbb614718
4 changed files with 36 additions and 24 deletions

View File

@@ -146,9 +146,9 @@ class NewMessageTagger(
fun getNostrAddress( fun getNostrAddress(
bechAddress: String, bechAddress: String,
restOfTheWord: String, restOfTheWord: String?,
): String { ): String {
return if (restOfTheWord.isEmpty()) { return if (restOfTheWord.isNullOrEmpty()) {
"nostr:$bechAddress" "nostr:$bechAddress"
} else { } else {
if (Bech32.ALPHABET.contains(restOfTheWord.get(0), true)) { if (Bech32.ALPHABET.contains(restOfTheWord.get(0), true)) {
@@ -159,7 +159,7 @@ class NewMessageTagger(
} }
} }
@Immutable data class DirtyKeyInfo(val key: Nip19Bech32.ParseReturn, val restOfWord: String) @Immutable data class DirtyKeyInfo(val key: Nip19Bech32.ParseReturn, val restOfWord: String?)
fun parseDirtyWordForKey(mightBeAKey: String): DirtyKeyInfo? { fun parseDirtyWordForKey(mightBeAKey: String): DirtyKeyInfo? {
var key = mightBeAKey var key = mightBeAKey
@@ -181,7 +181,7 @@ class NewMessageTagger(
val pubkey = val pubkey =
Nip19Bech32.uriToRoute(KeyPair(privKey = keyB32.bechToBytes()).pubKey.toNpub()) ?: return null Nip19Bech32.uriToRoute(KeyPair(privKey = keyB32.bechToBytes()).pubKey.toNpub()) ?: return null
return DirtyKeyInfo(pubkey, restOfWord) return DirtyKeyInfo(pubkey, restOfWord.ifEmpty { null })
} else if (key.startsWith("npub1", true)) { } else if (key.startsWith("npub1", true)) {
if (key.length < 63) { if (key.length < 63) {
return null return null
@@ -192,7 +192,7 @@ class NewMessageTagger(
val pubkey = Nip19Bech32.uriToRoute(keyB32) ?: return null val pubkey = Nip19Bech32.uriToRoute(keyB32) ?: return null
return DirtyKeyInfo(pubkey, restOfWord) return DirtyKeyInfo(pubkey, restOfWord.ifEmpty { null })
} else if (key.startsWith("note1", true)) { } else if (key.startsWith("note1", true)) {
if (key.length < 63) { if (key.length < 63) {
return null return null
@@ -203,7 +203,7 @@ class NewMessageTagger(
val noteId = Nip19Bech32.uriToRoute(keyB32) ?: return null val noteId = Nip19Bech32.uriToRoute(keyB32) ?: return null
return DirtyKeyInfo(noteId, restOfWord) return DirtyKeyInfo(noteId, restOfWord.ifEmpty { null })
} else if (key.startsWith("nprofile", true)) { } else if (key.startsWith("nprofile", true)) {
val pubkeyRelay = Nip19Bech32.uriToRoute(key) ?: return null val pubkeyRelay = Nip19Bech32.uriToRoute(key) ?: return null

View File

@@ -119,7 +119,7 @@ fun LoadOrCreateNote(
@Composable @Composable
private fun LoadAndDisplayEvent( private fun LoadAndDisplayEvent(
event: Event, event: Event,
additionalChars: String, additionalChars: String?,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
@@ -141,7 +141,7 @@ private fun LoadAndDisplayEvent(
private fun DisplayEvent( private fun DisplayEvent(
hex: HexKey, hex: HexKey,
kind: Int?, kind: Int?,
additionalChars: String, additionalChars: String?,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
@@ -164,7 +164,7 @@ private fun DisplayNoteLink(
it: Note, it: Note,
hex: HexKey, hex: HexKey,
kind: Int?, kind: Int?,
addedCharts: String, addedCharts: String?,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
@@ -218,7 +218,7 @@ private fun DisplayNoteLink(
@Composable @Composable
private fun DisplayAddress( private fun DisplayAddress(
nip19: Nip19Bech32.NAddress, nip19: Nip19Bech32.NAddress,
additionalChars: String, additionalChars: String?,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
@@ -245,16 +245,22 @@ private fun DisplayAddress(
} }
if (noteBase == null) { if (noteBase == null) {
if (additionalChars != null) {
Text( Text(
remember { "@${nip19.atag}$additionalChars" }, remember { "@${nip19.atag}$additionalChars" },
) )
} else {
Text(
remember { "@${nip19.atag}" },
)
}
} }
} }
@Composable @Composable
private fun DisplayUser( public fun DisplayUser(
userHex: HexKey, userHex: HexKey,
additionalChars: String, additionalChars: String?,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
@@ -274,16 +280,22 @@ private fun DisplayUser(
userBase?.let { RenderUserAsClickableText(it, additionalChars, nav) } userBase?.let { RenderUserAsClickableText(it, additionalChars, nav) }
if (userBase == null) { if (userBase == null) {
if (additionalChars != null) {
Text( Text(
remember { "@${userHex}$additionalChars" }, remember { "@${userHex}$additionalChars" },
) )
} else {
Text(
remember { "@$userHex" },
)
}
} }
} }
@Composable @Composable
private fun RenderUserAsClickableText( private fun RenderUserAsClickableText(
baseUser: User, baseUser: User,
additionalChars: String, additionalChars: String?,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
val userState by baseUser.live().userMetadataInfo.observeAsState() val userState by baseUser.live().userMetadataInfo.observeAsState()

View File

@@ -60,7 +60,7 @@ class NewMessageTaggerKeyParseTest {
"1532adbe1b369beca9af174076c4736faeb5ef527f1275a4af200121c0f55605", "1532adbe1b369beca9af174076c4736faeb5ef527f1275a4af200121c0f55605",
(result?.key?.entity as? Nip19Bech32.Note)?.hex, (result?.key?.entity as? Nip19Bech32.Note)?.hex,
) )
assertEquals("", result?.restOfWord) assertEquals(null, result?.restOfWord)
} }
@Test @Test
@@ -73,7 +73,7 @@ class NewMessageTaggerKeyParseTest {
"460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c", "460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c",
(result?.key?.entity as? Nip19Bech32.NPub)?.hex, (result?.key?.entity as? Nip19Bech32.NPub)?.hex,
) )
assertEquals("", result?.restOfWord) assertEquals(null, result?.restOfWord)
} }
@Test @Test

View File

@@ -53,7 +53,7 @@ object Nip19Bech32 {
) )
@Immutable @Immutable
data class ParseReturn(val entity: Entity, val additionalChars: String = "") data class ParseReturn(val entity: Entity, val additionalChars: String? = null)
interface Entity interface Entity
@@ -96,7 +96,7 @@ object Nip19Bech32 {
if (type == null) return null if (type == null) return null
return parseComponents(type, key, additionalChars) return parseComponents(type, key, additionalChars.ifEmpty { null })
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("NIP19 Parser", "Issue trying to Decode NIP19 $uri: ${e.message}", e) Log.e("NIP19 Parser", "Issue trying to Decode NIP19 $uri: ${e.message}", e)
} }
@@ -123,7 +123,7 @@ object Nip19Bech32 {
"nembed1" -> nembed(bytes) "nembed1" -> nembed(bytes)
else -> null else -> null
}?.let { }?.let {
ParseReturn(it, additionalChars ?: "") ParseReturn(it, additionalChars)
} }
} catch (e: Throwable) { } catch (e: Throwable) {
Log.w("NIP19 Parser", "Issue trying to Decode NIP19 $key: ${e.message}", e) Log.w("NIP19 Parser", "Issue trying to Decode NIP19 $key: ${e.message}", e)