From b09fd320523d12f5a4d333ed9f24fe2701dadd2e Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 20 Nov 2026 09:25:19 +0200 Subject: [PATCH] feat: every viewer gets the member rows' options, with their reasons (member-row-actions, Phase 2) ChatRepository.memberActionOptions is the one loader of what each row offers: the policy over the group's state, with having left and whose devices a keyed room waits on added from the database. A NIP-17 room and a legacy room answer from their kind. The view model asks for every room, not only one this device governs; the row still shows only what is open until the explanation screen exists. Co-Authored-By: Claude Opus 5.5 --- .../repository/DatabaseChatRepository.kt | 34 +++++++++++++++++++ .../giza/compose/repository/ChatRepository.kt | 14 ++++++++ .../ui/composable/ChatRoomDetailScreen.kt | 3 ++ .../ui/view/model/ChatRoomDetailViewModel.kt | 6 +++- .../ui/view/state/ChatRoomDetailUIState.kt | 9 +++-- .../compose/managers/AdminChangesJvmTest.kt | 33 ++++++++++++++++++ .../ui/composable/ChatRoomMemberRowJvmTest.kt | 3 +- 7 files changed, 98 insertions(+), 4 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt index d1bd0264..ced7d949 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt @@ -15,6 +15,9 @@ import mobi.giza.compose.managers.GovernanceGate import mobi.giza.compose.managers.PromotionManager import mobi.giza.compose.managers.GovernanceFollowThrough import mobi.giza.compose.managers.GovernanceSupport +import mobi.giza.compose.managers.MemberActionContext +import mobi.giza.compose.managers.MemberActionPolicy +import mobi.giza.compose.managers.MemberActionRoom import mobi.giza.compose.managers.MarmotInboundManager import mobi.giza.compose.managers.AdminFlags import mobi.giza.compose.managers.FoundingAdmins @@ -58,6 +61,7 @@ import mobi.giza.compose.identity.Identity import mobi.giza.compose.identity.toKeyPair import mobi.giza.compose.managers.ShareRefreshManager import mobi.giza.compose.ui.view.state.GovernanceStatus +import mobi.giza.compose.ui.view.state.MemberActionOption import mobi.giza.compose.managers.RotationHold import mobi.giza.compose.ui.view.state.SharesSummary import mobi.giza.compose.ui.view.state.RotationPreview @@ -725,6 +729,36 @@ class DatabaseChatRepository( else GovernanceStatus.Waiting(missing) } + override suspend fun memberActionOptions(localChatRoom: LocalChatRoom): Map> { + val chatRoom = localChatRoom.chatRoom + val me = chatRoom.userPublicKey + val rows = localChatRoom.localParticipants.map { it.participant.participantPublicKey } + val hasLeft = chatRoom.leftGroupAt != null + // Off the current profile there is no admin policy to read: the room's kind + // is the whole answer, and the participants' flags say which role to name. + val flaggedAdmins = localChatRoom.localParticipants + .filter { it.participant.adminAt != null } + .map { it.participant.participantPublicKey } + .toSet() + val context = when { + chatRoom.mlsGroupState == null -> + MemberActionContext(MemberActionRoom.NIP17, me, admins = flaggedAdmins, members = emptySet(), hasLeft = hasLeft) + chatRoom.marmotProfile() != MarmotProfile.CURRENT -> + MemberActionContext(MemberActionRoom.LEGACY, me, admins = flaggedAdmins, members = emptySet(), hasLeft = hasLeft) + else -> { + // A row whose state this build cannot read has no rules to read either. + val group = runCatching { chatRoom.toMlsGroup() }.getOrNull() ?: return emptyMap() + val context = AdminGovernance.context(group, me, AdminGovernance.adminFloor(database, chatRoom.id, group)) + context.copy( + hasLeft = hasLeft, + // Only asked when it is the answer: a keyed room not yet governed. + waitingOn = if (context.isKeyed && !context.governed) GovernanceSupport.missing(database, chatRoom.id, group) else emptySet(), + ) + } + } + return MemberActionPolicy.options(context, rows) + } + override suspend fun requireSignatures(localChatRoom: LocalChatRoom) { database.marmotOutboundDao().requireSignatures(localChatRoom) } diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt index e2eb7a0e..3b348614 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt @@ -24,7 +24,9 @@ import mobi.giza.compose.managers.MarmotGroupCreation import mobi.giza.compose.managers.SubgroupManager import mobi.giza.compose.nostr.MarmotProfile import mobi.giza.compose.managers.PromotionManager +import mobi.giza.compose.managers.MemberActionPolicy import mobi.giza.compose.ui.view.state.GovernanceStatus +import mobi.giza.compose.ui.view.state.MemberActionOption import mobi.giza.compose.ui.view.state.SharesSummary import mobi.giza.compose.ui.view.state.RotationPreview @@ -318,6 +320,16 @@ interface ChatRepository { */ suspend fun governanceStatus(chatRoomId: String): GovernanceStatus? + /** + * What each member row of [localChatRoom] offers, and why an item is not open: + * [MemberActionPolicy] over the room's state and what only the database knows. + * Every room and every viewer gets an answer -- a member who is not an admin is + * told why, not shown nothing. The detail screen and the screen that explains a + * closed action both read it here, so they cannot disagree. + * docs/member-row-actions.md, Phase 2. + */ + suspend fun memberActionOptions(localChatRoom: LocalChatRoom): Map> + /** Makes a keyed room's changes need the group's signature. Throws with words a person can act on. */ suspend fun requireSignatures(localChatRoom: LocalChatRoom) @@ -626,6 +638,8 @@ interface ChatRepository { override suspend fun governanceStatus(chatRoomId: String): GovernanceStatus? = null + override suspend fun memberActionOptions(localChatRoom: LocalChatRoom): Map> = emptyMap() + override suspend fun requireSignatures(localChatRoom: LocalChatRoom) = Unit override suspend fun promoteMember(localChatRoom: LocalChatRoom, memberPublicKey: HexKey) = Unit 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 bd8af3ab..a39a74d2 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 @@ -1092,6 +1092,9 @@ fun ChatRoomDetailScreen( trailingContent = { val memberPublicKey = localParticipant.participant.participantPublicKey val actions = chatRoomDetailUIState.memberActions[memberPublicKey].orEmpty() + .filter { it.isAvailable } + .map { it.action } + .toSet() if (actions.isNotEmpty()) { // What this device may do to this member, decided by the // view model from the group's state. Every one is a commit 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 21fc93d2..ca35a249 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 @@ -150,7 +150,11 @@ class ChatRoomDetailViewModel( } else { emptySet() }, - memberActions = group?.takeIf { canGovern }?.let { AdminGovernance.memberActions(it, me, chatRepository.adminFloor(chatRoomId)) }.orEmpty(), + // For every viewer, not only an admin: what cannot be done says why. + // docs/member-row-actions.md, Phase 2. + memberActions = runCatching { chatRepository.memberActionOptions(localChatRoom) } + .onFailure { if (it is CancellationException) throw it else logger.w(throwable = it) { "Couldn't read $chatRoomId's member actions" } } + .getOrElse { emptyMap() }, adminLeave = group?.takeIf { roomIsOpen }?.let { AdminGovernance.adminLeave(it, me) }, governance = runCatching { chatRepository.governanceStatus(chatRoomId) } .onFailure { if (it is CancellationException) throw it } diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/ChatRoomDetailUIState.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/ChatRoomDetailUIState.kt index 916e6703..450d25a2 100755 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/ChatRoomDetailUIState.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/ChatRoomDetailUIState.kt @@ -104,8 +104,13 @@ sealed interface ChatRoomDetailUIState { * rotation is behind its flag. */ val shareHolders: Set = emptySet(), - /** What this device may do to each member, by public key; absent means nothing. */ - val memberActions: Map> = emptyMap(), + /** + * What each member row's menu offers, by public key, open or not, with the + * reasons an item is not; absent means no menu. Every other member has an + * entry whatever the viewer's role, and this device's own row has one only + * when stepping down is open. docs/member-row-actions.md, D1 and D2. + */ + val memberActions: Map> = emptyMap(), /** How **Leave group** goes when this device is an admin; null when it is not. */ val adminLeave: AdminLeave? = null, /** Where a keyed room stands on needing the group's signature; null for any other room. */ diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/AdminChangesJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/AdminChangesJvmTest.kt index 9d0c2aaf..aff79994 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/AdminChangesJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/AdminChangesJvmTest.kt @@ -37,6 +37,8 @@ import mobi.giza.compose.nostr.MarmotProfile import mobi.giza.compose.nostr.Relays import mobi.giza.compose.ui.view.state.AdminLeave import mobi.giza.compose.ui.view.state.MemberAction +import mobi.giza.compose.ui.view.state.MemberActionBlocker +import mobi.giza.compose.ui.view.state.MemberActionOption /** * Promoting and demoting in a convenient room, which has no key to sign with and @@ -233,6 +235,37 @@ class AdminChangesJvmTest { assertEquals(AdminLeave.STEP_DOWN_FIRST, AdminGovernance.adminLeave(appGroup(), user)) } + /** + * The rows' options as the screen reads them, through the repository: every + * item is listed, and one the group would refuse says why. + * docs/member-row-actions.md, Phase 2. + */ + @Test + fun `the rows' options list every action, and say why one is not open`() = runBlocking { + convenientRoomWithMember() + + assertEquals( + mapOf(member.hex to listOf(MemberActionOption(MemberAction.MAKE_ADMIN), MemberActionOption(MemberAction.REMOVE_FROM_GROUP))), + chatRepository.memberActionOptions(room()), + "the only admin's own row offered to step down, or a member's row was missing", + ) + + chatRepository.promoteMember(room(), member.hex) + okTheLatestCommit() + + val options = chatRepository.memberActionOptions(room()) + assertEquals(listOf(MemberActionOption(MemberAction.STEP_DOWN)), options[user]) + assertEquals( + listOf(MemberActionOption(MemberAction.REMOVE_AS_ADMIN), MemberActionOption(MemberAction.REMOVE_FROM_GROUP)), + options[member.hex], + ) + + // Having left, nothing is open, and every item says so first. + db.chatRoomDao().upsert(room().chatRoom.copy(leftGroupAt = kotlin.time.Clock.System.now())) + val afterLeaving = chatRepository.memberActionOptions(room()).getValue(member.hex) + assertTrue(afterLeaving.all { it.blockers.firstOrNull() == MemberActionBlocker.YouLeft }) + } + @Test fun `an admin with another admin beside them steps down, then leaves`() = runBlocking { convenientRoomWithMember() diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/ChatRoomMemberRowJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/ChatRoomMemberRowJvmTest.kt index cc1c4eae..ca67475a 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/ChatRoomMemberRowJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/ChatRoomMemberRowJvmTest.kt @@ -24,6 +24,7 @@ import mobi.giza.compose.ui.composable.widgets.ProvideSnackbarHost import mobi.giza.compose.ui.theme.GizaTheme import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState import mobi.giza.compose.ui.view.state.MemberAction +import mobi.giza.compose.ui.view.state.MemberActionOption import kotlin.test.Test import kotlin.test.assertEquals @@ -80,7 +81,7 @@ class ChatRoomMemberRowJvmTest { onNavigateToRoute = {}, state = state().copy( adminPublicKeys = setOf(activeUserPublicKey), - memberActions = mapOf(named to setOf(MemberAction.MAKE_ADMIN, MemberAction.REMOVE_FROM_GROUP)), + memberActions = mapOf(named to listOf(MemberActionOption(MemberAction.MAKE_ADMIN), MemberActionOption(MemberAction.REMOVE_FROM_GROUP))), ), ) { onAllNodesWithContentDescription("Member actions").assertCountEquals(1)