fix: an admin's refused leave is shown, not thrown out of the view model (member-admin-promotion, Phase 10a)

leaveGroup called the repository inside viewModelScope.launch and caught
nothing, so "An admin cannot leave; hand over admin first" escaped the
coroutine: never shown, and on Android a crash. It now goes to an onFailed
callback the screen reports through the notifier, as disbandGroup does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-20 01:07:57 +02:00
1 parent 8088efd732
commit 2ed83c55ab
4 files changed
+106 -12

No files matched your search

@@ -106,6 +106,7 @@
<string name="key_recovery">Key recovery</string>
<string name="learn_more">Learn more</string>
<string name="leave_group">Leave group</string>
<string name="couldn_t_leave_the_group">Couldn't leave the group</string>
<string name="remove_from_group">Remove from group</string>
<string name="disband_group">Disband group</string>
<string name="group_disbanded">This group was disbanded. Nothing more can be sent into it, and nobody can rejoin.</string>
@@ -81,6 +81,7 @@ import mobi.giza.compose.ui.view.model.ChatRoomDetailViewModel
import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import mobi.giza.compose.ui.theme.spacing
import mobi.giza.compose.resources.couldn_t_leave_the_group
import mobi.giza.compose.resources.run_by_members
import mobi.giza.compose.resources.parent_group
import mobi.giza.compose.resources.a_subgroup
@@ -183,6 +184,7 @@ fun ChatRoomDetailScreen(
// composable, and a lambda passed to onClick is not.
val notify = rememberNotifier(rememberCoroutineScope())
val removalRequested = stringResource(Res.string.member_removal_requested)
val couldNotLeave = stringResource(Res.string.couldn_t_leave_the_group)
val rotationRequested = stringResource(Res.string.share_rotation_requested)
val sharesActionSent = stringResource(Res.string.shares_action_sent)
@@ -810,7 +812,8 @@ fun ChatRoomDetailScreen(
onClick = {
chatRoomDetailViewModel.leaveGroup(
localChatRoom = chatRoomDetailUIState.localChatRoom,
onPopBackToRoute = onPopBackToRoute
onPopBackToRoute = onPopBackToRoute,
onFailed = { failure -> notify(failure ?: couldNotLeave) },
)
}
) {
@@ -194,23 +194,37 @@ class ChatRoomDetailViewModel(
}
}
/**
* Leaves the group, then leaves the screen. A refusal -- an admin, who has to
* hand over admin first -- goes to [onFailed] in the words the refusal used, and
* the screen stays. It used to escape the coroutine, which nobody saw and which
* on Android is a crash. See docs/member-admin-promotion.md, Phase 10.
*/
fun leaveGroup(
localChatRoom: LocalChatRoom,
onPopBackToRoute: (Route) -> Unit
onPopBackToRoute: (Route) -> Unit,
onFailed: (String?) -> Unit = {},
) {
viewModelScope.launch(Dispatchers.IO) {
chatRepository.leaveChatRoom(
localChatRoom,
onCompletion = {
viewModelScope.launch(Dispatchers.Main) {
onPopBackToRoute.invoke(
HomeRoute(
activeUserPublicKey = activeUserPublicKey,
val failure = runCatching {
chatRepository.leaveChatRoom(
localChatRoom,
onCompletion = {
viewModelScope.launch(Dispatchers.Main) {
onPopBackToRoute.invoke(
HomeRoute(
activeUserPublicKey = activeUserPublicKey,
)
)
)
}
}
}
)
)
}.exceptionOrNull()
if (failure is CancellationException) throw failure
if (failure != null) {
logger.w(throwable = failure) { "Couldn't leave $chatRoomId" }
viewModelScope.launch(Dispatchers.Main) { onFailed(failure.message) }
}
}
}
@@ -0,0 +1,76 @@
package mobi.giza.compose.ui.view.model
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.withTimeout
import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.NostrRepository
import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState
/**
* The room detail screen's view model, where the repository's refusals have to
* become something the screen can say. See docs/member-admin-promotion.md.
*/
@OptIn(ExperimentalCoroutinesApi::class)
class ChatRoomDetailViewModelJvmTest {
@BeforeTest
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
}
@AfterTest
fun tearDown() {
Dispatchers.resetMain()
}
private val room = LocalChatRoom(
chatRoom = ChatRoom(id = "room", userPublicKey = "me", subject = "A room", description = null, mlsGroupState = null),
localParticipants = emptyList(),
)
private fun viewModel(chatRepository: ChatRepository) = ChatRoomDetailViewModel(
chatRoomId = "room",
activeUserPublicKey = "me",
relayHint = null,
initialChatRoomDetailUIState = ChatRoomDetailUIState.Loading,
nostrRepository = NostrRepository.NO_OP_NOSTR_REPOSITORY,
chatRepository = chatRepository,
)
/**
* Phase 10's first step. The refusal used to be thrown inside the view model's
* coroutine and caught by nothing: never shown, and on Android a crash.
*/
@Test
fun `an admin's refused leave reaches the screen in the refusal's own words`() = runBlocking {
val refusal = "An admin cannot leave; hand over admin first"
val repository = object : ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY {
override suspend fun leaveChatRoom(localChatRoom: LocalChatRoom, onCompletion: () -> Unit) {
throw IllegalStateException(refusal)
}
}
val reported = CompletableDeferred<String?>()
var poppedBack = false
viewModel(repository).leaveGroup(
localChatRoom = room,
onPopBackToRoute = { poppedBack = true },
onFailed = { reported.complete(it) },
)
assertEquals(refusal, withTimeout(5_000) { reported.await() })
assertEquals(false, poppedBack, "the screen was left although nothing left the group")
}
}