fix: stand up the chat room a ChillDKG proposal arrives for

Creating a NIP-17 group sends nothing to anybody. Membership under NIP-17 *is*
the p-tag set on each message, so the group only materialises on the other
members' devices when the first gift wrap lands. The chat-message branch of the
inbound path knows this and builds the room from the arriving payload's p-tags;
the ChillDKG branch did not. It looked the room up, found nothing, logged "DKG
payload for unknown chat room" and dropped the message.

That is fatal for the flow the feature is reached through.
SelectChatRoomTypeViewModel.createNip17ChatRoom writes the room and its
participants locally and navigates straight into the chat without publishing
anything, and ChatRoomDetailScreen offers "Shared Key" for exactly these rooms
(mlsGroupState == null). So "create group -> Shared Key -> Start key ceremony"
makes the ritual's own proposal the first event the group is ever heard of, and
every recipient dropped it. Nobody joined, and the coordinator sat on one host
key -- its own -- forever.

getOrCreateNip17ChatRoom builds the room the way the chat branch does: the
payload's p-tags plus its sender. That set is the aggregate ChatRoom.id is
derived from in the first place, so any payload that routes here already carries
the whole membership and there is nothing else to wait for. Placeholder profiles
are inserted first because both ChatRoom.userPublicKey and
Participant.participantPublicKey are foreign keys onto Profile, and a payload
whose membership does not include this device is refused rather than used to
build a room we are not a member of.

Deliberately not shared with the chat branch: that block also queues relay-list
and profile synchronisation per participant, which is best-effort enrichment
tangled into the surrounding loop's profilePublicKeysToSync map. Lifting it out
is worth doing on its own, not inside a fix whose job is to make the ceremony
reachable at all.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-09-05 00:12:58 +02:00
parent a611277d5d
commit 739314ccb4

View File

@@ -8,6 +8,7 @@ import press.mantra.compose.database.model.ChatMessage
import press.mantra.compose.database.model.ChatMessageBroadcastNostrEventRequestRelation
import press.mantra.compose.database.model.ChatRoom
import press.mantra.compose.database.model.Connection
import press.mantra.compose.database.model.GiftWrapPayload
import press.mantra.compose.database.model.MarmotGroupEvent
import press.mantra.compose.database.model.MarmotKeyPackage
import press.mantra.compose.database.model.Mention
@@ -17,6 +18,7 @@ import press.mantra.compose.database.model.NostrEventRelay
import press.mantra.compose.database.model.Participant
import press.mantra.compose.database.model.Post
import press.mantra.compose.database.model.Profile
import press.mantra.compose.database.model.intermdiate.LocalChatRoom
import press.mantra.compose.database.model.types.SynchronizationFilter
import press.mantra.compose.exceptions.GiftWrapImpersonationException
import press.mantra.compose.exceptions.GiftWrapSealDecryptionException
@@ -925,8 +927,17 @@ abstract class NostrDao(
// A ChillDKG ritual message for one of our NIP-17 groups.
// The manager is idempotent, so a redelivered message
// simply re-runs a step it has already taken.
val localChatRoom = database.chatRoomDao().findChatRoomById(
decryptedGiftWrapPayload.chatRoomId
//
// The room is created on demand for the same reason a
// chat message creates one: standing up a NIP-17 group
// sends nothing to anybody, so the ritual's proposal is
// routinely the first this group is heard of. Membership
// is the payload's p-tags either way.
val localChatRoom = getOrCreateNip17ChatRoom(
decryptedGiftWrapPayload = decryptedGiftWrapPayload,
activeKeyPair = activeKeyPair,
nostrEventId = nostrEvent.id,
relayURL = relayURL
)
if (localChatRoom == null) {
@@ -1227,6 +1238,70 @@ abstract class NostrDao(
}
/**
* The NIP-17 room a decrypted payload belongs to, standing it up if this is the
* first the device has heard of the group.
*
* Membership under NIP-17 *is* the p-tag set on the message, and the room id is
* the aggregate of it, so any payload that routes here carries everything the
* room needs. Creating a group locally sends nothing to anyone, which is why the
* first arrival for a group is just as likely to be a key ceremony as a message.
*/
private suspend fun getOrCreateNip17ChatRoom(
decryptedGiftWrapPayload: GiftWrapPayload,
activeKeyPair: KeyPair,
nostrEventId: String,
relayURL: String
): LocalChatRoom? {
database.chatRoomDao().findChatRoomById(decryptedGiftWrapPayload.chatRoomId)?.let { return it }
val participants = decryptedGiftWrapPayload.participantPTags(
NormalizedRelayUrl(relayURL) // TODO: Get relayURL for publicKey profile...
).map {
Participant(
participantPublicKey = it.pubKey,
chatRoomId = decryptedGiftWrapPayload.chatRoomId,
relayHint = it.relayHint?.url
)
}
if (participants.none { it.participantPublicKey == activeKeyPair.pubKey.toHex() }) {
logger.w("Payload ${decryptedGiftWrapPayload.id} builds a room this device isn't in")
return null
}
// Both ChatRoom.userPublicKey and Participant.participantPublicKey are
// foreign keys onto Profile, so every member needs a row before either.
participants.forEach { participant ->
if (database.profileDao().getProfileByPublicKey(participant.participantPublicKey) == null) {
database.profileDao().insertPlaceholderProfile(
Profile(
displayName = "LOADING...",
publicKey = participant.participantPublicKey,
createdAt = GENESIS_AT,
nostrEventId = nostrEventId, // Will get overwritten by sync
)
)
}
}
database.chatRoomDao().upsert(
ChatRoom(
id = decryptedGiftWrapPayload.chatRoomId,
userPublicKey = activeKeyPair.pubKey.toHex(),
subject = decryptedGiftWrapPayload.parseSubject(),
description = null,
createdAt = decryptedGiftWrapPayload.createdAt,
initialGiftWrapPayloadId = decryptedGiftWrapPayload.id,
mlsGroupState = null
)
)
database.participantDao().upsert(participants)
return database.chatRoomDao().findChatRoomById(decryptedGiftWrapPayload.chatRoomId)
}
/**
* Persist the rescheduling using a transaction so that we are assured of the existence of the
* [ChatMessageBroadcastNostrEventRequestRelation] when we produce the [press.mantra.compose.database.model.BroadcastNostrEventReceipt]