diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/model/ChatMessage.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/model/ChatMessage.kt index 04f8665d..93ea6dbe 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/model/ChatMessage.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/model/ChatMessage.kt @@ -561,7 +561,26 @@ data class ChatMessage( const val TYPE_CONFEDERATION_SIGNING_PROPOSED = "confederationSigningProposed" const val TYPE_CONFEDERATION_SIGNING_FAILED = "confederationSigningFailed" - val CONFEDERATION_TYPES = setOf( + /** + * A formed confederation's profile, decided by its groups (docs/confederation-profile.md, D8): + * a group proposing a change, another endorsing or objecting to it, and the new profile once + * the confederation has signed it. Written by each device into its own member groups' rooms. + * [frostSigningSessionId] carries the change's id, or for a published profile the + * confederation's, so the line opens what it is about. + */ + const val TYPE_CONFEDERATION_PROFILE_PROPOSED = "confederationProfileProposed" + const val TYPE_CONFEDERATION_PROFILE_ENDORSED = "confederationProfileEndorsed" + const val TYPE_CONFEDERATION_PROFILE_OBJECTED = "confederationProfileObjected" + const val TYPE_CONFEDERATION_PROFILE_PUBLISHED = "confederationProfilePublished" + + val CONFEDERATION_PROFILE_TYPES = setOf( + TYPE_CONFEDERATION_PROFILE_PROPOSED, + TYPE_CONFEDERATION_PROFILE_ENDORSED, + TYPE_CONFEDERATION_PROFILE_OBJECTED, + TYPE_CONFEDERATION_PROFILE_PUBLISHED, + ) + + val CONFEDERATION_TYPES = CONFEDERATION_PROFILE_TYPES + setOf( TYPE_CONFEDERATION_PROPOSED, TYPE_CONFEDERATION_ACCEPTED, TYPE_CONFEDERATION_REJECTED, diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ConfederationProfileManager.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ConfederationProfileManager.kt index 2cd71b5e..c3785686 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ConfederationProfileManager.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ConfederationProfileManager.kt @@ -6,7 +6,9 @@ import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.crypto.verify import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent import kotlin.time.Clock +import kotlin.time.Instant import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.model.ChatMessage import mobi.giza.compose.database.model.ConfederationEvent import mobi.giza.compose.database.model.FrostSigningSession import mobi.giza.compose.database.model.GroupSignedEvent @@ -54,7 +56,7 @@ object ConfederationProfileManager { logger.w("Profile statement ${rumor.id.take(8)} of kind ${rumor.kind} is not well formed; dropping") return false } - return database.confederationDao().record( + val filed = database.confederationDao().record( ConfederationEvent( id = rumor.id, kind = rumor.kind, @@ -67,6 +69,61 @@ object ConfederationProfileManager { confederationId = confederation, ) ) + // Once per statement, by construction: the row above is written once. An answer filed before + // its change had nothing to name, so the change writes their lines now: wraps arrive in any order. + if (filed) runCatching { + announce(database, rumor, changeId) + if (rumor.kind == ConfederationKinds.PROFILE_CHANGE) { + database.confederationDao().getEventsForProposal(changeId) + .filter { it.kind == ConfederationKinds.ENDORSEMENT || it.kind == ConfederationKinds.OBJECTION } + .forEach { announce(database, it.toEvent(), changeId) } + } + }.onFailure { logger.w(throwable = it) { "Filed ${rumor.id.take(8)} but could not write its line" } } + return filed + } + + /** + * A system line in each member group's room this device holds, for [statement] about [changeId] + * (D8). Written by each device for itself, sending nothing; the line carries the change's id so + * it opens the change. + */ + private suspend fun announce(database: GizaDatabase, statement: Event, changeId: HexKey) { + val change = change(database, changeId) ?: return + val name = membership(database, change.confederation)?.name ?: "the confederation" + change.groups.mapNotNull { database.chatRoomDao().findChatRoomById(it) }.forEach { room -> + val roomId = room.chatRoom.id + val who = ConfederationManager.nameOf(database, statement.pubKey, roomId) + val proposer = ConfederationManager.nameOf(database, change.proposingGroup, roomId).let { if (it == "This group") "this group" else it } + val (type, content) = when (statement.kind) { + ConfederationKinds.PROFILE_CHANGE -> ChatMessage.TYPE_CONFEDERATION_PROFILE_PROPOSED to + if (statement.pubKey == roomId) "This group proposed a new profile for $name" else "$who proposes a new profile for $name" + ConfederationKinds.ENDORSEMENT -> ChatMessage.TYPE_CONFEDERATION_PROFILE_ENDORSED to + "$who endorsed $proposer's change to $name's profile" + ConfederationKinds.OBJECTION -> { + val reason = ConfederationProfileStatements.parseAnswer(statement)?.reason?.takeIf { it.isNotBlank() }?.let { ": $it" } ?: "" + ChatMessage.TYPE_CONFEDERATION_PROFILE_OBJECTED to if (statement.pubKey == change.proposingGroup) { + "$who withdrew its change to $name's profile$reason" + } else { + "$who objected to $proposer's change to $name's profile$reason" + } + } + else -> return@forEach + } + database.chatMessageDao().upsert( + ChatMessage( + senderPublicKey = statement.pubKey, + isUserMessage = false, + giftWrapPayloadId = null, + marmotGroupEventId = null, + marmotInnerEventId = null, + chatRoomId = roomId, + content = content, + messageType = type, + createdAt = Instant.fromEpochSeconds(statement.createdAt), + frostSigningSessionId = changeId, + ) + ) + } } /** diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationClassifier.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationClassifier.kt index c13494b2..2b9bc0d6 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationClassifier.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationClassifier.kt @@ -57,6 +57,9 @@ object NotificationClassifier { Line.TYPE_CONFEDERATION_ACCEPTED, Line.TYPE_CONFEDERATION_REJECTED, Line.TYPE_CONFEDERATION_FORMED, + // Another group asking this one about the confederation's profile, and the profile it ends in. + Line.TYPE_CONFEDERATION_PROFILE_PROPOSED, + Line.TYPE_CONFEDERATION_PROFILE_PUBLISHED, Line.TYPE_INBOX_KEY_COMPLETE, Line.TYPE_MEMBER_ADDED, Line.TYPE_MEMBER_LEFT, @@ -102,6 +105,8 @@ object NotificationClassifier { Line.TYPE_CONFEDERATION_CEREMONY_FAILED, Line.TYPE_CONFEDERATION_SIGNING_PROPOSED, Line.TYPE_CONFEDERATION_SIGNING_FAILED, + Line.TYPE_CONFEDERATION_PROFILE_ENDORSED, + Line.TYPE_CONFEDERATION_PROFILE_OBJECTED, Line.TYPE_INBOX_KEY_STARTED, Line.TYPE_INBOX_KEY_FAILED, Line.TYPE_INBOX_ANNOUNCED, diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt index 3bdbb693..63e076d7 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt @@ -40,6 +40,7 @@ import androidx.compose.material.icons.filled.CallMerge import androidx.compose.material.icons.filled.Campaign import androidx.compose.material.icons.filled.FactCheck import androidx.compose.material.icons.filled.Draw +import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.Download import androidx.compose.material.icons.filled.History import androidx.compose.material.icons.filled.Hub @@ -916,6 +917,10 @@ private fun RitualNotice( ChatMessage.TYPE_CONFEDERATION_FORMED -> Icons.Default.Hub ChatMessage.TYPE_CONFEDERATION_SIGNING_PROPOSED -> Icons.Default.Draw ChatMessage.TYPE_CONFEDERATION_SIGNING_FAILED -> Icons.Default.ErrorOutline + ChatMessage.TYPE_CONFEDERATION_PROFILE_PROPOSED -> Icons.Default.Edit + ChatMessage.TYPE_CONFEDERATION_PROFILE_ENDORSED -> Icons.Default.CheckCircle + ChatMessage.TYPE_CONFEDERATION_PROFILE_OBJECTED -> Icons.Default.Block + ChatMessage.TYPE_CONFEDERATION_PROFILE_PUBLISHED -> Icons.Default.Hub else -> Icons.Default.PanTool diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationProfileReceiveJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationProfileReceiveJvmTest.kt new file mode 100644 index 00000000..fe6e834d --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ConfederationProfileReceiveJvmTest.kt @@ -0,0 +1,113 @@ +package mobi.giza.compose.managers + +import androidx.room3.Room +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray +import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher +import com.vitorpamplona.quartz.nip01Core.crypto.Nip01Crypto +import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate +import fr.acinq.bitcoin.PrivateKey +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Clock +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.builder.getRoomDatabase +import mobi.giza.compose.database.model.ChatMessage +import mobi.giza.compose.database.model.ChatRoom +import mobi.giza.compose.database.model.NostrEvent +import mobi.giza.compose.database.model.Profile +import mobi.giza.compose.extensions.toHex +import mobi.giza.compose.managers.ConfederationStatementFixture.key +import mobi.giza.compose.managers.ConfederationStatementFixture.npub +import mobi.giza.compose.managers.ConfederationStatementFixture.seal +import mobi.giza.compose.nostr.confederation.ConfederationCharterEvent +import mobi.giza.compose.nostr.confederation.ConfederationKinds +import mobi.giza.compose.nostr.confederation.ConfederationProfileStatements +import mobi.giza.compose.notifications.NotificationClassifier + +/** + * What a member group's device writes when it hears about a change to its confederation's profile + * (docs/confederation-profile.md, Phase 3): a line in each of its member groups' rooms, once per + * statement, whatever order the wraps arrive in. + */ +class ConfederationProfileReceiveJvmTest { + private val db: GizaDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder()) + + private val agora = key(1) + private val polis = key(2) + private val ekklesia = key(3) + private val atlasKey = key(9) + private val atlas = npub(atlasKey) + private val me = npub(key(20)) + private val now = Clock.System.now().epochSeconds + + @BeforeTest + fun setUp() = runBlocking { + // This device is in Polis and Ekklesia. + db.nostrEventDao().upsert(NostrEvent(id = "e$me".take(64), pubKey = me, kind = 0, tags = emptyArray(), content = "{}", sig = "0".repeat(128))) + db.profileDao().upsert(Profile(publicKey = me, nostrEventId = "e$me".take(64))) + listOf(polis to "#Polis", ekklesia to "#Ekklesia").forEach { (k, subject) -> + db.chatRoomDao().upsert(ChatRoom(id = npub(k), userPublicKey = me, subject = subject, description = null, mlsGroupState = "00")) + } + val tags = ConfederationCharterEvent.assembleTags("Atlas", listOf(npub(agora), npub(polis), npub(ekklesia)), threshold = 2) + ConfederationManager.record(db, signed(atlasKey, EventTemplate(now - 60, ConfederationKinds.CHARTER, tags, ""))) + } + + @AfterTest + fun close() = db.close() + + private fun signed(by: PrivateKey, template: EventTemplate): Event { + val pubKey = npub(by) + val id = EventHasher.hashId(pubKey, template.createdAt, template.kind, template.tags, template.content) + return Event(id, pubKey, template.createdAt, template.kind, template.tags, template.content, Nip01Crypto.sign(id.hexToByteArray(), by.value.toByteArray()).toHex()) + } + + private val changeRumor: Event = ConfederationProfileStatements.change( + npub(agora), listOf(npub(polis), npub(ekklesia)), atlas, 2, null, emptyArray(), """{"name":"Atlas"}""", now, now + 1000, + ) + private val change: ConfederationProfileStatements.Change = ConfederationProfileStatements.parseChange(changeRumor)!! + + private suspend fun lines(room: PrivateKey) = db.chatMessageDao().getChatMessagesByChatRoomId(npub(room)) + .map { it.chatMessage }.filter { it.messageType in ChatMessage.CONFEDERATION_PROFILE_TYPES } + + @Test + fun `a change from another group is a line in each of this device's member groups' rooms, that opens it`() = runBlocking { + ConfederationProfileManager.recordStatement(db, changeRumor, seal(agora, changeRumor), npub(polis)) + listOf(polis, ekklesia).forEach { room -> + val line = lines(room).single() + assertEquals(ChatMessage.TYPE_CONFEDERATION_PROFILE_PROPOSED, line.messageType) + assertEquals("${ConfederationManager.nameOf(db, npub(agora))} proposes a new profile for Atlas", line.content) + assertEquals(change.id, line.frostSigningSessionId, "the line opens the change") + } + // Heard again, nothing new. + ConfederationProfileManager.recordStatement(db, changeRumor, seal(agora, changeRumor), npub(polis)) + assertEquals(1, lines(polis).size) + } + + @Test + fun `an answer that arrives before its change gets its line when the change does`() = runBlocking { + val objection = ConfederationProfileStatements.objection(npub(ekklesia), change, "Not that name", now + 5) + ConfederationProfileManager.recordStatement(db, objection, seal(ekklesia, objection), npub(polis)) + assertEquals(emptyList(), lines(polis)) + + ConfederationProfileManager.recordStatement(db, changeRumor, seal(agora, changeRumor), npub(polis)) + val polisLines = lines(polis).sortedBy { it.createdAt } + assertEquals( + listOf(ChatMessage.TYPE_CONFEDERATION_PROFILE_PROPOSED, ChatMessage.TYPE_CONFEDERATION_PROFILE_OBJECTED), + polisLines.map { it.messageType }, + ) + assertEquals("${ConfederationManager.nameOf(db, npub(ekklesia))} objected to ${ConfederationManager.nameOf(db, npub(agora))}'s change to Atlas's profile: Not that name", polisLines[1].content) + assertEquals("This group objected to ${ConfederationManager.nameOf(db, npub(agora))}'s change to Atlas's profile: Not that name", lines(ekklesia).single { it.messageType == ChatMessage.TYPE_CONFEDERATION_PROFILE_OBJECTED }.content) + } + + @Test + fun `another group proposing notifies, and the answers are quiet`() { + assertEquals(true, ChatMessage.TYPE_CONFEDERATION_PROFILE_PROPOSED in NotificationClassifier.GROUP_ACTIVITY) + assertEquals(true, ChatMessage.TYPE_CONFEDERATION_PROFILE_PUBLISHED in NotificationClassifier.GROUP_ACTIVITY) + assertEquals(true, ChatMessage.TYPE_CONFEDERATION_PROFILE_ENDORSED in NotificationClassifier.SILENT) + assertEquals(true, ChatMessage.TYPE_CONFEDERATION_PROFILE_OBJECTED in NotificationClassifier.SILENT) + } +} diff --git a/docs/confederation-profile.md b/docs/confederation-profile.md index f5c6d71a..703a12f1 100644 --- a/docs/confederation-profile.md +++ b/docs/confederation-profile.md @@ -504,6 +504,15 @@ or after it closed). Where it differs from the above: Tests: `ConfederationProfileReceiveJvmTest`, covering out-of-order arrival, a seal from a group that is not a member (dropped and logged), and a duplicate. +**Built.** `ConfederationProfileReceiveJvmTest` 3/3: a change from another group is one line in each +of this device's member groups' rooms, carrying the change's id; heard twice, still one; an objection +that arrives before its change gets its line when the change does; the classifier's two sets. The +routing itself landed in Phase 2. A seal that is not its rumor's author's is dropped by +`recordStatement`, tested in Phase 1's storage test. The lines carry the change's id in +`frostSigningSessionId`, the column the confederation's signing lines already use for an attempt's +id, so no schema change: Phase 4 makes them open the change. `TYPE_CONFEDERATION_PROFILE_PROPOSED` +and `…_PUBLISHED` are group activity; `…_ENDORSED` and `…_OBJECTED` are silent. + ### Phase 4: the screens (D1, D2, D3) - `ConfederationScreen` + `ConfederationViewModel` + `ConfederationUIState` (loading, error,