Chat lines are routed once, in core
session.ChatFeed decides which conversation a message, history, server event or notice goes to and renders it; frontends get the lines through chat.ChatSink and key conversations by chat.ChatKey. ServerSession.Listener no longer passes on raw chat events. Swing now labels our own messages with our nickname on the server and links our name to us in private chats. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
package eu.lixko.ts3j.client.android
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import eu.lixko.ts3j.client.chat.ChatKey
|
||||
import eu.lixko.ts3j.client.chat.ChatScope
|
||||
import eu.lixko.ts3j.client.text.HtmlText
|
||||
import eu.lixko.ts3j.client.text.TextQuery
|
||||
|
||||
/** Which conversation a chat line belongs to. */
|
||||
sealed interface ChatKey {
|
||||
data object Server : ChatKey
|
||||
data object Channel : ChatKey
|
||||
data class Private(val clientId: Int) : ChatKey
|
||||
}
|
||||
|
||||
/** One entry of a chat, as HTML in the form [eu.lixko.ts3j.client.text.ChatHtml] writes. */
|
||||
@Immutable
|
||||
data class ChatLine(val id: Long, val html: String) {
|
||||
@@ -31,25 +26,25 @@ data class Conversation(
|
||||
/** Every conversation of the session: the server's, the channel's, then the private ones. */
|
||||
@Immutable
|
||||
data class Chats(
|
||||
val server: Conversation = Conversation(ChatKey.Server, "Server"),
|
||||
val channel: Conversation = Conversation(ChatKey.Channel, "Channel"),
|
||||
val server: Conversation = Conversation(ChatKey.SERVER, "Server"),
|
||||
val channel: Conversation = Conversation(ChatKey.CHANNEL, "Channel"),
|
||||
val private: List<Conversation> = emptyList(),
|
||||
) {
|
||||
val all: List<Conversation> get() = listOf(server, channel) + private
|
||||
|
||||
val anyUnread: Boolean get() = all.any { it.unread }
|
||||
|
||||
operator fun get(key: ChatKey): Conversation? = when (key) {
|
||||
ChatKey.Server -> server
|
||||
ChatKey.Channel -> channel
|
||||
is ChatKey.Private -> private.firstOrNull { it.key == key }
|
||||
operator fun get(key: ChatKey): Conversation? = when (key.scope()) {
|
||||
ChatScope.SERVER -> server
|
||||
ChatScope.CHANNEL -> channel
|
||||
ChatScope.PRIVATE -> private.firstOrNull { it.key == key }
|
||||
}
|
||||
|
||||
/** Applies [change] to a conversation, opening a private one titled [title] if needed. */
|
||||
fun update(key: ChatKey, title: String = "", change: (Conversation) -> Conversation): Chats = when (key) {
|
||||
ChatKey.Server -> copy(server = change(server))
|
||||
ChatKey.Channel -> copy(channel = change(channel))
|
||||
is ChatKey.Private -> {
|
||||
fun update(key: ChatKey, title: String = "", change: (Conversation) -> Conversation): Chats = when (key.scope()) {
|
||||
ChatScope.SERVER -> copy(server = change(server))
|
||||
ChatScope.CHANNEL -> copy(channel = change(channel))
|
||||
ChatScope.PRIVATE -> {
|
||||
val existing = private.indexOfFirst { it.key == key }
|
||||
if (existing < 0) {
|
||||
copy(private = private + change(Conversation(key, title)))
|
||||
@@ -59,7 +54,7 @@ data class Chats(
|
||||
}
|
||||
}
|
||||
|
||||
fun close(key: ChatKey.Private): Chats = copy(private = private.filter { it.key != key })
|
||||
fun close(key: ChatKey): Chats = copy(private = private.filter { it.key != key })
|
||||
|
||||
companion object {
|
||||
/** As many lines as a log history loads; older ones leave the conversation. */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package eu.lixko.ts3j.client.android
|
||||
|
||||
import android.net.Uri
|
||||
import eu.lixko.ts3j.client.chat.ChatKey
|
||||
import eu.lixko.ts3j.client.chat.ChatScope
|
||||
import eu.lixko.ts3j.client.config.Bookmark
|
||||
import eu.lixko.ts3j.client.contacts.ContactType
|
||||
@@ -81,7 +82,7 @@ class ServerController internal constructor(
|
||||
set(value) {
|
||||
field = value
|
||||
if (value != null) markRead(value)
|
||||
if (value is ChatKey.Private) app.notifier.clear(id, value.clientId)
|
||||
if (value != null && value.isPrivate) app.notifier.clear(id, value.clientId())
|
||||
}
|
||||
|
||||
/** Counts the answers to info requests, for the details on screen to be read again. */
|
||||
@@ -91,8 +92,8 @@ class ServerController internal constructor(
|
||||
val images = ServerImages(session.icons(), session.avatars())
|
||||
|
||||
/** A private chat a notification asked to show, until the UI took it up. */
|
||||
private val _chatRequest = MutableStateFlow<ChatKey.Private?>(null)
|
||||
val chatRequest: StateFlow<ChatKey.Private?> = _chatRequest
|
||||
private val _chatRequest = MutableStateFlow<ChatKey?>(null)
|
||||
val chatRequest: StateFlow<ChatKey?> = _chatRequest
|
||||
|
||||
private val lineIds = AtomicLong()
|
||||
|
||||
@@ -386,8 +387,8 @@ class ServerController internal constructor(
|
||||
// ---- chat ----
|
||||
|
||||
/** Opens the private chat with a client, loading what was said before. */
|
||||
fun openPrivateChat(clientId: Int): ChatKey.Private {
|
||||
val key = ChatKey.Private(clientId)
|
||||
fun openPrivateChat(clientId: Int): ChatKey {
|
||||
val key = ChatKey.privateWith(clientId)
|
||||
val client = session.model().getClient(clientId)
|
||||
if (client != null) {
|
||||
_chats.update { chats ->
|
||||
@@ -398,34 +399,23 @@ class ServerController internal constructor(
|
||||
return key
|
||||
}
|
||||
|
||||
fun closePrivateChat(key: ChatKey.Private) = _chats.update { it.close(key) }
|
||||
fun closePrivateChat(key: ChatKey) = _chats.update { it.close(key) }
|
||||
|
||||
fun send(key: ChatKey, text: String) = session.sendChat(scope(key), peer(key), text)
|
||||
fun send(key: ChatKey, text: String) = session.sendChat(key, text)
|
||||
|
||||
/** Shares the file at [uri] in a chat, through myTeamSpeak; failures are told in the chat or as errors. */
|
||||
suspend fun sendFile(key: ChatKey, uri: Uri) {
|
||||
app.openShared(uri)
|
||||
.onSuccess { session.sendAttachment(scope(key), peer(key), app.attachments, it.data, it.size, it.name) }
|
||||
.onSuccess { session.sendAttachment(key, app.attachments, it.data, it.size, it.name) }
|
||||
.onFailure { app.error(it.message ?: "Could not read the file") }
|
||||
}
|
||||
|
||||
private fun scope(key: ChatKey) = when (key) {
|
||||
ChatKey.Server -> ChatScope.SERVER
|
||||
ChatKey.Channel -> ChatScope.CHANNEL
|
||||
is ChatKey.Private -> ChatScope.PRIVATE
|
||||
}
|
||||
|
||||
private fun peer(key: ChatKey) = if (key is ChatKey.Private) key.clientId else 0
|
||||
|
||||
/** Where files shared on this server lie; empty before it is known. */
|
||||
private val room: String get() = session.serverUniqueId().orEmpty()
|
||||
|
||||
fun markRead(key: ChatKey) {
|
||||
if (_chats.value[key]?.unread == true) _chats.update { chats -> chats.update(key) { it.copy(unread = false) } }
|
||||
}
|
||||
|
||||
internal fun requestChat(clientId: Int) {
|
||||
_chatRequest.value = ChatKey.Private(clientId)
|
||||
_chatRequest.value = ChatKey.privateWith(clientId)
|
||||
}
|
||||
|
||||
fun chatRequestHandled() {
|
||||
@@ -434,22 +424,21 @@ class ServerController internal constructor(
|
||||
|
||||
/** Answers a private message from its notification. */
|
||||
internal fun reply(clientId: Int, text: String) {
|
||||
if (session.isConnected) send(ChatKey.Private(clientId), text) else app.notifier.clear(id, clientId)
|
||||
if (session.isConnected) send(ChatKey.privateWith(clientId), text) else app.notifier.clear(id, clientId)
|
||||
}
|
||||
|
||||
private fun append(key: ChatKey, title: String, html: String) {
|
||||
if (html.isEmpty()) return
|
||||
private fun addLine(key: ChatKey, title: String, html: String) {
|
||||
val line = ChatLine(lineIds.incrementAndGet(), html)
|
||||
_chats.update { chats -> chats.update(key, title) { it.append(line, read = viewing == key) } }
|
||||
}
|
||||
|
||||
private fun setHistory(key: ChatKey, title: String, entries: List<String>) {
|
||||
val lines = entries.map { ChatHtml.logEntry(it, room) }.filter { it.isNotEmpty() }
|
||||
.map { ChatLine(lineIds.incrementAndGet(), it) }
|
||||
private fun replaceLines(key: ChatKey, title: String, html: List<String>) {
|
||||
val lines = html.map { ChatLine(lineIds.incrementAndGet(), it) }
|
||||
_chats.update { chats -> chats.update(key, title) { it.copy(lines = lines) } }
|
||||
}
|
||||
|
||||
private fun system(text: String) = append(ChatKey.Server, "", ChatHtml.system(text))
|
||||
/** One of this app's own notices, in the server chat. */
|
||||
private fun system(text: String) = addLine(ChatKey.SERVER, "", ChatHtml.system(text))
|
||||
|
||||
// ---- snapshots ----
|
||||
|
||||
@@ -491,30 +480,33 @@ class ServerController internal constructor(
|
||||
if (structureChanged.getAndSet(false)) roots = session.model().buildTree()
|
||||
_tree.value = ServerTree.of(roots, session.model(), session.contacts().store(),
|
||||
app.settings.colorContacts.get(), app.settings.countryFlags.get(), app.settings.clientRowColors)
|
||||
val serverName = if (session.isConnected) session.model().serverName.orEmpty() else ""
|
||||
val channelName = session.currentChannel()?.name.orEmpty()
|
||||
_chats.update {
|
||||
it.copy(
|
||||
server = it.server.copy(title = serverName.ifBlank { "Server" }),
|
||||
channel = it.channel.copy(title = channelName.ifBlank { "Channel" }),
|
||||
)
|
||||
}
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
|
||||
private inner class Listener : ServerSession.Listener {
|
||||
override fun onSessionChanged() = refresh()
|
||||
override fun onSystemMessage(text: String) = system(text)
|
||||
|
||||
override fun onOwnChat(scope: ChatScope, peerClientId: Int, peerName: String, text: String) {
|
||||
val self = session.model().getClient(session.selfClientId())?.nickname ?: app.settings.nickname.get()
|
||||
val html = ChatHtml.message(session.selfClientId(), session.selfUniqueId(), "$self (you)", text, room)
|
||||
append(key(scope, peerClientId), peerName, html)
|
||||
if (scope == ChatScope.PRIVATE) app.notifier.sent(id, peerClientId, ChatHtml.plainMessage(text))
|
||||
override fun append(key: ChatKey, title: String, html: String) = addLine(key, title, html)
|
||||
|
||||
override fun setHistory(key: ChatKey, title: String, html: List<String>) = replaceLines(key, title, html)
|
||||
|
||||
override fun rename(key: ChatKey, name: String) = _chats.update {
|
||||
when (key.scope()) {
|
||||
ChatScope.SERVER -> it.copy(server = it.server.copy(title = name.ifBlank { "Server" }))
|
||||
ChatScope.CHANNEL -> it.copy(channel = it.channel.copy(title = name.ifBlank { "Channel" }))
|
||||
else -> it
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStatus(status: String) {}
|
||||
override fun closePrivateChats() = _chats.update { it.copy(private = emptyList()) }
|
||||
|
||||
override fun onPrivateMessage(fromClientId: Int, fromName: String, message: String) {
|
||||
if (!app.foreground) app.notifier.received(id, fromClientId, fromName, ChatHtml.plainMessage(message))
|
||||
}
|
||||
|
||||
override fun onPrivateMessageSent(toClientId: Int, message: String) =
|
||||
app.notifier.sent(id, toClientId, ChatHtml.plainMessage(message))
|
||||
|
||||
override fun onConnected() {
|
||||
network.onConnected()
|
||||
@@ -525,7 +517,6 @@ class ServerController internal constructor(
|
||||
override fun onDisconnected(reason: String?) {
|
||||
network.onDisconnected()
|
||||
app.notifier.clearServer(id)
|
||||
_chats.update { it.copy(private = emptyList()) }
|
||||
app.disconnected(this@ServerController)
|
||||
scheduleTree(structure = true)
|
||||
refresh()
|
||||
@@ -548,20 +539,6 @@ class ServerController internal constructor(
|
||||
|
||||
override fun onIconsUpdated() = images.loaded()
|
||||
|
||||
override fun onChat(scope: ChatScope, fromClientId: Int, fromUniqueId: String?, fromName: String, message: String) {
|
||||
append(key(scope, fromClientId), fromName, ChatHtml.message(fromClientId, fromUniqueId, fromName, message, room))
|
||||
if (scope == ChatScope.PRIVATE && !app.foreground) {
|
||||
app.notifier.received(id, fromClientId, fromName, ChatHtml.plainMessage(message))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onChatHistory(scope: ChatScope, htmlEntries: List<String>) {
|
||||
if (scope != ChatScope.PRIVATE) setHistory(key(scope, 0), "", htmlEntries)
|
||||
}
|
||||
|
||||
override fun onPrivateChatHistory(clientId: Int, nickname: String, htmlEntries: List<String>) =
|
||||
setHistory(ChatKey.Private(clientId), nickname, htmlEntries)
|
||||
|
||||
override fun onTalkStateChanged(clientId: Int, talking: Boolean) {
|
||||
if (clientId == session.selfClientId() && micOwner) app.talking(talking)
|
||||
scheduleTree(structure = false)
|
||||
@@ -570,16 +547,8 @@ class ServerController internal constructor(
|
||||
override fun onError(message: String) = app.error(message)
|
||||
|
||||
override fun onPoke(fromName: String, message: String) = app.poked(fromName, message)
|
||||
|
||||
override fun onServerLog(message: String) = append(ChatKey.Server, "", ChatHtml.serverLog(message))
|
||||
override fun onServerLogHtml(html: String) = append(ChatKey.Server, "", ChatHtml.logEntry(html, room))
|
||||
}
|
||||
|
||||
private fun key(scope: ChatScope, peerClientId: Int): ChatKey = when (scope) {
|
||||
ChatScope.SERVER -> ChatKey.Server
|
||||
ChatScope.CHANNEL -> ChatKey.Channel
|
||||
ChatScope.PRIVATE -> ChatKey.Private(peerClientId)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TREE_FRAME_MS = 30L
|
||||
|
||||
@@ -17,9 +17,9 @@ import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import eu.lixko.ts3j.client.android.ChatKey
|
||||
import eu.lixko.ts3j.client.android.ServerController
|
||||
import eu.lixko.ts3j.client.android.SessionController
|
||||
import eu.lixko.ts3j.client.chat.ChatKey
|
||||
import eu.lixko.ts3j.client.myts.ChatAttachment
|
||||
import eu.lixko.ts3j.client.myts.MyTsException
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -49,11 +49,11 @@ import androidx.compose.ui.input.key.type
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import eu.lixko.ts3j.client.android.ChatKey
|
||||
import eu.lixko.ts3j.client.android.ChatLine
|
||||
import eu.lixko.ts3j.client.android.ChatMatch
|
||||
import eu.lixko.ts3j.client.android.Chats
|
||||
import eu.lixko.ts3j.client.android.Conversation
|
||||
import eu.lixko.ts3j.client.chat.ChatKey
|
||||
import eu.lixko.ts3j.client.text.TextQuery
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.filter
|
||||
@@ -65,7 +65,7 @@ fun ChatPane(
|
||||
selected: Conversation,
|
||||
canSend: Boolean,
|
||||
onSelect: (ChatKey) -> Unit,
|
||||
onClose: (ChatKey.Private) -> Unit,
|
||||
onClose: (ChatKey) -> Unit,
|
||||
onSend: (String) -> Unit,
|
||||
/** Picks a file to share in [selected]. */
|
||||
onAttach: () -> Unit,
|
||||
@@ -162,7 +162,7 @@ private fun ConversationChip(
|
||||
conversation: Conversation,
|
||||
selected: Boolean,
|
||||
onSelect: (ChatKey) -> Unit,
|
||||
onClose: (ChatKey.Private) -> Unit,
|
||||
onClose: (ChatKey) -> Unit,
|
||||
) {
|
||||
val key = conversation.key
|
||||
FilterChip(
|
||||
@@ -170,7 +170,7 @@ private fun ConversationChip(
|
||||
onClick = { onSelect(key) },
|
||||
label = { Text(conversation.title, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
leadingIcon = if (conversation.unread) ({ Badge() }) else null,
|
||||
trailingIcon = if (key is ChatKey.Private) ({
|
||||
trailingIcon = if (key.isPrivate) ({
|
||||
Icon(Icons.Filled.Close, "Close", Modifier.size(18.dp).clickable { onClose(key) })
|
||||
}) else null,
|
||||
)
|
||||
|
||||
@@ -73,7 +73,6 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.LifecycleResumeEffect
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import eu.lixko.ts3j.client.android.ChannelItem
|
||||
import eu.lixko.ts3j.client.android.ChatKey
|
||||
import eu.lixko.ts3j.client.android.ChatMatch
|
||||
import eu.lixko.ts3j.client.android.ServerController
|
||||
import eu.lixko.ts3j.client.android.SessionController
|
||||
@@ -81,6 +80,8 @@ import eu.lixko.ts3j.client.android.SessionState
|
||||
import eu.lixko.ts3j.client.android.TreeFind
|
||||
import eu.lixko.ts3j.client.android.TreeSwipe
|
||||
import eu.lixko.ts3j.client.android.findIn
|
||||
import eu.lixko.ts3j.client.chat.ChatKey
|
||||
import eu.lixko.ts3j.client.chat.ChatScope
|
||||
import eu.lixko.ts3j.client.config.Settings
|
||||
import eu.lixko.ts3j.client.config.WhisperList
|
||||
import eu.lixko.ts3j.client.myts.ChatAttachment
|
||||
@@ -90,17 +91,17 @@ import kotlinx.coroutines.launch
|
||||
|
||||
private val ChatKeySaver = Saver<ChatKey, String>(
|
||||
save = { key ->
|
||||
when (key) {
|
||||
ChatKey.Server -> "server"
|
||||
ChatKey.Channel -> "channel"
|
||||
is ChatKey.Private -> "private:${key.clientId}"
|
||||
when (key.scope()) {
|
||||
ChatScope.SERVER -> "server"
|
||||
ChatScope.CHANNEL -> "channel"
|
||||
ChatScope.PRIVATE -> "private:${key.clientId()}"
|
||||
}
|
||||
},
|
||||
restore = { saved ->
|
||||
when {
|
||||
saved == "server" -> ChatKey.Server
|
||||
saved.startsWith("private:") -> ChatKey.Private(saved.substringAfter(':').toInt())
|
||||
else -> ChatKey.Channel
|
||||
saved == "server" -> ChatKey.SERVER
|
||||
saved.startsWith("private:") -> ChatKey.privateWith(saved.substringAfter(':').toInt())
|
||||
else -> ChatKey.CHANNEL
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -144,7 +145,7 @@ fun ServerScreen(
|
||||
var showChat by rememberSaveable { mutableStateOf(false) }
|
||||
// Out here, so the tree keeps its place while the chat or another screen replaces it.
|
||||
val treeList = rememberLazyListState()
|
||||
var chatKey by rememberSaveable(stateSaver = ChatKeySaver) { mutableStateOf<ChatKey>(ChatKey.Channel) }
|
||||
var chatKey by rememberSaveable(stateSaver = ChatKeySaver) { mutableStateOf<ChatKey>(ChatKey.CHANNEL) }
|
||||
val conversation = chats[chatKey] ?: chats.channel
|
||||
var clientSheet by remember { mutableStateOf<Int?>(null) }
|
||||
var clientSheetFromTree by remember { mutableStateOf(false) }
|
||||
@@ -418,7 +419,7 @@ fun ServerScreen(
|
||||
canSend = state.connected,
|
||||
onSelect = { chatKey = it },
|
||||
onClose = { key ->
|
||||
if (chatKey == key) chatKey = ChatKey.Channel
|
||||
if (chatKey == key) chatKey = ChatKey.CHANNEL
|
||||
server.closePrivateChat(key)
|
||||
},
|
||||
onSend = { server.send(conversation.key, it) },
|
||||
|
||||
Reference in New Issue
Block a user