Queue chat bulk actions through SQL outbox

This commit is contained in:
sevenhill
2026-07-05 22:16:30 +03:00
parent f57faead53
commit dcc74252c6
10 changed files with 443 additions and 50 deletions
@@ -122,9 +122,6 @@ class QMaxRepository(
return
}
withFreshSession(session) {
api.clearChatHistories(it.serverUrl, it.accessToken, ids)
}
messageCache.clearMessages(session, ids)
val updatedChats = cachedChats(session).map { chat ->
if (chat.id in ids) {
@@ -134,6 +131,9 @@ class QMaxRepository(
}
}
messageCache.saveChats(session, orderedChats(updatedChats))
withFreshSession(session) {
api.clearChatHistories(it.serverUrl, it.accessToken, ids)
}
}
suspend fun deleteChats(session: QMaxSession, chatIds: Collection<String>) {
@@ -142,12 +142,12 @@ class QMaxRepository(
return
}
withFreshSession(session) {
api.deleteChats(it.serverUrl, it.accessToken, ids)
}
messageCache.clearMessages(session, ids)
val updatedChats = cachedChats(session).filterNot { it.id in ids }
messageCache.saveChats(session, orderedChats(updatedChats))
withFreshSession(session) {
api.deleteChats(it.serverUrl, it.accessToken, ids)
}
}
suspend fun searchMessages(session: QMaxSession, chatId: String, query: String): List<MessageDto> {
@@ -401,7 +401,6 @@ class QMaxViewModel(private val repository: QMaxRepository) : ViewModel() {
val ids = state.value.selectedChatIds
if (ids.isEmpty()) return@launchLoading
repository.clearChatHistories(session, ids)
val updatedChats = normalizeChats(
state.value.chats.map { chat ->
if (chat.id in ids) {
@@ -422,6 +421,7 @@ class QMaxViewModel(private val repository: QMaxRepository) : ViewModel() {
selectedChatIds = emptySet()
)
persistCurrentChats()
repository.clearChatHistories(session, ids)
refreshChats(showLoading = false)
}
@@ -430,7 +430,6 @@ class QMaxViewModel(private val repository: QMaxRepository) : ViewModel() {
val ids = state.value.selectedChatIds
if (ids.isEmpty()) return@launchLoading
repository.deleteChats(session, ids)
val selectedChatId = state.value.selectedChat?.id
val selectedChatDeleted = selectedChatId != null && selectedChatId in ids
val updatedChats = normalizeChats(state.value.chats.filterNot { it.id in ids })
@@ -451,6 +450,7 @@ class QMaxViewModel(private val repository: QMaxRepository) : ViewModel() {
realtime.joinChat(null)
}
persistCurrentChats()
repository.deleteChats(session, ids)
refreshChats(showLoading = false)
}