Refactoring NIP05 verification

This commit is contained in:
Vitor Pamplona
2023-05-06 20:02:54 -04:00
parent bba45dd0a7
commit 90227e1cdc
3 changed files with 113 additions and 110 deletions
@@ -38,6 +38,8 @@ open class Event(
override fun toJson(): String = gson.toJson(this) override fun toJson(): String = gson.toJson(this)
fun hasAnyTaggedUser() = tags.any { it.size > 1 && it[0] == "p" }
fun taggedUsers() = tags.filter { it.size > 1 && it[0] == "p" }.map { it[1] } fun taggedUsers() = tags.filter { it.size > 1 && it[0] == "p" }.map { it[1] }
fun taggedEvents() = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] } fun taggedEvents() = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] }
@@ -83,7 +85,7 @@ open class Event(
override fun getReward(): BigDecimal? { override fun getReward(): BigDecimal? {
return try { return try {
tags.filter { it.firstOrNull() == "reward" }.mapNotNull { it.getOrNull(1)?.let { BigDecimal(it) } }.firstOrNull() tags.firstOrNull { it.size > 1 && it[0] == "reward" }?.get(1)?.let { BigDecimal(it) }
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
@@ -23,6 +23,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalUriHandler import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.UriHandler
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
@@ -92,22 +93,36 @@ fun ObserveDisplayNip05Status(baseUser: User, columnModifier: Modifier = Modifie
val userState by baseUser.live().metadata.observeAsState() val userState by baseUser.live().metadata.observeAsState()
val user = userState?.user ?: return val user = userState?.user ?: return
user.nip05()?.let { nip05 ->
val parts = nip05.split("@")
if (parts.size == 2) {
val nip05Verified by nip05VerificationAsAState(user.info!!, user.pubkeyHex)
Column(modifier = columnModifier) {
DisplayNIP05(parts[0], parts[1], nip05Verified)
}
}
}
}
@Composable
private fun DisplayNIP05(
user: String,
domain: String,
nip05Verified: Boolean?
) {
val uri = LocalUriHandler.current val uri = LocalUriHandler.current
user.nip05()?.let { nip05 ->
if (nip05.split("@").size == 2) {
Column(modifier = columnModifier) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
if (nip05.split("@")[0] != "_") { if (user != "_") {
Text( Text(
text = AnnotatedString(nip05.split("@")[0]), text = AnnotatedString(user),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f), color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )
} }
val nip05Verified by nip05VerificationAsAState(user.info!!, user.pubkeyHex)
if (nip05Verified == null) { if (nip05Verified == null) {
Icon( Icon(
tint = Color.Yellow, tint = Color.Yellow,
@@ -138,16 +153,13 @@ fun ObserveDisplayNip05Status(baseUser: User, columnModifier: Modifier = Modifie
} }
ClickableText( ClickableText(
text = AnnotatedString(nip05.split("@")[1]), text = AnnotatedString(domain),
onClick = { nip05.let { runCatching { uri.openUri("https://${it.split("@")[1]}") } } }, onClick = { runCatching { uri.openUri("https://${domain}") } },
style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(0.52f)), style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(0.52f)),
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Visible overflow = TextOverflow.Visible
) )
} }
}
}
}
} }
@Composable @Composable
@@ -225,7 +225,6 @@ fun NoteComposeInner(
modifier = Modifier modifier = Modifier
.padding(start = if (!isBoostedNote && !isQuotedNote) 10.dp else 0.dp) .padding(start = if (!isBoostedNote && !isQuotedNote) 10.dp else 0.dp)
) { ) {
val (value, elapsed1) = measureTimedValue {
FirstUserInfoRow( FirstUserInfoRow(
baseNote = baseNote, baseNote = baseNote,
showAuthorPicture = isQuotedNote, showAuthorPicture = isQuotedNote,
@@ -233,19 +232,14 @@ fun NoteComposeInner(
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
navController = navController navController = navController
) )
}
Log.d("Time", "$elapsed1 Line 1 of each post ${noteEvent.content()}")
if (noteEvent !is RepostEvent && !makeItShort && !isQuotedNote) { if (noteEvent !is RepostEvent && !makeItShort && !isQuotedNote) {
val (value, elapsed2) = measureTimedValue {
SecondUserInfoRow( SecondUserInfoRow(
note, note,
account, account,
navController navController
) )
} }
Log.d("Time", "$elapsed2 Line 2 of each post")
}
Spacer(modifier = Modifier.height(2.dp)) Spacer(modifier = Modifier.height(2.dp))
@@ -555,7 +549,7 @@ private fun RenderBadgeAward(
var awardees by remember { mutableStateOf<List<User>>(listOf()) } var awardees by remember { mutableStateOf<List<User>>(listOf()) }
Text(text = stringResource(R.string.award_granted_to)) Text(text = stringResource(R.string.award_granted_to))
/*
LaunchedEffect(key1 = note) { LaunchedEffect(key1 = note) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
awardees = noteEvent.awardees().mapNotNull { hex -> awardees = noteEvent.awardees().mapNotNull { hex ->
@@ -591,7 +585,7 @@ private fun RenderBadgeAward(
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
navController = navController navController = navController
) )
}*/ }
ReactionsRow(note, accountViewModel, navController) ReactionsRow(note, accountViewModel, navController)
@@ -704,9 +698,7 @@ private fun ReplyRow(
) { ) {
val noteEvent = note.event val noteEvent = note.event
if (noteEvent is TextNoteEvent && if (noteEvent is TextNoteEvent && (note.replyTo != null || noteEvent.hasAnyTaggedUser())) {
(note.replyTo != null || noteEvent.mentions().isNotEmpty())
) {
val replyingDirectlyTo = note.replyTo?.lastOrNull() val replyingDirectlyTo = note.replyTo?.lastOrNull()
if (replyingDirectlyTo != null && unPackReply) { if (replyingDirectlyTo != null && unPackReply) {
NoteCompose( NoteCompose(
@@ -733,10 +725,7 @@ private fun ReplyRow(
} }
Spacer(modifier = Modifier.height(5.dp)) Spacer(modifier = Modifier.height(5.dp))
} else if ( } else if (noteEvent is ChannelMessageEvent && (note.replyTo != null || noteEvent.hasAnyTaggedUser())) {
noteEvent is ChannelMessageEvent &&
(note.replyTo != null || noteEvent.mentions().isNotEmpty())
) {
val sortedMentions = noteEvent.mentions() val sortedMentions = noteEvent.mentions()
.mapNotNull { LocalCache.checkGetOrCreateUser(it) } .mapNotNull { LocalCache.checkGetOrCreateUser(it) }
.toSet() .toSet()