Basic logic for adding new artifact
This commit is contained in:
1 parent
9444f8280b
commit
0d79c3ecee
5 files changed
+115
-10
No files matched your search
+21
@@ -5,6 +5,8 @@ import androidx.room3.ForeignKey
|
|||||||
import androidx.room3.PrimaryKey
|
import androidx.room3.PrimaryKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
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 com.vitorpamplona.quartz.nip31Alts.AltTag
|
||||||
import press.mantra.compose.database.model.traits.OptionalNostrEventEntity
|
import press.mantra.compose.database.model.traits.OptionalNostrEventEntity
|
||||||
import press.mantra.compose.database.model.traits.TimestampedEntity
|
import press.mantra.compose.database.model.traits.TimestampedEntity
|
||||||
@@ -87,6 +89,25 @@ data class MantraArtifact(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
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? {
|
fun fromArtifactEvent(artifactEvent: ArtifactEvent, chatRoomId: HexKey, ): MantraArtifact? {
|
||||||
return artifactEvent.artifactMetadata?.let { artifactMetadata ->
|
return artifactEvent.artifactMetadata?.let { artifactMetadata ->
|
||||||
artifactEvent.dialectId()?.let { dialectId ->
|
artifactEvent.dialectId()?.let { dialectId ->
|
||||||
|
|||||||
+51
-2
@@ -1,14 +1,63 @@
|
|||||||
package press.mantra.compose.database.repository
|
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 kotlinx.coroutines.CoroutineScope
|
||||||
import press.mantra.compose.database.MantraDatabase
|
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 press.mantra.compose.repository.MantraRepository
|
||||||
|
import kotlin.time.Instant
|
||||||
|
|
||||||
class DatabaseMantraRepository(
|
class DatabaseMantraRepository(
|
||||||
val database: MantraDatabase,
|
val database: MantraDatabase,
|
||||||
val scope: CoroutineScope
|
val scope: CoroutineScope
|
||||||
): MantraRepository {
|
): MantraRepository {
|
||||||
override fun addArtifact(name: String, url: String, versionLabel: String) {
|
override suspend fun addArtifact(name: String, url: String, versionLabel: String): MarmotInnerEvent? {
|
||||||
TODO("Implement add artifact")
|
// 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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-2
@@ -36,11 +36,25 @@ class ArtifactEvent(
|
|||||||
const val ALT_DESCRIPTION = "Artifact"
|
const val ALT_DESCRIPTION = "Artifact"
|
||||||
|
|
||||||
fun build(
|
fun build(
|
||||||
content: String,
|
name: String,
|
||||||
|
url: String,
|
||||||
|
visibility: String,
|
||||||
|
license: String,
|
||||||
|
dialectId: String,
|
||||||
createdAt: Long = TimeUtils.now(),
|
createdAt: Long = TimeUtils.now(),
|
||||||
initializer: TagArrayBuilder<ArtifactEvent>.() -> Unit = {},
|
initializer: TagArrayBuilder<ArtifactEvent>.() -> Unit = {},
|
||||||
) = eventTemplate(KIND, content, createdAt) {
|
) = eventTemplate(KIND, name, createdAt) {
|
||||||
alt(ALT_DESCRIPTION)
|
alt(ALT_DESCRIPTION)
|
||||||
|
addUnique(
|
||||||
|
ArtifactMetadataTag.assemble(
|
||||||
|
url = url,
|
||||||
|
visibility = visibility,
|
||||||
|
license = license
|
||||||
|
)
|
||||||
|
)
|
||||||
|
addUnique(
|
||||||
|
DialectIdTag.assemble(dialectId)
|
||||||
|
)
|
||||||
initializer()
|
initializer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-2
@@ -1,18 +1,33 @@
|
|||||||
package press.mantra.compose.repository
|
package press.mantra.compose.repository
|
||||||
|
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
|
import press.mantra.compose.database.model.MarmotInnerEvent
|
||||||
|
|
||||||
interface MantraRepository {
|
interface MantraRepository {
|
||||||
fun addArtifact(
|
suspend fun addArtifact(
|
||||||
name: String,
|
name: String,
|
||||||
url: String,
|
url: String,
|
||||||
versionLabel: String
|
versionLabel: String
|
||||||
|
): MarmotInnerEvent?
|
||||||
|
|
||||||
|
suspend fun addArtifactVersion(
|
||||||
|
artifactId: HexKey,
|
||||||
|
versionLabel: String
|
||||||
)
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val NO_OP_MANTRA_REPOSITORY = object: MantraRepository{
|
val NO_OP_MANTRA_REPOSITORY = object: MantraRepository{
|
||||||
override fun addArtifact(
|
override suspend fun addArtifact(
|
||||||
name: String,
|
name: String,
|
||||||
url: String,
|
url: String,
|
||||||
versionLabel: String
|
versionLabel: String
|
||||||
|
): MarmotInnerEvent? {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun addArtifactVersion(
|
||||||
|
artifactId: HexKey,
|
||||||
|
versionLabel: String
|
||||||
) {
|
) {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -20,6 +20,7 @@ import kotlinx.coroutines.IO
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import press.mantra.compose.database.model.intermdiate.LocalChatRoom
|
import press.mantra.compose.database.model.intermdiate.LocalChatRoom
|
||||||
import press.mantra.compose.repository.MantraRepository
|
import press.mantra.compose.repository.MantraRepository
|
||||||
|
import press.mantra.compose.repository.MarmotRepository
|
||||||
import press.mantra.compose.ui.view.state.AddArtifactUIState
|
import press.mantra.compose.ui.view.state.AddArtifactUIState
|
||||||
|
|
||||||
class AddArtifactViewModel(
|
class AddArtifactViewModel(
|
||||||
@@ -29,7 +30,7 @@ class AddArtifactViewModel(
|
|||||||
initialAddArtifactUIState: AddArtifactUIState,
|
initialAddArtifactUIState: AddArtifactUIState,
|
||||||
val nostrRepository: NostrRepository,
|
val nostrRepository: NostrRepository,
|
||||||
val chatRepository: ChatRepository,
|
val chatRepository: ChatRepository,
|
||||||
val mantraRepository: MantraRepository
|
val mantraRepository: MantraRepository,
|
||||||
): ViewModel() {
|
): ViewModel() {
|
||||||
|
|
||||||
var addArtifactUIState: AddArtifactUIState by mutableStateOf(initialAddArtifactUIState)
|
var addArtifactUIState: AddArtifactUIState by mutableStateOf(initialAddArtifactUIState)
|
||||||
@@ -79,9 +80,14 @@ class AddArtifactViewModel(
|
|||||||
name = name,
|
name = name,
|
||||||
url = url,
|
url = url,
|
||||||
versionLabel = versionLabel
|
versionLabel = versionLabel
|
||||||
)
|
)?.let { marmotInnerEvent ->
|
||||||
// TODO: Navigate to artifact...
|
|
||||||
onSuccess.invoke()
|
|
||||||
|
onSuccess.invoke()
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
|
onFailure.invoke()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user