diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml
index e7914059..3c845be7 100644
--- a/composeApp/src/commonMain/composeResources/values/strings.xml
+++ b/composeApp/src/commonMain/composeResources/values/strings.xml
@@ -547,6 +547,10 @@
How many of you have to agree to a change?
Fewer approvals
More approvals
+ You already have a group with these people: %1$s.
+ You already have groups with these people: %1$s.
+ This will be a separate group with the same name, with a key and room of its own.
+ an unnamed group
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.
A group needs at least %1$s people, you included. Go back and pick more.
Pick at least %1$s people
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseDkgRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseDkgRepository.kt
index c2c54b30..767083e9 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseDkgRepository.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseDkgRepository.kt
@@ -1,6 +1,7 @@
package mobi.giza.compose.database.repository
import mobi.giza.compose.database.GizaDatabase
+import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.DkgParticipantMessage
import mobi.giza.compose.database.model.DkgSession
import mobi.giza.compose.database.model.types.DkgRitualStage
@@ -57,6 +58,21 @@ class DatabaseDkgRepository(
override suspend fun getLatestSessionForChatRoom(chatRoomId: String): DkgSession? =
database.dkgSessionDao().getLatestSessionForChatRoom(chatRoomId)
+ override fun observeOwnSessionsForChatRoom(chatRoomId: String): Flow> =
+ database.dkgSessionDao().observeOwnSessionsForChatRoom(chatRoomId)
+ .map { sessions -> sessions.filter { it.stage != DkgRitualStage.FAILED } }
+
+ override suspend fun getOwnSessionsAmong(memberPublicKeys: Set): List = try {
+ // The id `NostrNip17Dao.createNip17ChatRoom` gives these people's room,
+ // derived without making it.
+ database.dkgSessionDao()
+ .getOwnSessionsForChatRoom(ChatRoom.deriveChatRoomId(memberPublicKeys))
+ .filter { it.stage != DkgRitualStage.FAILED }
+ } catch (e: Throwable) {
+ logger.e("Error reading the groups ${memberPublicKeys.size} people already share", e)
+ emptyList()
+ }
+
override suspend fun proposeRitual(
localChatRoom: LocalChatRoom,
userPublicKey: HexKey,
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/DkgRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/DkgRepository.kt
index 99cfd194..916c4b57 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/DkgRepository.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/DkgRepository.kt
@@ -65,6 +65,26 @@ interface DkgRepository {
suspend fun getLatestSessionForChatRoom(chatRoomId: String): DkgSession?
+ /**
+ * Every group of its own [chatRoomId] holds, newest first, failed ones left
+ * out: in a NIP-17 room, the groups its members have made there.
+ *
+ * The same people always share one NIP-17 room -- its id is derived from them --
+ * so a second group they make is a second ceremony in that room rather than a
+ * room of its own (docs/nip17-multiple-ceremonies.md).
+ */
+ fun observeOwnSessionsForChatRoom(chatRoomId: String): Flow>
+
+ /**
+ * The groups the people in [memberPublicKeys] have already made together, as
+ * [observeOwnSessionsForChatRoom] has them, read from the room they would share
+ * without making it. Empty if they have made none, or share no room yet.
+ *
+ * [memberPublicKeys] is everybody, the reader included: it is the set the room's
+ * id is derived from.
+ */
+ suspend fun getOwnSessionsAmong(memberPublicKeys: Set): List
+
/**
* Opens a ritual in [localChatRoom], making this device the coordinator.
*
@@ -219,6 +239,12 @@ interface DkgRepository {
override suspend fun getLatestSessionForChatRoom(chatRoomId: String): DkgSession? = null
+ override fun observeOwnSessionsForChatRoom(chatRoomId: String): Flow> =
+ flowOf(emptyList())
+
+ override suspend fun getOwnSessionsAmong(memberPublicKeys: Set): List =
+ emptyList()
+
override suspend fun proposeRitual(
localChatRoom: LocalChatRoom,
userPublicKey: HexKey,
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectQuorumScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectQuorumScreen.kt
index 960706a4..8dd6af9e 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectQuorumScreen.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/SelectQuorumScreen.kt
@@ -48,6 +48,10 @@ import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.DkgRepository
import mobi.giza.compose.repository.NostrRepository
import mobi.giza.compose.resources.Res
+import mobi.giza.compose.resources.you_already_have_a_group_with_these_people
+import mobi.giza.compose.resources.you_already_have_groups_with_these_people
+import mobi.giza.compose.resources.this_will_be_a_separate_group_with_the_same_name
+import mobi.giza.compose.resources.an_unnamed_group
import mobi.giza.compose.resources.create_group
import mobi.giza.compose.resources.creating_the_group_starts_a_key_ceremony
import mobi.giza.compose.resources.all_of_you_administer_this_group_together
@@ -224,6 +228,13 @@ fun SelectQuorumScreen(
}
}
+ if (selectQuorumUIState.existingGroups.isNotEmpty()) {
+ ExistingGroupsCard(
+ existingGroups = selectQuorumUIState.existingGroups,
+ sharesName = selectQuorumUIState.sharesNameWith(name),
+ )
+ }
+
Text(
text = stringResource(Res.string.all_of_you_administer_this_group_together, adminCount),
style = MaterialTheme.typography.bodyLarge
@@ -287,6 +298,49 @@ fun SelectQuorumScreen(
}
}
+/**
+ * The groups these same people already have together. Information, not a gate: they
+ * may make as many as they like, and each is a key and a room of its own. It is for
+ * the member who meant to open one of these. docs/nip17-multiple-ceremonies.md, Phase 2.
+ */
+@Composable
+private fun ExistingGroupsCard(
+ existingGroups: List,
+ sharesName: Boolean,
+) {
+ val unnamed = stringResource(Res.string.an_unnamed_group)
+ val names = existingGroups.joinToString(", ") { it.name?.takeIf(String::isNotBlank) ?: unnamed }
+
+ Card(
+ modifier = Modifier.fillMaxWidth(),
+ colors = CardDefaults.cardColors(
+ containerColor = MaterialTheme.colorScheme.secondaryContainer,
+ contentColor = MaterialTheme.colorScheme.onSecondaryContainer
+ )
+ ) {
+ Column(
+ modifier = Modifier.padding(MaterialTheme.spacing.containerPadding),
+ verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)
+ ) {
+ Text(
+ text = if (existingGroups.size == 1) {
+ stringResource(Res.string.you_already_have_a_group_with_these_people, names)
+ } else {
+ stringResource(Res.string.you_already_have_groups_with_these_people, names)
+ },
+ style = MaterialTheme.typography.bodyMedium
+ )
+
+ if (sharesName) {
+ Text(
+ text = stringResource(Res.string.this_will_be_a_separate_group_with_the_same_name),
+ style = MaterialTheme.typography.bodyMedium
+ )
+ }
+ }
+ }
+}
+
/**
* Asks how many admins have to sign off on a change. Stepped rather than typed:
* the range is small, both ends are bounded, and the caption has to keep up.
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectQuorumViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectQuorumViewModel.kt
index bf0b1ab4..cb6e867d 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectQuorumViewModel.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SelectQuorumViewModel.kt
@@ -109,8 +109,21 @@ class SelectQuorumViewModel(
nostrRepository.getProfileWithPublicKey(publicKey)
}
+ // The groups these people already have together, read from the room they
+ // share without making it. A row from before ceremonies carried a name is
+ // named by its room, which is what named it then.
+ val existingGroups = dkgRepository
+ .getOwnSessionsAmong((memberPublicKeys + activeUserPublicKey).toSet())
+ .map { session ->
+ SelectQuorumUIState.ExistingGroup(
+ name = session.subject
+ ?: chatRepository.getChatRoomByIdentifier(session.chatRoomId)?.chatRoom?.subject
+ )
+ }
+
selectQuorumUIState = SelectQuorumUIState.Loaded(
- members = members
+ members = members,
+ existingGroups = existingGroups,
)
}
}
@@ -216,14 +229,20 @@ class SelectQuorumViewModel(
// The room's membership is the set this screen was opened for, so the
// quorum picked against [adminCount] is the same t-of-n the ceremony is
- // asked for. A room that already has a ceremony -- the id is derived from
- // its members, so making the same group twice returns the same room --
- // hands that one back instead of opening a second.
+ // asked for.
+ //
+ // Named outright, never left to the room. The room's id is derived from
+ // its members, so the same people making a second group get the first
+ // group's room back, still under the first group's name; the group being
+ // made here is called what the user typed, and so is the room its key
+ // makes. A ceremony still running under this name is handed back rather
+ // than opened twice. docs/nip17-multiple-ceremonies.md, Phase 2.
val session = dkgRepository.proposeRitual(
localChatRoom = localChatRoom,
userPublicKey = activeUserPublicKey,
nostrPrivateKey = nostrPrivateKey,
- threshold = quorum.value
+ threshold = quorum.value,
+ subject = name.takeIf { it.isNotBlank() },
)
withContext(Dispatchers.Main) {
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/SelectQuorumUIState.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/SelectQuorumUIState.kt
index 5879294b..9df208dd 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/SelectQuorumUIState.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/state/SelectQuorumUIState.kt
@@ -5,8 +5,24 @@ import mobi.giza.compose.database.model.Profile
sealed interface SelectQuorumUIState {
data class Loaded(
/** The picked members, for naming them back to the user. */
- val members: List
- ): SelectQuorumUIState
+ val members: List,
+ /**
+ * The groups these same people have already made together, newest first.
+ *
+ * Said rather than refused: the same people may make as many groups as they
+ * like, and each one is a key and a room of its own. It is here for the user
+ * who meant to open one of these and started making another instead.
+ * docs/nip17-multiple-ceremonies.md, Phase 2.
+ */
+ val existingGroups: List = emptyList(),
+ ): SelectQuorumUIState {
+ /** Whether the group being made would share its name with one of [existingGroups]. */
+ fun sharesNameWith(name: String): Boolean =
+ existingGroups.any { it.name?.trim()?.equals(name.trim(), ignoreCase = true) == true }
+ }
+
+ /** A group already made by the same people: its name, or null if it has none. */
+ data class ExistingGroup(val name: String?)
data class Error(
val message: String
diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/GroupCreationJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/GroupCreationJvmTest.kt
index d7babc51..ef74bc90 100644
--- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/GroupCreationJvmTest.kt
+++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/GroupCreationJvmTest.kt
@@ -1,5 +1,7 @@
package mobi.giza.compose.ui.view.model
+import kotlin.test.assertFalse
+import mobi.giza.compose.database.model.types.DkgRitualStage
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import fr.acinq.bitcoin.ByteVector32
@@ -89,6 +91,9 @@ class GroupCreationJvmTest {
private class Recorder {
val nip17Rooms = mutableListOf>()
val thresholds = mutableListOf()
+ val subjects = mutableListOf()
+ val askedAmong = mutableListOf>()
+ var existing: List = emptyList()
var addMembersCalls = 0
var keyPackageLookups = 0
@@ -140,10 +145,16 @@ class GroupCreationJvmTest {
subject: String?,
): DkgSession? {
thresholds += threshold
+ subjects += subject
// No session: the screen then stays put and says the ceremony did
// not start, which keeps navigation out of the test.
return null
}
+
+ override suspend fun getOwnSessionsAmong(memberPublicKeys: Set): List {
+ askedAmong += memberPublicKeys
+ return existing
+ }
}
val nostrRepository = object : NostrRepository by NostrRepository.NO_OP_NOSTR_REPOSITORY {
@@ -195,6 +206,73 @@ class GroupCreationJvmTest {
assertTrue(viewModel.keyCeremonyNotStarted.value)
}
+ /**
+ * The same people making a second group land in the first group's room, which
+ * keeps the first group's name. The group being made is called what was typed,
+ * so the ceremony carries that name rather than leaving it to the room.
+ * docs/nip17-multiple-ceremonies.md, Phase 2.
+ */
+ @Test
+ fun `the ceremony is named after the group, not left to the room`(): Unit = runBlocking {
+ val recorder = Recorder()
+ val viewModel = viewModel(recorder, listOf(bob, cleo, dora))
+
+ viewModel.createGroup(onNavigateToRoute = {})
+ viewModel.awaitSettled()
+
+ assertEquals(listOf("Checkers"), recorder.subjects)
+ }
+
+ /** Said, not refused: the user may mean to make another, or to open one of these. */
+ @Test
+ fun `the groups these people already have are named before another is made`(): Unit = runBlocking {
+ val recorder = Recorder()
+ recorder.existing = listOf(session("one", subject = "Chess"), session("two", subject = "checkers "))
+ val viewModel = viewModel(recorder, listOf(bob, cleo, dora))
+
+ viewModel.initiate()
+ val loaded = withTimeout(5_000) {
+ while (viewModel.selectQuorumUIState !is SelectQuorumUIState.Loaded) delay(10)
+ viewModel.selectQuorumUIState as SelectQuorumUIState.Loaded
+ }
+
+ assertEquals(listOf(setOf(creator, bob, cleo, dora)), recorder.askedAmong, "asked about everybody, the creator too")
+ assertEquals(
+ listOf(SelectQuorumUIState.ExistingGroup("Chess"), SelectQuorumUIState.ExistingGroup("checkers ")),
+ loaded.existingGroups,
+ )
+ assertTrue(loaded.sharesNameWith("Checkers"), "a namesake is flagged, however it was typed")
+ assertFalse(loaded.sharesNameWith("Draughts"))
+ }
+
+ @Test
+ fun `people who have made nothing together are told nothing`(): Unit = runBlocking {
+ val recorder = Recorder()
+ val viewModel = viewModel(recorder, listOf(bob, cleo, dora))
+
+ viewModel.initiate()
+ val loaded = withTimeout(5_000) {
+ while (viewModel.selectQuorumUIState !is SelectQuorumUIState.Loaded) delay(10)
+ viewModel.selectQuorumUIState as SelectQuorumUIState.Loaded
+ }
+
+ assertEquals(emptyList(), loaded.existingGroups)
+ }
+
+ private fun session(id: String, subject: String?) = DkgSession(
+ id = id,
+ chatRoomId = "a1".repeat(32),
+ coordinatorPublicKey = creator,
+ userPublicKey = creator,
+ threshold = 2,
+ participantCount = 4,
+ subject = subject,
+ hostPublicKey = "02" + "11".repeat(32),
+ round1Random = "22".repeat(32),
+ round2AuxRandom = "33".repeat(32),
+ stage = DkgRitualStage.COMPLETE,
+ )
+
@Test
fun `a group below the floor is never created`(): Unit = runBlocking {
val recorder = Recorder()