Also send a giftWrapMessage to self for message history

This commit is contained in:
Kgothatso Ngako
2026-06-06 04:25:53 +02:00
parent f67ebd60b2
commit f3c7e21fa3
5 changed files with 43 additions and 21 deletions

View File

@@ -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')"
]
}
}

View File

@@ -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<Participant>
@Upsert
suspend fun upsert(participants: List<Participant>)

View File

@@ -37,6 +37,8 @@ data class GiftWrapPayload( // TODO: Rename this to GiftWrapPayload...
*/
val publicKey: String,
val chatRoomId: String,
/**
* Payload Event Kind
*/

View File

@@ -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
)
}
}
}

View File

@@ -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<Event>(
createdAt = giftWrapPayload.createdAt.epochSeconds,
kind = giftWrapPayload.kind,
tags = giftWrapPayload.tags,
content = giftWrapPayload.content
)
logger.d("GiftWrap Payload Event: ${giftWrapPayloadEvent.toJson()}")
val giftWrapPayloadEvent = nostrSignerSync.signNormal<Event>(
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<SealedRumorEvent>(
@@ -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 {