diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 4764f8d400..91c69503c8 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -210,6 +210,7 @@ const val firstNameField = "first_name" const val lastNameField = "last_name" const val languageCodeField = "language_code" const val addedToAttachmentMenuField = "added_to_attachment_menu" +const val allowsWriteToPMField = "allows_write_to_pm" const val isPremiumField = "is_premium" const val hasPrivateForwardsField = "has_private_forwards" const val hasRestrictedVoiceAndVideoMessagesField = "has_restricted_voice_and_video_messages" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/forum/WriteAccessAllowed.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/forum/WriteAccessAllowed.kt index 2d6a630f5b..78eab7af83 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/forum/WriteAccessAllowed.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/forum/WriteAccessAllowed.kt @@ -1,12 +1,74 @@ package dev.inmo.tgbotapi.types.message.ChatEvents.forum +import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChatEvent import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ForumEvent import dev.inmo.tgbotapi.types.webAppNameField +import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder -@Serializable -data class WriteAccessAllowed( - @SerialName(webAppNameField) - val webAppName: String? = null -) : ForumEvent +@Serializable(WriteAccessAllowed.Companion::class) +sealed interface WriteAccessAllowed : ChatEvent { + val webAppName: String? + get() = null + val fromRequest: Boolean + get() = false + val fromAttachmentMenu: Boolean + get() = false + + @Serializable + object Other : WriteAccessAllowed + @Serializable + data class FromWebAppLink( + override val webAppName: String + ) : WriteAccessAllowed + @Serializable + object FromRequest : WriteAccessAllowed { + override val fromRequest: Boolean + get() = true + } + + @Serializable + object FromAttachmentMenu : WriteAccessAllowed { + override val fromAttachmentMenu: Boolean + get() = true + } + + @Serializable + private class WriteAccessAllowedRaw( + val web_app_name: String? = null, + val from_request: Boolean = false, + val from_attachment_menu: Boolean = false + ) + + companion object : KSerializer { + override val descriptor: SerialDescriptor + get() = WriteAccessAllowedRaw.serializer().descriptor + + override fun deserialize(decoder: Decoder): WriteAccessAllowed { + val raw = WriteAccessAllowedRaw.serializer().deserialize(decoder) + + return when { + raw.web_app_name != null -> FromWebAppLink(raw.web_app_name) + raw.from_request -> FromRequest + raw.from_attachment_menu -> Other + else -> Other + } + } + + override fun serialize(encoder: Encoder, value: WriteAccessAllowed) { + val raw = when (value) { + FromAttachmentMenu -> WriteAccessAllowedRaw(from_attachment_menu = true) + FromRequest -> WriteAccessAllowedRaw(from_request = true) + Other -> WriteAccessAllowedRaw() + is FromWebAppLink -> WriteAccessAllowedRaw(web_app_name = value.webAppName) + } + WriteAccessAllowedRaw.serializer().serialize(encoder, raw) + } + + operator fun invoke(webAppName: String?): WriteAccessAllowed = webAppName ?.let(::FromWebAppLink) ?: Other + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt index b17a6a456c..383832ef14 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt @@ -15,6 +15,7 @@ external class WebApp { val headerColor: HEXColor? fun setHeaderColor(color: Color.BackgroundColor) + fun setHeaderColor(color: Color.Hex) val backgroundColor: HEXColor? fun setBackgroundColor(color: Color.Hex) fun setBackgroundColor(color: Color.BackgroundColor) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt index 9c8ac9431e..75a018e789 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt @@ -20,6 +20,10 @@ external interface WebAppUser { val is_premium: Boolean? @JsName(photoUrlField) val photoUrl: String? + @JsName(addedToAttachmentMenuField) + val addedToAttachmentMenu: Boolean? + @JsName(allowsWriteToPMField) + val allowsWriteToPM: Boolean? } val WebAppUser.isPremium