first fully ready for trying system

This commit is contained in:
InsanusMokrassar 2022-03-11 22:26:06 +06:00
parent 559545438a
commit 369dcb12f5
24 changed files with 199 additions and 13 deletions

View File

@ -29,6 +29,8 @@ import dev.inmo.postssystem.features.content.common.ContentSerializersModuleConf
import dev.inmo.postssystem.features.content.common.OtherContentSerializerModuleConfigurator import dev.inmo.postssystem.features.content.common.OtherContentSerializerModuleConfigurator
import dev.inmo.postssystem.features.content.text.common.TextContentSerializerModuleConfigurator import dev.inmo.postssystem.features.content.text.common.TextContentSerializerModuleConfigurator
import dev.inmo.postssystem.features.status.client.StatusFeatureClient import dev.inmo.postssystem.features.status.client.StatusFeatureClient
import dev.inmo.postssystem.publicators.simple.client.SimplePublicatorService
import dev.inmo.postssystem.publicators.simple.client.SimplePublicatorServiceClient
import dev.inmo.postssystem.services.posts.client.ClientPostsService import dev.inmo.postssystem.services.posts.client.ClientPostsService
import dev.inmo.postssystem.services.posts.common.* import dev.inmo.postssystem.services.posts.common.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@ -138,5 +140,6 @@ fun getAuthorizedFeaturesDIModule(
ReadPostsService::class, ReadPostsService::class,
WritePostsService::class WritePostsService::class
) )
single<SimplePublicatorService> { SimplePublicatorServiceClient(get(serverUrlQualifier), get()) }
} }
} }

View File

@ -90,7 +90,7 @@ fun baseKoin(): Koin {
factory { AuthUIViewModel(get()) } factory { AuthUIViewModel(get()) }
factory { AuthView(get(), get(UIScopeQualifier)) } factory { AuthView(get(), get(UIScopeQualifier)) }
factory<PostCreateUIModel> { DefaultPostCreateUIModel(get()) } factory<PostCreateUIModel> { DefaultPostCreateUIModel(get(), get()) }
factory { PostCreateUIViewModel(get()) } factory { PostCreateUIViewModel(get()) }
factory { PostCreateView(get(), get(UIScopeQualifier)) } factory { PostCreateView(get(), get(UIScopeQualifier)) }
} }

View File

@ -5,6 +5,7 @@ import dev.inmo.postssystem.features.files.common.storage.WriteFilesStorage
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import io.ktor.utils.io.core.copyTo import io.ktor.utils.io.core.copyTo
import io.ktor.utils.io.streams.asOutput import io.ktor.utils.io.streams.asOutput
import io.ktor.utils.io.streams.writePacket
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import java.io.File import java.io.File
@ -38,7 +39,11 @@ class WriteDistFilesStorage(
file = newId.file file = newId.file
} while (file.exists()) } while (file.exists())
metasKeyValueRepo.set(newId, it.toMetaFileInfo()) metasKeyValueRepo.set(newId, it.toMetaFileInfo())
it.inputProvider().copyTo(file.outputStream().asOutput()) it.inputProvider().use { input ->
file.outputStream().asOutput().use { output ->
input.copyTo(output)
}
}
FullFileInfoStorageWrapper(newId, it) FullFileInfoStorageWrapper(newId, it)
} }

View File

@ -0,0 +1,3 @@
package dev.inmo.postssystem.features.publication.common
const val publicatorsRootPath = "publicators"

View File

@ -0,0 +1,18 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":postssystem.publicators.simple.common")
api project(":postssystem.features.publication.client")
}
}
}
}

View File

@ -0,0 +1,7 @@
package dev.inmo.postssystem.publicators.simple.client
import dev.inmo.postssystem.features.posts.common.PostId
interface SimplePublicatorService {
suspend fun publish(postId: PostId)
}

View File

@ -0,0 +1,33 @@
package dev.inmo.postssystem.publicators.simple.client
import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.postssystem.features.posts.common.PostId
import dev.inmo.postssystem.features.publication.common.publicatorsRootPath
import dev.inmo.postssystem.publicators.simple.common.simplePublicatorPublishPathPart
import dev.inmo.postssystem.publicators.simple.common.simplePublicatorRootPath
import kotlinx.serialization.builtins.serializer
class SimplePublicatorServiceClient(
baseUrl: String,
private val unifiedRequester: UnifiedRequester
) : SimplePublicatorService {
private val fullUrl = buildStandardUrl(
baseUrl,
buildStandardUrl(
publicatorsRootPath,
simplePublicatorRootPath
)
)
private val publishFullUrl = buildStandardUrl(
fullUrl,
simplePublicatorPublishPathPart
)
override suspend fun publish(postId: PostId) = unifiedRequester.unipost(
publishFullUrl,
PostId.serializer() to postId,
Unit.serializer()
)
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.postssystem.publicators.simple.client"/>

View File

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":postssystem.features.publication.common")
}
}
}
}

