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 9dec1a94..e9623642 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 @@ -928,7 +928,7 @@ fun ChatRoomDetailScreen( } } - item { + if (chatRoomDetailUIState.canInvite) item { TextButton( onClick = { // Navigate to the invite user screen... diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModel.kt index b2f6f434..9e9aa9ae 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModel.kt @@ -32,6 +32,18 @@ import mobi.giza.compose.repository.ChatRepository import mobi.giza.compose.repository.NostrRepository import mobi.giza.compose.ui.view.state.AddMemberToChatRoomConfirmationUIState +/** + * Inviting one person into a room that already exists -- the app's one way of + * adding a member after a room's founding. + * + * Only a room with a threshold key behind it takes one. A room without is a + * one-to-one chat or a group its creator runs alone, and a third person invited + * into either is a group run by one person, which Giza no longer makes. The + * people in it can make a group together instead, robust from the start. The + * rule lives here, at the one entrance `MarmotRoomEntryPointsJvmTest` pins, and + * not in the DAO, whose invite the tests drive on keyless rooms to exercise MLS + * itself. See docs/remove-convenient-groups.md, D7. + */ class AddMemberToChatRoomConfirmationViewModel( val chatRoomId: String, val activeUserPublicKey: HexKey, @@ -61,6 +73,13 @@ class AddMemberToChatRoomConfirmationViewModel( return@launch } + // Said before anything is looked up for the invitee: there is nothing to + // wait on the relays for if the room will not take them. + if (!takesNewMembers(chatRoomId)) { + addMemberToChatRoomConfirmationUIState = AddMemberToChatRoomConfirmationUIState.Error(KEYLESS_ROOM) + return@launch + } + val profile = nostrRepository.getProfileWithPublicKey(profilePublicKey) if (profile == null) { addMemberToChatRoomConfirmationUIState = @@ -135,6 +154,13 @@ class AddMemberToChatRoomConfirmationViewModel( onInviteSent: () -> Unit ) { viewModelScope.launch(Dispatchers.IO) { + // Again at the act, because a screen not drawing a button is not a guard. + if (!takesNewMembers(localChatRoom.chatRoom.id)) { + isActionPending.value = false + addMemberToChatRoomConfirmationUIState = AddMemberToChatRoomConfirmationUIState.Error(KEYLESS_ROOM) + return@launch + } + // Invite member. `inviteMember` can throw (no MLS state for the room, credential // identity mismatch); reporting success regardless popped the user back to the chat // as though an invite had been sent when nothing was ever queued. @@ -167,9 +193,17 @@ class AddMemberToChatRoomConfirmationViewModel( } } + /** Whether [chatRoomId] has a threshold key behind it, which is what lets it grow. */ + private suspend fun takesNewMembers(chatRoomId: String): Boolean = + chatRepository.groupKeyState(chatRoomId) != null + companion object { private const val TAG = "AddMemberToChatRoomConfirmationViewModel" + /** Says what to do instead, because the person asking can do it. */ + const val KEYLESS_ROOM = + "Only a group can take new members. Make a group with everyone instead — it's run by all of you together." + /** How long to wait on the relays before telling the user we came up empty. */ private val RELAY_LOOKUP_TIMEOUT = 20.seconds 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 0a503e3a..8332bb3f 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 @@ -120,6 +120,12 @@ class ChatRoomDetailViewModel( canAddSubgroup = canSign && !subgroupsNeedAWallet, subgroupsNeedAWallet = canSign && subgroupsNeedAWallet, canGovern = canGovern, + // The same rule `AddMemberToChatRoomConfirmationViewModel` enforces; + // this only keeps the button from offering what it would refuse. + canInvite = groupKeyState != null && + localChatRoom.chatRoom.mlsGroupState != null && + !isDisbanded && + localChatRoom.chatRoom.leftGroupAt == null, isDisbanded = isDisbanded, shares = chatRepository.sharesSummary(localChatRoom), missingFoundingAdmins = missingFoundingAdmins, 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 a7fe3b32..34a6c6a9 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 @@ -73,6 +73,13 @@ sealed interface ChatRoomDetailUIState { * neither either. See docs/marmot-v2.md, Phase 7. */ val canGovern: Boolean = false, + /** + * Whether the room takes new members: a Marmot room with a threshold key + * behind it, still open. A one-to-one chat and a group its creator runs + * alone take none -- inviting a third person is how one would become a + * group run by one person. docs/remove-convenient-groups.md, D7. + */ + val canInvite: Boolean = false, /** * The group's lifecycle says it is over. Absorbing: nothing is sent into it * and nobody rejoins, so every write entrance is hidden and the room says so. diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/MarmotRoomEntryPointsJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/MarmotRoomEntryPointsJvmTest.kt index 46963df4..dfc704f0 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/MarmotRoomEntryPointsJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/MarmotRoomEntryPointsJvmTest.kt @@ -5,24 +5,29 @@ import kotlin.test.Test import kotlin.test.assertEquals /** - * A Marmot room is minted in exactly two places, and members are put into one - * only when it is founded. + * A Marmot room is minted in exactly two places, and grows after its founding + * through exactly one. * * - `MarmotGroupCreation`, a robust group's admin room or a subgroup's: its id is * derived from the threshold key the group made, and its admins are the - * ceremony's participants; + * ceremony's participants. It is also the only caller of `addMembers`, the + * founding invite; * - `MarmotOutboundDao.createMlsDirectMessageChatRoom`, a one-to-one chat started * from a profile: two people, its creator the only admin, and no key. A chat is * not a group, and two people cannot carry a threshold. * - * A third used to exist: the "convenient" group, which its creator ran alone. - * Every group a member makes is robust now -- see docs/remove-convenient-groups.md. + * After its founding a room takes a member only through + * `AddMemberToChatRoomConfirmationViewModel`, which refuses a room with no key + * behind it: a one-to-one chat grown by a third person would be a group run by + * one person. + * + * A third minter used to exist: the "convenient" group, which its creator ran + * alone. Every group a member makes is robust now -- see + * docs/remove-convenient-groups.md. * * Like `CeremonyEntryPointsJvmTest` this counts calls rather than proving - * anything about them, which is the point: it fails the day a third minter - * appears, or the day something can add a member to a room after its founding - * -- which would let a one-to-one chat grow into a group run by one person. - * Adding either is a decision somebody makes here. + * anything about them, which is the point: it fails the day another minter or + * another way in appears, and adding one is a decision somebody makes here. */ class MarmotRoomEntryPointsJvmTest { @@ -45,10 +50,18 @@ class MarmotRoomEntryPointsJvmTest { } @Test - fun `members are added to a room only at its founding`() { + fun `a room's founding invite is sent from one place`() { assertEquals( setOf("MarmotGroupCreation"), callersOf("chatRepository.addMembers("), ) } + + @Test + fun `a member joins an existing room through one screen, which checks for a key`() { + assertEquals( + setOf("AddMemberToChatRoomConfirmationViewModel"), + callersOf("chatRepository.inviteMember("), + ) + } } diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModelJvmTest.kt new file mode 100644 index 00000000..836163dc --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/AddMemberToChatRoomConfirmationViewModelJvmTest.kt @@ -0,0 +1,125 @@ +package mobi.giza.compose.ui.view.model + +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +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.GroupKeyState +import mobi.giza.compose.database.model.MarmotKeyPackage +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.AddMemberToChatRoomConfirmationUIState +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Instant + +/** + * Only a room with a threshold key behind it takes a member after its founding. + * + * A one-to-one chat, or a group its creator runs alone, grown by one more person + * is a group run by one person -- the kind Giza no longer makes. + * docs/remove-convenient-groups.md, D7. + */ +@OptIn(ExperimentalCoroutinesApi::class) +class AddMemberToChatRoomConfirmationViewModelJvmTest { + @BeforeTest + fun setUp() { + Dispatchers.setMain(UnconfinedTestDispatcher()) + } + + @AfterTest + fun tearDown() { + Dispatchers.resetMain() + } + + private val ada = "a".repeat(64) + private val bob = "b".repeat(64) + private val cleo = "c".repeat(64) + private val roomId = "70".repeat(32) + + private val room = LocalChatRoom( + chatRoom = ChatRoom(id = roomId, userPublicKey = ada, subject = null, description = null, mlsGroupState = "state"), + localParticipants = emptyList(), + ) + + private val cleoPackage = MarmotKeyPackage(id = "cc".repeat(32), publicKey = cleo, tlsEncodedMarmotKeyPackage = ByteArray(0)) + + private class Device(private val room: LocalChatRoom, private val keyed: Boolean) : ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY { + val invited = mutableListOf() + + override suspend fun getChatRoomByIdentifier(id: HexKey): LocalChatRoom? = room.takeIf { id == room.chatRoom.id } + + override suspend fun groupKeyState(chatRoomId: String): GroupKeyState? = + if (keyed && chatRoomId == room.chatRoom.id) { + GroupKeyState( + chatRoomId = chatRoomId, + dkgSessionId = "ceremony", + thresholdPublicKey = "02" + "11".repeat(32), + derivationPath = "m", + announcedBy = chatRoomId, + announcedAt = Instant.fromEpochSeconds(1_700_000_000), + ) + } else { + null + } + + override suspend fun inviteMember(localChatRoom: LocalChatRoom, peerPublicKey: HexKey, peerKeyPackage: MarmotKeyPackage) { + invited += peerPublicKey + } + } + + private fun viewModel(device: Device) = AddMemberToChatRoomConfirmationViewModel( + chatRoomId = roomId, + activeUserPublicKey = ada, + profilePublicKey = cleo, + relayHint = null, + initialAddMemberToChatRoomConfirmationUIState = AddMemberToChatRoomConfirmationUIState.Loading, + nostrRepository = NostrRepository.NO_OP_NOSTR_REPOSITORY, + chatRepository = device, + ) + + private suspend fun AddMemberToChatRoomConfirmationViewModel.awaitError() = withTimeout(5_000) { + while (addMemberToChatRoomConfirmationUIState !is AddMemberToChatRoomConfirmationUIState.Error) delay(10) + addMemberToChatRoomConfirmationUIState as AddMemberToChatRoomConfirmationUIState.Error + } + + @Test + fun `a room with no key behind it says so before looking anybody up`(): Unit = runBlocking { + val viewModel = viewModel(Device(room, keyed = false)) + viewModel.initiate() + + assertEquals(AddMemberToChatRoomConfirmationViewModel.KEYLESS_ROOM, viewModel.awaitError().message) + } + + @Test + fun `a room with no key behind it is not invited into`(): Unit = runBlocking { + val device = Device(room, keyed = false) + val viewModel = viewModel(device) + + viewModel.inviteToChatRoom(room, cleo, cleoPackage, onInviteSent = {}) + + assertEquals(AddMemberToChatRoomConfirmationViewModel.KEYLESS_ROOM, viewModel.awaitError().message) + assertEquals(emptyList(), device.invited) + } + + @Test + fun `a group with a key takes a new member`(): Unit = runBlocking { + val device = Device(room, keyed = true) + val viewModel = viewModel(device) + var sent = false + + viewModel.inviteToChatRoom(room, cleo, cleoPackage, onInviteSent = { sent = true }) + withTimeout(5_000) { while (!sent) delay(10) } + + assertEquals(listOf(cleo), device.invited) + } +} diff --git a/docs/remove-convenient-groups.md b/docs/remove-convenient-groups.md index 6473c180..92862d6f 100644 --- a/docs/remove-convenient-groups.md +++ b/docs/remove-convenient-groups.md @@ -60,12 +60,11 @@ only admin, which is structurally a convenient group. It stays as it is because: - two people cannot carry a meaningful threshold (D2), so the robust alternative is a 2-of-2 key that freezes the chat the day one phone is lost; -- the app has no path that adds a third member to an existing room (`addMembers` is - called only at creation, by `SelectChatRoomTypeViewModel` and `MarmotGroupCreation`), - so a one-to-one chat cannot grow into a group run by one person. - -Phase 2 records the second point as a test, so that adding an "add member" action -later is a decision someone makes in that test, not a side effect. +- ~~the app has no path that adds a third member to an existing room~~. **Wrong, and + found while building Phase 2:** the detail screen's "Invite new member" reaches + `chatRepository.inviteMember` through `AddMemberToChatRoomConfirmationViewModel`, + for any room. So a one-to-one chat *could* grow into a group run by one person. + D7 closes that, and Phase 2b builds it. Confirmed before any phase was built. If one-to-one chats should ever be robust too, that is a separate plan: a 2-of-2 key, the ceremony started from a profile, and an answer for a peer who never comes online. @@ -80,7 +79,7 @@ convenient without anyone choosing it. After this plan, the picker will not let of fewer than three be made at all, and will say why, pointing anyone with only one person in mind at a direct message. Confirmed before any phase was built. -**D3. Convenient rooms that already exist keep working, unchanged.** That covers rooms +**D3. Convenient rooms that already exist keep working, unchanged -- except that, by D7, they take no new members.** That covers rooms this device made before the change and rooms a Bennu peer makes and invites a Giza member into. Receiving, reading, sending, leaving and the creator's own governance (removing a member, disbanding) all stay. There is no migration, and there is **no @@ -103,6 +102,20 @@ new `Quorum` object in `managers/`, next to `ChillDkgRitualManager`, which enfor They move in a phase of their own (Phase 1), so the deletion in Phase 4 changes no behaviour. +**D7. Only a room with a key behind it takes members after its founding.** Added +during the build, when D1's second reason turned out to be false. A Marmot room with +no `GroupKeyState` -- a one-to-one chat, or a convenient group made before this plan +or by a Bennu peer -- takes no new members. One more person in either would be a group +its creator runs alone, which is what this plan stops Giza making. The people in it +can make a group together instead. This narrows D3: a legacy convenient group's +creator can still remove members and disband it, but no longer add to it. + +The rule lives in `AddMemberToChatRoomConfirmationViewModel`, the app's one caller of +`chatRepository.inviteMember`. It is not in `MarmotOutboundDao.inviteMemberToChatRoom`, +because twenty-odd tests drive that DAO on keyless fixture rooms to exercise MLS +itself (epoch races, publish-before-apply, convergence). `MarmotRoomEntryPointsJvmTest` +pins the single entrance instead. + ## What changes for a person making a group | step | today | after | @@ -236,6 +249,27 @@ become a group without someone changing this line. **Done when** no path through the app makes a Marmot room with a single admin and more than one member. +### Phase 2b: a room with no key takes no new members (D7) + +Not in the plan as first written; see D1. + +- `AddMemberToChatRoomConfirmationViewModel` checks `chatRepository.groupKeyState` + twice. The first check is in `initiate`, before the invitee's key package is looked + up. The second is in `inviteToChatRoom`, at the act itself. A keyless room gets an + `Error` that says to make a group instead. +- `ChatRoomDetailUIState.canInvite` is true for a room with key state, MLS state, not + disbanded and not left. The "Invite new member" button shows only when it is true. + This also takes the button off NIP-17 rooms, where an invite could only fail for + want of MLS state. +- `MarmotRoomEntryPointsJvmTest` adds a test that `chatRepository.inviteMember(` is + called only from that view model. + +**Tests.** Add `AddMemberToChatRoomConfirmationViewModelJvmTest` with three cases. A +keyless room is refused on load, and refused at the act with no invite sent. A keyed +room is invited into. + +**Done when** a one-to-one chat cannot become a group, from any screen. + ### Phase 3: the member picker holds the floor (D2) **`SelectChatRoomMembersViewModel`**: