feat: give a queued message somewhere to say who it is private to

Two nullable columns and the v5 migration that adds them, ahead of the
code that fills them, so the schema lands on its own and can be reverted
on its own.

MarmotInnerEvent.directMessageRecipientPublicKey is the outbound signal.
The notary reads a queued row and has no other way to know a message is
meant for one member rather than the room -- the plaintext is identical
either way -- so this is what routes it into the gift wrap path. Inbound
rows leave it null on purpose: the recipient is on the wrap's `p` tag,
which is where every member reads it from, so a second copy on the row
would be a second thing that can disagree.

ChatMessage.directMessageRecipientPublicKey is what the transcript reads.
Both lines a direct message can produce need it -- the one its two parties
see, and the "sent a private message to Bob" line everybody else gets --
and holding it on the row keeps the view model off a join for a fact it
already has to render.

MarmotInnerEvent hand-writes equals and hashCode over every field, so both
are extended too. A field missing from those is not a compile error and
not a test failure; it is two rows that differ comparing equal, which
surfaces much later as an upsert that does nothing.

Room generates the migration -- verified as two ADD COLUMNs with no table
rebuild, so nothing is copied and nothing can be dropped:

  ALTER TABLE `ChatMessage` ADD COLUMN `directMessageRecipientPublicKey` TEXT DEFAULT NULL
  ALTER TABLE `MarmotInnerEvent` ADD COLUMN `directMessageRecipientPublicKey` TEXT DEFAULT NULL

Rows written before this come back null, which reads as "not a direct
message" -- the only answer that is true of all of them.

v5 is an AutoMigration entry rather than a hand-written Migration like
MIGRATION_3_4 next to it, because that one rewrote data without changing
shape and this one changes shape without touching data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-09-05 18:58:57 +02:00
parent 79e99ae702
commit 296011dcd5
4 changed files with 5086 additions and 2 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -164,7 +164,7 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
UnsignedNostrEvent::class,
Zap::class
],
version = 4,
version = 5,
autoMigrations = [
// v2 only adds the DkgSession/DkgParticipantMessage tables, so Room can
// generate the migration itself — nothing existing changes shape.
@@ -173,11 +173,16 @@ val GENESIS_AT = Instant.fromEpochMilliseconds(1231006505000L)
// additions need no default and drop no data, so Room generates this one
// too. Rituals already in flight come back with all three null, which reads
// as "not approved yet" and simply asks the member for each step.
AutoMigration(from = 2, to = 3)
AutoMigration(from = 2, to = 3),
// v4 changes no schema at all -- it rewrites `dkgApprovalNeeded` chat rows
// into one type per ritual step. Data, not shape, so it is a manual
// migration passed to the builder rather than an entry here. See
// MIGRATION_3_4.
// v5 adds a nullable direct message recipient to MarmotInnerEvent and
// ChatMessage. Nullable additions need no default and drop no data, so Room
// generates this one. Rows written before it come back null, which reads as
// "not a direct message" -- the only answer that is true of all of them.
AutoMigration(from = 4, to = 5),
]
)
@ColumnTypeConverters(MantraConverters::class)

View File

@@ -77,6 +77,15 @@ data class ChatMessage(
val content: String,
val messageType: String = "message",
/**
* Who a direct message was for, or null for anything else.
*
* Carried on the row rather than read back off the wrap so the transcript can name
* the recipient without a join, on both the line the two of them can read and the
* line everybody else gets. See docs/marmot-direct-messages.md.
*/
val directMessageRecipientPublicKey: HexKey? = null,
override val createdAt: Instant = Clock.System.now(),
override val updatedAt: Instant = createdAt,
override val savedAt: Instant = Clock.System.now(),

View File

@@ -69,6 +69,18 @@ data class MarmotInnerEvent( // TODO: Rename this to GiftWrapPayload...
*/
val marmotGroupEventId: String? = null,
/**
* Who this is a direct message to, or null for an ordinary group message.
*
* Set on the way out only. It is what tells the notary to gift wrap this row for one
* member instead of sending it to the whole group, and afterwards what tells the
* transcript the row was private. Inbound rows leave it null -- the recipient is on
* the wrap's `p` tag, which is where every member reads it from.
*
* See docs/marmot-direct-messages.md.
*/
val directMessageRecipientPublicKey: HexKey? = null,
/**
* Current time
*/
@@ -118,6 +130,7 @@ data class MarmotInnerEvent( // TODO: Rename this to GiftWrapPayload...
if (content != other.content) return false
if (quotedEventId != other.quotedEventId) return false
if (marmotGroupEventId != other.marmotGroupEventId) return false
if (directMessageRecipientPublicKey != other.directMessageRecipientPublicKey) return false
if (createdAt != other.createdAt) return false
if (updatedAt != other.updatedAt) return false
if (savedAt != other.savedAt) return false
@@ -137,6 +150,7 @@ data class MarmotInnerEvent( // TODO: Rename this to GiftWrapPayload...
result = 31 * result + content.hashCode()
result = 31 * result + (quotedEventId?.hashCode() ?: 0)
result = 31 * result + (marmotGroupEventId?.hashCode() ?: 0)
result = 31 * result + (directMessageRecipientPublicKey?.hashCode() ?: 0)
result = 31 * result + createdAt.hashCode()
result = 31 * result + updatedAt.hashCode()
result = 31 * result + savedAt.hashCode()