optimistic update message on send
This commit is contained in:
@@ -42,8 +42,8 @@ import rust.nostr.sdk.TagKind
|
||||
import rust.nostr.sdk.Timestamp
|
||||
import rust.nostr.sdk.UnsignedEvent
|
||||
import rust.nostr.sdk.UnwrappedGift
|
||||
import rust.nostr.sdk.giftWrapAsync
|
||||
import rust.nostr.sdk.initLogger
|
||||
import rust.nostr.sdk.makePrivateMsgAsync
|
||||
import rust.nostr.sdk.nip17ExtractRelayList
|
||||
import kotlin.time.Duration
|
||||
|
||||
@@ -551,7 +551,8 @@ class Nostr {
|
||||
to: List<PublicKey>,
|
||||
content: String,
|
||||
subject: String? = null,
|
||||
replies: List<EventId> = emptyList()
|
||||
replies: List<EventId> = emptyList(),
|
||||
onNewMessage: ((UnsignedEvent) -> Unit)? = null
|
||||
) {
|
||||
try {
|
||||
val currentUser =
|
||||
@@ -578,20 +579,32 @@ class Nostr {
|
||||
}
|
||||
}
|
||||
|
||||
for (receiver in to.plus(currentUser)) {
|
||||
// Construct the gift wrap event
|
||||
val event = makePrivateMsgAsync(
|
||||
signer = signer,
|
||||
receiver = receiver,
|
||||
message = content,
|
||||
rumorExtraTags = tags
|
||||
)
|
||||
for (receiver in listOf(currentUser) + to) {
|
||||
// Construct the rumor event
|
||||
// NEVER SIGN this event with the current user signer
|
||||
val rumor = EventBuilder
|
||||
.privateMsgRumor(receiver = receiver, message = content)
|
||||
.tags(tags)
|
||||
.build(currentUser)
|
||||
// Ensure the event ID is set
|
||||
.ensureId()
|
||||
|
||||
println("Sending message to: ${receiver.toBech32()}")
|
||||
// Emit the rumor to the chat screen
|
||||
if (receiver == currentUser) {
|
||||
onNewMessage?.invoke(rumor)
|
||||
}
|
||||
|
||||
// Construct the gift wrap event
|
||||
val gift = giftWrapAsync(
|
||||
signer = signer,
|
||||
receiverPubkey = receiver,
|
||||
rumor = rumor,
|
||||
extraTags = tags
|
||||
)
|
||||
|
||||
// Send the event to receiver's NIP-17 relays
|
||||
client?.sendEvent(
|
||||
event = event,
|
||||
event = gift,
|
||||
target = SendEventTarget.toNip17(),
|
||||
ackPolicy = AckPolicy.none(),
|
||||
authenticationTimeout = Duration.parse("2s")
|
||||
|
||||
@@ -333,7 +333,12 @@ class NostrViewModel(
|
||||
to = room.members.toList(),
|
||||
content = message,
|
||||
subject = room.subject,
|
||||
replies = replies
|
||||
replies = replies,
|
||||
onNewMessage = { event ->
|
||||
viewModelScope.launch {
|
||||
_newEvents.emit(event)
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
showError("Error: ${e.message}")
|
||||
|
||||
@@ -125,6 +125,27 @@ fun Timestamp.ago(): String {
|
||||
}
|
||||
}
|
||||
|
||||
fun Timestamp.formatAsGroupHeader(): String {
|
||||
val timeZone = TimeZone.currentSystemDefault()
|
||||
val inputInstant = Instant.fromEpochSeconds(this.asSecs().toLong())
|
||||
val inputDate = inputInstant.toLocalDateTime(timeZone).date
|
||||
|
||||
val now = Clock.System.now()
|
||||
val today = now.toLocalDateTime(timeZone).date
|
||||
val yesterday = today.minus(1, DateTimeUnit.DAY)
|
||||
|
||||
return when (inputDate) {
|
||||
today -> "Today"
|
||||
yesterday -> "Yesterday"
|
||||
else -> {
|
||||
val day = inputDate.day.toString().padStart(2, '0')
|
||||
val month = inputDate.month.number.toString().padStart(2, '0')
|
||||
val year = inputDate.year.toString().takeLast(2)
|
||||
"$day/$month/$year"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Timestamp.humanReadable(): String {
|
||||
val timeZone = TimeZone.currentSystemDefault()
|
||||
val inputInstant = Instant.fromEpochSeconds(this.asSecs().toLong())
|
||||
|
||||
Reference in New Issue
Block a user