diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 733a0b2c..258025f3 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -106,6 +106,7 @@ Key recovery Learn more Leave group + Couldn't leave the group Remove from group Disband group This group was disbanded. Nothing more can be sent into it, and nobody can rejoin. diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt index 2b3ac801..ee1573dd 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt @@ -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) }, ) } ) { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt index 3cb13a0e..f66f7f62 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt @@ -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) } + } } } diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModelJvmTest.kt new file mode 100644 index 00000000..cdc80b66 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModelJvmTest.kt @@ -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() + 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") + } +}