diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 733a0b2c..79276c4a 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -479,4 +479,16 @@ Waiting on %1$s since %2$s. If they have stopped answering, any member can abandon the ceremony. No key is made, and the group can start again. Open the room + Admin + Founding members aren't admins + %1$s took part in the key ceremony that made this group, but this room doesn't list them as admins. + Make them admins + Asked the group to make them admins + Not every founding member was invited + %1$s took part in the key ceremony that made this group, but the invitation to this room never went out. + Invite them + Invited them to the room + Some of them haven't published a key package yet + Admin room + Every member of this group is an admin there. 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 75987d93..0b28ef4f 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 @@ -8,6 +8,7 @@ import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import mobi.giza.compose.managers.FoundingAdmins +import mobi.giza.compose.managers.SharedKeyDerivation import mobi.giza.compose.managers.CeremonyFollowThrough import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate @@ -114,6 +115,19 @@ class DatabaseChatRepository( emptySet() } + override suspend fun adminRoomOf(chatRoomId: String): String? = try { + // The room's own ceremony, not a subgroup's it hosted: a subgroup's key + // makes the subgroup's room, which is not this group's admin room. + database.dkgSessionDao().getLatestSessionFor(chatRoomId, parentChatRoomId = null) + ?.takeIf { it.stage == DkgRitualStage.COMPLETE } + ?.thresholdPublicKey + ?.let { SharedKeyDerivation.marmotGroupId(it) } + ?.takeIf { it != chatRoomId && database.chatRoomDao().findChatRoomById(it) != null } + } catch (e: Throwable) { + logger.e("Error finding the admin room of $chatRoomId", e) + null + } + override suspend fun followThroughCeremonies(keyPair: KeyPair) = CeremonyFollowThrough.observe(database = database, chatRepository = this, keyPair = keyPair) 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 20d1eee1..894c444e 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt @@ -237,6 +237,14 @@ interface ChatRepository { /** The founders [localChatRoom]'s founding invite never reached; empty when there are none to reach. */ suspend fun uninvitedFounders(localChatRoom: LocalChatRoom): Set + /** + * The admin room [chatRoomId]'s own key ceremony made, if this device holds + * it: where the members of a robust group's NIP-17 room are admins. Null for + * a room with no finished ceremony of its own, and for one whose admin room + * this device does not hold. + */ + suspend fun adminRoomOf(chatRoomId: String): String? + /** * Takes every key ceremony [keyPair] coordinated from a key to a working room, * until cancelled: asks the group to sign its key state (a subgroup's parent to @@ -489,6 +497,8 @@ interface ChatRepository { override suspend fun uninvitedFounders(localChatRoom: LocalChatRoom): Set = emptySet() + override suspend fun adminRoomOf(chatRoomId: String): String? = null + override suspend fun sendChatMessage( text: String, 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..6e7340ad 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 @@ -14,6 +14,9 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material.icons.Icons +import mobi.giza.compose.managers.MarmotGroupCreation +import androidx.compose.material3.CardDefaults +import androidx.compose.material.icons.filled.AdminPanelSettings import androidx.compose.material.icons.filled.Key import androidx.compose.material.icons.filled.AccountTree import androidx.compose.material.icons.filled.Autorenew @@ -81,6 +84,18 @@ 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.every_member_of_this_group_administers_it_there +import mobi.giza.compose.resources.admin_room +import mobi.giza.compose.resources.founding_members_have_no_key_package +import mobi.giza.compose.resources.founding_members_invited +import mobi.giza.compose.resources.invite_them +import mobi.giza.compose.resources.s_took_part_in_the_key_ceremony_but_were_not_invited +import mobi.giza.compose.resources.not_every_founding_member_was_invited +import mobi.giza.compose.resources.founding_members_made_admins_requested +import mobi.giza.compose.resources.make_them_admins +import mobi.giza.compose.resources.s_took_part_in_the_key_ceremony_but_are_not_admins +import mobi.giza.compose.resources.founding_members_are_not_admins +import mobi.giza.compose.resources.admin import mobi.giza.compose.resources.run_by_members import mobi.giza.compose.resources.parent_group import mobi.giza.compose.resources.a_subgroup @@ -185,6 +200,9 @@ fun ChatRoomDetailScreen( val removalRequested = stringResource(Res.string.member_removal_requested) val rotationRequested = stringResource(Res.string.share_rotation_requested) val sharesActionSent = stringResource(Res.string.shares_action_sent) + val madeAdminsRequested = stringResource(Res.string.founding_members_made_admins_requested) + val foundersInvited = stringResource(Res.string.founding_members_invited) + val foundersHaveNoKeyPackage = stringResource(Res.string.founding_members_have_no_key_package) // Offered after removing a member who still holds a share of the room's key: // the removal took them out of the room, not out of the key @@ -347,6 +365,94 @@ fun ChatRoomDetailScreen( } } + // The founding members, where the room is not as the + // ceremony that made its key left it: founders its + // policy does not list as admins, or founders its + // founding invite never reached. Under the key and the + // parent, because it is about the same thing -- who the + // group that made this room is. docs/robust-group-admins.md, + // Phase 5. + if (chatRoomDetailUIState.missingFoundingAdmins.isNotEmpty()) { + item { + FoundingMembersCard( + title = stringResource(Res.string.founding_members_are_not_admins), + body = stringResource( + Res.string.s_took_part_in_the_key_ceremony_but_are_not_admins, + chatRoomDetailUIState.founderNamesFor(chatRoomDetailUIState.missingFoundingAdmins), + ), + // An admin of a room the app governs: the + // repair is a commit only they can make. + actionLabel = stringResource(Res.string.make_them_admins) + .takeIf { chatRoomDetailUIState.canGovern }, + isActionPending = chatRoomDetailViewModel.isActionPending.value, + onAction = { + chatRoomDetailViewModel.addMissingFoundingAdmins( + localChatRoom = chatRoomDetailUIState.localChatRoom, + missing = chatRoomDetailUIState.missingFoundingAdmins, + ) { failure -> notify(failure ?: madeAdminsRequested) } + }, + ) + } + } + + if (chatRoomDetailUIState.uninvitedFounders.isNotEmpty()) { + item { + FoundingMembersCard( + title = stringResource(Res.string.not_every_founding_member_was_invited), + body = stringResource( + Res.string.s_took_part_in_the_key_ceremony_but_were_not_invited, + chatRoomDetailUIState.founderNamesFor(chatRoomDetailUIState.uninvitedFounders), + ), + actionLabel = stringResource(Res.string.invite_them), + isActionPending = chatRoomDetailViewModel.isActionPending.value, + onAction = { + chatRoomDetailViewModel.inviteFounders(chatRoomId) { invite -> + notify( + when (invite) { + is MarmotGroupCreation.FoundingInvite.BlockedOn -> foundersHaveNoKeyPackage + is MarmotGroupCreation.FoundingInvite.Failed -> invite.reason + else -> foundersInvited + } + ) + } + }, + ) + } + } + + // A robust group's NIP-17 room has no admins of its own; + // its members are admins of the room its key made. + chatRoomDetailUIState.adminRoomId?.let { adminRoomId -> + item { + Card( + modifier = Modifier.fillMaxWidth(), + onClick = { + onNavigateToRoute.invoke( + ChatRoomDetailRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = adminRoomId, + relayHint = relayHint + ) + ) + } + ) { + ListItem( + leadingContent = { + Icon(Icons.Default.AdminPanelSettings, contentDescription = Decorative) + }, + trailingContent = { + Icon(Icons.Default.ChevronRight, contentDescription = Decorative) + }, + headlineContent = { + Text(text = stringResource(Res.string.admin_room)) + }, + supportingContent = { + Text(text = stringResource(Res.string.every_member_of_this_group_administers_it_there)) + } + ) + } + } + } item { chatRoomDetailUIState.localChatRoom.chatRoom.description?.let { Text( @@ -729,10 +835,21 @@ fun ChatRoomDetailScreen( ) }, supportingContent = { - localParticipant.profile?.about?.let { - Text( - text = it - ) + Column { + // From the group's admin policy, through + // `AdminFlags` -- docs/robust-group-admins.md. + if (localParticipant.participant.adminAt != null) { + Text( + text = stringResource(Res.string.admin), + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.primary, + ) + } + localParticipant.profile?.about?.let { + Text( + text = it + ) + } } } @@ -1329,4 +1446,61 @@ private fun ChatRoomMessagingScreenPreview() { ) } } -} \ No newline at end of file +} + +/** + * Something about this room's founding members that is not as the ceremony + * left it -- founders it does not list as admins, or founders its invite never + * reached -- and, where this device can, the one action that repairs it. + * + * On the error container: the room works, but not as the group that made it + * agreed, and that is worth more than a supporting line. docs/robust-group-admins.md, + * Phase 5. + */ +@Composable +private fun FoundingMembersCard( + title: String, + body: String, + actionLabel: String?, + isActionPending: Boolean, + onAction: () -> Unit, +) { + Card( + modifier = Modifier.fillMaxWidth(), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.errorContainer, + contentColor = MaterialTheme.colorScheme.onErrorContainer, + ) + ) { + Column( + modifier = Modifier.padding(MaterialTheme.spacing.containerPadding), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap), + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap), + ) { + Icon(Icons.Default.AdminPanelSettings, contentDescription = Decorative) + Text(text = title, style = MaterialTheme.typography.titleSmall) + } + + Text(text = body, style = MaterialTheme.typography.bodyMedium) + + actionLabel?.let { label -> + TextButton( + onClick = onAction, + enabled = !isActionPending, + colors = ButtonDefaults.textButtonColors( + contentColor = MaterialTheme.colorScheme.onErrorContainer + ), + ) { + Text(label) + } + } + } + } +} + +/** The founders [publicKeys] names, as the screen calls them, in a stable order. */ +private fun ChatRoomDetailUIState.Loaded.founderNamesFor(publicKeys: Set): String = + publicKeys.map { founderNames[it] ?: it }.sorted().joinToString(", ") 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 83a7739b..a857793a 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 @@ -19,6 +19,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO import kotlinx.coroutines.launch import mobi.giza.compose.managers.FoundingAdmins +import mobi.giza.compose.managers.MarmotGroupCreation import mobi.giza.compose.database.model.intermdiate.LocalChatRoom import mobi.giza.compose.database.model.types.MarmotReindexReport import mobi.giza.compose.managers.WalletRefusal @@ -98,6 +99,14 @@ class ChatRoomDetailViewModel( ?.let { Event.fromJsonOrNull(it) } ?.let(WalletRefusal::isGuarded) == true + // The founding members, against what the ceremony that made the + // room's key said: who the policy leaves out, and who the founding + // invite never reached. docs/robust-group-admins.md, Phases 3 and 4. + val missingFoundingAdmins = (chatRepository.foundingAdmins(chatRoomId) as? FoundingAdmins.Check.Missing) + ?.publicKeys + .orEmpty() + val uninvitedFounders = chatRepository.uninvitedFounders(localChatRoom) + ChatRoomDetailUIState.Loaded( localChatRoom = localChatRoom, groupKeyState = groupKeyState, @@ -109,9 +118,13 @@ class ChatRoomDetailViewModel( canGovern = canGovern, isDisbanded = isDisbanded, shares = chatRepository.sharesSummary(localChatRoom), - missingFoundingAdmins = (chatRepository.foundingAdmins(chatRoomId) as? FoundingAdmins.Check.Missing) - ?.publicKeys - .orEmpty(), + missingFoundingAdmins = missingFoundingAdmins, + uninvitedFounders = uninvitedFounders, + founderNames = (missingFoundingAdmins + uninvitedFounders).associateWith { publicKey -> + nostrRepository.getProfileWithPublicKey(publicKey)?.humanReadableNameOrPubkey() + ?: publicKey.take(SHORTENED_PUBLIC_KEY_LENGTH) + }, + adminRoomId = if (localChatRoom.chatRoom.mlsGroupState == null) chatRepository.adminRoomOf(chatRoomId) else null, ) } } @@ -146,10 +159,36 @@ class ChatRoomDetailViewModel( * warning stays until then. docs/robust-group-admins.md, Phase 3. */ fun addMissingFoundingAdmins(localChatRoom: LocalChatRoom, missing: Set, onDone: (String?) -> Unit = {}) { + if (isActionPending.value) return + isActionPending.value = true viewModelScope.launch(Dispatchers.IO) { val failure = runCatching { chatRepository.addAdmins(localChatRoom, missing) }.exceptionOrNull() initiateChatRoomDetail() - viewModelScope.launch(Dispatchers.Main) { onDone(failure?.message) } + viewModelScope.launch(Dispatchers.Main) { + isActionPending.value = false + onDone(failure?.message) + } + } + } + + /** + * Finishes the room's founding invite, and reloads the screen. Reports null + * when it went out, or why not -- naming the founders still without a key + * package, since only they can publish one. docs/robust-group-admins.md, + * Phase 4. + */ + fun inviteFounders(chatRoomId: String, onDone: (MarmotGroupCreation.FoundingInvite) -> Unit = {}) { + // Waits on relays for key packages, so a second tap would ask twice. + if (isActionPending.value) return + isActionPending.value = true + viewModelScope.launch(Dispatchers.IO) { + val invite = runCatching { chatRepository.finishFoundingInvite(chatRoomId) } + .getOrElse { MarmotGroupCreation.FoundingInvite.Failed(it.message ?: "Couldn't invite them. Please try again.") } + initiateChatRoomDetail() + viewModelScope.launch(Dispatchers.Main) { + isActionPending.value = false + onDone(invite) + } } } @@ -288,6 +327,9 @@ class ChatRoomDetailViewModel( companion object { private const val TAG = "ChatRoomDetailViewModel" + /** Enough of a key to tell two founders apart when no profile is stored. */ + private const val SHORTENED_PUBLIC_KEY_LENGTH = 12 + fun factory( activeUserPublicKey: HexKey, chatRoomId: String, 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 20b85009..a7fe3b32 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 @@ -91,6 +91,26 @@ sealed interface ChatRoomDetailUIState { * `FoundingAdmins.check`; docs/robust-group-admins.md, Phase 3. */ val missingFoundingAdmins: Set = emptySet(), + /** + * Founding members this room's founding invite never reached -- only ever + * non-empty on the device that made the room, while it is still at epoch 0. + * See `MarmotGroupCreation.uninvitedFounders`; docs/robust-group-admins.md, + * Phase 4. + */ + val uninvitedFounders: Set = emptySet(), + /** + * What to call the founders the two sets above name. They need not be in + * this room -- an uninvited one is not, by definition -- so the room's own + * rows cannot be asked. + */ + val founderNames: Map = emptyMap(), + /** + * The admin room this group's ceremony made, when this is the group's + * NIP-17 room and this device holds it. A NIP-17 room has no admins of its + * own; this is where its members are admins. docs/robust-group-admins.md, + * Phase 5. + */ + val adminRoomId: String? = null, ): ChatRoomDetailUIState data class Error(