From f3c7e21fa359273aadf9cb6f022b7aaeda245cd5 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 6 Jun 2026 04:25:53 +0200 Subject: [PATCH] Also send a giftWrapMessage to self for message history --- .../1.json | 12 ++++-- .../compose/database/dao/ParticipantDao.kt | 2 + .../compose/database/model/GiftWrapPayload.kt | 2 + .../compose/database/model/GiftWrapSeal.kt | 8 +++- .../repository/DatabaseChatRepository.kt | 40 +++++++++++-------- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/composeApp/schemas/ac.cord.auxiliary.compose.database.AuxDatabase/1.json b/composeApp/schemas/ac.cord.auxiliary.compose.database.AuxDatabase/1.json index 505de9b9..18638e11 100644 --- a/composeApp/schemas/ac.cord.auxiliary.compose.database.AuxDatabase/1.json +++ b/composeApp/schemas/ac.cord.auxiliary.compose.database.AuxDatabase/1.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "7aff70ac118cd5856e8a4939d62a2ed8", + "identityHash": "630d9cf4853f9f393e7e043f4cd03a6d", "entities": [ { "tableName": "BroadcastNostrEventReceipt", @@ -1007,7 +1007,7 @@ }, { "tableName": "GiftWrapPayload", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `publicKey` TEXT NOT NULL, `kind` INTEGER NOT NULL, `tags` TEXT NOT NULL, `content` TEXT NOT NULL, `quotedEventId` TEXT, `giftWrapSealId` TEXT, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, `savedAt` INTEGER NOT NULL, `viewedAt` INTEGER, `deletedAt` INTEGER, FOREIGN KEY(`giftWrapSealId`) REFERENCES `GiftWrapSeal`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `publicKey` TEXT NOT NULL, `chatRoomId` TEXT NOT NULL, `kind` INTEGER NOT NULL, `tags` TEXT NOT NULL, `content` TEXT NOT NULL, `quotedEventId` TEXT, `giftWrapSealId` TEXT, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, `savedAt` INTEGER NOT NULL, `viewedAt` INTEGER, `deletedAt` INTEGER, FOREIGN KEY(`giftWrapSealId`) REFERENCES `GiftWrapSeal`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", "fields": [ { "fieldPath": "id", @@ -1021,6 +1021,12 @@ "affinity": "TEXT", "notNull": true }, + { + "fieldPath": "chatRoomId", + "columnName": "chatRoomId", + "affinity": "TEXT", + "notNull": true + }, { "fieldPath": "kind", "columnName": "kind", @@ -2857,7 +2863,7 @@ ], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7aff70ac118cd5856e8a4939d62a2ed8')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '630d9cf4853f9f393e7e043f4cd03a6d')" ] } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/ParticipantDao.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/ParticipantDao.kt index 808c6444..983034f5 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/ParticipantDao.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/dao/ParticipantDao.kt @@ -15,6 +15,8 @@ interface ParticipantDao { @Query("SELECT * FROM Participant WHERE participantPublicKey = :participantPublicKey") fun findParticipantByPublicKey(participantPublicKey: String): Participant? + @Query("SELECT * FROM Participant WHERE chatRoomId = :chatRoomId") + fun findParticipantsByChatRoomId(chatRoomId: String): List @Upsert suspend fun upsert(participants: List) diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapPayload.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapPayload.kt index c3b2d36f..f7c10c39 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapPayload.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapPayload.kt @@ -37,6 +37,8 @@ data class GiftWrapPayload( // TODO: Rename this to GiftWrapPayload... */ val publicKey: String, + val chatRoomId: String, + /** * Payload Event Kind */ diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapSeal.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapSeal.kt index 6aab42d3..c552b093 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapSeal.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/model/GiftWrapSeal.kt @@ -95,14 +95,20 @@ data class GiftWrapSeal( )?.let { giftWrapPayload -> logger.d("giftWrapPayload: ${giftWrapPayload.toJson()}") - return GiftWrapPayload( + val giftWrapPayload = GiftWrapPayload( giftWrapSealId = id, publicKey = giftWrapPayload.pubKey, kind = giftWrapPayload.kind, tags = giftWrapPayload.tags, content = giftWrapPayload.content, createdAt = Instant.fromEpochSeconds(giftWrapPayload.createdAt), + chatRoomId = "" ) + return giftWrapPayload.aggregatedParticipantsPublicKey()?.let { + giftWrapPayload.copy( + chatRoomId = it + ) + } } } diff --git a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/repository/DatabaseChatRepository.kt index b07d0c95..fdc1e09f 100644 --- a/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/repository/DatabaseChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/ac/cord/auxiliary/compose/database/repository/DatabaseChatRepository.kt @@ -113,6 +113,7 @@ class DatabaseChatRepository( kind = messageType, tags = receiverTags.toTypedArray(), content = text, + chatRoomId = localChatRoom.chatRoom.id, publicKey = localChatRoom.chatRoom.userPublicKey ) ) @@ -143,21 +144,22 @@ class DatabaseChatRepository( giftWrapPayload: GiftWrapPayload, nostrSignerSync: NostrSignerSync ) { - val wrapperKeyPair = KeyPair() - val wrapperSigner = NostrSignerSync( - keyPair = wrapperKeyPair - ) + database.participantDao().findParticipantsByChatRoomId(giftWrapPayload.chatRoomId).forEachIndexed { index, participant -> + val wrapperKeyPair = KeyPair() + val wrapperSigner = NostrSignerSync( + keyPair = wrapperKeyPair + ) - val giftWrapPayloadEvent = nostrSignerSync.signNormal( - createdAt = giftWrapPayload.createdAt.epochSeconds, - kind = giftWrapPayload.kind, - tags = giftWrapPayload.tags, - content = giftWrapPayload.content - ) - logger.d("GiftWrap Payload Event: ${giftWrapPayloadEvent.toJson()}") + val giftWrapPayloadEvent = nostrSignerSync.signNormal( + createdAt = giftWrapPayload.createdAt.epochSeconds, + kind = giftWrapPayload.kind, + tags = giftWrapPayload.tags, + content = giftWrapPayload.content + ) + logger.d("GiftWrap Payload Event: ${giftWrapPayloadEvent.toJson()}") - giftWrapPayload.tags.taggedUsers().forEachIndexed { index, receiverPublicKey -> - logger.d("Seal to $receiverPublicKey") + + logger.d("Seal to $participant") val payload = giftWrapPayloadEvent.toJson() val sealedRumorEvent = nostrSignerSync.signNormal( @@ -166,7 +168,7 @@ class DatabaseChatRepository( tags = emptyArray(), content = nostrSignerSync.nip44Encrypt( plaintext = payload, - toPublicKey = receiverPublicKey.pubKey + toPublicKey = participant.participantPublicKey ) ) logger.d("SealedRumorEvent: ${sealedRumorEvent.toJson()}") @@ -175,11 +177,11 @@ class DatabaseChatRepository( createdAt = TimeUtils.randomWithTwoDays(), kind = GiftWrapEvent.KIND, tags = arrayOf( - PTag.assemble(receiverPublicKey.pubKey, receiverPublicKey.relayHint) + PTag.assemble(participant.participantPublicKey, participant.relayHint?.let { NormalizedRelayUrl(it) }) ), content = wrapperSigner.nip44Encrypt( plaintext = sealedRumorEvent.toJson(), - toPublicKey = receiverPublicKey.pubKey + toPublicKey = participant.participantPublicKey ) ) logger.d("giftWrapEvent: ${giftWrapEvent.toJson()}") @@ -191,9 +193,13 @@ class DatabaseChatRepository( ), sealedRumorEvent = sealedRumorEvent, giftWrapEvent = giftWrapEvent, - receiverPublicKey = receiverPublicKey + receiverPublicKey = PTag( + pubKey = participant.participantPublicKey, + relayHint = participant.relayHint?.let { NormalizedRelayUrl(it) } + ) ) } + } companion object {