diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 7c733a8f..f214324c 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -554,6 +554,10 @@ This will be a separate group with the same name, with a key and room of its own. an unnamed group Open %1$s + Groups + No groups have been made in this conversation yet. + Key ceremony in progress + Has its key, and is waiting for its room New group with these members The new group and its room are called this. Nothing is signed for the group without a quorum of you, and the group outlives any one of you. Creating it starts a key ceremony every member takes part in. 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 d8351c8d..d5b49ada 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 @@ -1,5 +1,6 @@ package mobi.giza.compose.database.repository +import mobi.giza.compose.repository.GroupMadeHere import mobi.giza.compose.repository.AdminRoom import mobi.giza.compose.nostr.GroupPost import mobi.giza.compose.nostr.GroupNostrProfile @@ -169,6 +170,29 @@ class DatabaseChatRepository( emptyList() } + override suspend fun groupsMadeIn(chatRoomId: String): List = try { + val roomSubject = database.chatRoomDao().findChatRoomById(chatRoomId)?.chatRoom?.subject + database.dkgSessionDao().getOwnSessionsForChatRoom(chatRoomId) + .filter { it.stage != DkgRitualStage.FAILED } + .map { ceremony -> + val roomId = ceremony.thresholdPublicKey + ?.takeIf { ceremony.stage == DkgRitualStage.COMPLETE } + ?.let { SharedKeyDerivation.marmotGroupId(it) } + GroupMadeHere( + dkgSessionId = ceremony.id, + name = ceremony.subject ?: roomSubject, + status = when { + roomId == null -> GroupMadeHere.Status.KeyCeremonyRunning + database.chatRoomDao().findChatRoomById(roomId) == null -> GroupMadeHere.Status.WaitingForRoom + else -> GroupMadeHere.Status.Ready(roomId) + }, + ) + } + } catch (e: Throwable) { + logger.e("Error listing the groups made in $chatRoomId", e) + emptyList() + } + override suspend fun reconcileAdminFlags(): Int = AdminFlags.reconcileAll(database) override suspend fun followThroughCeremonies(keyPair: KeyPair) = kotlinx.coroutines.coroutineScope { 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 f30c7b3e..c4ec020f 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt @@ -326,6 +326,14 @@ interface ChatRepository { */ suspend fun adminRoomsOf(chatRoomId: String): List + /** + * Every group the members of NIP-17 room [chatRoomId] have made in it, newest + * first, failed ones left out: each one's ceremony and, once it has a key, the + * room it makes. Empty for a room that has made none, and for a Marmot room, + * which holds no group's ceremony of its own. + */ + suspend fun groupsMadeIn(chatRoomId: String): List + /** * Brings every Marmot room's admin flags in line with its group's policy, for * the rooms joined before the Welcome wrote any. Once per start. See @@ -662,6 +670,8 @@ interface ChatRepository { override suspend fun adminRoomsOf(chatRoomId: String): List = emptyList() + override suspend fun groupsMadeIn(chatRoomId: String): List = emptyList() + override suspend fun reconcileAdminFlags(): Int = 0 diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/GroupMadeHere.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/GroupMadeHere.kt new file mode 100644 index 00000000..86ce575c --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/GroupMadeHere.kt @@ -0,0 +1,29 @@ +package mobi.giza.compose.repository + +/** + * One group a NIP-17 room's members have made in it, as the room's details list it. + * + * The same people share one NIP-17 room however many groups they make -- its id is + * derived from them -- so the room is where each group's ceremony was held rather + * than the group itself. Each of these is one ceremony, and once it has a key, the + * room that key makes (docs/nip17-multiple-ceremonies.md). + * + * [name] is the name the ceremony was opened under, or the room's for a ceremony + * from before names were carried. + */ +data class GroupMadeHere( + val dkgSessionId: String, + val name: String?, + val status: Status, +) { + sealed interface Status { + /** Its ceremony is still running. */ + data object KeyCeremonyRunning : Status + + /** It has its key, and the room the key makes is not on this device yet. */ + data object WaitingForRoom : Status + + /** Its room, [chatRoomId], is on this device. */ + data class Ready(val chatRoomId: String) : Status + } +} 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 02ecd69f..7ed88db6 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 @@ -1,5 +1,14 @@ package mobi.giza.compose.ui.composable +import mobi.giza.compose.ui.composable.widgets.EmptyState +import mobi.giza.compose.resources.new_group_with_these_members +import mobi.giza.compose.resources.waiting_for_its_room +import mobi.giza.compose.resources.key_ceremony_in_progress +import mobi.giza.compose.resources.an_unnamed_group +import mobi.giza.compose.resources.no_groups_have_been_made_in_this_conversation_yet +import mobi.giza.compose.resources.groups +import mobi.giza.compose.ui.composable.navigation.routes.DkgRitualRoute +import mobi.giza.compose.repository.GroupMadeHere import mobi.giza.compose.ui.composable.widgets.LocalCanSign import mobi.giza.compose.resources.propose_event import mobi.giza.compose.ui.composable.navigation.routes.ProposeGroupEventRoute @@ -680,39 +689,35 @@ fun ChatRoomDetailScreen( } } - // A robust group's NIP-17 room has no admins of its own; - // its members are admins of the rooms its keys made, one - // per group they have made here. - chatRoomDetailUIState.adminRooms.forEach { adminRoom -> - val adminRoomId = adminRoom.chatRoomId - item(key = "admin-room-$adminRoomId") { - Card( - modifier = Modifier.fillMaxWidth(), - onClick = { + // A NIP-17 room has no admins of its own, and it is not a + // group either: it is where the groups its members have + // made had their ceremonies, and the same people share one + // however many they make. Each group is listed with where + // it stands; one with a room opens it, where its members + // are admins. docs/nip17-multiple-ceremonies.md, Phase 5. + if (chatRoomDetailUIState.localChatRoom.chatRoom.mlsGroupState == null) { + item(key = "groups-made-here") { + GroupsMadeHere( + groups = chatRoomDetailUIState.groups, + onOpen = { group -> onNavigateToRoute.invoke( - ChatRoomDetailRoute( - activeUserPublicKey = activeUserPublicKey, - chatRoomId = adminRoomId, - relayHint = relayHint - ) + when (val status = group.status) { + is GroupMadeHere.Status.Ready -> ChatRoomDetailRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = status.chatRoomId, + relayHint = relayHint + ) + // The ladder says what a group without + // its room is still waiting on. + else -> DkgRitualRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + dkgSessionId = group.dkgSessionId + ) + } ) } - ) { - ListItem( - leadingContent = { - Icon(Icons.Default.AdminPanelSettings, contentDescription = Decorative) - }, - trailingContent = { - Icon(Icons.Default.ChevronRight, contentDescription = Decorative) - }, - headlineContent = { - Text(text = adminRoom.name ?: stringResource(Res.string.admin_room)) - }, - supportingContent = { - Text(text = stringResource(Res.string.every_member_of_this_group_administers_it_there)) - } - ) - } + ) } } item { @@ -1205,10 +1210,11 @@ fun ChatRoomDetailScreen( // names the ceremony they are about, never from here, // which has no way to say which. // - // To the creation screen, which hands over to the group's - // ceremony if it already has one: the ceremony screen only - // shows a ceremony it is named, and this button has none - // to name. See docs/ceremony-creation.md. + // To the creation screen: the room's first group, or another + // made by the same people beside the ones listed above + // (docs/nip17-multiple-ceremonies.md). The ceremony screen + // only shows a ceremony it is named, and this button has + // none to name. See docs/ceremony-creation.md. if (chatRoomDetailUIState.localChatRoom.chatRoom.mlsGroupState == null) { item { TextButton( @@ -1223,14 +1229,20 @@ fun ChatRoomDetailScreen( ) { Icon( Icons.Default.Key, - contentDescription = "Shared key" + contentDescription = Decorative ) Spacer( modifier = Modifier.width(MaterialTheme.spacing.space125) ) - Text(stringResource(Res.string.shared_key)) + Text( + if (chatRoomDetailUIState.groups.isEmpty()) { + stringResource(Res.string.shared_key) + } else { + stringResource(Res.string.new_group_with_these_members) + } + ) } } } @@ -2380,3 +2392,73 @@ private fun GroupDmRowCard( Card(modifier = Modifier.fillMaxWidth(), onClick = onClick) { content() } } } + +/** + * The groups a NIP-17 room's members have made in it, each with where it stands. + * + * The same people share one NIP-17 room however many groups they make, so the room + * is not the group: each group is a ceremony held here and, once it has a key, a + * room of its own. This used to be one admin-room card, which showed the newest + * group and hid the rest (docs/nip17-multiple-ceremonies.md, Phase 5). + */ +@Composable +internal fun GroupsMadeHere( + groups: List, + onOpen: (GroupMadeHere) -> Unit, +) { + Column( + modifier = Modifier.fillMaxWidth(), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap) + ) { + Text( + text = stringResource(Res.string.groups), + style = MaterialTheme.typography.labelMedium + ) + + if (groups.isEmpty()) { + EmptyState( + message = stringResource(Res.string.no_groups_have_been_made_in_this_conversation_yet), + icon = Icons.Default.AdminPanelSettings, + ) + } + + val unnamed = stringResource(Res.string.an_unnamed_group) + groups.forEach { group -> + Card( + modifier = Modifier.fillMaxWidth(), + onClick = { onOpen(group) } + ) { + ListItem( + leadingContent = { + // The status is said beside it, so the icon only echoes it. + Icon( + imageVector = when (group.status) { + is GroupMadeHere.Status.Ready -> Icons.Default.AdminPanelSettings + else -> Icons.Default.Key + }, + contentDescription = Decorative + ) + }, + trailingContent = { + Icon(Icons.Default.ChevronRight, contentDescription = Decorative) + }, + headlineContent = { + Text(text = group.name?.takeIf { it.isNotBlank() } ?: unnamed) + }, + supportingContent = { + Text( + text = when (group.status) { + GroupMadeHere.Status.KeyCeremonyRunning -> + stringResource(Res.string.key_ceremony_in_progress) + GroupMadeHere.Status.WaitingForRoom -> + stringResource(Res.string.waiting_for_its_room) + is GroupMadeHere.Status.Ready -> + stringResource(Res.string.every_member_of_this_group_administers_it_there) + } + ) + } + ) + } + } + } +} 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 865a58cd..a40d9237 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 @@ -174,7 +174,7 @@ class ChatRoomDetailViewModel( nostrRepository.getProfileWithPublicKey(publicKey)?.humanReadableNameOrPubkey() ?: publicKey.take(SHORTENED_PUBLIC_KEY_LENGTH) }, - adminRooms = if (localChatRoom.chatRoom.mlsGroupState == null) chatRepository.adminRoomsOf(chatRoomId) else emptyList(), + groups = if (localChatRoom.chatRoom.mlsGroupState == null) chatRepository.groupsMadeIn(chatRoomId) else emptyList(), soleAdminName = soleAdminOf(localChatRoom, hasKey = groupKeyState != null)?.let { publicKey -> nostrRepository.getProfileWithPublicKey(publicKey)?.humanReadableNameOrPubkey() ?: publicKey.take(SHORTENED_PUBLIC_KEY_LENGTH) 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 efc96840..4f858ce0 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 @@ -1,6 +1,6 @@ package mobi.giza.compose.ui.view.state -import mobi.giza.compose.repository.AdminRoom +import mobi.giza.compose.repository.GroupMadeHere import com.vitorpamplona.quartz.nip01Core.core.HexKey import mobi.giza.compose.database.model.GroupKeyState import mobi.giza.compose.managers.SubgroupManager @@ -166,12 +166,13 @@ sealed interface ChatRoomDetailUIState { */ val founderNames: Map = emptyMap(), /** - * The admin rooms this room's ceremonies made, when this is a NIP-17 room - * and this device holds them. A NIP-17 room has no admins of its own; these - * are where its members are admins, one per group they have made here. - * docs/robust-group-admins.md, Phase 5, and docs/nip17-multiple-ceremonies.md. + * The groups this room's members have made in it, when this is a NIP-17 + * room: each one's ceremony, and the room its key makes once it has one. A + * NIP-17 room has no admins of its own; those rooms are where its members + * are admins, one per group. docs/robust-group-admins.md, Phase 5, and + * docs/nip17-multiple-ceremonies.md, Phase 5. */ - val adminRooms: List = emptyList(), + val groups: List = emptyList(), /** * Who runs this group alone, when it is one: a Marmot room with no key * behind it, more than two members and one admin -- a group made before diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/CeremonyEntryPointsJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/CeremonyEntryPointsJvmTest.kt index ca4d5cc6..46f86670 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/CeremonyEntryPointsJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/CeremonyEntryPointsJvmTest.kt @@ -11,7 +11,9 @@ import kotlin.test.assertEquals * - group creation (`SelectQuorumViewModel`), a new group's own key; * - the subgroup picker (`SelectSubgroupAdminsViewModel`), a subgroup's key, * held in its parent's room; - * - `CreateSharedKeyViewModel`, an existing NIP-17 group that has no key yet. + * - `CreateSharedKeyViewModel`, a new group from the members of an existing NIP-17 + * room: its first, or another beside the ones they have + * (docs/nip17-multiple-ceremonies.md). * * A fourth used to exist: the ceremony screen offered "Start key ceremony" from * the same button slot its approvals used, and a member coming back from diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GroupsInOneRoomJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GroupsInOneRoomJvmTest.kt index 96cfc445..66459de0 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GroupsInOneRoomJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/GroupsInOneRoomJvmTest.kt @@ -9,6 +9,7 @@ import mobi.giza.compose.database.model.types.DkgApprovalStep import mobi.giza.compose.database.model.types.DkgRitualStage import mobi.giza.compose.database.repository.DatabaseChatRepository import mobi.giza.compose.repository.AdminRoom +import mobi.giza.compose.repository.GroupMadeHere import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -88,6 +89,37 @@ class GroupsInOneRoomJvmTest { } } + /** The room's details list each group, from its ceremony to its room. */ + @Test + fun `each group the room's members made is listed with where it stands`(): Unit = runBlocking { + CeremonyNetwork.nip17(size = 3, subject = "Ekklesia").use { network -> + val repository = DatabaseChatRepository(network.coordinator.db, CoroutineScope(Dispatchers.Default)) + + val ekklesia = completed(network, subject = null) + val agora = network.propose(threshold = 3, subject = "Agora") + + assertEquals( + setOf( + GroupMadeHere(ekklesia.id, "Ekklesia", GroupMadeHere.Status.WaitingForRoom), + GroupMadeHere(agora.id, "Agora", GroupMadeHere.Status.KeyCeremonyRunning), + ), + repository.groupsMadeIn(network.roomId).toSet(), + ) + + val ekklesiaRoom = standUpRoom(network, ekklesia, "#Ekklesia") + assertEquals( + GroupMadeHere.Status.Ready(ekklesiaRoom), + repository.groupsMadeIn(network.roomId).single { it.dkgSessionId == ekklesia.id }.status, + ) + + // A failed one is gone from the list; it is there to be tried again. + network.coordinator.db.dkgSessionDao().upsert( + assertNotNull(network.session(network.coordinator, agora.id)).copy(stage = DkgRitualStage.FAILED) + ) + assertEquals(listOf(ekklesia.id), repository.groupsMadeIn(network.roomId).map { it.dkgSessionId }) + } + } + /** * "The key this room signs with" has no answer in a NIP-17 room holding two, and * guessing the newest would sign as a group nobody named. Nothing asks it of a diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/GroupsMadeHereJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/GroupsMadeHereJvmTest.kt new file mode 100644 index 00000000..efe75a90 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/GroupsMadeHereJvmTest.kt @@ -0,0 +1,63 @@ +package mobi.giza.compose.ui.composable + +import androidx.compose.ui.test.ComposeUiTest +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.runDesktopComposeUiTest +import mobi.giza.compose.repository.GroupMadeHere +import mobi.giza.compose.ui.theme.GizaTheme +import kotlin.test.Test +import kotlin.test.assertEquals + +/** + * A NIP-17 room's details list every group its members have made in it, each with + * where it stands. It used to be one admin-room card, which showed the newest group + * and hid the rest. docs/nip17-multiple-ceremonies.md, Phase 5. + */ +@OptIn(ExperimentalTestApi::class) +class GroupsMadeHereJvmTest { + + private val agora = GroupMadeHere("a".repeat(64), "Agora", GroupMadeHere.Status.KeyCeremonyRunning) + private val forum = GroupMadeHere("f".repeat(64), "Forum", GroupMadeHere.Status.WaitingForRoom) + private val ekklesia = GroupMadeHere("e".repeat(64), "Ekklesia", GroupMadeHere.Status.Ready("9".repeat(64))) + + private fun render( + groups: List, + onOpen: (GroupMadeHere) -> Unit = {}, + assertions: ComposeUiTest.() -> Unit, + ) = runDesktopComposeUiTest(width = 400, height = 800) { + setContent { GizaTheme { GroupsMadeHere(groups = groups, onOpen = onOpen) } } + assertions() + } + + @Test + fun `every group is listed, with where it stands`() = render(listOf(agora, forum, ekklesia)) { + onNodeWithText("Agora").assertExists() + onNodeWithText("Key ceremony in progress").assertExists() + onNodeWithText("Forum").assertExists() + onNodeWithText("Has its key, and is waiting for its room").assertExists() + onNodeWithText("Ekklesia").assertExists() + onNodeWithText("Every member of this group is an admin there.").assertExists() + } + + @Test + fun `a group is opened by the one tapped`() { + var opened: GroupMadeHere? = null + render(listOf(agora, ekklesia), onOpen = { opened = it }) { + onNodeWithText("Ekklesia").performClick() + } + assertEquals(ekklesia, opened) + } + + @Test + fun `a room that has made no group says so`() = render(emptyList()) { + onNodeWithText("No groups have been made in this conversation yet.").assertExists() + } + + @Test + fun `a group with no name is not a blank row`() = + render(listOf(agora.copy(name = null))) { + onNodeWithText("an unnamed group").assertExists() + } +} diff --git a/docs/shared-key-ceremony.md b/docs/shared-key-ceremony.md index f7e2b4dd..5e3e48c2 100644 --- a/docs/shared-key-ceremony.md +++ b/docs/shared-key-ceremony.md @@ -157,8 +157,14 @@ protocol checks this, so `ChillDkgRitualOrderingTest` does. A ceremony is started in exactly three places, each of which knows what it is for: group creation (`SelectQuorumViewModel`) for a new group's own key, the subgroup picker (`SelectSubgroupAdminsViewModel`) for a subgroup's, and -`CreateSharedKeyScreen` for an existing NIP-17 group that has no key yet. -`CeremonyEntryPointsJvmTest` counts them. `DkgRitualScreen` shows one ceremony, +`CreateSharedKeyScreen` for a new group made from the members of an existing +NIP-17 room. `CeremonyEntryPointsJvmTest` counts them. + +The same people always share one NIP-17 room, since its id is derived from +them, so a second group they make is a second ceremony in that room. The room +is where each group's ceremony is held, not the group, and every ceremony after +the room's first carries a name of its own. The room's details list the groups +made in it. See [nip17-multiple-ceremonies.md](./nip17-multiple-ceremonies.md). `DkgRitualScreen` shows one ceremony, named by its route, and never starts one: it used to, from the same button slot its approvals used, and a member coming back from approving a subgroup's ceremony opened a second, whole-room one in the parent's room. A Marmot room