feat: add self-chat #13
@@ -9,25 +9,32 @@ import su.reya.coop.Room
|
|||||||
import su.reya.coop.short
|
import su.reya.coop.short
|
||||||
|
|
||||||
fun Room.displayNameFlow(viewModel: NostrViewModel): Flow<String> {
|
fun Room.displayNameFlow(viewModel: NostrViewModel): Flow<String> {
|
||||||
if (!subject.isNullOrBlank()) return flowOf<String>(subject!!)
|
// Return early if there's a custom subject/room name
|
||||||
|
subject?.takeIf { it.isNotBlank() }?.let { return flowOf(it) }
|
||||||
|
|
||||||
val memberFlows = members.map { viewModel.getMetadata(it) }
|
val displayMembers = if (isGroup()) members.take(2) else members.take(1)
|
||||||
|
if (displayMembers.isEmpty()) return flowOf("Unknown")
|
||||||
|
|
||||||
|
return combine(displayMembers.map { viewModel.getMetadata(it) }) { metadataArray ->
|
||||||
|
val names = metadataArray.mapIndexed { i, metadata ->
|
||||||
|
val profile = metadata?.asRecord()
|
||||||
|
profile?.name?.takeIf { it.isNotBlank() }
|
||||||
|
?: profile?.displayName?.takeIf { it.isNotBlank() }
|
||||||
|
?: displayMembers[i].short()
|
||||||
|
}
|
||||||
|
|
||||||
return combine(memberFlows) { metadataArray ->
|
|
||||||
if (isGroup()) {
|
if (isGroup()) {
|
||||||
val profiles = metadataArray.map { it?.asRecord() }
|
val combined = names.joinToString(", ")
|
||||||
val names = profiles.take(2).mapNotNull { it?.name ?: it?.displayName }
|
val extraCount = members.size - names.size
|
||||||
var combined = names.joinToString(", ")
|
if (extraCount > 0) "$combined, +$extraCount" else combined
|
||||||
if (profiles.size > 2) combined += ", +${profiles.size - 2}"
|
|
||||||
combined.ifBlank { "Unknown group" }
|
|
||||||
} else {
|
} else {
|
||||||
val profile = metadataArray.firstOrNull()?.asRecord()
|
val name = names.first()
|
||||||
profile?.name ?: profile?.displayName ?: members.firstOrNull()?.short() ?: "Unknown"
|
if (displayMembers.first() == viewModel.currentUser()) "$name (you)" else name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Room.pictureFlow(viewModel: NostrViewModel): Flow<String?> {
|
fun Room.pictureFlow(viewModel: NostrViewModel): Flow<String?> {
|
||||||
val firstMember = members.firstOrNull() ?: return kotlinx.coroutines.flow.flowOf(null)
|
val firstMember = members.firstOrNull() ?: return flowOf(null)
|
||||||
return viewModel.getMetadata(firstMember).map { it?.asRecord()?.picture }
|
return viewModel.getMetadata(firstMember).map { it?.asRecord()?.picture }
|
||||||
}
|
}
|
||||||
@@ -699,7 +699,7 @@ class Nostr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun sendMessage(
|
suspend fun sendMessage(
|
||||||
to: List<PublicKey>,
|
to: Set<PublicKey>,
|
||||||
content: String,
|
content: String,
|
||||||
subject: String? = null,
|
subject: String? = null,
|
||||||
replies: List<EventId> = emptyList(),
|
replies: List<EventId> = emptyList(),
|
||||||
@@ -728,7 +728,7 @@ class Nostr {
|
|||||||
tags.add(Tag.publicKey(pubkey))
|
tags.add(Tag.publicKey(pubkey))
|
||||||
}
|
}
|
||||||
|
|
||||||
for (receiver in listOf(currentUser) + to) {
|
for (receiver in setOf(currentUser) + to) {
|
||||||
// Construct the rumor event
|
// Construct the rumor event
|
||||||
// NEVER SIGN this event with the current user signer
|
// NEVER SIGN this event with the current user signer
|
||||||
val rumor = EventBuilder
|
val rumor = EventBuilder
|
||||||
|
|||||||
@@ -548,7 +548,7 @@ class NostrViewModel(
|
|||||||
try {
|
try {
|
||||||
val room = getChatRoom(roomId)
|
val room = getChatRoom(roomId)
|
||||||
nostr.sendMessage(
|
nostr.sendMessage(
|
||||||
to = room.members.toList(),
|
to = room.members,
|
||||||
content = message,
|
content = message,
|
||||||
subject = room.subject,
|
subject = room.subject,
|
||||||
replies = replies,
|
replies = replies,
|
||||||
|
|||||||
Reference in New Issue
Block a user