View File

@ -0,0 +1,4 @@
package dev.inmo.postssystem.publicators.simple.common
const val simplePublicatorRootPath = "simple"
const val simplePublicatorPublishPathPart = "publish"

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.postssystem.publicators.simple.common"/>

View File

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppJavaProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":postssystem.publicators.simple.common")
api project(":postssystem.features.publication.server")
}
}
}
}

View File

@ -0,0 +1,17 @@
package dev.inmo.postssystem.publicators.simple.server
import dev.inmo.micro_utils.ktor.server.configurators.ApplicationRoutingConfigurator
import dev.inmo.postssystem.features.common.common.singleWithRandomQualifier
import dev.inmo.postssystem.features.common.server.sessions.ModuleLoader
import kotlinx.serialization.json.JsonObject
import org.koin.core.module.Module
class SimplePublicationTriggerLoader : ModuleLoader {
override fun Module.load(config: JsonObject) {
singleWithRandomQualifier<ApplicationRoutingConfigurator.Element> {
SimplePublicatorRoutingConfigurator(
get()
)
}
}
}

View File

@ -0,0 +1,32 @@
package dev.inmo.postssystem.publicators.simple.server
import dev.inmo.micro_utils.ktor.server.configurators.ApplicationRoutingConfigurator
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.ktor.server.uniload
import dev.inmo.postssystem.features.posts.common.PostId
import dev.inmo.postssystem.features.publication.common.publicatorsRootPath
import dev.inmo.postssystem.features.publication.server.PublicationManager
import dev.inmo.postssystem.publicators.simple.common.simplePublicatorPublishPathPart
import dev.inmo.postssystem.publicators.simple.common.simplePublicatorRootPath
import io.ktor.application.call
import io.ktor.routing.*
import kotlinx.serialization.builtins.serializer
class SimplePublicatorRoutingConfigurator(
private val publicationManager: PublicationManager
) : ApplicationRoutingConfigurator.Element {
override fun Route.invoke() {
route(publicatorsRootPath) {
route(simplePublicatorRootPath) {
post(simplePublicatorPublishPathPart) {
val postId = call.uniload(PostId.serializer())
call.unianswer(
Unit.serializer(),
publicationManager.publish(postId)
)
}
}
}
}
}

View File

@ -11,7 +11,7 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
api project(":postssystem.publicators.template.common") api project(":postssystem.publicators.template.common")
api project(":postssystem.features.common.client") api project(":postssystem.features.publication.client")
} }
} }
} }

View File

@ -10,7 +10,7 @@ kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api project(":postssystem.features.common.common") api project(":postssystem.features.publication.common")
} }
} }
} }

View File

@ -10,7 +10,7 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
api project(":postssystem.publicators.template.common") api project(":postssystem.publicators.template.common")
api project(":postssystem.features.common.server") api project(":postssystem.features.publication.server")
} }
} }
} }

View File

@ -25,6 +25,8 @@ dependencies {
api project(":postssystem.services.posts.server") api project(":postssystem.services.posts.server")
api project(":postssystem.publicators.simple.server")
api project(":postssystem.targets.telegram.loader.server") api project(":postssystem.targets.telegram.loader.server")
api project(":postssystem.targets.telegram.publication.server") api project(":postssystem.targets.telegram.publication.server")

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,8 @@
"filesFolder": "/tmp/files", "filesFolder": "/tmp/files",
"debugMode": true, "debugMode": true,
"modules": [ "modules": [
"dev.inmo.postssystem.targets.telegram.loader.server.TelegramTargetModuleLoader" "dev.inmo.postssystem.targets.telegram.loader.server.TelegramTargetModuleLoader",
"dev.inmo.postssystem.publicators.simple.server.SimplePublicationTriggerLoader"
], ],
"telegram": { "telegram": {
"botToken": "YOUR BOT TOKEN", "botToken": "YOUR BOT TOKEN",

View File

@ -13,6 +13,7 @@ kotlin {
api project(":postssystem.features.common.client") api project(":postssystem.features.common.client")
api project(":postssystem.services.posts.common") api project(":postssystem.services.posts.common")
api project(":postssystem.features.posts.client") api project(":postssystem.features.posts.client")
api project(":postssystem.publicators.simple.client")
} }
} }
} }

