Basic logic for adding new artifact
This commit is contained in:
@@ -5,6 +5,8 @@ import androidx.room3.ForeignKey
|
||||
import androidx.room3.PrimaryKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
|
||||
import com.vitorpamplona.quartz.nip31Alts.AltTag
|
||||
import press.mantra.compose.database.model.traits.OptionalNostrEventEntity
|
||||
import press.mantra.compose.database.model.traits.TimestampedEntity
|
||||
@@ -87,6 +89,25 @@ data class MantraArtifact(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun fromArtifactEventTemplate(artifactEventTemplate: EventTemplate<ArtifactEvent>, chatRoomId: HexKey, userPublicKey: HexKey): MantraArtifact? {
|
||||
return fromArtifactEvent(
|
||||
ArtifactEvent(
|
||||
id = EventHasher.hashId(
|
||||
pubKey = userPublicKey, // TODO: get pub key...
|
||||
tags = artifactEventTemplate.tags,
|
||||
content = artifactEventTemplate.content,
|
||||
createdAt = artifactEventTemplate.createdAt,
|
||||
kind = artifactEventTemplate.kind
|
||||
),
|
||||
content = artifactEventTemplate.content,
|
||||
tags = artifactEventTemplate.tags,
|
||||
createdAt = artifactEventTemplate.createdAt,
|
||||
pubKey = userPublicKey,
|
||||
sig = "" // Unsigned....
|
||||
),
|
||||
chatRoomId = chatRoomId
|
||||
)
|
||||
}
|
||||
fun fromArtifactEvent(artifactEvent: ArtifactEvent, chatRoomId: HexKey, ): MantraArtifact? {
|
||||
return artifactEvent.artifactMetadata?.let { artifactMetadata ->
|
||||
artifactEvent.dialectId()?.let { dialectId ->
|
||||
|
||||
@@ -1,14 +1,63 @@
|
||||
package press.mantra.compose.database.repository
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import press.mantra.compose.database.MantraDatabase
|
||||
import press.mantra.compose.database.model.MantraArtifact
|
||||
import press.mantra.compose.database.model.MarmotInnerEvent
|
||||
import press.mantra.compose.nostr.nip30303.ArtifactEvent
|
||||
import press.mantra.compose.repository.MantraRepository
|
||||
import kotlin.time.Instant
|
||||
|
||||
class DatabaseMantraRepository(
|
||||
val database: MantraDatabase,
|
||||
val scope: CoroutineScope
|
||||
): MantraRepository {
|
||||
override fun addArtifact(name: String, url: String, versionLabel: String) {
|
||||
TODO("Implement add artifact")
|
||||
override suspend fun addArtifact(name: String, url: String, versionLabel: String): MarmotInnerEvent? {
|
||||
// Check that user is admin...
|
||||
// TODO: Call mantraDao.addArtifact
|
||||
val artifactEventTemplate = ArtifactEvent.build(
|
||||
name = name,
|
||||
url = url,
|
||||
visibility = "private",
|
||||
license = "cc",
|
||||
dialectId = "7de5ab261eac58a01bd46953b5a93b4f80e9501ab01c4adcc9338d8e7507a98d", // TODO: Get this as a paramter...
|
||||
)
|
||||
// TODO: Save this as an marmotInner event...
|
||||
|
||||
MantraArtifact.fromArtifactEventTemplate(
|
||||
artifactEventTemplate = artifactEventTemplate,
|
||||
chatRoomId = "",
|
||||
userPublicKey = ""
|
||||
)?.let { mantraArtifact ->
|
||||
database.mantraArtifactDao().upsert(
|
||||
mantraArtifact
|
||||
)
|
||||
|
||||
// If this is private we create a marmot inner event... if its permissioned we create a marmot publicMessage? if it's public we publish a nostr event?
|
||||
database.marmotInnerEventDao().upsert(
|
||||
MarmotInnerEvent(
|
||||
id = mantraArtifact.id,
|
||||
publicKey = mantraArtifact.publicKey,
|
||||
createdAt = mantraArtifact.createdAt,
|
||||
tags = artifactEventTemplate.tags,
|
||||
content = artifactEventTemplate.content,
|
||||
chatRoomId = mantraArtifact.chatRoomId
|
||||
)
|
||||
)
|
||||
|
||||
// also create an artifactVersion
|
||||
addArtifactVersion(
|
||||
artifactId = mantraArtifact.id,
|
||||
versionLabel = versionLabel
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
override suspend fun addArtifactVersion(artifactId: HexKey, versionLabel: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
@@ -36,11 +36,25 @@ class ArtifactEvent(
|
||||
const val ALT_DESCRIPTION = "Artifact"
|
||||
|
||||
fun build(
|
||||
content: String,
|
||||
name: String,
|
||||
url: String,
|
||||
visibility: String,
|
||||
license: String,
|
||||
dialectId: String,
|
||||
createdAt: Long = TimeUtils.now(),
|
||||
initializer: TagArrayBuilder<ArtifactEvent>.() -> Unit = {},
|
||||
) = eventTemplate(KIND, content, createdAt) {
|
||||
) = eventTemplate(KIND, name, createdAt) {
|
||||
alt(ALT_DESCRIPTION)
|
||||
addUnique(
|
||||
ArtifactMetadataTag.assemble(
|
||||
url = url,
|
||||
visibility = visibility,
|
||||
license = license
|
||||
)
|
||||
)
|
||||
addUnique(
|
||||
DialectIdTag.assemble(dialectId)
|
||||
)
|
||||
initializer()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,33 @@
|
||||
package press.mantra.compose.repository
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import press.mantra.compose.database.model.MarmotInnerEvent
|
||||
|
||||
interface MantraRepository {
|
||||
fun addArtifact(
|
||||
suspend fun addArtifact(
|
||||
name: String,
|
||||
url: String,
|
||||
versionLabel: String
|
||||
): MarmotInnerEvent?
|
||||
|
||||
suspend fun addArtifactVersion(
|
||||
artifactId: HexKey,
|
||||
versionLabel: String
|
||||
)
|
||||
|
||||
companion object {
|
||||
val NO_OP_MANTRA_REPOSITORY = object: MantraRepository{
|
||||
override fun addArtifact(
|
||||
override suspend fun addArtifact(
|
||||
name: String,
|
||||
url: String,
|
||||
versionLabel: String
|
||||
): MarmotInnerEvent? {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun addArtifactVersion(
|
||||
artifactId: HexKey,
|
||||
versionLabel: String
|
||||
) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import kotlinx.coroutines.IO
|
||||
import kotlinx.coroutines.launch
|
||||
import press.mantra.compose.database.model.intermdiate.LocalChatRoom
|
||||
import press.mantra.compose.repository.MantraRepository
|
||||
import press.mantra.compose.repository.MarmotRepository
|
||||
import press.mantra.compose.ui.view.state.AddArtifactUIState
|
||||
|
||||
class AddArtifactViewModel(
|
||||
@@ -29,7 +30,7 @@ class AddArtifactViewModel(
|
||||
initialAddArtifactUIState: AddArtifactUIState,
|
||||
val nostrRepository: NostrRepository,
|
||||
val chatRepository: ChatRepository,
|
||||
val mantraRepository: MantraRepository
|
||||
val mantraRepository: MantraRepository,
|
||||
): ViewModel() {
|
||||
|
||||
var addArtifactUIState: AddArtifactUIState by mutableStateOf(initialAddArtifactUIState)
|
||||
@@ -79,9 +80,14 @@ class AddArtifactViewModel(
|
||||
name = name,
|
||||
url = url,
|
||||
versionLabel = versionLabel
|
||||
)
|
||||
// TODO: Navigate to artifact...
|
||||
onSuccess.invoke()
|
||||
)?.let { marmotInnerEvent ->
|
||||
|
||||
|
||||
onSuccess.invoke()
|
||||
return@launch
|
||||
}
|
||||
|
||||
onFailure.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user