Chat room creation
This commit is contained in:
1 parent
60bb70c641
commit
55198de44e
14 files changed
+414
-118
No files matched your search
@@ -28,36 +28,36 @@ abstract class NostrNip17Dao(
|
||||
val logger = Logger.withTag(TAG)
|
||||
|
||||
@Transaction
|
||||
open suspend fun getOrCreateChatRoom(activeUserPublicKey: HexKey, publicKey: String, relayHint: String?, defaultSubject: String? = null): at.torch.compose.database.model.intermdiate.LocalChatRoom? {
|
||||
logger.d("getOrCreateChatRoom: $publicKey")
|
||||
open suspend fun getOrCreateChatRoom(chatRoomId: String, activeUserPublicKey: HexKey, relayHint: String?, defaultSubject: String? = null, mlsGroupState: String? = null): LocalChatRoom? {
|
||||
logger.d("getOrCreateChatRoom: $chatRoomId")
|
||||
|
||||
|
||||
val hexKeys = setOf(
|
||||
activeUserPublicKey,
|
||||
publicKey
|
||||
// publicKey
|
||||
)
|
||||
|
||||
val chatRoomId = _root_ide_package_.at.torch.compose.database.model.ChatRoom.Companion.deriveChatRoomId(hexKeys)
|
||||
logger.d("chatRoomId: $chatRoomId")
|
||||
val localChatRoom = database.chatRoomDao().findChatRoomById(chatRoomId)
|
||||
|
||||
if (localChatRoom != null) {
|
||||
// TODO: might want to update the mlsGroupState?
|
||||
return localChatRoom
|
||||
} else {
|
||||
val profiles = database.profileDao().getProfileByPublicKeys(hexKeys.toList())
|
||||
|
||||
if (profiles.isNotEmpty()) {
|
||||
// Create new chatRoom
|
||||
val chatRoom = _root_ide_package_.at.torch.compose.database.model.ChatRoom(
|
||||
val chatRoom = ChatRoom(
|
||||
id = chatRoomId,
|
||||
userPublicKey = activeUserPublicKey,
|
||||
subject = defaultSubject,
|
||||
mlsGroupState = mlsGroupState
|
||||
)
|
||||
database.chatRoomDao().upsert(chatRoom)
|
||||
|
||||
database.participantDao().upsert(
|
||||
profiles.map {
|
||||
_root_ide_package_.at.torch.compose.database.model.Participant(
|
||||
Participant(
|
||||
participantPublicKey = it.publicKey,
|
||||
chatRoomId = chatRoomId,
|
||||
relayHint = relayHint
|
||||
@@ -65,7 +65,7 @@ abstract class NostrNip17Dao(
|
||||
}
|
||||
)
|
||||
|
||||
return _root_ide_package_.at.torch.compose.database.model.intermdiate.LocalChatRoom(
|
||||
return LocalChatRoom(
|
||||
chatRoom = chatRoom
|
||||
)
|
||||
}
|
||||
@@ -81,7 +81,7 @@ abstract class NostrNip17Dao(
|
||||
giftWrapEvent: GiftWrapEvent,
|
||||
receiverPublicKey: PTag
|
||||
) {
|
||||
val nostrEvent = _root_ide_package_.at.torch.compose.database.model.NostrEvent(
|
||||
val nostrEvent = NostrEvent(
|
||||
id = giftWrapEvent.id,
|
||||
content = giftWrapEvent.content,
|
||||
createdAt = Instant.fromEpochSeconds(giftWrapEvent.createdAt),
|
||||
@@ -99,7 +99,7 @@ abstract class NostrNip17Dao(
|
||||
|
||||
chatMessage?.let {
|
||||
database.chatMessageNostrEventRelationDao().upsert(
|
||||
_root_ide_package_.at.torch.compose.database.model.ChatMessageNostrEventRelation(
|
||||
ChatMessageNostrEventRelation(
|
||||
chatMessageId = it.id,
|
||||
nostrEventId = nostrEvent.id
|
||||
)
|
||||
@@ -112,7 +112,7 @@ abstract class NostrNip17Dao(
|
||||
authors = arrayOf(
|
||||
receiverPublicKey.pubKey
|
||||
),
|
||||
since = _root_ide_package_.at.torch.compose.database.GENESIS_AT,
|
||||
since = GENESIS_AT,
|
||||
limit = 5
|
||||
)
|
||||
logger.d("Found messageRelayLists: $chatMessageRelayListEvents")
|
||||
@@ -134,7 +134,7 @@ abstract class NostrNip17Dao(
|
||||
logger.d("Submit BroadcastNostrEventRequest to $relayUrl")
|
||||
val broadcastNostrEventRequestIds = database.broadcastNostrEventRequestDao().insert(
|
||||
listOf(
|
||||
_root_ide_package_.at.torch.compose.database.model.BroadcastNostrEventRequest(
|
||||
BroadcastNostrEventRequest(
|
||||
nostrEventId = nostrEvent.id,
|
||||
relayURL = relayUrl.url
|
||||
)
|
||||
@@ -144,7 +144,7 @@ abstract class NostrNip17Dao(
|
||||
chatMessage?.let {
|
||||
broadcastNostrEventRequestIds.forEach {
|
||||
database.chatMessageBroadcastNostrEventRequestRelationDao().upsert(
|
||||
_root_ide_package_.at.torch.compose.database.model.ChatMessageBroadcastNostrEventRequestRelation(
|
||||
ChatMessageBroadcastNostrEventRequestRelation(
|
||||
chatMessageId = chatMessage.id,
|
||||
broadcastNostrEventRequestId = it
|
||||
)
|
||||
@@ -159,7 +159,7 @@ abstract class NostrNip17Dao(
|
||||
|
||||
|
||||
database.giftWrapMessageDao().upsert(
|
||||
_root_ide_package_.at.torch.compose.database.model.GiftWrapMessage(
|
||||
GiftWrapMessage(
|
||||
id = giftWrapEvent.id,
|
||||
nostrEventId = nostrEvent.id,
|
||||
content = giftWrapEvent.content,
|
||||
@@ -173,7 +173,7 @@ abstract class NostrNip17Dao(
|
||||
)
|
||||
|
||||
database.giftWrapSealDao().upsert(
|
||||
_root_ide_package_.at.torch.compose.database.model.GiftWrapSeal(
|
||||
GiftWrapSeal(
|
||||
id = sealedRumorEvent.id,
|
||||
publicKey = sealedRumorEvent.pubKey,
|
||||
content = sealedRumorEvent.content,
|
||||
|
||||
@@ -52,6 +52,8 @@ data class ChatRoom(
|
||||
*/
|
||||
val subject: String?,
|
||||
|
||||
val mlsGroupState: String?, // TODO: Can this be nullable???
|
||||
|
||||
/**
|
||||
* Derived from kind14/15 tags
|
||||
* [
|
||||
|
||||
+7
-4
@@ -4,6 +4,7 @@ import at.torch.compose.database.GENESIS_AT
|
||||
import at.torch.compose.database.model.ChatMessage
|
||||
import at.torch.compose.database.model.GiftWrapPayload
|
||||
import co.touchlab.kermit.Logger
|
||||
import com.vitorpamplona.quartz.marmot.mls.group.MlsGroupState
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Kind
|
||||
@@ -45,16 +46,18 @@ class DatabaseChatRepository(
|
||||
|
||||
|
||||
override suspend fun getOrCreateChatRoom(
|
||||
chatRoomId: String,
|
||||
activeUserPublicKey: HexKey,
|
||||
publicKey: String,
|
||||
relayHint: String?,
|
||||
defaultSubject: String?
|
||||
defaultSubject: String?,
|
||||
mlsGroupState: String?
|
||||
): at.torch.compose.database.model.intermdiate.LocalChatRoom? = try {
|
||||
return database.nostrNip17Dao().getOrCreateChatRoom(
|
||||
chatRoomId = chatRoomId,
|
||||
activeUserPublicKey = activeUserPublicKey,
|
||||
publicKey = publicKey,
|
||||
relayHint = relayHint,
|
||||
defaultSubject = defaultSubject
|
||||
defaultSubject = defaultSubject,
|
||||
mlsGroupState = mlsGroupState
|
||||
)
|
||||
} catch (e: Throwable) {
|
||||
logger.e("Error getting or creating chat", e)
|
||||
|
||||
+7
-34
@@ -8,6 +8,7 @@ import at.torch.compose.database.model.UnsignedNostrEvent
|
||||
import at.torch.compose.nostr.Relays
|
||||
import co.touchlab.kermit.Logger
|
||||
import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageEvent
|
||||
import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageRelayListEvent
|
||||
import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageRotationManager.Companion.KEY_PACKAGE_LIFETIME_SECONDS
|
||||
import com.vitorpamplona.quartz.marmot.mip00KeyPackages.KeyPackageUtils
|
||||
import com.vitorpamplona.quartz.marmot.mls.crypto.Ed25519
|
||||
@@ -161,10 +162,11 @@ class DatabaseNostrRepository(
|
||||
)
|
||||
|
||||
unsignedNostrEvents.add(
|
||||
_root_ide_package_.at.torch.compose.database.model.UnsignedNostrEvent(
|
||||
UnsignedNostrEvent(
|
||||
pubKey = publicKey,
|
||||
kind = AdvertisedRelayListEvent.KIND,
|
||||
tags = _root_ide_package_.at.torch.compose.nostr.Relays.DefaultNIP65List.map { it.toTagArray() }
|
||||
kind = KeyPackageRelayListEvent.KIND,
|
||||
tags = Relays.DefaultDMRelayList.map { arrayOf("relay", it.url) }
|
||||
.plusElement(AltTag.assemble(KeyPackageRelayListEvent.ALT_DESCRIPTION))
|
||||
.toTypedArray(),
|
||||
content = ""
|
||||
)
|
||||
@@ -173,10 +175,10 @@ class DatabaseNostrRepository(
|
||||
|
||||
|
||||
unsignedNostrEvents.add(
|
||||
_root_ide_package_.at.torch.compose.database.model.UnsignedNostrEvent(
|
||||
UnsignedNostrEvent(
|
||||
pubKey = publicKey,
|
||||
kind = SearchRelayListEvent.KIND,
|
||||
tags = _root_ide_package_.at.torch.compose.nostr.Relays.DefaultSearchRelayList.map {
|
||||
tags = Relays.DefaultSearchRelayList.map {
|
||||
RelayTag.assemble(
|
||||
it
|
||||
)
|
||||
@@ -189,35 +191,6 @@ class DatabaseNostrRepository(
|
||||
)
|
||||
)
|
||||
|
||||
unsignedNostrEvents.add(
|
||||
_root_ide_package_.at.torch.compose.database.model.UnsignedNostrEvent(
|
||||
pubKey = publicKey,
|
||||
kind = IndexerRelayListEvent.KIND,
|
||||
tags = arrayOf(AltTag.assemble(IndexerRelayListEvent.ALT)),
|
||||
privateTags = _root_ide_package_.at.torch.compose.nostr.Relays.DefaultIndexerRelayList.map {
|
||||
RelayTag.assemble(
|
||||
it
|
||||
)
|
||||
}.toTypedArray(),
|
||||
content = ""
|
||||
)
|
||||
)
|
||||
|
||||
unsignedNostrEvents.add(
|
||||
_root_ide_package_.at.torch.compose.database.model.UnsignedNostrEvent(
|
||||
pubKey = publicKey,
|
||||
kind = ChannelListEvent.KIND,
|
||||
tags = arrayOf(
|
||||
AltTag.assemble(
|
||||
ChannelListEvent.ALT
|
||||
)
|
||||
),
|
||||
privateTags = _root_ide_package_.at.torch.compose.nostr.Relays.DefaultChannels.map { it.toTagArray() }
|
||||
.toTypedArray(),
|
||||
content = ""
|
||||
)
|
||||
)
|
||||
|
||||
unsignedNostrEvents.add(
|
||||
UnsignedNostrEvent(
|
||||
pubKey = publicKey,
|
||||
|
||||
Reference in new issue
Block a user