1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-09 09:53:47 +00:00

improve new chat member difference

This commit is contained in:
InsanusMokrassar 2024-08-14 22:33:11 +06:00
parent 49f7954db7
commit e9a172565e
6 changed files with 56 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.chat.member
import dev.inmo.tgbotapi.abstracts.WithUser import dev.inmo.tgbotapi.abstracts.WithUser
import dev.inmo.tgbotapi.types.statusField import dev.inmo.tgbotapi.types.statusField
import dev.inmo.tgbotapi.types.untilDateField
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
@ -11,6 +12,7 @@ import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.jsonPrimitive
@ -19,11 +21,13 @@ sealed interface ChatMember : WithUser {
@Serializable(StatusSerializer::class) @Serializable(StatusSerializer::class)
enum class Status( enum class Status(
val status: String, val status: String,
val deserializationStrategy: DeserializationStrategy<ChatMember> val deserializationStrategy: DeserializationStrategy<ChatMember>,
val checker: (String, JsonObject) -> Boolean = { outerStatus, _ -> outerStatus == status },
) { ) {
Creator("creator", OwnerChatMember.serializer()), Creator("creator", OwnerChatMember.serializer()),
Administrator("administrator", AdministratorChatMemberImpl.serializer()), Administrator("administrator", AdministratorChatMemberImpl.serializer()),
Member("member", MemberChatMemberImpl.serializer()), Member("member", MemberChatMemberImpl.serializer(), { status, json -> status == "member" && json[untilDateField] ?.jsonPrimitive == null }),
SubscriptionMember("member", SubscriptionMemberChatMemberImpl.serializer(), { status, json -> status == "member" && json[untilDateField] ?.jsonPrimitive != null }),
Restricted("restricted", RestrictedChatMember.serializer()), Restricted("restricted", RestrictedChatMember.serializer()),
Left("left", LeftChatMemberImpl.serializer()), Left("left", LeftChatMemberImpl.serializer()),
Kicked("kicked", KickedChatMember.serializer()) Kicked("kicked", KickedChatMember.serializer())
@ -56,7 +60,7 @@ object ChatMemberSerializer : KSerializer<ChatMember> {
val json = JsonObject.serializer().deserialize(decoder) val json = JsonObject.serializer().deserialize(decoder)
val status = json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json") val status = json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json")
return ChatMember.Status.values().firstNotNullOfOrNull { return ChatMember.Status.values().firstNotNullOfOrNull {
if (it.status == status) { if (it.checker(status, json)) {
nonstrictJsonFormat.decodeFromJsonElement(it.deserializationStrategy, json) nonstrictJsonFormat.decodeFromJsonElement(it.deserializationStrategy, json)
} else { } else {
null null
@ -68,6 +72,7 @@ object ChatMemberSerializer : KSerializer<ChatMember> {
when (value) { when (value) {
is OwnerChatMember -> OwnerChatMember.serializer().serialize(encoder, value) is OwnerChatMember -> OwnerChatMember.serializer().serialize(encoder, value)
is AdministratorChatMemberImpl -> AdministratorChatMemberImpl.serializer().serialize(encoder, value) is AdministratorChatMemberImpl -> AdministratorChatMemberImpl.serializer().serialize(encoder, value)
is SubscriptionMemberChatMemberImpl -> SubscriptionMemberChatMemberImpl.serializer().serialize(encoder, value)
is MemberChatMemberImpl -> MemberChatMemberImpl.serializer().serialize(encoder, value) is MemberChatMemberImpl -> MemberChatMemberImpl.serializer().serialize(encoder, value)
is RestrictedChatMember -> RestrictedChatMember.serializer().serialize(encoder, value) is RestrictedChatMember -> RestrictedChatMember.serializer().serialize(encoder, value)
is LeftChatMemberImpl -> LeftChatMemberImpl.serializer().serialize(encoder, value) is LeftChatMemberImpl -> LeftChatMemberImpl.serializer().serialize(encoder, value)

View File

@ -1,11 +1,6 @@
package dev.inmo.tgbotapi.types.chat.member package dev.inmo.tgbotapi.types.chat.member
import dev.inmo.tgbotapi.abstracts.types.UntilDate
import korlibs.time.DateTime
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(ChatMemberSerializer::class) @Serializable(ChatMemberSerializer::class)
sealed interface MemberChatMember : ChatMember, UntilDate { sealed interface MemberChatMember : ChatMember
val until: DateTime?
get() = untilDate ?.asDate
}

View File

@ -8,10 +8,7 @@ import kotlinx.serialization.*
@Serializable @Serializable
data class MemberChatMemberImpl( data class MemberChatMemberImpl(
@SerialName(userField) @SerialName(userField)
override val user: User, override val user: User
@SerialName(untilDateField)
@Serializable(TelegramDateSerializer::class)
override val untilDate: TelegramDate? = null
) : MemberChatMember { ) : MemberChatMember {
@SerialName(statusField) @SerialName(statusField)
@Required @Required

View File

@ -0,0 +1,13 @@
package dev.inmo.tgbotapi.types.chat.member
import dev.inmo.tgbotapi.abstracts.types.UntilDate
import dev.inmo.tgbotapi.types.TelegramDate
import korlibs.time.DateTime
import kotlinx.serialization.Serializable
@Serializable(ChatMemberSerializer::class)
sealed interface SubscriptionMemberChatMember : MemberChatMember, UntilDate {
override val untilDate: TelegramDate
val until: DateTime
get() = untilDate.asDate
}

View File

@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.types.chat.member
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.User
import korlibs.time.DateTime
import kotlinx.serialization.*
@Serializable
data class SubscriptionMemberChatMemberImpl(
@SerialName(userField)
override val user: User,
@SerialName(untilDateField)
@Serializable(TelegramDateSerializer::class)
override val untilDate: TelegramDate
) : SubscriptionMemberChatMember {
@SerialName(statusField)
@Required
@EncodeDefault
override val status: ChatMember.Status = ChatMember.Status.SubscriptionMember
}

View File

@ -170,6 +170,7 @@ import dev.inmo.tgbotapi.types.chat.member.MemberChatMember
import dev.inmo.tgbotapi.types.chat.member.OwnerChatMember import dev.inmo.tgbotapi.types.chat.member.OwnerChatMember
import dev.inmo.tgbotapi.types.chat.member.RestrictedChatMember import dev.inmo.tgbotapi.types.chat.member.RestrictedChatMember
import dev.inmo.tgbotapi.types.chat.member.SpecialRightsChatMember import dev.inmo.tgbotapi.types.chat.member.SpecialRightsChatMember
import dev.inmo.tgbotapi.types.chat.member.SubscriptionMemberChatMember
import dev.inmo.tgbotapi.types.dice.BasketballDiceAnimationType import dev.inmo.tgbotapi.types.dice.BasketballDiceAnimationType
import dev.inmo.tgbotapi.types.dice.BowlingDiceAnimationType import dev.inmo.tgbotapi.types.dice.BowlingDiceAnimationType
import dev.inmo.tgbotapi.types.dice.CubeDiceAnimationType import dev.inmo.tgbotapi.types.dice.CubeDiceAnimationType
@ -772,6 +773,18 @@ public inline fun <T>
OptionallyWithUser.ifSpecialRightsChatMember(block: (SpecialRightsChatMember) -> T): T? = OptionallyWithUser.ifSpecialRightsChatMember(block: (SpecialRightsChatMember) -> T): T? =
specialRightsChatMemberOrNull() ?.let(block) specialRightsChatMemberOrNull() ?.let(block)
public inline fun OptionallyWithUser.subscriptionMemberChatMemberOrNull():
SubscriptionMemberChatMember? = this as?
dev.inmo.tgbotapi.types.chat.member.SubscriptionMemberChatMember
public inline fun OptionallyWithUser.subscriptionMemberChatMemberOrThrow():
SubscriptionMemberChatMember = this as
dev.inmo.tgbotapi.types.chat.member.SubscriptionMemberChatMember
public inline fun <T>
OptionallyWithUser.ifSubscriptionMemberChatMember(block: (SubscriptionMemberChatMember) -> T):
T? = subscriptionMemberChatMemberOrNull() ?.let(block)
public inline fun OptionallyWithUser.leftChatMemberEventOrNull(): LeftChatMemberEvent? = this as? public inline fun OptionallyWithUser.leftChatMemberEventOrNull(): LeftChatMemberEvent? = this as?
dev.inmo.tgbotapi.types.message.ChatEvents.LeftChatMemberEvent dev.inmo.tgbotapi.types.message.ChatEvents.LeftChatMemberEvent