a little updates

This commit is contained in:
InsanusMokrassar 2022-05-07 21:54:19 +06:00
parent a3ff08af27
commit 55d0e1b55f
10 changed files with 53 additions and 57 deletions

View File

@ -16,9 +16,5 @@ kotlin {
api project(":postssystem.features.auth.common")
}
}
clientMain {
dependencies {
}
}
}
}

View File

@ -9,6 +9,8 @@ import dev.inmo.micro_utils.common.Either
import dev.inmo.micro_utils.common.either
import dev.inmo.micro_utils.repos.*
import dev.inmo.postssystem.features.auth.client.createAuthorizedFeaturesDIModule
import dev.inmo.postssystem.features.auth.client.ui.AuthUIError.AuthIncorrect
import dev.inmo.postssystem.features.auth.client.ui.AuthUIError.ServerUnavailable
import dev.inmo.postssystem.features.common.common.DBDropper
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
@ -83,8 +85,8 @@ data class DefaultAuthSettings(
currentModule ?.let { koin.loadModules(listOf(currentModule)) }
}
return when {
!serverAvailable -> ServerUnavailableAuthUIError
!authCorrect -> AuthIncorrectAuthUIError
!serverAvailable -> ServerUnavailable
!authCorrect -> AuthIncorrect
else -> {
_authorizedDIModule.value = newModule
null

View File

@ -3,18 +3,23 @@ package dev.inmo.postssystem.features.auth.client.ui
import kotlinx.serialization.Serializable
@Serializable
sealed class AuthUIError
@Serializable
object ServerUnavailableAuthUIError : AuthUIError()
@Serializable
object AuthIncorrectAuthUIError : AuthUIError()
sealed class AuthUIError {
@Serializable
object ServerUnavailable : AuthUIError()
@Serializable
object AuthIncorrect : AuthUIError()
}
@Serializable
sealed class AuthUIState
@Serializable
data class InitAuthUIState(val showError: AuthUIError? = null) : AuthUIState()
val DefaultInitAuthUIState = InitAuthUIState()
@Serializable
object LoadingAuthUIState : AuthUIState()
@Serializable
object AuthorizedAuthUIState : AuthUIState()
sealed class AuthUIState {
@Serializable
data class Init(val showError: AuthUIError? = null) : AuthUIState()
@Serializable
object Loading : AuthUIState()
@Serializable
object Authorized : AuthUIState()
companion object {
val DefaultInit = Init()
}
}

View File

@ -9,26 +9,26 @@ import kotlinx.coroutines.launch
class DefaultAuthUIModel(
private val scope: CoroutineScope,
private val authSettings: AuthSettings
) : AbstractUIModel<AuthUIState>(LoadingAuthUIState), AuthUIModel {
) : AbstractUIModel<AuthUIState>(AuthUIState.Loading), AuthUIModel {
init {
scope.launch {
_currentState.value = LoadingAuthUIState
_currentState.value = AuthUIState.Loading
authSettings.loadingJob.join()
if (authSettings.authorizedDIModule.value == null) {
_currentState.value = DefaultInitAuthUIState
_currentState.value = AuthUIState.DefaultInit
} else {
_currentState.value = AuthorizedAuthUIState
_currentState.value = AuthUIState.Authorized
}
}
}
override suspend fun initAuth(serverUrl: String, creds: AuthCreds) {
_currentState.value = LoadingAuthUIState
_currentState.value = AuthUIState.Loading
val authError = authSettings.auth(serverUrl, creds)
if (authError == null) {
_currentState.value = AuthorizedAuthUIState
_currentState.value = AuthUIState.Authorized
} else {
_currentState.value = InitAuthUIState(authError)
_currentState.value = AuthUIState.Init(authError)
}
}

View File

@ -9,6 +9,8 @@ import dev.inmo.postssystem.features.auth.client.ui.*
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.fsm.common.StatesMachine
import dev.inmo.postssystem.features.auth.client.ui.AuthUIError.AuthIncorrect
import dev.inmo.postssystem.features.auth.client.ui.AuthUIError.ServerUnavailable
import dev.inmo.postssystem.features.common.common.*
import dev.inmo.postssystem.features.common.common.ui.JSView
import dev.inmo.postssystem.features.common.common.ui.fsm.*
@ -19,9 +21,11 @@ import org.jetbrains.compose.web.attributes.InputType
import org.jetbrains.compose.web.dom.Text
import org.w3c.dom.*
@ExperimentalStdlibApi
@EagerInitialization
val loader = DefaultModuleLoader {
factory { AuthView(get(), get(DefaultQualifiers.UIScopeQualifier), getAllDistinct()) }
singleWithRandomQualifier<UIFSMHandler.Registrator> {
singleWithRandomQualifier {
UIFSMHandler.Registrator {
strictlyOn(get<AuthView>())
}
@ -64,13 +68,13 @@ class AuthView(
}
}
TextField(
StandardInput(
InputType.Text,
usernameState,
disabled,
"Username",
)
TextField(
StandardInput(
InputType.Password,
passwordState,
disabled,
@ -90,23 +94,23 @@ class AuthView(
val viewJob = viewModel.currentState.subscribeSafelyWithoutExceptions(uiScope) {
when (it) {
is InitAuthUIState -> {
is AuthUIState.Init -> {
disabled.value = false
errorText.value = when (it.showError) {
ServerUnavailableAuthUIError -> "Server unavailable"
AuthIncorrectAuthUIError -> {
ServerUnavailable -> "Server unavailable"
AuthIncorrect -> {
passwordState.value = ""
"Username or password is incorrect"
}
null -> null
}
}
LoadingAuthUIState -> {
AuthUIState.Loading -> {
disabled.value = true
errorText.value = null
}
AuthorizedAuthUIState -> {
AuthUIState.Authorized -> {
completion.complete(state.from)
}
}

View File

@ -33,9 +33,10 @@ expect class FileBasedInputProvider : SimpleInputProvider {
}
@Serializable(SimpleInputProviderSerializer::class)
class CustomInputProvider(private val provider: () -> Input) : SimpleInputProvider {
override val contentBytes: Long?
get() = null
class CustomInputProvider(
override val contentBytes: Long? = null,
private val provider: () -> Input
) : SimpleInputProvider {
override fun invoke(): Input = provider()
}

View File

@ -7,11 +7,7 @@ import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
@Serializable(RoleSerializer::class)
interface Role {
companion object {
fun serializer() = RoleSerializer
}
}
interface Role
@Serializable
data class UnknownRole(val originalJson: JsonElement) : Role

View File

@ -8,11 +8,7 @@ import kotlinx.serialization.Serializable
private val justForLoading = RolesManagerRoleSerializer
@Serializable(RolesManagerRoleSerializer::class)
interface RolesManagerRole : Role {
companion object {
fun serializer() = RolesManagerRoleSerializer
}
}
interface RolesManagerRole : Role
@Serializable
object GeneralRolesManagerRole : RolesManagerRole {

View File

@ -1,6 +1,6 @@
package dev.inmo.postssystem.targets.telegram.loader.server
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl

View File

@ -6,14 +6,10 @@ import dev.inmo.postssystem.features.content.text.common.TextContent
import dev.inmo.postssystem.features.publication.server.PublicationPost
import dev.inmo.postssystem.features.publication.server.PublicationTarget
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
import dev.inmo.tgbotapi.requests.send.SendTextMessage
import dev.inmo.tgbotapi.requests.send.media.*
import dev.inmo.tgbotapi.types.ChatId
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.delay
class PublicationTargetTelegram(
@ -24,7 +20,7 @@ class PublicationTargetTelegram(
post.content.mapNotNull {
when (val content = it.content) {
is BinaryContent -> {
val storageFile by lazy {
val multipartFile by lazy {
MultipartFile(content.filename.name) {
content.inputProvider()
}
@ -32,17 +28,17 @@ class PublicationTargetTelegram(
when (content.mimeType) {
is KnownMimeTypes.Image.Jpeg,
is KnownMimeTypes.Image.Png -> {
SendPhoto(targetChatId, storageFile)
SendPhoto(targetChatId, multipartFile)
}
is KnownMimeTypes.Video.Mp4 -> {
SendVideo(targetChatId, storageFile)
SendVideo(targetChatId, multipartFile)
}
is KnownMimeTypes.Audio.Mpeg -> {
SendAudio(targetChatId, storageFile)
SendAudio(targetChatId, multipartFile)
}
else -> SendDocument(
targetChatId,
storageFile
multipartFile
)
}
}