mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-26 22:36:29 +02:00
add signer support in the user metadata screen
This commit is contained in:
@@ -186,14 +186,17 @@ class Account(
|
||||
}
|
||||
}
|
||||
|
||||
fun sendNewUserMetadata(toString: String, identities: List<IdentityClaim>) {
|
||||
if (!isWriteable()) return
|
||||
fun sendNewUserMetadata(toString: String, identities: List<IdentityClaim>, signEvent: Boolean = true): MetadataEvent? {
|
||||
if (!isWriteable() && signEvent) return null
|
||||
|
||||
keyPair.privKey?.let {
|
||||
val event = MetadataEvent.create(toString, identities, keyPair.privKey!!)
|
||||
Client.send(event)
|
||||
LocalCache.consume(event)
|
||||
val event = MetadataEvent.create(toString, identities, keyPair.pubKey.toHexKey(), keyPair.privKey)
|
||||
if (!signEvent) {
|
||||
return event
|
||||
}
|
||||
Client.send(event)
|
||||
LocalCache.consume(event)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun reactionTo(note: Note, reaction: String): List<Note> {
|
||||
|
@@ -171,8 +171,7 @@ class MetadataEvent(
|
||||
.readerFor(UserMetadata::class.java)
|
||||
}
|
||||
|
||||
fun create(contactMetaData: String, identities: List<IdentityClaim>, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): MetadataEvent {
|
||||
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey()
|
||||
fun create(contactMetaData: String, identities: List<IdentityClaim>, pubKey: HexKey, privateKey: ByteArray?, createdAt: Long = TimeUtils.now()): MetadataEvent {
|
||||
val tags = mutableListOf<List<String>>()
|
||||
|
||||
identities.forEach {
|
||||
@@ -180,8 +179,8 @@ class MetadataEvent(
|
||||
}
|
||||
|
||||
val id = generateId(pubKey, createdAt, kind, tags, contactMetaData)
|
||||
val sig = CryptoUtils.sign(id, privateKey)
|
||||
return MetadataEvent(id.toHexKey(), pubKey, createdAt, tags, contactMetaData, sig.toHexKey())
|
||||
val sig = if (privateKey == null) null else CryptoUtils.sign(id, privateKey)
|
||||
return MetadataEvent(id.toHexKey(), pubKey, createdAt, tags, contactMetaData, sig?.toHexKey() ?: "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,11 @@ import androidx.compose.material.Surface
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -28,6 +33,10 @@ import androidx.compose.ui.window.DialogProperties
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.vitorpamplona.amethyst.R
|
||||
import com.vitorpamplona.amethyst.model.Account
|
||||
import com.vitorpamplona.amethyst.model.LocalCache
|
||||
import com.vitorpamplona.amethyst.service.PackageUtils
|
||||
import com.vitorpamplona.amethyst.service.model.Event
|
||||
import com.vitorpamplona.amethyst.service.relays.Client
|
||||
import com.vitorpamplona.amethyst.ui.theme.placeholderText
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -37,6 +46,7 @@ import kotlinx.coroutines.withContext
|
||||
fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
||||
val postViewModel: NewUserMetadataViewModel = viewModel()
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
postViewModel.load(account)
|
||||
@@ -61,6 +71,25 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
||||
Column(
|
||||
modifier = Modifier.padding(10.dp)
|
||||
) {
|
||||
var event by remember { mutableStateOf<Event?>(null) }
|
||||
if (event != null) {
|
||||
SignerDialog(
|
||||
onClose = {
|
||||
event = null
|
||||
},
|
||||
onPost = {
|
||||
scope.launch(Dispatchers.IO) {
|
||||
Client.send(it)
|
||||
LocalCache.verifyAndConsume(it, null)
|
||||
event = null
|
||||
postViewModel.clear()
|
||||
onClose()
|
||||
}
|
||||
},
|
||||
event = event!!
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
@@ -74,15 +103,22 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
||||
|
||||
PostButton(
|
||||
onPost = {
|
||||
postViewModel.create()
|
||||
onClose()
|
||||
if (PackageUtils.isAmberInstalled(context)) {
|
||||
event = postViewModel.create(false)
|
||||
println(event)
|
||||
} else {
|
||||
postViewModel.create(true)
|
||||
onClose()
|
||||
}
|
||||
},
|
||||
true
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier.padding(10.dp).verticalScroll(rememberScrollState())
|
||||
modifier = Modifier
|
||||
.padding(10.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(1f),
|
||||
|
@@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode
|
||||
import com.vitorpamplona.amethyst.model.Account
|
||||
import com.vitorpamplona.amethyst.service.model.GitHubIdentity
|
||||
import com.vitorpamplona.amethyst.service.model.MastodonIdentity
|
||||
import com.vitorpamplona.amethyst.service.model.MetadataEvent
|
||||
import com.vitorpamplona.amethyst.service.model.TwitterIdentity
|
||||
import com.vitorpamplona.amethyst.ui.components.MediaCompressor
|
||||
import id.zelory.compressor.Compressor.compress
|
||||
@@ -73,7 +74,7 @@ class NewUserMetadataViewModel : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun create() {
|
||||
fun create(signEvent: Boolean): MetadataEvent? {
|
||||
// Tries to not delete any existing attribute that we do not work with.
|
||||
val latest = account.userProfile().info?.latestMetadata
|
||||
val currentJson = if (latest != null) {
|
||||
@@ -122,11 +123,16 @@ class NewUserMetadataViewModel : ViewModel() {
|
||||
val writer = StringWriter()
|
||||
ObjectMapper().writeValue(writer, currentJson)
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
account.sendNewUserMetadata(writer.buffer.toString(), newClaims)
|
||||
if (signEvent) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
account.sendNewUserMetadata(writer.buffer.toString(), newClaims, signEvent)
|
||||
}
|
||||
clear()
|
||||
} else {
|
||||
return account.sendNewUserMetadata(writer.buffer.toString(), newClaims, signEvent)
|
||||
}
|
||||
|
||||
clear()
|
||||
return null
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
|
Reference in New Issue
Block a user