View File

@ -2,20 +2,25 @@ package dev.inmo.postssystem.services.posts.client.ui.create
import dev.inmo.postssystem.features.common.common.AbstractUIModel import dev.inmo.postssystem.features.common.common.AbstractUIModel
import dev.inmo.postssystem.features.content.common.Content import dev.inmo.postssystem.features.content.common.Content
import dev.inmo.postssystem.publicators.simple.client.SimplePublicatorService
import dev.inmo.postssystem.services.posts.common.FullNewPost import dev.inmo.postssystem.services.posts.common.FullNewPost
import dev.inmo.postssystem.services.posts.common.WritePostsService import dev.inmo.postssystem.services.posts.common.WritePostsService
import kotlinx.coroutines.delay
class DefaultPostCreateUIModel( class DefaultPostCreateUIModel(
private val postCreationService: WritePostsService private val postCreationService: WritePostsService,
private val publicationService: SimplePublicatorService
) : PostCreateUIModel, AbstractUIModel<PostCreateUIState>( ) : PostCreateUIModel, AbstractUIModel<PostCreateUIState>(
PostCreateUIState.Init PostCreateUIState.Init
) { ) {
override suspend fun create(content: List<Content>) { override suspend fun create(content: List<Content>) {
runCatching { runCatching {
_currentState.value = PostCreateUIState.Uploading _currentState.value = PostCreateUIState.Uploading
postCreationService.create( val post = postCreationService.create(
FullNewPost(content) FullNewPost(content)
) ) ?: return@runCatching
delay(1000L)
publicationService.publish(post.id)
}.onFailure { }.onFailure {
_currentState.value = PostCreateUIState.Fail _currentState.value = PostCreateUIState.Fail
}.onSuccess { }.onSuccess {

View File

@ -53,6 +53,10 @@ String[] includes = [
":features:publication:client", ":features:publication:client",
":features:publication:server", ":features:publication:server",
":publicators:simple:common",
":publicators:simple:client",
":publicators:simple:server",
":targets:telegram:publication:server", ":targets:telegram:publication:server",
":targets:telegram:loader:common", ":targets:telegram:loader:common",
":targets:telegram:loader:server", ":targets:telegram:loader:server",

View File

@ -12,6 +12,7 @@ import dev.inmo.tgbotapi.requests.send.SendTextMessage
import dev.inmo.tgbotapi.requests.send.media.* import dev.inmo.tgbotapi.requests.send.media.*
import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.utils.StorageFile import dev.inmo.tgbotapi.utils.StorageFile
import dev.inmo.tgbotapi.utils.StorageFileInfo
import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.readBytes import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -25,7 +26,9 @@ class PublicationTargetTelegram(
when (val content = it.content) { when (val content = it.content) {
is BinaryContent -> { is BinaryContent -> {
val storageFile by lazy { val storageFile by lazy {
StorageFile(content.filename.name, content.inputProvider().readBytes()).asMultipartFile() StorageFile(StorageFileInfo(content.filename.name)) {
content.inputProvider()
}.asMultipartFile()
} }
when (content.mimeType) { when (content.mimeType) {
is KnownMimeTypes.Image.Jpeg, is KnownMimeTypes.Image.Jpeg,
@ -38,7 +41,10 @@ class PublicationTargetTelegram(
is KnownMimeTypes.Audio.Mpeg -> { is KnownMimeTypes.Audio.Mpeg -> {
SendAudio(targetChatId, storageFile) SendAudio(targetChatId, storageFile)
} }
else -> null else -> SendDocument(
targetChatId,
storageFile
)
} }
} }
is TextContent -> { is TextContent -> {
@ -47,7 +53,16 @@ class PublicationTargetTelegram(
else -> null else -> null
} }
}.forEach { request -> }.forEach { request ->
bot.executeUnsafe(request, 3, 1000L) repeat(3) {
runCatching {
bot.execute(request)
}.onFailure {
it.printStackTrace()
}.onSuccess {
return@forEach
}
delay(1000L)
}
delay(100L) delay(100L)
} }
} }