send dms using amber

This commit is contained in:
greenart7c3
2023-08-18 10:57:17 -03:00
parent 3b07b5d8dd
commit 7ea92bc068
4 changed files with 70 additions and 15 deletions

View File

@@ -1069,7 +1069,8 @@ class Account(
markAsSensitive = wantsToMarkAsSensitive, markAsSensitive = wantsToMarkAsSensitive,
zapRaiserAmount = zapRaiserAmount, zapRaiserAmount = zapRaiserAmount,
geohash = geohash, geohash = geohash,
privateKey = keyPair.privKey!!, pubKey = keyPair.pubKey.toHexKey(),
privateKey = keyPair.privKey,
advertiseNip18 = false advertiseNip18 = false
) )

View File

@@ -101,7 +101,7 @@ fun SignerDialog(
} }
signature = it.data?.getStringExtra("signature") ?: "" signature = it.data?.getStringExtra("signature") ?: ""
if (type == SignerType.NIP04_DECRYPT) { if (type != SignerType.SIGN_EVENT) {
onPost( onPost(
signature signature
) )

View File

@@ -64,13 +64,18 @@ import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map import androidx.lifecycle.map
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.ServersAvailable import com.vitorpamplona.amethyst.model.ServersAvailable
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.NostrChatroomDataSource import com.vitorpamplona.amethyst.service.NostrChatroomDataSource
import com.vitorpamplona.amethyst.service.PackageUtils
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.ui.actions.CloseButton import com.vitorpamplona.amethyst.ui.actions.CloseButton
import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel
import com.vitorpamplona.amethyst.ui.actions.PostButton import com.vitorpamplona.amethyst.ui.actions.PostButton
import com.vitorpamplona.amethyst.ui.actions.SignerDialog
import com.vitorpamplona.amethyst.ui.actions.SignerType
import com.vitorpamplona.amethyst.ui.actions.UploadFromGallery import com.vitorpamplona.amethyst.ui.actions.UploadFromGallery
import com.vitorpamplona.amethyst.ui.components.ObserveDisplayNip05Status import com.vitorpamplona.amethyst.ui.components.ObserveDisplayNip05Status
import com.vitorpamplona.amethyst.ui.note.ClickableUserPicture import com.vitorpamplona.amethyst.ui.note.ClickableUserPicture
@@ -93,6 +98,7 @@ import com.vitorpamplona.amethyst.ui.theme.StdPadding
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.ChatMessageEvent import com.vitorpamplona.quartz.events.ChatMessageEvent
import com.vitorpamplona.quartz.events.ChatroomKey import com.vitorpamplona.quartz.events.ChatroomKey
import com.vitorpamplona.quartz.events.Event
import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -278,6 +284,49 @@ fun ChatroomScreen(
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
var event by remember { mutableStateOf<Event?>(null) }
if (event != null) {
SignerDialog(
onClose = {
event = null
},
onPost = {
scope.launch(Dispatchers.IO) {
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
data = event!!.toJson()
)
}
var message by remember { mutableStateOf<String?>(null) }
if (message != null) {
SignerDialog(
onClose = {
message = null
},
onPost = {
scope.launch(Dispatchers.IO) {
event = accountViewModel.account.sendPrivateMessage(
message = it,
toUser = room.users.first(),
replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false,
signEvent = false
)
message = null
}
},
data = message!!,
pubKey = room.users.first(),
type = SignerType.NIP04_ENCRYPT
)
}
// LAST ROW // LAST ROW
PrivateMessageEditFieldRow(newPostModel, isPrivate = true, accountViewModel) { PrivateMessageEditFieldRow(newPostModel, isPrivate = true, accountViewModel) {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
@@ -290,13 +339,17 @@ fun ChatroomScreen(
wantsToMarkAsSensitive = false wantsToMarkAsSensitive = false
) )
} else { } else {
accountViewModel.account.sendPrivateMessage( if (!accountViewModel.isWriteable() && PackageUtils.isAmberInstalled(context)) {
message = newPostModel.message.text, message = newPostModel.message.text
toUser = room.users.first(), } else {
replyingTo = replyTo.value, accountViewModel.account.sendPrivateMessage(
mentions = null, message = newPostModel.message.text,
wantsToMarkAsSensitive = false toUser = room.users.first(),
) replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false
)
}
} }
newPostModel.message = TextFieldValue("") newPostModel.message = TextFieldValue("")

View File

@@ -86,7 +86,8 @@ class PrivateDmEvent(
replyTos: List<String>? = null, replyTos: List<String>? = null,
mentions: List<String>? = null, mentions: List<String>? = null,
zapReceiver: String?, zapReceiver: String?,
privateKey: ByteArray, pubKey: HexKey,
privateKey: ByteArray?,
createdAt: Long = TimeUtils.now(), createdAt: Long = TimeUtils.now(),
publishedRecipientPubKey: ByteArray? = null, publishedRecipientPubKey: ByteArray? = null,
advertiseNip18: Boolean = true, advertiseNip18: Boolean = true,
@@ -94,12 +95,12 @@ class PrivateDmEvent(
zapRaiserAmount: Long?, zapRaiserAmount: Long?,
geohash: String? = null geohash: String? = null
): PrivateDmEvent { ): PrivateDmEvent {
val content = CryptoUtils.encryptNIP04( val message = if (advertiseNip18) { nip18Advertisement } else { "" } + msg
if (advertiseNip18) { nip18Advertisement } else { "" } + msg, val content = if (privateKey == null) message else CryptoUtils.encryptNIP04(
message,
privateKey, privateKey,
recipientPubKey recipientPubKey
) )
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey()
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
publishedRecipientPubKey?.let { publishedRecipientPubKey?.let {
tags.add(listOf("p", publishedRecipientPubKey.toHexKey())) tags.add(listOf("p", publishedRecipientPubKey.toHexKey()))
@@ -124,8 +125,8 @@ class PrivateDmEvent(
} }
val id = generateId(pubKey, createdAt, kind, tags, content) val id = generateId(pubKey, createdAt, kind, tags, content)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (privateKey == null) null else CryptoUtils.sign(id, privateKey)
return PrivateDmEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey()) return PrivateDmEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig?.toHexKey() ?: "")
} }
} }
} }